!geek

Code, learn & share

Introducing f.js

Javascript code is quite verbose when compared to other langauges. One of the features I missed coming from the ruby world is lambdas and the &: sugar.

1
2
3
books.map { |book| => book.title }
# or even better
books.map(&:title)

compare this with javascript code

1
books.map(function(book) { return book.title })

Good news is ES6 might come with proposed arrow functions, but it might take a while before all the browsers implement this. This is where f.js can be handy. This library will help to write redable code by writing less. You can write the above code as

1
2
3
books.map(f.y('book => book.title'))
// or
books.map(f.x('title'))

f.js supports methods, negation and includes utilities like noop and identity. Examples

1
2
3
4
5
6
7
8
9
10
11
movies.filter(f.x('!watched'))
people.map(f.x('fullName()'))
shows.filter(f.x('!isGood()'))

//Lambda with multiple parameters
//sort movies in descending order on rating
movies.sort(f.y('(m1, m2) => m2.rating - m1.rating'));

//Noop and identity examples
books.forEach(callback || f()) //f() returns noop function
movies.filter(filterFunction || f(true)) //f(arg) returns identity function

The f.y(lambda) method uses same syntax as arrow functions. Once ES6 arrow functions feature is available on all browsers, write a regex to remove usage of f.y and say bye to f.js

Code & Documentation: https://github.com/endeepak/f.js

Comments