Useful shorthand methods mostly for arrays but see the list below.
About
All methods defined tries to work w/ the given array, avoiding creating a new one. Except if thats the direct intent of the function (.clone).
By requiring it, the returned object will be a function. It has the full API on its properties, however you also extend a simple object or the original prototypes (this is DANGEROUS, only use it if you know what you're doing).
Install
# Navigate to your projects directory and use npm to install it as a dependency: npm install -S short-methods
Usage
In your code: var shortMethods = require('short-methods');
After this you can use it multiple ways:
var
array;
# Use the properties of the returned object:
array = [1, 2, 3];
shortMethods.array.remove(array, 2); // ===> array [1, 3];
shortMethods.array.removeAt(array, 2); // ===> array [1, 2];
# Use the properties of the returned object in short format:
array = [1, 2, 3];
shortMethods.a.remove(array, 2); // ===> array [1, 3];
shortMethods.a.removeAt(array, 2); // ===> array [1, 2];
# Extend a specific object:
array = shortMethods([1, 2, 3]);
array.remove(2); // ===> array [1, 3];
array.removeAt(2); // ===> array [1, 2];
# Extend prototypes (this is DANGEROUS, only use it if you know what you're doing):
shortMethods(true);
array = [1, 2, 3];
array.remove(2); // ===> array [1, 3];
array.removeAt(2); // ===> array [1, 2];
API
Short list w/ the default behavior, for more info about parameter usage check out the specs.
<Number>.random(number)
Returns a number between 0 and number.
<Array>.clear()
Clears the array.
<Array>.clone()
Clones the array.
<Array>.count(item)
Counts the occurences of an item in the array.
<Array>.get(val, key)
Find an item in the array, useful if you have an array of objects.
<Array>.has(item)
Check if the array has a specific item.
<Array>.remap(callback)
Remap all elements of an array by the returned value of the callback.
<Array>.remove(item)
Remove the first occurence of an element in the array.
<Array>.removeAt(index)
Remove an element on a specific position in the array.
<Array>.union(array)
Unions two array.
<Array>.uniq()
Make the elements of an array uniq.
License
MIT © p1100i