map-filter-reduce
Advanced tools
Comparing version 2.2.1 to 3.0.1
17
index.js
var pull = require('pull-stream') | ||
var filter = require('./filter') | ||
var map = require('./map') | ||
var reduce = require('./reduce') | ||
var make = require('./make') | ||
var SinkThrough = require('pull-sink-through') | ||
@@ -15,3 +13,3 @@ | ||
var s = k.substring(1) | ||
if(k[0] == '$' && exports[s]) return exports[s](q[k]) | ||
if(k[0] == '$' && exports[s]) return exports[s](q) | ||
throw new Error('unknown function:'+ k) | ||
@@ -39,16 +37,19 @@ } | ||
exports.filter = function (q) { | ||
return pull.filter(filter(q)) | ||
return pull.filter(make(q)) | ||
} | ||
exports.map = function (q) { | ||
return pull(pull.map(map(q)),pull.filter()) | ||
return pull(pull.map(make(q)),pull.filter()) | ||
} | ||
exports.reduce = function (q, cb) { | ||
//TODO: realtime reduce. | ||
if(cb) | ||
return pull.reduce(reduce(q), null, cb) | ||
return pull.reduce(make(q), null, cb) | ||
return pull(SinkThrough(function (cb) { | ||
return pull.reduce(reduce(q), null, cb) | ||
return pull.reduce(make(q), null, cb) | ||
}), pull.flatten()) | ||
} | ||
{ | ||
"name": "map-filter-reduce", | ||
"description": "", | ||
"version": "2.2.1", | ||
"version": "3.0.1", | ||
"homepage": "https://github.com/dominictarr/map-filter-reduce", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -220,9 +220,2 @@ # map-filter-reduce | ||
Sometimes, it's easier to work with an array or stream of the groups, | ||
but sometimes we'd rather have elements grouped into an object. | ||
For that we use the `$group` operator. | ||
``` js | ||
{$reduce: {$group: ['country', 'city'], $reduce: {$count: true}}} | ||
``` | ||
This would return a object, with the shape `{<country>:{<city>: <population>}}`. | ||
@@ -245,1 +238,2 @@ | ||
var tape = require('tape') | ||
var filter = require('../filter') | ||
var make = require('../make') | ||
function filter (q) { | ||
return make({$filter: q}) | ||
} | ||
var input = [ | ||
@@ -4,0 +7,0 @@ { |
var tape = require('tape') | ||
var filter = require('../filter') | ||
var make = require('../make') | ||
function filter(query) { | ||
return make({$filter: query}) | ||
} | ||
var data = [ | ||
@@ -5,0 +9,0 @@ 'string', |
var tape = require('tape') | ||
var make = require('../map') | ||
var make = require('../make') | ||
var inputs = [ | ||
@@ -9,3 +10,3 @@ 'string' | ||
function map(q, v) { | ||
return make(q)(v) | ||
return make({$map: q})(v) | ||
} | ||
@@ -17,2 +18,3 @@ | ||
t.deepEqual(map('key', {key: 1}), 1) | ||
//xxx | ||
t.deepEqual(map({foo: true}, {foo: 1, bar: 2}), {foo: 1}) | ||
@@ -48,3 +50,1 @@ t.deepEqual(map({bar: 'bar'}, {foo: 1, bar: 2}), {bar: 2}) | ||
@@ -16,3 +16,3 @@ var pull = require('pull-stream') | ||
pull.values(data), | ||
mfr.reduce(query), | ||
mfr.reduce({$reduce: query}), | ||
pull.collect(function (err, ary) { | ||
@@ -19,0 +19,0 @@ if(err) throw err |
var tape = require('tape') | ||
var R = require('../reduce') | ||
var r = require('../make') | ||
var numbers = [1,2,3,4,5] | ||
function R(query) { | ||
return r({$reduce:query}) | ||
} | ||
tape('easy', function (t) { | ||
@@ -65,17 +69,17 @@ t.equal(numbers.reduce(R({$count: true})), 5) | ||
) | ||
return t.end() | ||
t.deepEqual( | ||
objs.reduce(R({ | ||
$group: 'baz', $reduce: {foo: {$max:'foo'}, bar: {$sum: 'bar'}} | ||
}), null), | ||
{"true": {foo: 10, bar: 10}, "false": {foo: 0, bar: 5}} | ||
) | ||
// return t.end() | ||
// t.deepEqual( | ||
// objs.reduce(R({ | ||
// $group: 'baz', $reduce: {foo: {$max:'foo'}, bar: {$sum: 'bar'}} | ||
// }), null), | ||
// {"true": {foo: 10, bar: 10}, "false": {foo: 0, bar: 5}} | ||
// ) | ||
// | ||
// t.deepEqual( | ||
// objs.reduce(R({ | ||
// $group: 'baz', $reduce: {foo: {$max:'foo'}, bar: {$collect: 'bar'}} | ||
// }), null), | ||
// {"true": {foo: 10, bar: [2,3,5]}, "false": {foo: 0, bar: [1,4]}} | ||
// ) | ||
t.deepEqual( | ||
objs.reduce(R({ | ||
$group: 'baz', $reduce: {foo: {$max:'foo'}, bar: {$collect: 'bar'}} | ||
}), null), | ||
{"true": {foo: 10, bar: [2,3,5]}, "false": {foo: 0, bar: [1,4]}} | ||
) | ||
t.end() | ||
@@ -100,118 +104,1 @@ }) | ||
var groups = [ | ||
{ | ||
name: 'pfraze', country: 'US', dwelling: 'apartment' | ||
}, | ||
{ | ||
name: 'substack', country: 'US', dwelling: 'house' | ||
}, | ||
{ | ||
name: 'mix', country: 'NZ', dwelling: 'house' | ||
}, | ||
{ | ||
name: 'du5t', country: 'US', dwelling: 'apartment' | ||
}, | ||
{ | ||
name: 'dominic', country: 'NZ', dwelling: 'sailboat' | ||
} | ||
] | ||
tape('more groups', function (t) { | ||
t.deepEqual(groups.reduce(R({ | ||
country: 'country', dwelling: 'dwelling', people: {$collect: 'name'} | ||
}), null), [ | ||
{ | ||
country: 'NZ', dwelling: 'house', people: ['mix'] | ||
}, | ||
{ | ||
country: 'NZ', dwelling: 'sailboat', people: ['dominic'] | ||
}, | ||
{ | ||
country: 'US', dwelling: 'apartment', people: ['pfraze', 'du5t'] | ||
}, | ||
{ | ||
country: 'US', dwelling: 'house', people: ['substack'] | ||
} | ||
]) | ||
t.end() | ||
}) | ||
tape('more groups, object', function (t) { | ||
t.deepEqual(groups.reduce(R({ | ||
$group: ['country', 'dwelling'], | ||
$reduce: {$collect: 'name'} | ||
}), null), | ||
{ | ||
US: { | ||
apartment: ['pfraze', 'du5t'], | ||
house: ['substack'] | ||
}, | ||
NZ: { | ||
house: ['mix'], | ||
sailboat: ['dominic'] | ||
} | ||
} | ||
) | ||
t.end() | ||
}) | ||
tape('nested object groups', function (t) { | ||
t.deepEqual( | ||
groups.reduce(R({ | ||
$group: 'country', | ||
$reduce: { | ||
population: {$count: true}, | ||
housing: {$group: 'dwelling', $reduce: { $count: true }} | ||
} | ||
}), null), | ||
{ US: { population: 3, housing: { apartment: 2, house: 1 } }, | ||
NZ: { population: 2, housing: { house: 1, sailboat: 1 } } } | ||
) | ||
t.end() | ||
}) | ||
tape('nested array groups', function (t) { | ||
t.deepEqual( | ||
groups.reduce(R({ | ||
dwelling: 'dwelling', | ||
citizens: {$reduce: { | ||
name: 'name', country: 'country' | ||
}} | ||
}), null), | ||
[ | ||
{dwelling: 'apartment', citizens: [ | ||
{name: 'du5t', country: 'US'}, | ||
{name: 'pfraze', country: 'US'} | ||
]}, | ||
{dwelling: 'house', citizens: [ | ||
{name: 'mix', country: 'NZ'}, | ||
{name: 'substack', country: 'US'} | ||
]}, | ||
{dwelling: 'sailboat', citizens: [ | ||
{name: 'dominic', country: 'NZ'} | ||
]} | ||
] | ||
) | ||
t.end() | ||
}) | ||
tape('nested array groups', function (t) { | ||
t.deepEqual( | ||
groups.reduce(R({ | ||
dwelling: 'dwelling', | ||
citizens: {$group: 'country', $reduce: { | ||
$count: true | ||
}} | ||
}), null), | ||
[ | ||
{dwelling: 'apartment', citizens: {US: 2}}, | ||
{dwelling: 'house', citizens: {NZ: 1, US: 1}}, | ||
{dwelling: 'sailboat', citizens: {NZ: 1}} | ||
] | ||
) | ||
t.end() | ||
}) | ||
34
util.js
@@ -7,2 +7,4 @@ 'use strict' | ||
var isInteger = Number.isInteger | ||
function isBoolean (b) { return 'boolean' === typeof b } | ||
@@ -18,2 +20,7 @@ | ||
// [] or {} | ||
function isContainer (o) { | ||
return o && 'object' == typeof o | ||
} | ||
function has(o, k) { | ||
@@ -142,11 +149,13 @@ return Object.hasOwnProperty.call(o, k) | ||
exports.isString = isString | ||
exports.isNumber = isNumber | ||
exports.isBasic = isBasic | ||
exports.isArray = isArray | ||
exports.isObject = isObject | ||
exports.isRange = isRange | ||
exports.isExact = isExact | ||
exports.isLtgt = isLtgt | ||
exports.isFunction = isFunction | ||
exports.isString = isString | ||
exports.isNumber = isNumber | ||
exports.isInteger = isInteger | ||
exports.isBasic = isBasic | ||
exports.isArray = isArray | ||
exports.isObject = isObject | ||
exports.isContainer = isContainer | ||
exports.isRange = isRange | ||
exports.isExact = isExact | ||
exports.isLtgt = isLtgt | ||
exports.isFunction = isFunction | ||
@@ -168,8 +177,1 @@ exports.has = has | ||
31863
21
983
238