base-utils
v0.0.10
Published
Extensions to underscore.js for manipulating arrays and objects
Downloads
5
Readme
base-utils.js
Add-on utility functions inspired by Underscore
Usage
_.mixin(require('base-utils');
Methods
###Utility
- toInt - Convert string to decimal integer.
- trim - Remove multiple, leading or trailing spaces.
- initialCaps - Make the first character of string capitalized
- enclose - Wrap up a function and arguement.
- args - Wrap up a list of arguments into an array.
- forceToArray - Wrap up a list of arguments into an array.
- coerce - Convert a string to specific data type.
- filterNonAscii - Filter out all the NULL characters from a string.
- buildHTML - Build html string.
- wait - Execute a function after given seconds.
###Arrays
- found - Find whether a item is inside an array.
- compare - Compare two lists to see if list2 contains all the member of lsit1
- identical - Compare two arrays to see if they are the same.
- bfind - Use binary search to find an item in a sorted array.
- reverse - Reverse the order of items in array.
###Objects
- select - Return an object with selected properties.
- clean - Clean the properties with given property value
- pfetch - Fetch the value of property with given property tag name
- fetch - Return the value of first hit given the properties.
- hfetch - Return the value of property given the path in object.
- walk - Traverse a object and call supplied function for every property.
Takes in a string and return the decimal integer. The string can represent either positive integer or negative integer. Illegal input string will return null. Input integer will return this integer.
Example:
_.toInt("-5")+_.toInt("4")+_.toInt(1)
Result:
0
Takes in a string and return the modified string with no leading and trailing space and no multiple spaces inside string.
Example:
_.trim(' hello js-base; ')
Result:
'hello js-base;'
Takes in a string and return the modified string with the first character capitalized.
Example:
_.initialCaps('i am a red fox')
Result:
'I am a red fox'
Wraps a function in a closure and returns it so the returned function has access to the arguments of the original function. Useful when firing 'click' events
Example:
enclose = _.enclose(function(end) {
return end;
}, 'yes!');
Result:
enclose() == 'yes!'
Takes in a list of arguments and return the wraped array. If the input is an array already, return itself.
Example:
_.args('a','b')
Result:
['a','b']
Takes in one or more arguements and return the converted array.
Example:
_.forceToArray(2,3,4)
Result:
[2,3,4]
Convert the input data to a specific data-type.
Example:
_.isNumber(_.coerce('number',"1"));
_.isString(_.coerce('string', 0));
_.isBoolean(_.coerce('boolean', true))
_.isDate(_.coerce('date', new Date()));
Result:
all true
Takes in a string and filter out the NULL character and return the modified string
Example:
_.filterNonAscii("1\0 2\0 3\0 4");
Result:
"1 2 3 4"
Builds the html string given the tag, html value and attributes.
Example:
_.buildHTML('div', 'yes!', {'id': '123'});
_.buildHTML('div', {'id': '123'});
_.buildHTML('div', 'yes!')
Result:
'<div id="123">yes!</div>'
'<div id="123"/>'
'<div>yes!</div>'
Executes the function after a certain amount of time in seconds.
Example:
_.wait(1, function(){
...
});
Takes in an array and an item and return true if the item is inside the array; return false if otherwise.
Example:
_.found(['a', 'b', 'item', 'd'], 'item')
Result:
true
Takes in two lists: list1 and list2. Return true if all the members in list1 are in list2; return false if otherwise. The list could be an array.
Example:
_.compare(('a', 'b'), ['a','b'])
Result:
true
Takes in two arrays: array1 and array2. Return ture if array1 and array2 have the same items in the same order.
Example:
_.identical([1, 2, 3], [1, 2, 3])
Result:
true
Takes in a sorted array and use binary search to find the item and return the array with this item as element.
Example:
_.bfind([1, 2, 3, 4], 2)
Result:
[2]
Returns an object containing only the selected keys. Argument can be an array of strings or separate argument strings.
Example:
_.reverse([1, 2, 3, 4])
Result:
[4, 3, 2, 1]
Takes in an object and property list. Return an object with only the properties from the list.
Example:
obj = {
'var1': {
'var2': {
'var3': 'var3-value'
},
'var4': 'var4-value',
'var6': 'another-value'
},
'var5': Date()
};
_.select(obj, ['var1', 'var5'])
Result:
{
var1:
{ var2: { var3: 'var3-value' },
var4: 'var4-value',
var6: 'another-value' },
var5: 'Fri Oct 18 2013 16:19:35 GMT-0400 (EDT)'
}
Cleans the properties with selected value. If no value is given, clean the properties with undefined value.
Example:
_.clean({'a': 'yes', 'b': undefined, 'c': 'again!', 'd': undefined });
_.clean({'a': 'yes', 'b': undefined, 'c': 'again!', 'd': undefined }, 'yes');
Result:
{ a: 'yes', c: 'again!' }
{'b': undefined, 'c': 'again!', 'd': undefined }
Fetchs the property's value with given property name.
Example:
obj = {
'var1': {
'var2': {
'var3': 'var3-value'
},
'var4': 'var4-value',
'var6': 'another-value'
},
'var5': Date()
};
_.pfetch(obj, 'var4');
Result:
"var4-value"
Returns the value of first property of this object, which matches one of the property-name-list. If it is an array, return the position of the given value and return -1 if none found.
Example:
_.fetch({'a':1, 'b':2, 'item': 3}, 'item')
_.fetch(['a', 'b', 'item', 'd'], 'item')
Result:
3
2
Returns the value of property given the path in object.
Example:
obj = {
'var1': {
'var2': {
'var3': 'var3-value'
},
'var4': 'var4-value',
'var6': 'another-value'
},
'var5': Date()
};
['var1/var6', 'var1/var2/var3', '/var1/var5', 'var6'].forEach(function(path) {
tmp.push(_.hfetch(obj, path));
});
Result:
[ 'another-value', 'var3-value', undefined, 'another-value' ]
Traverses a object and call supplied function for every property.
Example:
obj = {
'var1': {
'var2': {
'var3': 'var3-value'
},
'var4': 'var4-value',
'var6': 'another-value'
},
'var5': Date()
};
tmp = [];
_.walk(obj, function(item, name) {
tmp.push(name);
});
Result:
['var1','var2','var3','var4','var6','var5']
License
MIT