Trine - functional utility library for modern JavaScript

December 31, 2015

As we know, functional programming in JavaScript is not the best experience, unlike the plain functional languages (Haskell, Lisp, etc). But new standards of JavaScript - ES6 (ECMAScript 2015) and ES7(ECMAScript 2016) introduce some improvements and allows you to write code in more functional style:


But there’s more: new function bind syntax performs function binding and method extraction. The previous example could be rewritten as:


ES6 also introduces the concept of iterators - protocol, that most JS collection types implement. You can extend your custom collections to support the same protocol, and generators functions support it too. So, if you looking for utility library that supports new function bind syntax, and iterators, you can take a look on Trine.


First of all, you may want to use all features of ES6 and most of ES7 standards with Babel:

npm install --save babel babel-preset-stage-0 babel-preset-es2015
  "presets": ["stage-0", "es2015"]

Then install Trine:

npm install --save trine

and import to your project required functions:

import { last } from "trine/iterable/last"

const lastCh = ["a", "b", "c"]::last(0) // yields 'c' // c


Trine provides common boolean helpers:

true::not() // false
true::and(false) // false
true::or(false) // true
true::xor(false) // true
0::not() // true


10::min(100) - // 10
  100::abs() // 100
4::div(2) // 2
4::mod(3) // 1
2::pow(4)::max(15) // 16


Extract , compare or convert values to functions:

"foo"::equals("bar")::is(false) // true
const func = { foo: "bar" }::prop("foo")::toFunction()
func() // bar


Trine includes partial helper:

parseInt::partial(_, 2)("1010") // 10


let obj = { foo: "bar" }
obj::keys().next().value // foo
obj::values().next().value // bar


let nums = [5, 1]
nums::count().next().value // 2
nums::reverse()::to(Array) // 1, 5
  ::to(Array) // a, b, c
  [(nums, [2, 3])]::flatten()
  ::map(function () {
    return this * 3
  ::to(Array) // 15, 3, 6, 9


If you like new syntax of ES6, ES7 and want to use iterators, function binding, you can use Trine as base utility library. If you want to know more about Trine, you can take a look at it’s documentation .

Neiro © 2015 - 2020