npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

base-utils

v0.0.10

Published

Extensions to underscore.js for manipulating arrays and objects

Downloads

11

Readme

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