Comparing version 0.1.4 to 0.2.0
@@ -39,3 +39,3 @@ (function (exports) { | ||
* | ||
* See the their README files for help. | ||
* Extra help is supplied inside each library file. | ||
* | ||
@@ -42,0 +42,0 @@ */ |
189
lib/array.js
@@ -6,9 +6,10 @@ (function (JSUS) { | ||
// ARRAYs | ||
///////// | ||
// Add the filter method to ARRAY objects in case the method is not | ||
// supported natively. | ||
// See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/ARRAY/filter#Compatibility | ||
/** | ||
* Add the filter method to ARRAY objects in case the method is not | ||
* supported natively. | ||
* See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/ARRAY/filter#Compatibility | ||
* | ||
* @TODO: make a static method instead | ||
* | ||
*/ | ||
if (!Array.prototype.filter) { | ||
@@ -38,23 +39,23 @@ Array.prototype.filter = function(fun /*, thisp */) { | ||
/** | ||
* Removes an element from the the array, and returns it. | ||
* For objects, deep equality comparison is performed | ||
* through JSUS.equals. | ||
* | ||
* If no element is removed returns FALSE. | ||
* | ||
*/ | ||
ARRAY.removeElement = function (needle, haystack) { | ||
// console.log('needle'); | ||
// console.log(needle); | ||
// console.log('haystack'); | ||
// console.log(haystack); | ||
if ('object' === typeof needle) { | ||
var func = JSUS.equals; | ||
} | ||
else { | ||
var func = function (a,b) { | ||
return (a === b); | ||
} | ||
} | ||
} | ||
else { | ||
var func = function (a,b) { | ||
return (a === b); | ||
} | ||
} | ||
for (var i=0; i < haystack.length; i++) { | ||
if (func(needle, haystack[i])){ | ||
// console.log('hays'); | ||
// console.log(haystack); | ||
return haystack.splice(i,1); | ||
@@ -65,6 +66,12 @@ } | ||
return false; | ||
}; | ||
/** | ||
* Returns TRUE if the element is contained in the array, | ||
* FALSE otherwise. | ||
* | ||
* For objects, deep equality comparison is performed | ||
* through JSUS.equals. | ||
* | ||
*/ | ||
ARRAY.in_array = function (needle, haystack) { | ||
@@ -82,5 +89,4 @@ if ('undefined' === typeof needle || !haystack) return false; | ||
for(var i=0;i<haystack.length;i++){ | ||
if (func.call(this,needle,haystack[i])) return true; | ||
for (var i = 0; i < haystack.length; i++) { | ||
if (func.call(this, needle, haystack[i])) return true; | ||
} | ||
@@ -93,3 +99,7 @@ return false; | ||
* The last group could have less elements. | ||
* | ||
* | ||
* @TODO: explain the differences of all the methods below | ||
* @see ARRAY.getGroupsSizeN | ||
* @see ARRAY.generateCombinations | ||
* @see ARRAY.matchN | ||
*/ | ||
@@ -104,2 +114,6 @@ ARRAY.getNGroups = function (array, N) { | ||
* | ||
* @see ARRAY.getNGroups | ||
* @see ARRAY.generateCombinations | ||
* @see ARRAY.matchN | ||
* | ||
*/ | ||
@@ -148,2 +162,32 @@ ARRAY.getGroupsSizeN = function (array, N) { | ||
/** | ||
* Generates all distinct combinations of exactly | ||
* r elements each and returns them into an array. | ||
* | ||
* @see ARRAY.getGroupSizeN | ||
* @see ARRAY.getNGroups | ||
* @see ARRAY.matchN | ||
* | ||
*/ | ||
ARRAY.generateCombinations = function (array, r) { | ||
function values(i, a) { | ||
var ret = []; | ||
for (var j = 0; j < i.length; j++) ret.push(a[i[j]]); | ||
return ret; | ||
} | ||
var n = array.length; | ||
var indices = []; | ||
for (var i = 0; i < r; i++) indices.push(i); | ||
var final = []; | ||
for (var i = n - r; i < n; i++) final.push(i); | ||
while (!JSUS.equals(indices, final)) { | ||
callback(values(indices, array)); | ||
var i = r - 1; | ||
while (indices[i] == n - r + i) i -= 1; | ||
indices[i] += 1; | ||
for (var j = i + 1; j < r; j++) indices[j] = indices[i] + j - i; | ||
} | ||
return values(indices, array); | ||
}; | ||
/** | ||
* Match each element of the array with N random others. | ||
@@ -155,7 +199,10 @@ * If strict is equal to true, elements cannot be matched multiple times. | ||
* would be able to match all the elements instead. | ||
* | ||
* @see ARRAY.getGroupSizeN | ||
* @see ARRAY.getNGroups | ||
* @see ARRAY.generateCombinations | ||
* | ||
*/ | ||
ARRAY.matchN = function (array, N, strict) { | ||
// console.log('TO MATCH'); | ||
// console.log(array.length); | ||
// | ||
var result = [] | ||
@@ -185,7 +232,13 @@ var len = array.length; | ||
ARRAY.arraySelfConcat = function(array) { | ||
/** | ||
* Appends an array to itself and return a new array. | ||
* | ||
* The original array is not modified. | ||
* | ||
*/ | ||
ARRAY.arraySelfConcat = function (array) { | ||
var i = 0; | ||
var len = array.length; | ||
var result = [] | ||
for (;i<len;i++) { | ||
for (; i < len; i++) { | ||
result = result.concat(array[i]); | ||
@@ -198,4 +251,6 @@ } | ||
/** | ||
* Compute the intersection between two arrays. | ||
* Computes the intersection between two arrays. | ||
* | ||
* Arrays can contain both primitive types and objects. | ||
* | ||
*/ | ||
@@ -209,6 +264,8 @@ ARRAY.arrayIntersect = function (a1, a2) { | ||
/** | ||
* Perform a diff between two arrays. | ||
* Performs a diff between two arrays. | ||
* | ||
* Arrays can contain both primitive types and objects. | ||
* Returns all the values of the first array which are not present | ||
* in the second one. | ||
* | ||
*/ | ||
@@ -222,4 +279,6 @@ ARRAY.arrayDiff = function (a1, a2) { | ||
/** | ||
* http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle | ||
* Shuffles the elements of the array using the Fischer algorithm. | ||
* | ||
* See http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle | ||
* | ||
*/ | ||
@@ -239,58 +298,12 @@ ARRAY.shuffle = function (array) { | ||
ARRAY.getNRandom = function (array, N, callback) { | ||
/** | ||
* Select N random elements from the array and returns them. | ||
* | ||
*/ | ||
ARRAY.getNRandom = function (array, N) { | ||
return ARRAY.shuffle(array).slice(0,N); | ||
}; | ||
ARRAY.generateCombinations = function (array, r, callback) { | ||
function equal(a, b) { | ||
for (var i = 0; i < a.length; i++) { | ||
if (a[i] != b[i]) return false; | ||
} | ||
return true; | ||
} | ||
function values(i, a) { | ||
var ret = []; | ||
for (var j = 0; j < i.length; j++) ret.push(a[i[j]]); | ||
return ret; | ||
} | ||
var n = array.length; | ||
var indices = []; | ||
for (var i = 0; i < r; i++) indices.push(i); | ||
var final = []; | ||
for (var i = n - r; i < n; i++) final.push(i); | ||
while (!equal(indices, final)) { | ||
callback(values(indices, array)); | ||
var i = r - 1; | ||
while (indices[i] == n - r + i) i -= 1; | ||
indices[i] += 1; | ||
for (var j = i + 1; j < r; j++) indices[j] = indices[i] + j - i; | ||
} | ||
callback(values(indices, array)); | ||
}; | ||
//TODO: Improve | ||
ARRAY.print_r = function (array) { | ||
for (var i=0,len=array.length;i<len;i++){ | ||
var el = array[i]; | ||
if (typeof(el) === 'ARRAY'){ | ||
ARRAY.print_r(el); | ||
} | ||
else { | ||
if (typeof(el) === 'Object'){ | ||
for (var key in el) { | ||
if (el.hasOwnProperty(key)){ | ||
console.log(key + ' ->\n'); | ||
ARRAY.print_r(el[key]); | ||
} | ||
} | ||
} | ||
else { | ||
console.log(el); | ||
} | ||
} | ||
} | ||
}; | ||
JSUS.extend(ARRAY); | ||
})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS); |
495
lib/dom.js
(function (JSUS) { | ||
/** | ||
* Helper library to perform generic operation with DOM elements. | ||
* | ||
* The general syntax is the following: Every HTML element has associated | ||
* a get* and a add* method, whose syntax is very similar. | ||
* | ||
* - The get* method creates the element and returns it. | ||
* - The add* method creates the element, append it as child to | ||
* a root element, and then returns it. | ||
* | ||
* The syntax of both method is the same, but the add* method | ||
* needs the root element as first parameter. E.g. | ||
* | ||
* getButton(id, text, attributes); | ||
* addButton(root, id, text, attributes); | ||
* | ||
* The last parameter is generally an object containing a list of | ||
* of key-values pairs as additional attributes to set to the element. | ||
* | ||
* Only the methods which do not follow the above-mentioned syntax | ||
* will receive further explanation. | ||
* | ||
*/ | ||
function DOM(){}; | ||
//Returns true if it is a DOM node | ||
/** | ||
* Write a text, or append an HTML element or node, into the | ||
* the root element. | ||
* | ||
* @see DOM.writeln | ||
* | ||
*/ | ||
DOM.write = function (root, text) { | ||
if (!root) return; | ||
if (!text) return; | ||
var content = (!JSUS.isNode(text) || !JSUS.isElement(text)) ? document.createTextNode(text) : text; | ||
root.appendChild(content); | ||
return content; | ||
}; | ||
/** | ||
* Write a text, or append an HTML element or node, into the | ||
* the root element and adds a break immediately after. | ||
* | ||
* @see DOM.writeln | ||
* | ||
*/ | ||
DOM.writeln = function (root, text, rc) { | ||
if (!root) return; | ||
var br = this.addBreak(root, rc); | ||
return (text) ? DOM.write(root, text) : br; | ||
}; | ||
/** | ||
* Returns TRUE if the object is a DOM node | ||
* | ||
*/ | ||
DOM.isNode = function(o){ | ||
@@ -11,5 +68,8 @@ return ( | ||
); | ||
} | ||
}; | ||
//Returns true if it is a DOM element | ||
/** | ||
* Returns TRUE if the object is a DOM element | ||
* | ||
*/ | ||
DOM.isElement = function(o) { | ||
@@ -20,4 +80,431 @@ return ( | ||
); | ||
} | ||
}; | ||
/** | ||
* Creates a generic HTML element with id and attributes as specified, | ||
* and returns it. | ||
* | ||
* @see DOM.addAttributes2Elem | ||
* | ||
*/ | ||
DOM.getElement = function (elem, id, attributes) { | ||
var e = document.createElement(elem); | ||
if ('undefined' !== typeof id) { | ||
e.id = id; | ||
} | ||
return this.addAttributes2Elem(e, attributes); | ||
}; | ||
/** | ||
* Creates a generic HTML element with id and attributes as specified, | ||
* appends it to the root element, and returns it. | ||
* | ||
* @see DOM.getElement | ||
* @see DOM.addAttributes2Elem | ||
* | ||
*/ | ||
DOM.addElement = function (elem, root, id, attributes) { | ||
var el = this.getElement(elem, id, attributes); | ||
return root.appendChild(el); | ||
}; | ||
/** | ||
* Add attributes to an HTML element and returns it. | ||
* | ||
* Attributes are defined as key-values pairs. | ||
* Attributes 'style', and 'label' are ignored. | ||
* | ||
* @see DOM.style | ||
* @see DOM.addLabel | ||
* | ||
*/ | ||
DOM.addAttributes2Elem = function (e, a) { | ||
if (!e || !a) return e; | ||
if ('object' != typeof a) return e; | ||
var specials = ['id', 'label', 'style']; | ||
for (var key in a) { | ||
if (a.hasOwnProperty(key)) { | ||
if (!JSUS.in_array(key, specials)) { | ||
e.setAttribute(key,a[key]); | ||
} | ||
else if (key === 'id') { | ||
e.id = a[key]; | ||
} | ||
// TODO: handle special cases | ||
// else { | ||
// | ||
// // If there is no parent node, the legend cannot be created | ||
// if (!e.parentNode) { | ||
// node.log('Cannot add label: no parent element found', 'ERR'); | ||
// continue; | ||
// } | ||
// | ||
// this.addLabel(e.parentNode, e, a[key]); | ||
// } | ||
} | ||
} | ||
return e; | ||
}; | ||
/** | ||
* Appends a list of options into a HTML select element. | ||
* The second parameter list is an object containing | ||
* a list of key-values pairs as text-value attributes for | ||
* the option. | ||
* | ||
*/ | ||
DOM.populateSelect = function (select, list) { | ||
if (!select || !list) return; | ||
for (var key in list) { | ||
if (list.hasOwnProperty(key)) { | ||
var opt = document.createElement('option'); | ||
opt.value = list[key]; | ||
opt.appendChild(document.createTextNode(key)); | ||
select.appendChild(opt); | ||
} | ||
} | ||
}; | ||
// Get / Add Elements | ||
DOM.getButton = function (id, text, attributes) { | ||
var sb = document.createElement('button'); | ||
sb.id = id; | ||
sb.appendChild(document.createTextNode(text || 'Send')); | ||
return this.addAttributes2Elem(sb, attributes); | ||
}; | ||
DOM.addButton = function (root, id, text, attributes) { | ||
var b = this.getButton(id, text, attributes); | ||
return root.appendChild(b); | ||
}; | ||
DOM.getFieldset = function (id, legend, attributes) { | ||
var f = this.getElement('fieldset', id, attributes); | ||
var l = document.createElement('Legend'); | ||
l.appendChild(document.createTextNode(legend)); | ||
f.appendChild(l); | ||
return f; | ||
}; | ||
DOM.addFieldset = function (root, id, legend, attributes) { | ||
var f = this.getFieldset(id, legend, attributes); | ||
return root.appendChild(f); | ||
}; | ||
DOM.getTextInput = function (id, attributes) { | ||
var mt = document.createElement('input'); | ||
mt.id = id; | ||
mt.setAttribute('type', 'text'); | ||
return this.addAttributes2Elem(mt, attributes); | ||
}; | ||
DOM.addTextInput = function (root, id, attributes) { | ||
var ti = this.getTextInput(id, attributes); | ||
return root.appendChild(ti); | ||
}; | ||
DOM.getCanvas = function (id, attributes) { | ||
var canvas = document.createElement('canvas'); | ||
var context = canvas.getContext('2d'); | ||
if (!context) { | ||
alert('Canvas is not supported'); | ||
return false; | ||
} | ||
canvas.id = id; | ||
return this.addAttributes2Elem(canvas, attributes); | ||
}; | ||
DOM.addCanvas = function (root, id, attributes) { | ||
var c = this.getCanvas(id, attributes); | ||
return root.appendChild(c); | ||
}; | ||
DOM.getSlider = function (id, attributes) { | ||
var slider = document.createElement('input'); | ||
slider.id = id; | ||
slider.setAttribute('type', 'range'); | ||
return this.addAttributes2Elem(slider, attributes); | ||
}; | ||
DOM.addSlider = function (root, id, attributes) { | ||
var s = this.getSlider(id, attributes); | ||
return root.appendChild(s); | ||
}; | ||
DOM.getRadioButton = function (id, attributes) { | ||
var radio = document.createElement('input'); | ||
radio.id = id; | ||
radio.setAttribute('type', 'radio'); | ||
return this.addAttributes2Elem(radio, attributes); | ||
}; | ||
DOM.addRadioButton = function (root, id, attributes) { | ||
var rb = this.getRadioButton(id, attributes); | ||
return root.appendChild(rb); | ||
}; | ||
// DOM.addJQuerySlider = function (root, id, attributes) { | ||
// var slider = document.createElement('div'); | ||
// slider.id = id; | ||
// slider.slider(attributes); | ||
// root.appendChild(slider); | ||
// return slider; | ||
// }; | ||
DOM.getLabel = function (forElem, id, labelText, attributes) { | ||
if (!forElem) return false; | ||
var label = document.createElement('label'); | ||
label.id = id; | ||
label.appendChild(document.createTextNode(labelText)); | ||
if ('undefined' === typeof forElem.id) { | ||
forElem.id = this.generateUniqueId(); | ||
} | ||
label.setAttribute('for', forElem.id); | ||
this.addAttributes2Elem(label, attributes); | ||
return label; | ||
}; | ||
DOM.addLabel = function (root, forElem, id, labelText, attributes) { | ||
if (!root || !forElem || !labelText) return false; | ||
var l = this.getLabel(forElem, id, labelText, attributes); | ||
root.insertBefore(l, forElem); | ||
return l; | ||
}; | ||
DOM.getSelect = function (id, attributes) { | ||
return this.getElement('select', id, attributes); | ||
}; | ||
DOM.addSelect = function (root, id, attributes) { | ||
return this.addElement('select', root, id, attributes); | ||
}; | ||
DOM.getIFrame = function (id, attributes) { | ||
var attributes = {'name' : id}; // For Firefox | ||
return this.getElement('iframe', id, attributes); | ||
}; | ||
DOM.addIFrame = function (root, id, attributes) { | ||
var ifr = this.getIFrame(id, attributes); | ||
return root.appendChild(ifr); | ||
}; | ||
DOM.addBreak = function (root, rc) { | ||
var RC = rc || 'br'; | ||
var br = document.createElement(RC); | ||
return root.appendChild(br); | ||
//return this.insertAfter(br,root); | ||
}; | ||
DOM.addCSS = function (root, css, id, attributes) { | ||
var attributes = attributes || {'rel' : 'stylesheet', | ||
'type': 'text/css'}; | ||
attributes.href = css; | ||
var id = id || 'maincss'; | ||
return this.addElement('link', root, id, attributes); | ||
}; | ||
DOM.getDiv = function (id, attributes) { | ||
return this.getElement('div', id, attributes); | ||
}; | ||
DOM.addDiv = function (root, id, attributes) { | ||
return this.addElement('div', root, id, attributes); | ||
}; | ||
/** | ||
* Provides a simple way to highlight an HTML element | ||
* by adding a colored border around it. | ||
* | ||
* Three pre-defined modes are implemented: | ||
* | ||
* - OK: green | ||
* - WARN: yellow | ||
* - ERR: red (default) | ||
* | ||
* Alternatively, it is possible to specify a custom | ||
* color as HEX value. Examples: | ||
* | ||
* highlight(myDiv, 'WARN'); // yellow border | ||
* highlight(myDiv); // red border | ||
* highlight(myDiv, '#CCC'); // grey border | ||
* | ||
* @see DOM.addBorder | ||
* @see DOM.style | ||
* | ||
*/ | ||
DOM.highlight = function (elem, code) { | ||
if (!elem) return; | ||
// default value is ERR | ||
switch (code) { | ||
case 'OK': | ||
var color = 'green'; | ||
break; | ||
case 'WARN': | ||
var color = 'yellow'; | ||
break; | ||
case 'ERR': | ||
var color = 'red'; | ||
break; | ||
default: | ||
if (code[0] === '#') { | ||
var color === code; | ||
} | ||
else { | ||
var color = 'red'; | ||
} | ||
} | ||
return this.addBorder(elem, color); | ||
}; | ||
/** | ||
* Adds a border around the specified element. Color, | ||
* width, and type can be specified. | ||
* | ||
*/ | ||
DOM.addBorder = function (elem, color, witdh, type) { | ||
if (!elem) return; | ||
var color = color || 'red'; | ||
var width = width || '5px'; | ||
var type = type || 'solid'; | ||
var properties = { border: width + ' ' + type + ' ' + color } | ||
return this.style(elem,properties); | ||
}; | ||
/** | ||
* Styles an element as an in-line css. | ||
* Takes care to add new styles, and not overwriting previuous | ||
* attributes. | ||
* | ||
* Returns the element. | ||
* | ||
* @see DOM.setAttribute | ||
*/ | ||
DOM.style = function (elem, properties) { | ||
if (!elem || !properties) return; | ||
var style = ''; | ||
for (var i in properties) { | ||
style += i + ': ' + properties[i] + '; '; | ||
}; | ||
return elem.setAttribute('style',style); | ||
}; | ||
/** | ||
* Removes a specific class from the class attribute | ||
* of a given element. | ||
* | ||
* Returns the element. | ||
*/ | ||
DOM.removeClass = function (el, c) { | ||
if (!el || !c) return; | ||
var regexpr = '/(?:^|\s)' + c + '(?!\S)/'; | ||
var o = el.className = el.className.replace( regexpr, '' ); | ||
return el; | ||
}; | ||
/** | ||
* Add a class to the class attribute of the given | ||
* element. Takes care not to overwrite already | ||
* existing classes. | ||
* | ||
*/ | ||
DOM.addClass = function (el, c) { | ||
if (!el || !c) return; | ||
if (c instanceof Array) c = c.join(' '); | ||
if ('undefined' === typeof el.className) { | ||
el.className = c; | ||
} | ||
else { | ||
el.className += ' ' + c; | ||
} | ||
return el; | ||
}; | ||
/** | ||
* Remove all children from a node. | ||
* | ||
*/ | ||
DOM.removeChildrenFromNode = function (e) { | ||
if (!e) return false; | ||
while (e.hasChildNodes()) { | ||
e.removeChild(e.firstChild); | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Insert a node element after another one. | ||
* | ||
* The first parameter is the node to add. | ||
* | ||
*/ | ||
DOM.insertAfter = function (node, referenceNode) { | ||
referenceNode.insertBefore(node, referenceNode.nextSibling); | ||
}; | ||
/** | ||
* Generate a unique id for the page (frames included). | ||
* | ||
* TODO: now it always create big random strings, it does not actually | ||
* check if the string exists. | ||
* | ||
*/ | ||
DOM.generateUniqueId = function (prefix) { | ||
var search = [window]; | ||
if (window.frames) { | ||
search = search.concat(window.frames); | ||
} | ||
function scanDocuments(id) { | ||
var found = true; | ||
while (found) { | ||
for (var i=0; i< search.length; i++) { | ||
found = search[i].document.getElementById(id); | ||
if (found) { | ||
id = '' + id + '_' + JSUS.randomInt(0, 1000); | ||
break; | ||
} | ||
} | ||
} | ||
return id; | ||
}; | ||
return scanDocuments(prefix + '_' + JSUS.randomInt(0, 10000000)); | ||
//return scanDocuments(prefix); | ||
}; | ||
/** | ||
* Creates a blank HTML page with the html and body | ||
* elements already appended. | ||
* | ||
*/ | ||
DOM.getBlankPage = function() { | ||
var html = document.createElement('html'); | ||
html.appendChild(document.createElement('body')); | ||
return html; | ||
}; | ||
// DOM.findLastElement = function(o) { | ||
@@ -24,0 +511,0 @@ // if (!o) return; |
@@ -5,3 +5,10 @@ (function (JSUS) { | ||
/** | ||
* Allows to execute the eval function within a given | ||
* context. If no context is passed a reference to the | ||
* this object is used. | ||
* | ||
*/ | ||
EVAL.eval = function (str, context) { | ||
var context = context || this; | ||
// Eval must be called indirectly | ||
@@ -8,0 +15,0 @@ // i.e. eval.call is not possible |
128
lib/obj.js
(function (JSUS) { | ||
/** | ||
* | ||
* OBJ: functions working with js objects. | ||
* | ||
* | ||
*/ | ||
function OBJ(){}; | ||
/** | ||
* Checks for deep equality between two objects, | ||
* string or primitive types. | ||
* | ||
* All nested properties are checked, and if they differ | ||
* in at least one returns FALSE, otherwise TRUE. | ||
* | ||
*/ | ||
OBJ.equals = function (o1, o2) { | ||
// console.log('Equals'); | ||
// console.log(o1); | ||
// console.log(o2); | ||
if (!o1 || !o2) return false; | ||
@@ -24,4 +33,2 @@ | ||
} | ||
@@ -45,4 +52,3 @@ for (var p in o1) { | ||
} | ||
// Check whether o2 has extra properties | ||
@@ -56,9 +62,12 @@ // TODO: improve, some properties have already been checked! | ||
} | ||
//console.log('yes'); | ||
return true; | ||
}; | ||
OBJ.getListSize = function (list) { | ||
/** | ||
* Returns the number of own properties, prototype | ||
* chain excluded, stored in the object / list. | ||
* | ||
*/ | ||
OBJ.getListSize = OBJ.getOwnPropertiesSize = function (list) { | ||
var n = 0; | ||
@@ -70,10 +79,13 @@ for (var key in list) { | ||
} | ||
//console.log('Calculated list length ' + n); | ||
return n; | ||
}; | ||
/** | ||
* Explodes an object into an array of keys and values, | ||
* according to the specified parameters. | ||
* A fixed level of recursion can be set. | ||
* | ||
* @api private | ||
* | ||
*/ | ||
OBJ._obj2Array = function(obj, keyed, level, cur_level) { | ||
@@ -109,2 +121,6 @@ if ('object' !== typeof obj) return [obj]; | ||
* the whole object gets totally unfolded into an array. | ||
* | ||
* @see OBJ._obj2Array | ||
* @see OBJ._obj2KeyedArray | ||
* | ||
*/ | ||
@@ -120,7 +136,13 @@ OBJ.obj2Array = function (obj, level) { | ||
* @see OBJ.obj2Array | ||
* | ||
*/ | ||
OBJ.obj2KeyedArray = function (obj, level) { | ||
OBJ.obj2KeyedArray = OBJ.obj2KeyArray = function (obj, level) { | ||
return OBJ._obj2Array(obj, true, level); | ||
}; | ||
/** | ||
* Scans an object an returns all the keys of the properties, | ||
* into an array. Nested objects are also evaluated. | ||
* | ||
*/ | ||
OBJ.objGetAllKeys = function (obj) { | ||
@@ -139,4 +161,2 @@ var result = []; | ||
/** | ||
@@ -160,8 +180,5 @@ * Creates an array of key:value objects. | ||
/** | ||
* Creates a perfect copy of the obj | ||
* Creates a perfect copy of the object passed as parameter. | ||
* | ||
*/ | ||
@@ -185,4 +202,5 @@ OBJ.clone = function (obj) { | ||
/** | ||
* Performs a left join on the keys of two objects. In case keys overlaps | ||
* the values from obj2 are taken. | ||
* Performs a *left* join on the keys of two objects. In case keys overlaps | ||
* the values from obj2 are taken. | ||
* | ||
*/ | ||
@@ -209,12 +227,12 @@ OBJ.join = function (obj1, obj2) { | ||
* Merges two objects in one. In case keys overlaps the values from | ||
* obj2 are taken. | ||
* obj2 are taken. | ||
* | ||
* Returns a new object, the original ones are not modified. | ||
* | ||
*/ | ||
OBJ.merge = function (obj1, obj2) { | ||
var clone = OBJ.clone(obj1); | ||
// console.log('CLONE'); | ||
// console.log(clone); | ||
if (!obj2) return clone; | ||
for (var i in obj2) { | ||
if (obj2.hasOwnProperty(i)) { | ||
//console.log(i); | ||
if ( 'object' === typeof obj2[i] ) { | ||
@@ -232,3 +250,8 @@ clone[i] = OBJ.merge(obj1[i],obj2[i]); | ||
/** | ||
* Set the. | ||
* Like OBJ.merge, but only overlapping keys are copied. | ||
* | ||
* Returns a new object, the original ones are not modified. | ||
* | ||
* @see OBJ.merge | ||
* | ||
*/ | ||
@@ -248,2 +271,22 @@ OBJ.mergeOnKey = function (obj1, obj2, key) { | ||
/** | ||
* Merges on key called 'value'. | ||
* | ||
* @see OBJ.mergeOnKey | ||
* | ||
* @deprecated | ||
* | ||
*/ | ||
OBJ.mergeOnValue = function (obj1, obj2) { | ||
return OBJ.mergeOnKey(obj1, obj2, 'value'); | ||
}; | ||
/** | ||
* Creates a copy of an object containing only the | ||
* properties passed as second parameter. | ||
* | ||
* The parameter select can be an array of strings, | ||
* or the name of a property. | ||
* | ||
*/ | ||
OBJ.subobj = function (o, select) { | ||
@@ -253,5 +296,5 @@ if (!o) return false; | ||
if (!select) return out; | ||
if (!(select instanceof Array)) select = [select]; | ||
for (var i=0; i<select.length;i++) { | ||
var key = select[i]; | ||
//console.log(key); | ||
if ('undefined' !== typeof o[key]) { | ||
@@ -263,7 +306,15 @@ out[key] = o[key]; | ||
}; | ||
OBJ.mergeOnValue = function (obj1, obj2) { | ||
return OBJ.mergeOnKey(obj1, obj2, 'value'); | ||
}; | ||
/** | ||
* Sets the value of a nested property of an object, | ||
* and returns it. | ||
* | ||
* If the object is not passed a new one is created. | ||
* If the nested property is not existing, a new one | ||
* is created. | ||
* | ||
* The original object is modified. | ||
* | ||
*/ | ||
OBJ.setNestedValue = function (str, value, obj) { | ||
@@ -287,2 +338,3 @@ var obj = obj || {}; | ||
* Returns undefined if the nested key is not found. | ||
* | ||
*/ | ||
@@ -289,0 +341,0 @@ OBJ.getNestedValue = function (str, obj) { |
@@ -5,2 +5,7 @@ (function (JSUS) { | ||
/** | ||
* Generates a pseudo-random floating point number between | ||
* a and b. | ||
* | ||
*/ | ||
RANDOM.random = function (a, b) { | ||
@@ -12,2 +17,6 @@ var a = a || 0; | ||
/** | ||
* Generates a pseudo-random integer between | ||
* a and b. | ||
*/ | ||
RANDOM.randomInt = function (a, b) { | ||
@@ -14,0 +23,0 @@ return Math.floor(RANDOM.random(a, b) + 1); |
@@ -5,3 +5,10 @@ (function (JSUS) { | ||
TIME.getDate = function() { | ||
/** | ||
* Returns a string representation of the current date | ||
* and time formatted as follows: | ||
* | ||
* dd-mm-yyyy hh:mm:ss milliseconds | ||
* | ||
*/ | ||
TIME.getDate = TIME.getFullDate = function() { | ||
var d = new Date(); | ||
@@ -15,2 +22,9 @@ var date = d.getUTCDate() + '-' + (d.getUTCMonth()+1) + '-' + d.getUTCFullYear() + ' ' | ||
/** | ||
* Returns a string representation of the current time | ||
* formatted as follows: | ||
* | ||
* hh:mm:ss | ||
* | ||
*/ | ||
TIME.getTime = function() { | ||
@@ -24,5 +38,5 @@ var d = new Date(); | ||
/** | ||
* Returns an array days, minutes, seconds, mi | ||
* @param ms time in milliseconds | ||
* @return array | ||
* Parse an integer number representing millisecodns, | ||
* and returns an array of days, hours, minutes and seconds | ||
* | ||
*/ | ||
@@ -29,0 +43,0 @@ TIME.parseMilliseconds = function (ms) { |
{ | ||
"name": "JSUS", | ||
"description": "JavaScript UtilS. Collection of general purpose functions. JSUS helps!", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"keywords": [ "util", "general", "array", "eval", "time", "date", "object"], | ||
@@ -6,0 +6,0 @@ "author": "Stefano Balietti <futur.dorko@gmail.com>", |
16
test.js
var JSUS = require('./jsus.js').JSUS; | ||
console.log(JSUS._classes); | ||
// | ||
//console.log(JSUS._classes); | ||
var a = ['a', 'b', 'c', 'd']; | ||
var N = 4; | ||
console.log(JSUS.getDate()); | ||
//console.log(JSUS.generateCombinations(a, N)); | ||
//console.log(JSUS.getGroupsSizeN(a, N)); | ||
//console.log(JSUS.getNGroups(a, N)); | ||
//console.log(JSUS.matchN(a, N)); | ||
//for (var a in JSUS.JSUS._classes) { | ||
@@ -6,0 +18,0 @@ // console.log(a); |
35013
1209