-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (57 loc) · 1.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* A Factory Module for the Modulous-Two Package
* The proper use of this module would be to do something
* similar to this:
*
* // example.js
* const even = require('modulous-two')('simple')
*
* const test1 = even(2) // true
* const test2 = even(3) // false
* // end of example
*
* @author Mitchell Stack
* @param {String} sol The name of the selected solution
* @return {Function} Return the isEven function requested
*/
const isEven = (sol) => {
const solutions = [{
name: 'simple',
func: require('./solutions/sol_simple.js')
}, {
name: 'traditional',
func: require('./solutions/sol_traditional.js')
}, {
name: 'binary-string',
func: require('./solutions/sol_binaryString.js')
}, {
name: 'node-is-even',
func: require('./solutions/mod_isEven.js')
}, {
name: 'node-big-integer',
func: require('./solutions/mod_bigInteger.js')
}, {
name: 'regex',
func: require('./solutions/sol_regex.js')
}]
// Returns an array of possible solutions by name
const names = solutions.map((s) => s.name)
// Gets the index of the name chosen from sol arg
const solution = solutions[names.indexOf(sol)]
// Return function
let fn
try {
// Assign return function as requested
fn = solution.func
} catch (e) {
// Default to simple solution if error
fn = solutions[0].func
}
// For testing purposes, return available names
if (sol === 'DEBUG') {
return names
}
// Return the new function
return fn
}
module.exports = isEven