-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomposable.js
More file actions
389 lines (352 loc) · 13.7 KB
/
composable.js
File metadata and controls
389 lines (352 loc) · 13.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
(function () {
// Establish the root object, `window` (`self`) in the browser.
// We use `self` instead of `window` for `WebWorker` support.
var root = typeof self === 'object' && self.self === self && self;
// Transformation helper functions
var isString = function (arg) {
return typeof arg === 'string';
},
isNumber = function (arg) {
return typeof arg === 'number';
},
isFunction = function (arg) {
return typeof arg === 'function';
},
isArray = function (arg) {
return Array.isArray ? Array.isArray(arg) : Object.prototype.toString.call(arg) === '[object Array]';
},
isNodeList = function (arg) {
return Object.prototype.toString.call(arg) === '[object NodeList]';
},
stringIsRegExp = function (string) {
return /^\/(?:\S|\s)*\/[gimy]{0,4}$/.test(string);
},
toRegExp = function (pattern) {
var patternParts = pattern.split('/');
var flagPart = patternParts[patternParts.length - 1];
var patternPart = pattern.slice(1, (flagPart.length + 1) * -1);
return new RegExp(patternPart, flagPart);
},
mapObject = function (object, func) {
var key, newObject = {};
for (key in object) {
if (object.hasOwnProperty(key)) {
newObject[key] = func(key, object[key]);
}
}
return newObject;
},
filterObject = function (object, func) {
var key, newObject = {};
for (key in object) {
if (object.hasOwnProperty(key) && func(key, object[key])) {
newObject[key] = object[key];
}
}
return newObject;
},
// FIXME: cachedQueryFactory should not use DOM nodes as keys to the cache.
cachedQueryFactory = function (cacheKey) {
return function (selector) {
var _this = this;
if (!_this.hasOwnProperty('cache')) {
_this.cache = {};
}
if (!_this.cache.hasOwnProperty(cacheKey)) {
_this.cache[cacheKey] = {};
}
return function (node) {
if (!node) {
return null;
}
if (!_this.cache[cacheKey].hasOwnProperty(node)) {
_this.cache[cacheKey][node] = {};
}
if (!_this.cache[cacheKey][node].hasOwnProperty(selector)) {
_this.cache[cacheKey][node][selector] = node[cacheKey](selector);
}
return _this.cache[cacheKey][node][selector];
};
};
};
// Composable transformations
var transformations = {
// Global object getters
window: function () {
return root;
},
document: function () {
return root.document;
},
// DOM node transformations
querySelectorAll: function (selector) {
return function (node) {
return node ? node.querySelectorAll(selector) : null;
};
},
querySelector: function (selector) {
return function (node) {
return node ? node.querySelector(selector) : null;
};
},
innerHTML: function (node) {
return node ? node.innerHTML : null;
},
innerText: function (node) {
return node ? node.innerText || node.textContent : null;
},
rootInnerText: function (node) {
if (!node) return null;
var textNodes = [];
for (var i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].nodeType === node.TEXT_NODE) {
textNodes.push(Composable.T.trim(Composable.T.innerText(node.childNodes[i])));
}
}
return Composable.T.trim(textNodes.join(' '));
},
value: function (node) {
return node ? node.value : null;
},
getAttribute: function (attr) {
return function (node) {
return node && node.getAttribute ? node.getAttribute(attr) : null;
};
},
// Number transformations
toInt: function (item) {
return isString(item) || isNumber(item) ? parseInt(item, 10) : null;
},
toFloat: function (item) {
return isString(item) || isNumber(item) ? parseFloat(item) : null;
},
round: function (item) {
return isNumber(item) ? Math.round(item) : null;
},
multiplyBy: function (factor) {
return function (number) {
return isNumber(number) ? factor * number : null;
};
},
// String transformations
htmlToText: function (html) {
var div = root.document.createElement('div');
div.innerHTML = html;
var firstChild = div.firstChild;
return firstChild ? firstChild.nodeValue + String() : '';
},
reverse: function (item) {
if (isString(item)) {
return item.split('').reverse().join('');
} else if (isArray(item)) {
return item.reverse();
}
return null;
},
toString: function (item) {
return item ? item + String() : null;
},
trim: function (text) {
return isString(text) ? text.trim() : null;
},
split: function (delimeter, limit) {
var args = [delimeter];
if (typeof limit !== 'undefined') {
args.push(parseInt(limit, 10));
}
return function (text) {
return isString(text) ? text.split.apply(text, args) : null;
};
},
join: function (delimeter) {
return function (array) {
return isArray(array) ? array.join(delimeter) : null;
};
},
replace: function (pattern, replacement) {
pattern = isString(pattern) && stringIsRegExp(pattern) ? toRegExp(pattern) : pattern;
return function (text) {
return isString(text) ? text.replace(pattern, replacement) : null;
};
},
match: function (pattern) {
pattern = isString(pattern) && stringIsRegExp(pattern) ? toRegExp(pattern) : pattern;
return function (text) {
return isString(text) ? text.match(pattern) : null;
};
},
// Array transformations
getIndex: function (index) {
index = parseInt(index, 10);
return function (array) {
return isArray(array) && array.length > index ? array[index] : null;
};
},
slice: function (start, stop) {
var args = [parseInt(start, 10)];
if (typeof stop !== 'undefined') {
args.push(parseInt(stop, 10));
}
return function (input) {
var output;
if (isString(input)) {
output = input ? String.prototype.slice.apply(input, args) : null;
} else if (isArray(input) || isNodeList(input)) {
// Slice needs to work on NodeList instances
output = input ? Array.prototype.slice.apply(input, args) : null;
}
return output;
};
},
// Object transformations
getProperty: function (property) {
return function (object) {
return object ? object[property] : null;
};
},
getProperties: function (propertyString) {
return function (object) {
if (!object) {
return null;
}
var properties = propertyString.split('.'),
property;
while (properties.length) {
property = properties.shift();
object = object ? object[property] : null;
}
return object;
};
}
};
var arrayTransformations = {
map: function (method) {
return function (array) {
return isArray(array) || isNodeList(array) ? Array.prototype.map.call(array, method) : null;
};
}
};
// Allow usage as a function without `new` operator
var Composable = function (config) {
if (!(this instanceof Composable)) {
return new Composable(config);
}
return this.extract(config);
};
Composable.__objectMerge = function () {
var extractedProperties = {};
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'object') {
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
extractedProperties[key] = arguments[i][key];
}
}
}
}
return extractedProperties;
};
// Put transformations on Composable object for global referencing without `new` operator
Composable.prototype.T = Composable.T = Composable.__objectMerge(transformations, arrayTransformations);
// Extract and format data from the DOM based on config
Composable.prototype.extract = function (config) {
var _this = this;
var filteredConfig = filterObject(config, function (key, spec) {
return isArray(spec);
});
return mapObject(filteredConfig, function (key, spec) {
var i, item = null;
for (i = 0; i < spec.length; i += 1) {
item = _this.applyTransformation(item, spec[i]);
}
return item;
});
};
// Apply each transformation to the item, in series
Composable.prototype.applyTransformation = function (item, transformationInput) {
var xform = this.getTransformation(transformationInput);
if (!xform) {
throw 'Transformation "' + transformationInput + '" invalid';
}
return xform(item);
};
Composable.prototype.getTransformation = function (transformationInput) {
if (isFunction(transformationInput)) {
return transformationInput;
} else if (!isArray(transformationInput) && !isString(transformationInput)) {
throw 'Invalid Input Type: ' + typeof transformationInput;
}
if (!transformationInput || !transformationInput.length) {
// either command string is empty or no sub command given in recursion
return null;
}
var transformation;
if (isArray(transformationInput)) {
if (transformationInput.length === 1 && this.T.hasOwnProperty(transformationInput[0])) {
return this.T[transformationInput[0]];
}
transformation = this.getTransformationForArrayTransformation(transformationInput);
} else {
if (this.T.hasOwnProperty(transformationInput)) {
return this.T[transformationInput];
}
transformation = this.getTransformationForStringTransformation(transformationInput);
}
if (!transformation) {
return null;
}
return this.T[transformation.command].apply(this, transformation.args);
};
Composable.prototype.getTransformationForArrayTransformation = function (transformationInput) {
var command = transformationInput[0];
if (!this.T.hasOwnProperty(command)) {
return null;
}
var args = transformationInput.slice(1);
if (arrayTransformations.hasOwnProperty(command)) {
var subCommand = this.getTransformation(args);
if (subCommand) {
args = [subCommand];
}
}
return {command: command, args: args};
};
Composable.prototype.getTransformationForStringTransformation = function (transformationInput) {
var transformationArray = transformationInput.split(':');
var command = transformationArray.shift();
if (!this.T.hasOwnProperty(command)) {
return null;
}
var commandArgString = transformationArray.join(':');
var subCommand;
if (arrayTransformations.hasOwnProperty(command)) {
subCommand = this.getTransformation(commandArgString);
}
var args = subCommand ? [subCommand] : Composable.extractArgs(commandArgString);
return {command: command, args: args};
};
Composable.extractArgs = function (string) {
string = string || '';
var argRegexMatch = string.match(/^(\/.*?(?:(?:\\\\)+|[^\\])\/[gimy]{0,4})(?:,|$)/i);
var args = [];
if (argRegexMatch && argRegexMatch.length) {
var regexWithFlagsWithoutComma = string.slice(0, argRegexMatch[1].length);
args.push(regexWithFlagsWithoutComma);
var lengthOfRegexWithComma = argRegexMatch[0].length;
string = string.slice(lengthOfRegexWithComma);
args = args.concat(string.split(','));
} else if (string.length) {
args = args.concat(string.split(','));
}
return args;
};
Composable.VERSION = '0.4.5';
// Make the object globally accessible
root.Composable = Composable;
// Define it for AMD
if (typeof define === 'function' && define.amd) {
define('composable', [], function () {
return Composable;
});
}
}());