Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
functools is a JavaScript library for functional programming.
Inspired by: Common Lisp, Clojure and Python.
Function Composition:
var compose = require("functools").compose;
compose(select, update, prettify, display)("body .messages");
Async Function Compositon:
function findFiles(path, callback){ ... }
function readContents(files, callback){ ... }
function upload(files, callback){}
compose.async(findFiles, readContents, upload)('~/messages', function(error, uploadResult){
...
});
Async Juxtaposition:
function turkish(word, callback){ /* some magic here */ }
function french(word, callback){ /* some magic here */ }
function polish(word, callback){ /* some magic here */ }
juxt.async({ 'tr': turkish, 'fr': french, 'pl': polish })("hello", function(error, results){
assert.equal(results.tr, "merhaba");
assert.equal(results.fr, "bonjour");
assert.equal(results.pl, "cześć");
});
Currying:
var fn = require("functools");
var pickEvens = fn.curry(fn.filter)(function(num){ return num%2==0 });
pickEvens([3,1,4]) // returns [4]
pickEvens([1,5,9,2,6,5]) // returns [2,6]
$ npm install functools
or
$ wget https://raw.github.com/azer/functools/master/lib/functools.js
Combine functions in a new one, passing the result of each function to next one, from left to right.
function cube(x){ return x*x*x };
compose(Math.sqrt,cube)(4); // returns 8
Asynchronous, continuation passing based version of compose function. Requires specified functions to call a callback function, passing an error object (if there is one) and the result to be carried.
function receiveMessage(message, callback){ ... callback(); }
function findRelatedUser(message, callback){ ... callback(null, user, message); }
function transmitMessage(user, message){ ... callback(); }
var messageTransmission = compose.async(receiveMessage, findRelatedUser, transmitMessage);
messageTransmission({ msg:"Hello !", 'user': 3 }, function(error, result){
...
})
Transform multiple-argument function into a chain of functions that return each other until all arguments are gathered.
function sum(x,y){ return x+y; }
var add3 = curry(sum, 3);
add3(14); // returns 17
add3(20); // returns 23
Return a new function which will call function with the gathered arguments.
function testPartial(){
var args = reduce(function(x,y){ x+", "+y },arguments);
console.log("this:",this);
console.log("args:",args);
}
partial(testPartial, [3,14], 3.14159)(1,5,9);
The example code above will output:
this: 3.14159
args: 3,14,1,5,9
Call function once for element in iterable.
each(function(el,ind,list){ console.assert( el == list[ind] ); }, [3,1,4]);
Invoke function once for each element of iterable. Creates a new iterable containing the values returned by the function.
function square(n){
return n*n;
}
map(square,[3,1,4,1,5,9]); // returns [9,1,16,1,25,81]
Objects can be passed as well;
var dict = { 'en':'hello', 'tr': 'merhaba', 'fr':'bonjour' };
function capitalize(){
return string.charAt(0).toUpperCase() + string.slice(1);
}
map(capitalize, dict); // returns { 'en':'Hello', 'tr':'Merhaba', 'fr':'Bonjour' }
Apply async function to every item of iterable, receiving a callback function which takes error (if there is) and replacement parameters.
function readFile(id, callback){ ... callback(undefined, data); }
map.async(readFile, ['./foo/bar', './foo/qux', './corge'], function(error, files){
if(error) throw error;
console.log(files[0]); // will put the content of ./foo/bar
});
Construct a new array from those elements of iterable for which function returns true.
filter(function(el,ind,list){ return el%2==0 },[3,1,4]); // returns [4]
Call async function once for each element in iterable, receiving a boolean parameter, and construct a new array of all the values for which function produces true
var users = [ 3, 5, 8, 13, 21 ]; // only user#3 and user#8 have permission in this example
function hasPermission(userId, callback){ ... callback(/* true or false */); }
filter.async(hasPermission, users, function(permittedUsers){
assert.equal(permittedUsers.length, 4);
});
Take a set of functions, return a function that is the juxtaposition of those functions. The returned function takes a variable number of arguments and returns a list containing the result of applying each fn to the arguments.
function inc1(n){ return n+1 };
function inc2(n){ return n+2 };
function inc3(n){ return n+3 };
juxt(inc1, inc2, inc3)(314); // returns [315,316,317]
Async implementation of juxt.
function turkish(word, callback){ /* some magic here */ }
function french(word, callback){ /* some magic here */ }
function polish(word, callback){ /* some magic here */ }
juxt.async(turkish, french, polish)("hello", function(error, results){
assert.equal(results[0], "merhaba");
assert.equal(results[1], "bonjour");
assert.equal(results[2], "cześć");
});
Apply function cumulatively to the items of iterable, as to reduce the iterable to a single value
reduce(function(x,y){ return x*y }, [3,1,4]); // returns 12
Async implementation of reduce.
var users = [2, 3, 5, 8, 13];
function usernames(accum, userId){ ... callback(undefined, accum + ', ' + username); }
reduce.async(usernames, users, function(error, result){
if(error) throw error;
console.log(result); // foo, bar, qux ...
});
FAQs
Utilities for working with functions in JavaScript, with TypeScript
The npm package functools receives a total of 17 weekly downloads. As such, functools popularity was classified as not popular.
We found that functools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.