0.0.10 • Published 10 years ago

base-utils v0.0.10

Weekly downloads
113
License
-
Repository
github
Last release
10 years ago

base-utils.js

Add-on utility functions inspired by Underscore

Usage

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

Methods

###Utility

###Arrays

###Objects

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

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.3

11 years ago

0.0.1

11 years ago

0.0.0

11 years ago