FxJS - Functional Extensions for Javascript
FxJS is a functional programming library based on ECMAScript 6. Iterable, Iterator, Generator, Promise.
Getting Started
Installation
In Modern Browsers Supporting ES6
fx.js
is a bundle of FxJS written in the ECMAScript Module as a single script file that can be run in a browser.
Note: fx.js
uses thefx
, _
,L
, and C
properties of the window object as namespaces.
In Legacy ES5 Browsers
fx.es5.js
is the build of FxJS as an IE11 browser target.
Note: Like fx.js
, fx.es5.js
also use the window object'sfx
, _
,L
, and C
properties as namespace.
In Node.js
FxJS is developed as ECMAScript Module.
However, the files published in the fxjs
package are the CommonJS Module,
which is transpiled to support Node.js 6 version.
npm install fxjs
const FxJS = ;const _ = ;const L = ;const C = ; // The default module that imported has all the functions in fxjs, including Lazy and Concurrency.const reduce filterL mapC = FxJS; // You can also import the functions individually.const rangeL = ; _; // 40
Module bundlers generally don't support Tree-Shaking to CommonJS modules, so when using the fxjs
package, it is recommended that you import functions individually.
;;;;;;; ; // 40
ECMAScript Module
FxJS publishes the fxjs2
package, which is written only with the native ECMAScript Module.
In the fxjs2
package, the type
field is defined as module
in the package.json
file.
Development tools like mocha and jest do not yet support Native ESM, so be careful about using it.
npm install fxjs2
;; ; // 40
Iteration protocols
You can evaluate the iterator as a function of FxJS.
{ let a = 0 b = 1; while true a; a b = b a + b; } const f = ; const iterator = ;console; // { value: 0, done: false }console; // { value: 2, done: false }console; // { value: 8, done: false }console; // { value: undefined, done: true } ;// 10
Iterable programming
Any value can be used with FxJS if it has a [Symbol.iterator]()
method.
const res = ; ; // 9
Lazy evaluation
You can do 'lazy evaluation' as a function of the L
namespace.
const res = ; ; // 9
FRP style
Functional reactive programming style.
;// After 1 second 10// After 2 seconds 11// After 3 seconds 12
Promise/async/await
Asynchronous control is easy.
// L.interval = time => L.map(delay(time), L.range(Infinity)); await ;// After 1 second 30// After 2 seconds 31// After 3 seconds 32// After 4 seconds 33 const res = await ;// After 5 seconds 20// After 6 seconds 21// After 7 seconds 22 ;// 63
Concurrency
C
functions can be evaluated concurrency.
await ;// After 4 seconds// [page1, page2, page3, page4] const pages = await C;// After 1 second// [page1, page2, page3, page4]
Like Clojure Reducers, you can handle concurrency.
;// After 4 seconds// { html: 78, css: 36, is: 192 ... } ;// After 1 second// { html: 78, css: 36, is: 192 ... } ;// After 2 second// { html: 78, css: 36, is: 192 ... }
Error handling
You can use JavaScript standard error handling.
const b = ; console;// 111 try const b = ; console; catch c console;// { hi: 'ho' }
You can use async/await and try/catch to handle asynchronous error handling.
const b = await ; console;// 111 try const b = await ; console; catch c console;// { hi: 'ho' }
API
- Function
- Strict
- add
- append
- baseSel
- chunk
- compact
- countBy
- deepFlat
- defaults
- defaultTo
- delay
- difference
- differenceBy
- differenceWith
- divide
- drop
- dropRight
- dropUntil
- dropWhile
- each
- entries
- extend
- filter
- find
- findWhere
- flat
- flatMap
- groupBy
- head
- identity
- indexBy
- initial
- insert
- intersection
- intersectionBy
- intersectionWith
- join
- keys
- last
- map
- mapEntries
- mapObject
- max
- maxBy
- mean
- meanBy
- min
- minBy
- multiply
- noop
- object
- omit
- omitBy
- partition
- pick
- pickBy
- pluck
- prepend
- promiseAllEntries
- promiseAllObject
- range
- reduce
- reject
- remove
- repeat
- replace
- sel
- slice
- sort
- sortBy
- sortByDesc
- sortDesc
- split
- splitEvery
- subtract
- sum
- sumBy
- tail
- take
- take1
- takeAll
- takeUntil
- takeWhile
- times
- toIter
- union
- unionBy
- unionWith
- unique
- uniqueBy
- uniqueWith
- unzip
- update
- updateBy
- values
- zip
- zipObj
- zipWith
- Predicates
- Lazy
- L.append
- L.chunk
- L.compact
- L.concat
- L.constant
- L.deepFlat
- L.difference
- L.differenceBy
- L.differenceWith
- L.drop
- L.dropUntil
- L.dropWhile
- L.empty
- L.entries
- L.filter
- L.flat
- L.flatMap
- L.insert
- L.intersection
- L.intersectionBy
- L.intersectionWith
- L.interval
- L.keys
- L.limitLoad
- L.map
- L.mapEntries
- L.prepend
- L.range
- L.reject
- L.remove
- L.repeat
- L.reverse
- L.slice
- L.splitEvery
- L.take
- L.takeUntil
- L.takeWhile
- L.times
- L.union
- L.unionBy
- L.unionWith
- L.unique
- L.uniqueBy
- L.uniqueWith
- L.update
- L.updateBy
- L.values
- L.zip
- L.zipWithIndex
- Concurrency
- Stoppable
- String
Extention Libraries
The above libraries are based on FxJS. FxSQL and FxDOM are libraries that can handle SQL and DOM through functional APIs,respectively. FxContrib is contributors' library for FxJS, FxSQL and FxDOM.