base-utils

0.0.10 • Public • Published

base-utils.js

Add-on utility functions inspired by Underscore

Usage

_.mixin(require('base-utils');

Methods

Utility

Arrays

Objects

### _.toInt(string)

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
### _.trim(string)

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;'    
### _.initialCaps(string)

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'
### _.enclose(function, arguments)

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!'
### _.args(argument)

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']
### _.forceToArray(arguements)

Takes in one or more arguements and return the converted array.

Example:

_.forceToArray(2,3,4)

Result:

[2,3,4]
### _.coerce(data-type, data)

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
### _.filterNonAscii(string)

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"
### _.buildHTML(tag, html, attrs)

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>'
### _.wait(second, function)

Executes the function after a certain amount of time in seconds.

Example:

_.wait(1, function(){
	...
});
### _.found(array, item)

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 
### _.compare(list1, list2)

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
### _.identical(array1, array2)

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
### _.bfind(array, item, function)

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] 
### _.reverse(argument)

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]    
### _.select(object, array/argument list)

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)' 
 }
### _.clean(object, value)

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 }
### _.pfetch(object, property-name)

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"
### _.fetch(object/array, property-name-list/item)

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
### _.hfetch(object, path)

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' ]
### _.walk(object, function(object, property-name))

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

Readme

Keywords

none

Package Sidebar

Install

npm i base-utils

Weekly Downloads

11

Version

0.0.10

License

none

Last publish

Collaborators

  • rranauro