UNPKG

1.03 kBMarkdownView Raw
1# Chaining
2
3Math.js supports chaining operations by wrapping a value into a `Chain`.
4A chain can be created with the function `math.chain(value)`
5(formerly `math.select(value)`).
6All functions available in the math namespace can be executed via the chain.
7The functions will be executed with the chain's value as the first argument,
8followed by extra arguments provided by the function call itself.
9
10```js
11math.chain(3)
12 .add(4)
13 .subtract(2)
14 .done() // 5
15
16math.chain( [[1, 2], [3, 4]] )
17 .subset(math.index(0, 0), 8)
18 .multiply(3)
19 .done() // [[24, 6], [9, 12]]
20```
21
22### API
23
24A `Chain` is constructed as:
25
26```js
27math.chain()
28math.chain(value)
29```
30
31The `Chain` has all functions available in the `math` namespace, and has
32a number of special functions:
33
34 - `done()`
35 Finalize the chain and return the chain's value.
36 - `valueOf()`
37 The same as `done()`, returns the chain's value.
38 - `toString()`
39 Executes `math.format(value)` onto the chain's value, returning
40 a string representation of the value.
41