1 |
|
2 |
|
3 |
|
4 | const { create, factory, all } = require('../..')
|
5 | const math = create(all)
|
6 |
|
7 |
|
8 | const createCustomValue = factory('CustomValue', ['typed'], ({ typed }) => {
|
9 |
|
10 | function CustomValue (value) {
|
11 | this.value = value
|
12 | }
|
13 | CustomValue.prototype.isCustomValue = true
|
14 | CustomValue.prototype.toString = function () {
|
15 | return 'CustomValue:' + this.value
|
16 | }
|
17 |
|
18 |
|
19 | typed.addType({
|
20 | name: 'CustomValue',
|
21 | test: function (x) {
|
22 |
|
23 | return x && x.isCustomValue === true
|
24 | }
|
25 | })
|
26 |
|
27 | return CustomValue
|
28 | })
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const createAddCustomValue = factory('add', ['typed', 'CustomValue'], ({ typed, CustomValue }) => {
|
35 | return typed('add', {
|
36 | 'CustomValue, CustomValue': function (a, b) {
|
37 | return new CustomValue(a.value + b.value)
|
38 | }
|
39 | })
|
40 | })
|
41 |
|
42 |
|
43 | math.import([
|
44 | createCustomValue,
|
45 | createAddCustomValue
|
46 | ])
|
47 |
|
48 |
|
49 | const ans = math.add(new math.CustomValue(2), new math.CustomValue(3))
|
50 |
|
51 |
|
52 | console.log(ans.toString())
|
53 |
|