-
Notifications
You must be signed in to change notification settings - Fork 0
/
fm.js
173 lines (157 loc) · 3.45 KB
/
fm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
!function(root, name, make) {
if (typeof module != 'undefined' && module.exports) module.exports = make();
else root[name] = make();
}(this, 'fm', function() {
fm.prototype = Fm.prototype
var globe = this
var slice = [].slice
var push = [].push
/**
* @constructor
* @param {*=} value to wrap or Fm instance to clone
*/
function Fm(value) {
this.length = 1
this[0] = value instanceof fm ? value[0] : value
}
/**
* @param {*=} value to wrap or Fm instance to clone
* @return {Fm} an Fm instance
*/
function fm(value) {
return new Fm(value)
}
/**
* @param {Function} f
* @param {Array|Arguments} a
* @return {Array|undefined}
*/
function extra(f, a) {
if (a.length > f.length) return slice.call(a, f.length)
}
/**
* @param {Array} a
* @param {Array|Arguments} args
* @return {Array}
*/
function append(a, args) {
push.apply(a = a.slice(), args)
return a
}
/**
* @param {Function} f
* @param {*=} scope
* @return {Function}
*/
function bind(f, scope) {
var rest = extra(bind, arguments)
return rest ? function() {
return f.apply(scope, append(rest, arguments))
} : function() {
return f.apply(scope, arguments)
}
}
/**
* @param {Function} f
* @return {Function}
*/
function partial(f) {
var rest = extra(partial, arguments)
return rest ? function() {
return f.apply(this, append(rest, arguments))
} : function() {
return f.apply(this, arguments)
}
}
/**
* @param {*} method name
* @return {Function}
*/
function late(method) {
return typeof method == 'function' ? method : function() {
return this[method].apply(this, arguments)
}
}
/**
* @param {*=} value
* @return {Function}
*/
function constant(value) {
return function() {
return value
}
}
/**
* @return {Array}
*/
function got() {
return slice.call(arguments)
}
/**
* @param {Function} f
* @param {Number} i
* @return {Function}
*/
function eq(f, i) {
return function() {
return f.call(this, arguments[i < 0 ? i + arguments.length : i])
}
}
/**
* @param {Function} first
* @param {Function} next
* @return {Function}
*/
function flow(first, next) {
return function() {
return next.call(this, first.apply(this, arguments))
}
}
/**
* @this {Object} receiver
* @param {Object} from
*/
function mixin(from) {
var to = this
var pro = typeof to == 'function' && to.prototype
if (to == globe) throw new TypeError('@this')
for (var k in from) {
if (from.hasOwnProperty(k)) {
to[k] = from[k]
if (pro) pro[k] = mixin === from[k] ? mixin : method(k)
}
}
return this
}
/**
* @param {string|number} name
* @return {Function}
*/
function method(name) {
return function() {
var a = [this instanceof fm ? this[0] : this]
push.apply(a, arguments)
return fm[name].apply(fm, a)
}
}
/**
* @param {Function} f
* @return {Function}
*/
fm['slice'] = function(f) {
var rest = slice.call(arguments, 1, 3)
return function() {
return f.apply(this, slice.apply(arguments, rest))
}
}
fm['eq'] = eq
fm['bind'] = bind
fm['constant'] = constant
fm['got'] = got
fm['late'] = late
fm['mixin'] = mixin
fm['partial'] = partial
fm['stat'] = partial(bind, fm.call)
fm['flow'] = flow
return fm['mixin'](fm)
});