A library which adds some very useful functions to the base type classes available in JavaScript.
Extendere.js is a small library (5kb compressed) which adds some really useful functions to the base classes of many JavaScript native types.
Simple. You can either include the following script tag to include everything there is and rock and roll with Extendere:
<script src="https://cdn.rawgit.com/labsvisual/extendere.js/master/dist/extendere.min.js"></script>
<script>
ExtendereMath.expostMath();
</script>
or you can install it using bower: bower install Extendere.js --save
or you can download it and include the scripts manually.
For every element there is in the array, pass it to the callback. You can do whatever you want with that element after that.
var arr = [ 1, 2, 3 ],
nea = [];
arr.each( function( i ) {
nea.push( i );
});
console.log( nea );
Output: [1, 2, 3]
General syntax: Array.each( function( item ) {} )
Run a filter on each element present in the array. The filter
function should return
a value which has all the
transformations applied.
var arr = [ 1, 2, 3 ];
var x = arr.map( function( i ) {
return i * 2;
});
console.log( x );
Output: [2, 4, 6]
General syntax: Array.map( function( item ) {} )
Sorts the current array using the provided algorithm and returns the sorted instance.
var arr = [ 3, 2, 1, 5, 8, 9, 6 ];
var x = arr.sortWith( "bubblesort" );
console.log( x );
Output: [1, 2, 3, 5, 6, 8, 9]
General syntax: Array.sortWith( algorithm )
Sorts the current array using quicksort and returns the sorted instance.
var arr = [ 3, 2, 1 ];
var x = arr.sort();
console.log( x );
Output: [1, 2, 3]
General syntax: Array.sort()
If sorted, uses binary search to search for the element; returns the index, if found; -1 otherwise. If not sorted, it
sorts using the Array.sort
method (defined above) and then applied binary search.
var arr = [ 1, 4, 7, 2, 8 ];
var x = arr.indexOf( 2, false );
console.log( x );
Output: 3
General syntax: Array.indexOf( element, isSorted )
If offset is given, returns the last offset
elements; the last element, otherwise.
var arr = [ 1, 4, 7, 2, 8 ];
var x = arr.last();
var y = arr.last( 2 );
console.log( x );
console.log( y );
Output:
8
[2, 8]
General syntax: Array.last( offset )
Reduces nested arrays into one, single array.
var arr = [ 1, 2, [ 3, 4 [ 5, 6 ] ] ];
var x = arr.flatten();
console.log( x );
Output: [1, 2, 3, 4, 5, 6]
General syntax: Array.flatten()
Returns an array free of 'falsy' elements. Elements like false, 0, <empty string>, null, undefined, NaN
are
called falsy elements.
var arr = ( [ 0, 1, false, 2, '', 3, undefined, null, 0 / 0 ] ),
arx = arr.clean();
console.log( arx );
Output: [1, 2, 3]
General syntax: Array.clean()
Check is the specified value exists in the array. Returns true if it does, false otherwise.
var arr = [ 1, 2, 3 ];
console.log( arr.exists( 3 ) );
Output: true
General syntax: Array.exists( element )
Returns an array which is the union of all the arrays provided. Follows the same union rule as that in elementary set theory.
var arr = [ 1, 2, 3 ],
arx = [ 3, 4, 5 ],
ary = [ 6, 7, 8 ];
ar = arr.union( arx, ary );
console.log( ar );
Output: [1, 2, 3, 4, 5, 6, 7, 8]
General syntax: Array.union( secondArray, *arrays )
Returns an array which is the intersection of all the arrays provided. Follows the same intersection rule as that in elementary set theory.
var arr = [ 1, 2, 3 ],
arx = [ 2, 3, 4, 5 ],
ary = [ 2, 3 ];
ar = arr.intersect( arx, ary);
console.log( ar );
Output: [2, 3]
General syntax: Array.intersect( secondArray, *arrays )
Returns an array with all the duplicates removed.
var arr = [ 1, 2, 3, 2, 3, 2, 3, 4, 5, 6, 8, 2, 5, 7 ],
arx = [ 1, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1 ];
console.log( arr.removeDuplicates() );
console.log( arx.removeDuplicates() );
Output:
[1, 3, 4, 6, 8, 2, 5, 7]
[2, 1]
General syntax: Array.intersect( secondArray, *arrays )
Returns a random list of element(s) from the array of length length
; if none provided, returns a single random
element.
var arr = [ 1, 2, 3 ];
console.log( arr.atRandom() );
console.log( arr.atRandom( 2 ) );
Output:
1
[2, 1]
General syntax: Array.atRandom( length )
Returns true if the passed parameter is an array; false, otherwise.
var arr = [ 1, 2, 3 ];
console.log( Array.prototype.isArray.call ( arr ) );
Output: true
General syntax: Array.isArray( array )
Returns all the keys of the object as an array.
var obj = {
"name": "John Doe",
"email": "[email protected]"
};
var arr = obj.allKeys();
console.log( arr );
Output: ["name", "email"]
General syntax: Object.allKeys()
Returns all the values of the object as an array.
var obj = {
"name": "John Doe",
"email": "[email protected]"
};
var arr = obj.allKeys();
console.log( arr );
Output: ["John Doe", "[email protected]"]
General syntax: Object.allValues()
Run a filter on each value present in the object. The filter
function should return
a value which has all the
transformations applied.
var obj = {
"age": 1,
"newAge": 10
};
var arr = obj.mapObject( function( key, val ) {
return val * 2;
});
console.log( arr);
Output: { "age": 2, "newAge": 20 }
General syntax: Object.map( function( key, value ) {} )
Returns an array containing the key-value pairs as a separate array.
var obj = {
"age": 1,
"newAge": 10
};
var arr = obj.extract();
console.log( arr );
Output: [["age", 1], ["newAge", 10]]
General syntax: Object.extract()
Returns an object where the value is the key of the original object and vice-versa.
var obj = {
"age": 1,
"newAge": 10
};
var arr = obj.invert();
console.log( arr );
Output: { 1: "age", 10: "newAge" }
General syntax: Object.invert()
Returns an object where the value is the key of the original object and vice-versa.
var obj1 = {
"foo": "bar"
};
var obj2 = {
"bar": "foo"
};
var arr = obj1.merge( obj2 );
console.log( arr );
Output: { "foo": "bar", "bar": "foo" }
General syntax: Object.merge( secondArray )
Rounds the value val
to the specified number of decimals.
var a = 3.123912491519541;
console.log( window.math.round( a, 5 ) );
console.log( window.math.round( a, 6 ) );
console.log( window.math.round( a, 7 ) );
console.log( window.math.round( a, 8 ) );
Output:
3.12391
3.123912
3.1239125
3.12391249
General syntax: window.math.round( val, decimals )
Converts the value val
to its equivalent radian representation rounded to the specified number of decimals (default 2).
console.log( window.math.toRadians( 114.592 ) );
console.log( window.math.toRadians( 114.592, 8 ) );
Output:
2.00
2.0000077
General syntax: window.math.toRadians( val, roundTo )
Converts the value val
to its equivalent radian representation rounded to the specified number of decimals (default 2).
console.log( window.math.toDegrees( 2 ) );
console.log( window.math.toDegrees( 2, 8 ) );
Output:
114.59
114.59155903
General syntax: window.math.toRadians( val, roundTo )
Go back 1 page in history.
General syntax: window.goBack()
Go back to a depth of n
into history.
General syntax: window.goBackToDepth( n )
Invokes callback
when the DOM is ready: i.e. parsed and rendered.
window.onDomReady( function() {
console.log( "DOM ready" );
});
Outpue: DOM ready
General syntax: window.onDomReady( callback )
Gets the page which referred the user to the current page. The referrer is the page which leads the user to the page he is on; either through a link or otherwise.
General syntax: window.getReferrer()
Returns true if the current page was referred from expectedRef
; false, otherwise.
General syntax: window.wasReferredFrom( expectedRef )
Returns true if the provided URI is valid.
console.log( window.isValidUri( "http://helloworld.com" ) );
console.log( window.isValidUri( "http://helloworldom" ) );
Output:
true
false
General syntax: window.isValidUrl( uri )
That's it for this version. There are a lot of goodies planned for the next version. Hold tight.
You can open up a new issue in the issues tab, or clone this repo and fix a bug yourself. (Thanks).