-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasala.js
211 lines (167 loc) · 5.86 KB
/
masala.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.masala = factory();
}
}(this, function () {
"use strict";
var slice = Array.prototype.slice;
var call = Function.prototype.call;
var toString = Object.prototype.toString;
var toArray = function () { return call.apply(slice, arguments); };
var not = function (fn) { return function() { return !fn.apply(this, arguments); }; };
var isUndefined = function(key) { return this[key] === undefined; };
var notUndefined = not(isUndefined);
var existsIn = function (key) { return this.indexOf(key) != -1; };
var doesntExistIn = not(existsIn);
function isPlainObject (o) {
return (theTypeOf(o) === 'object'
&& (o.constructor === Object
|| typeof o.constructor === 'undefined'));
}
function getAllKeys (obj) {
var keys = [];
for (var key in obj) {
if (Object.prototype.propertyIsEnumerable.call(obj, key)) {
keys.push(key);
}
}
return keys;
}
function merge (dest, source) {
var validKeys = getAllKeys(source);
validKeys.forEach(function (key) {
dest[key] = source[key];
});
return dest;
}
function mergeNotNull (dest, source) {
var validKeys = getAllKeys(source)
.filter(notUndefined, source);
validKeys.forEach(function (key) {
dest[key] = source[key];
});
return dest;
}
function theTypeOf (thing) {
var type = toString.call(thing);
return type.toLowerCase().match(/^\[object (.+)\]$/)[1];
}
function makeArguments (numberOfArgs) {
var letters = [];
for ( var i = 1; i <= numberOfArgs; i++ ) letters.push("arg" + i);
return letters;
}
function wrapFunctionWithArity (arity, name, callback) {
var argList = makeArguments(arity);
var functionCode = 'return false || function ';
functionCode += name + '(';
functionCode += argList.join(', ') + ') {\n';
functionCode += 'return fn.apply(this, arguments);\n';
functionCode += '};'
return Function("fn", functionCode)(callback);
}
function genSauce (fn, optsPosition, existingOpts, optsRemaining, args, arity, isConstructor) {
var sauce = function (opts) {
var newExistingOpts = existingOpts;
// Check to see if the first argument is a plain object
var argsOffset = +(optsPosition !== -1 && isPlainObject(opts));
// A) Merge arguments
var nextArgs = args.concat(toArray(arguments, argsOffset)),
fnLength = fn.length - (optsPosition !== -1); // remove options-object from length
var nextOptsRemaining = optsRemaining;
if ( argsOffset ) {
// B) Merge options
var optsKeys = getAllKeys(opts),
optsGiven = optsKeys.filter(notUndefined, opts),
optsReset = optsKeys.filter(isUndefined, opts);
nextOptsRemaining = optsRemaining
.filter(doesntExistIn, optsGiven)
.concat(optsReset);
// We create a fresh new `existingOpts` object
// so that each masala'd function is independent
newExistingOpts = mergeNotNull(mergeNotNull({}, existingOpts), opts);
}
// If the stars have aligned, we apply the result
if ( (!nextOptsRemaining || nextOptsRemaining.length === 0) && nextArgs.length >= fnLength ) {
if ( nextArgs.length > fnLength ) nextArgs.splice(fnLength);
// Stick the options object back into the right place
if ( optsPosition !== -1 ) nextArgs.splice(optsPosition, 0, newExistingOpts);
if ( isConstructor ) {
if (this instanceof fn) {
var newObj = this;
} else {
var newObj = Object.create(fn.prototype);
}
var tempObj = fn.apply(newObj, nextArgs);
return tempObj || newObj;
} else {
return fn.apply(this, nextArgs);
}
}
var remainingArity = Math.max(0, fnLength - nextArgs.length);
return genSauce(fn, optsPosition, newExistingOpts, nextOptsRemaining, nextArgs, remainingArity, isConstructor);
};
var wrappedSauce = wrapFunctionWithArity(arity, fn.name, sauce);
wrappedSauce.options = (optsRemaining && optsRemaining.slice()) || [];
if (isConstructor) {
wrappedSauce.prototype = fn.prototype;
}
return wrappedSauce;
}
function masala (fn, optsPosition, opts) {
var args = toArray(arguments, 3),
arity = fn.length,
optsRemaining, defaultOpts,
isConstructor = !!(this && this.constructor === masala);
if ( isPlainObject(optsPosition) ) {
args = toArray(arguments, 2);
opts = optsPosition;
optsPosition = 0;
}
if ( isPlainObject(opts) ) {
optsRemaining = getAllKeys(opts).filter(isUndefined, opts);
defaultOpts = mergeNotNull({}, opts);
arity--;
} else {
args = toArray(arguments, 1);
optsPosition = -1;
}
arity -= args.length;
return genSauce(fn, optsPosition, defaultOpts, optsRemaining, args, arity, isConstructor);
}
// `masala.inherits` attempts to replicate the functionality of node.js's `util.inherits`
// while also allowing curry-like handling of options.
// To do this, `ctor` inherits any remaining options from `superCtor` so that the
// `ctor` isn't executed until it and `superCtor` has all the options they *both*
// require.
masala.inherits = function (ctor, superCtor, optsPosition, opts) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
if ( isPlainObject(optsPosition) ) {
opts = optsPosition;
optsPosition = 0;
}
var remainingOptions = superCtor.options || [];
if (!isPlainObject(opts)) {
opts = {};
}
var optionOpts = remainingOptions.reduce(function(obj, key){
obj[key] = undefined;
return obj;
}, {});
var mergedOpts = merge(opts, optionOpts);
return new masala(ctor, optsPosition, mergedOpts);
};
return masala;
}));