Last active
August 29, 2015 14:11
-
-
Save yeikos/fd2339ea61557926972d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(isNode) { | |
'use strict'; | |
var Public = function(options) { | |
// Public() -> new Public() | |
if (!(this instanceof Public)) | |
return new Public(options); | |
// Opciones por defecto | |
this.options = {}; | |
for(var key in Public.defaults) | |
this.options[key] = Public.defaults[key]; | |
if (options && typeof options === 'object') | |
for(var key in options) | |
this.options[key] = options[key]; | |
}, publicName = 'Calculator'; | |
// Opciones por defecto | |
Public.defaults = { | |
/** | |
* Volcado del resultado por consola. | |
* | |
* @type {Boolean} | |
*/ | |
debug: false | |
}; | |
// Prototipo | |
Public.prototype = { | |
/** | |
* Constructor de la instancia. | |
* | |
* @type {Function} | |
*/ | |
constructor: Public, | |
/** | |
* Opciones por defecto. | |
* | |
* @type {Object} | |
*/ | |
options: null, | |
/** | |
* Operación de suma. | |
* | |
* @return {Number} | |
* | |
* <code> | |
* sum(1, 2, 3); // 6 | |
* sum([1, 2, 3]); // 6 | |
* </code> | |
*/ | |
sum: function() { | |
return _exec.call(this, 'sum', arguments); | |
}, | |
/** | |
* Operación de resta. | |
* | |
* @return {Number} | |
* | |
* <code> | |
* sub(3, 2); // 1 | |
* sub([3, 2]); // 1 | |
* </code> | |
*/ | |
sub: function() { | |
return _exec.call(this, 'sub', arguments); | |
} | |
}; | |
/** | |
* Sucesión de Fibonacci. | |
* | |
* @param {Number} | |
* @return {Number} | |
* | |
* <code> | |
* finobacci(3); // [1, 1, 2] | |
* </code> | |
*/ | |
Public.fibonacci = function(size) { | |
var x = 0, | |
y = 0, | |
z = 1, | |
w = 0, | |
result = []; | |
for(w;w<size;++w) { | |
x = y + z; | |
z = y; | |
y = x; | |
result.push(x); | |
} | |
return result; | |
}; | |
/** | |
* Función privada para la ejecución de las distintas operaciones. | |
* | |
* @param {String} type | |
* @param {Array} items | |
* @return {Number} | |
*/ | |
function _exec(type, items) { | |
var sitems = (items[0] instanceof Array) ? | |
items[0] : items, | |
size = sitems.length, | |
result = sitems[0], | |
index = 1; | |
if (type === 'sum') { | |
for (index;index<size;++index) | |
result += sitems[index]; | |
} else { // sub | |
for (index;index<size;++index) | |
result -= sitems[index]; | |
} | |
if (this.options.debug) | |
console.log('_exec', result); | |
return result; | |
} | |
// Acceso público | |
return isNode ? (module.exports = Public) : (window[publicName] = Public); | |
})((typeof module === 'object' && module && typeof module.exports === 'object' && module.exports)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could pass isNode param, simply as