-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (42 loc) · 1.56 KB
/
index.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
'use strict';
const path = require('path');
const fs = require('fs');
const functionDirectoryPath = path.join(__dirname, 'functions');
const dotJsMatcher = /\.js$/;
const exportNames = fs.readdirSync(functionDirectoryPath)
.filter(name => dotJsMatcher.test(name))
.map(name => name.replace(dotJsMatcher, ''));
/**
* The main export, which has every function in functions/ added as a property
* @param {Object} [options] - settings for the import
* @param {(String|Boolean)} options.extendPrototype - If true, all functions
* in functions/ are added to the Set prototype. If an array, only the
* names specified in the array are added to the prototype
* @return {Function} self
*/
function handleExport(options) {
if (options && options.extendPrototype) {
let extensions = options.extendPrototype.forEach ? options.extendPrototype : exportNames;
extensions.forEach(name => {
extendSetPrototype(name, handleExport[name]);
});
}
return handleExport;
}
/**
* Adds a function to the Set prototype so that the set instance is passed as
* the first argument.
* @param {String} name - name of the prototype method
* @param {Function} method
*/
function extendSetPrototype(name, method) {
Set.prototype[name] = function() {
let argumentArray = Array.prototype.slice.call(arguments, 0)
argumentArray.unshift(this);
return method.apply(null, argumentArray);
};
}
exportNames.forEach(name => {
handleExport[name] = require(path.join(functionDirectoryPath, name));
});
module.exports = handleExport;