Karmagination API
- DOM Methods
- Utilities
General Utilities
Here are some utilities that are useful when you use Karmagination.
Karma.unique( array )
Returns array with unique items.
Karma.unique(['a', 'b', 'c', 'a', 'a']); // returns ['a', 'b', 'c']
Karma.makeArray( object )
Converts array-like objects to a true array so array methods will be available ie. splice, slice, push, pop etc.
<div>a</div> <div>a</div> <div>a</div>
var els = document.getElementsByTagName('div'); alert (els instanceof Array); // false var els_array = Karma.makeArray(els); alert (els_array instanceof Array); // true
Karma.namespace( string )
Creates a namespace for variables.
Karma.namespace('YAHOO.util.Dom'); YAHOO.util.Dom.test = 'a'; alert(YAHOO.util.Dom.test); // a
Karma.trim( string )
Trims empty spaces from both the left and right side of the side.
var temp = ' aaaaa '; alert(Karma.trim(temp)); // 'aaaaa'
Karma.grep( object, callback )
Returns an array of items the matches the condition in the callback function.
var a = ['a', 'a', 'c', 'd', 'b']; var b = Karma.grep(a, function(cur) { return cur === 'a'; }); alert(b); // ['a', 'a']
Karma.inArray( item, object )
Checks if an item is in the object(usually an array) and returns the index(starts with 0) of the first matched item. If none are found, returns -1.
Karma.inArray(1, [1,2,3]); // returns 0 Karma.inArray(4, [1,2,3]); // returns -1
Karma.map( array, callback )
A powerful function that can transform an array to another array with different value.
var a = [94569, 71686, 87954]; var b = Karma.map(a, function(item) { return 'Your zip code is ' + item; }); alert(b); // ['Your zip code is 94569', 'Your zip code is 71686', 'Your zip code is 87954']
Karma.merge( array1, array2, array3, ....., arrayN )
Merge n arrays together, returning a new array that has items from all the arrays
var a = [1,1,1]; var b = [2,2,2]; var c = Karma.merge(a, b); alert(c); // [1,1,1,2,2,2];
Karma.extend( object1, object2, ..., objectN )
Extends the first object and over-writting its properties.
var opts = { greeting: 'Hello' }; var opts2 = { greeting: 'Hello GrandPa!', eat: 'Kebob' }; Karma.extend(opts, opts2); alert(opts.greeting); // Hello GranPa! alert(opts.eat); // Kebob!