# base-utils

> Extensions to underscore.js for manipulating arrays and objects

Latest version **0.0.10** (published 2015-08-12) · 0 weekly downloads

## Install

```sh
npm install base-utils
pnpm add base-utils
yarn add base-utils
bun add base-utils
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.10 |
| Published | 2015-08-12 |
| First published | 2014-09-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ron Ranauro |
| Maintainers | rranauro |

## Links

- npm: https://www.npmjs.com/package/base-utils
- Repository: https://github.com/rranauro/base-utils
- Issues: https://github.com/rranauro/base-utils/issues
- npm.io page: https://npm.io/package/base-utils

## Dependencies (1)

- [underscore](https://npm.io/package/underscore.md)

## Recent versions

- 0.0.10 (latest) — 2015-08-12
- 0.0.9 — 2015-08-10
- 0.0.8 — 2015-08-10
- 0.0.6 — 2015-03-10
- 0.0.5 — 2015-01-30
- 0.0.3 — 2014-09-30
- 0.0.1 — 2014-09-08
- 0.0.0 — 2014-09-07

## README

# base-utils.js

Add-on utility functions inspired by Underscore

## Usage

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

## Methods
###Utility
* [toInt - Convert string to decimal integer.](#toInt)
* [trim - Remove multiple, leading or trailing spaces.](#trim)
* [initialCaps - Make the first character of string capitalized](#initialCaps)
* [enclose - Wrap up a function and arguement.](#enclose)
* [args - Wrap up a list of arguments into an array.](#args)
* [forceToArray - Wrap up a list of arguments into an array.](#forceToArray)
* [coerce - Convert a string to specific data type.](#coerce)
* [filterNonAscii - Filter out all the NULL characters from a string.](#filterNonAscii)
* [buildHTML - Build html string.](#buildHTML)
* [wait - Execute a function after given seconds.](#wait)

###Arrays
* [found - Find whether a item is inside an array.](#found)
* [compare - Compare two lists to see if list2 contains all the member of lsit1](#compare)
* [identical - Compare two arrays to see if they are the same.](#identical)
* [bfind - Use binary search to find an item in a sorted array.](#bfind)
* [reverse - Reverse the order of items in array.](#reverse)

###Objects
* [select - Return an object with selected properties.](#select)
* [clean - Clean the properties with given property value](#clean)
* [pfetch - Fetch the value of property with given property tag name](#pfetch)
* [fetch - Return the value of first hit given the properties.](#fetch)
* [hfetch - Return the value of property given the path in object.](#hfetch)
* [walk - Traverse a object and call supplied function for every property.](#walk)

<a name="toInt" />
### <span>_.</span>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

<a name="trim" />
### <span>_.</span>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;'    

<a name="initialCaps" />
### <span>_.</span>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'
 
<a name="enclose" />
### <span>_.</span>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!'

<a name="args" />
### <span>_.</span>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']

<a name="forceToArray" />
### <span>_.</span>forceToArray(arguements)

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

__Example:__

    _.forceToArray(2,3,4)
   
__Result:__

    [2,3,4]

<a name="coerce" />
### <span>_.</span>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

<a name="filterNonAscii" />
### <span>_.</span>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"

<a name="buildHTML" />
### <span>_.</span>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>'

<a name="wait" />
### <span>_.</span>wait(second, function)

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

__Example:__

    _.wait(1, function(){
		...
	});
 
<a name="found" />
### <span>_.</span>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 

<a name="compare" />
### <span>_.</span>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

<a name="identical" />
### <span>_.</span>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

<a name="bfind" />
### <span>_.</span>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] 

<a name="reverse" />
### <span>_.</span>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]    

<a name="select" />
### <span>_.</span>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)' 
     }

<a name="clean" />
### <span>_.</span>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 }

<a name="pfetch" />
### <span>_.</span>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"

<a name="fetch" />
### <span>_.</span>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

<a name="hfetch" />
### <span>_.</span>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' ]

<a name="walk" />
### <span>_.</span>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

---
_Source: https://npm.io/package/base-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
