0.0.0 • Published 10 years ago
morejs v0.0.0
more-js
An extended standard library for Javascript.
Built ins
odd, even add, subtract, multiply, divide (curried) empty
1, 2, 3.each(double);
Partial application
function add(x, y) {
return x + y;
}
add.partial(3)(5); // 8
add.partial(null, 5)(3); // 8
add.bound(3, 5)(); // 8
Function composition
function multiply(x, y) {
return x * y;
}
var maths = add.compose(multiply);
Ranges
(1).to(8); // [1, 2, ... 7, 8]
(1).til(8); // [1, 2, ..., 6, 7]
(1).til(10, 2); // [1, 3, 5, 7, 9]
'A'.to('Z'); // ['A', 'B', 'C', ..., 'Z']
String interpolation
("#hello").format({ hello: "World" }); // "World"
("$1").format("world"); // world
Array repeat
[3].repeat(3); // [3, 3, 3]
Each
[1, 2, 3].each(function(num, idx) {
console.log(num * idx);
});
{ 'name': 'Dan' }.each(function(key, value) {
console.log('Value');
});
Object map, filter, reduce
{ 'name': 'Dan' }.map(function(key, value) {
return [key, value];
});
{ 'name': 'Dan' }.filter(function(key, value) {
return false;
});
{ 'name': 'Dan' }.reduce(function(pre, cur) {
return cur + pre;
});
Cheat expressions
[1,2,3,4,5].where('> 5');
// builds string eval('1 > 5')
Compact
[null, 1, true, false, 0, 3].compact(); // [1, true, 3]
Head/tail/last
[1, 2, 3, 4].head(); // 1
[1, 2, 3, 4].tail(); // [2, 3, 4]
[1, 2, 3, 4].last(); // 4
Empty
[].empty(); // true
{}.empty(); // true
Reverse
[3, 2, 1].reverse(); // 1, 2, 3
Unique
[3, 3, 2, 1].unique(); // [3, 2, 1]
Difference, Intersection, Union
[3, 2, 1].difference([1, 4, 6]); // [4, 6]
[3, 2, 1].intersection([1, 4, 6]); // [1]
[3, 2, 1].union([1, 4, 6]); // [1, 2, 3, 4, 6]
Any
[3, 2, 1].any(function(number) {
return number % 2 == 0;
}); // true
All
[3, 2, 1].any(function(number) {
return number % 2 == 0;
}); // false
Take
[3, 2, 1].take(2); // [3, 2]
Drop
[3, 2, 1].drop(2); // [1]
Take while, drop while
[3, 2, 1].takeWhile(function(number) {
return number % 2 == 0;
});
0.0.0
10 years ago