steampipes
Advanced tools
Comparing version
220
lib/sort.js
@@ -13,110 +13,136 @@ // Generated by CoffeeScript 2.5.1 | ||
// #----------------------------------------------------------------------------------------------------------- | ||
// @$sort = ( settings ) -> | ||
// last = Symbol 'last' | ||
// settings = { key: null, settings..., } | ||
// collector = [] | ||
// return @$ { last, }, ( d, send ) => | ||
// if d is last | ||
// if ( key = settings.key )? | ||
// collector.sort ( a, b ) => | ||
// return -1 if a[ key ] < b[ key ] | ||
// return +1 if a[ key ] > b[ key ] | ||
// return 0 | ||
// else | ||
// collector.sort() | ||
// send d for d in collector | ||
// collector.length = 0 | ||
// return null | ||
// collector.push d | ||
// return null | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.$sort = function(settings) { | ||
var collector, last; | ||
last = Symbol('last'); | ||
settings = { | ||
key: null, | ||
...settings | ||
}; | ||
collector = []; | ||
return this.$({last}, (d, send) => { | ||
var i, key, len; | ||
if (d === last) { | ||
if ((key = settings.key) != null) { | ||
collector.sort((a, b) => { | ||
if (a[key] < b[key]) { | ||
return -1; | ||
} | ||
if (a[key] > b[key]) { | ||
return +1; | ||
} | ||
return 0; | ||
}); | ||
/* https://github.com/mziccard/node-timsort */ | ||
var $sort, TIMSORT, arity, direction, key, ref, ref1, ref2, sorter, type_of, validate_type; | ||
TIMSORT = require('timsort'); | ||
direction = 'ascending'; | ||
sorter = null; | ||
key = null; | ||
switch (arity = arguments.length) { | ||
case 0: | ||
null; | ||
break; | ||
case 1: | ||
direction = (ref = settings['direction']) != null ? ref : 'ascending'; | ||
sorter = (ref1 = settings['sorter']) != null ? ref1 : null; | ||
key = (ref2 = settings['key']) != null ? ref2 : null; | ||
break; | ||
default: | ||
throw new Error(`µ33893 expected 0 or 1 arguments, got ${arity}`); | ||
} | ||
//......................................................................................................... | ||
if (direction !== 'ascending' && direction !== 'descending') { | ||
throw new Error(`µ34658 expected 'ascending' or 'descending' for direction, got ${rpr(direction)}`); | ||
} | ||
//......................................................................................................... | ||
if (sorter == null) { | ||
//....................................................................................................... | ||
type_of = (x) => { | ||
/* NOTE for the purposes of magnitude comparison, `Infinity` can be treated as a number: */ | ||
var R; | ||
R = CND.type_of(x); | ||
if (R === 'infinity') { | ||
return 'number'; | ||
} else { | ||
collector.sort(); | ||
return R; | ||
} | ||
for (i = 0, len = collector.length; i < len; i++) { | ||
d = collector[i]; | ||
send(d); | ||
}; | ||
//....................................................................................................... | ||
validate_type = (type_a, type_b, include_list = false) => { | ||
if (type_a !== type_b) { | ||
throw new Error(`µ35423 unable to compare a ${type_a} to a ${type_b}`); | ||
} | ||
collector.length = 0; | ||
if (include_list) { | ||
if (type_a !== 'number' && type_a !== 'date' && type_a !== 'text' && type_a !== 'list') { | ||
throw new Error(`µ36188 unable to compare values of type ${type_a}`); | ||
} | ||
} else { | ||
if (type_a !== 'number' && type_a !== 'date' && type_a !== 'text') { | ||
throw new Error(`µ36953 unable to compare values of type ${type_a}`); | ||
} | ||
} | ||
return null; | ||
}; | ||
//....................................................................................................... | ||
if (key != null) { | ||
sorter = (a, b) => { | ||
a = a[key]; | ||
b = b[key]; | ||
validate_type(type_of(a), type_of(b), false); | ||
if ((direction === 'ascending' ? a > b : a < b)) { | ||
return +1; | ||
} | ||
if ((direction === 'ascending' ? a < b : a > b)) { | ||
return -1; | ||
} | ||
return 0; | ||
}; | ||
} else { | ||
//....................................................................................................... | ||
sorter = (a, b) => { | ||
var type_a, type_b; | ||
validate_type((type_a = type_of(a)), (type_b = type_of(b)), true); | ||
if (type_a === 'list') { | ||
a = a[0]; | ||
b = b[0]; | ||
validate_type(type_of(a), type_of(b), false); | ||
} | ||
if ((direction === 'ascending' ? a > b : a < b)) { | ||
return +1; | ||
} | ||
if ((direction === 'ascending' ? a < b : a > b)) { | ||
return -1; | ||
} | ||
return 0; | ||
}; | ||
} | ||
collector.push(d); | ||
return null; | ||
}); | ||
} | ||
//......................................................................................................... | ||
$sort = () => { | ||
var collector; | ||
collector = []; | ||
return this.$({ | ||
last: null | ||
}, (data, send) => { | ||
var i, len, x; | ||
if (data != null) { | ||
collector.push(data); | ||
} else { | ||
TIMSORT.sort(collector, sorter); | ||
for (i = 0, len = collector.length; i < len; i++) { | ||
x = collector[i]; | ||
send(x); | ||
} | ||
collector.length = 0; | ||
} | ||
return null; | ||
}); | ||
}; | ||
//......................................................................................................... | ||
return $sort(); | ||
}; | ||
// #----------------------------------------------------------------------------------------------------------- | ||
// @$sort = ( settings ) -> | ||
// ### https://github.com/mziccard/node-timsort ### | ||
// TIMSORT = require 'timsort' | ||
// direction = 'ascending' | ||
// sorter = null | ||
// key = null | ||
// switch arity = arguments.length | ||
// when 0 then null | ||
// when 1 | ||
// direction = settings[ 'direction' ] ? 'ascending' | ||
// sorter = settings[ 'sorter' ] ? null | ||
// key = settings[ 'key' ] ? null | ||
// else throw new Error "µ33893 expected 0 or 1 arguments, got #{arity}" | ||
// #......................................................................................................... | ||
// unless direction in [ 'ascending', 'descending', ] | ||
// throw new Error "µ34658 expected 'ascending' or 'descending' for direction, got #{rpr direction}" | ||
// #......................................................................................................... | ||
// unless sorter? | ||
// #....................................................................................................... | ||
// type_of = ( x ) => | ||
// ### NOTE for the purposes of magnitude comparison, `Infinity` can be treated as a number: ### | ||
// R = CND.type_of x | ||
// return if R is 'infinity' then 'number' else R | ||
// #....................................................................................................... | ||
// validate_type = ( type_a, type_b, include_list = no ) => | ||
// unless type_a is type_b | ||
// throw new Error "µ35423 unable to compare a #{type_a} to a #{type_b}" | ||
// if include_list | ||
// unless type_a in [ 'number', 'date', 'text', 'list', ] | ||
// throw new Error "µ36188 unable to compare values of type #{type_a}" | ||
// else | ||
// unless type_a in [ 'number', 'date', 'text', ] | ||
// throw new Error "µ36953 unable to compare values of type #{type_a}" | ||
// return null | ||
// #....................................................................................................... | ||
// if key? | ||
// sorter = ( a, b ) => | ||
// a = a[ key ] | ||
// b = b[ key ] | ||
// validate_type ( type_of a ), ( type_of b ), no | ||
// return +1 if ( if direction is 'ascending' then a > b else a < b ) | ||
// return -1 if ( if direction is 'ascending' then a < b else a > b ) | ||
// return 0 | ||
// #....................................................................................................... | ||
// else | ||
// sorter = ( a, b ) => | ||
// validate_type ( type_a = type_of a ), ( type_b = type_of b ), yes | ||
// if type_a is 'list' | ||
// a = a[ 0 ] | ||
// b = b[ 0 ] | ||
// validate_type ( type_of a ), ( type_of b ), no | ||
// return +1 if ( if direction is 'ascending' then a > b else a < b ) | ||
// return -1 if ( if direction is 'ascending' then a < b else a > b ) | ||
// return 0 | ||
// #......................................................................................................... | ||
// $sort = => | ||
// collector = [] | ||
// return @$ { last: null, }, ( data, send ) => | ||
// if data? | ||
// collector.push data | ||
// else | ||
// TIMSORT.sort collector, sorter | ||
// send x for x in collector | ||
// collector.length = 0 | ||
// return null | ||
// #......................................................................................................... | ||
// return $sort() | ||
}).call(this); | ||
//# sourceMappingURL=sort.js.map |
{ | ||
"name": "steampipes", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "Fast, simple data pipelines", | ||
@@ -33,3 +33,4 @@ "main": "lib/main.js", | ||
"intertype": "^3.1.1", | ||
"multimix": "^2.1.3" | ||
"multimix": "^2.1.4", | ||
"timsort": "^0.3.0" | ||
}, | ||
@@ -36,0 +37,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1118999
-0.44%6
20%8051
-0.45%+ Added
+ Added
Updated