Skip to content

Commit

Permalink
INIT
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz A.J. Wrona committed Jan 4, 2017
0 parents commit da8e46a
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Łukasz A.J. Wrona

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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# co-reduce-any
Reduce anything in Co-like manner
174 changes: 174 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/******************************************************************************/
/**
* Reduce anything in Co-like manner
*
* Provides a consistent interface for reducing arrays, strings,
* object literals, maps, sets, generators and streams. Synchronously
* when applicable.
*
* const collection = [ 1, 2, 3, 4 ]
*
* reduce(collection, function* (next) {
* const result = [ ]
* for (let pair; pair = yield next;) {
* const [ key, elem ] = pair
* const value = yield Promise.resolve(elem * key)
* }
* return result
* }).then(result => console.log(resul))
*
* yield is overloaded
*
* value = yield promise // converts promise to a value, like Co
* yield next // waits for new [ key, element ] pair
*
* The key:
* In arrays and strings, key is the elements' index
* In Maps and object literals, key is the element's key
* In generators Sets and node streams, key is the iteration's "i" provided
* for consistency
*
* If no promises are yielded function returns a value returned from
* supplied generator
*
* Iterates over node streams sequentially and always returns a promise.
*
* @module co-reduce-any
* @author Lukasz A.J. Wrona <[email protected]>
* @license MIT
*/
/******************************************************************************/

"use strict"
var typical = require("typical")
var Stream = require("stream")

/******************************************************************************/

var isPromise = typical.isPromise
var isObject = typical.isObject
var isIterable = typical.isIterable

var next = Object.freeze({ })

function* enumerateIterable(collection) {
var elem
var index = 0
for (elem of collection) {
yield [ index, elem ]
index += 1
}
}

// Enumerate map 1:1
function* enumerateMap(map) {
var pair
for (pair of map) {
yield pair
}
}

// Enumerate object literals
function* enumerateObject(object) {
var key
for (key in object) {
yield [ key, object[key] ]
}
}

// Inspired by Python's enumerate
function enumerate(collection) {
if (isObject(collection)) {
if (collection instanceof Map) {
return enumerateMap(collection)
} else if (isIterable(collection)) {
return enumerateIterable(collection)
} else {
return enumerateObject(collection)
}
} else {
throw new TypeError("Object cannot be enumerated")
}
}

function then(value, proc) {
return isPromise(value) ? value.then(proc) : proc(value)
}

function genStep(gen, chunk, err) {
var done, state, value
if (err) {
state = gen.throw(err)
} else {
state = gen.next(chunk)
}
done = state.done
value = state.value
if (done) {
return value
} else if (value !== next) {
if (isPromise(value)) {
return value.catch(function (err) {
return genStep(gen, undefined, err)
})
.then(function (value) {
return genStep(gen, value)
})
} else {
return genStep(gen, value)
}
}
}

function reduceStream(stream, generator) {
return new Promise(function (resolve, reject) {
var gen = generator(next)
var promise = Promise.resolve(genStep(gen))
stream.on("data", function (chunk) {
stream.pause()
promise = promise.then(function () {
stream.resume()
return genStep(gen, chunk)
})
.catch(reject)
})
stream.on("error", reject)
stream.on("end", function () {
promise = promise.then(function () {
return genStep(gen)
})
.then(resolve, reject)
})
})
}

function _reduceGen(inGen, outGen) {
var state = inGen.next()
return then(genStep(outGen, state.value), function (value) {
if (state.done) {
return then(genStep(outGen), function () {
return value
})
} else {
return _reduceGen(inGen, outGen)
}
})
}

function reduceGen(inGen, generator) {
var outGen = generator(next)
return then(genStep(outGen), function () {
return _reduceGen(inGen, outGen)
})
}

function reduceAny(collection, generator) {
if (collection instanceof Stream) {
return reduceStream(collection, generator)
} else {
return reduceGen(enumerate(collection), generator)
}
}

module.exports = reduceAny

45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "co-reduce-any",
"version": "1.0.0",
"description": "Reduce anything in Co-like manner",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha test/* --recursive",
"coverage": "istanbul cover node_modules/.bin/_mocha -- test/* --recursive"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lajw/co-reduce-any.git"
},
"keywords": [
"co",
"reduce",
"any",
"foreach",
"stream",
"map",
"set",
"array",
"object",
"generator",
"promise",
"asynchronous"
],
"author": "Lukasz A.J. Wrona <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/lajw/co-reduce-any/issues"
},
"homepage": "https://github.com/lajw/co-reduce-any#readme",
"dependencies": {
"typical": "^2.6.0"
},
"devDependencies": {
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"stream-array": "^1.1.2"
}
}
Loading

0 comments on commit da8e46a

Please sign in to comment.