A collection of helpful methods that JavaScript should already have, but doesn't.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
LICENSE.markdown
README.markdown

README.markdown

JavaScript-Utils

A collection of helpful methods that JavaScript should already have, but doesn't.

Updated Feb. 21, 2015

  • Renamed Object.setDefaults() to Object.setObjectDefaults() to avoid a conflict with certain objects in jQuery.

Installation

Include src/javascript-utils.js in your HTML document:

<script type="text/javascript" src="<path-to-javascript-utils>/src/javascript-utils.js"></script>

String.format()

String.format() brings Python-inspired string formatting to JavaScript. Now you can stop concatenating strings with the + operator like a caveman.

Usage:
  1. Insert format arguments enclosed in curly braces in a string:

     var string = 'Hello, my name is {firstname} {lastname}.';
    
  2. Call .format() on the string, passing in an object containing the format arguments and replacement values:

     var string = 'Hello, my name is {firstname} {lastname}.';
     var formattedString = string.format({
     	firstname: 'Tony',
     	lastname: 'Wallace'
     });
    
     console.log(formattedString); // 'Hello, my name is Tony Wallace.'
    
  3. .format() also works on a string literal:

     var formattedString = 'Hello, my name is {firstname} {lastname}.'.format({
     	firstname: 'Tony',
     	lastname: 'Wallace'
     });
    
     console.log(formattedString); // 'Hello, my name is Tony Wallace.'
    
  4. Format arguments may be reused. You can also insert whitespace between the arguments and curly braces:

     var formattedString = 'My first name is { firstname }. My last name is { lastname }. My full name is { firstname } { lastname }.'.format({
     	firstname: 'Tony',
     	lastname: 'Wallace'
     });
    
     console.log(formattedString); // 'My first name is Tony. My last name is Wallace. My full name is Tony Wallace.'
    

Object.setObjectDefaults()

Object.setObjectDefaults() populates an object (the source object) with the key-value pairs from a second object (the defaults object). If a key from the defaults is already present in the source, the source value is preferred. Example:

var source = {
	prop1: 'value1',
	prop2: 'value2'
};

source.setObjectDefaults({
	prop1: 'default1',
	prop2: 'default2',
	prop3: 'default3'
});

console.log(source);

/*
// Result:
{
 	prop1: 'value1',
	prop2: 'value2',
	prop3: 'default3'
}
*/

This is useful for providing default values in objects passed as function arguments:

function doSomething(options) {

	// Assign an empty object to options if it is null or undefined:
	options = options || {};
	// Set default values:
	options.setObjectDefaults({
		prop1: 'value1',
		prop2: 'value2'
	});

	// Safely work with the options...

}

Object.setObjectDefaults() handles nested objects by recursively copying values from the defaults for keys that do not exist in the source:

var source = {
	prop1: 'value1',
	prop2: 'value2',
	prop3: {
		nestedProp1: {
			nestedNestedProp1: 'nestedNestedValue1'
		}
	}
};

source.setObjectDefaults({
	prop1: 'default1',
	prop2: 'default2',
	prop3: {
		nestedProp1: {
			nestedNestedProp1: 'nestedNestedDefault1',
			nestedNestedProp2: 'nestedNestedDefault2'
		},
		nestedProp2: 'nestedDefault2'
	}
});

console.log(source);

/*
// Result:
{
 	prop1: 'value1',
	prop2: 'value2',
	prop3: {
		nestedProp1: {
			nestedNestedProp1: 'nestedNestedValue1',
			nestedNestedProp2: 'nestedNestedDefault2'
		},
		nestedProp2: 'nestedDefault2'
	}
}
*/

License:

JavaScript-Utils is made available under the MIT License. See LICENSE.markdown for details.

Credits:

JavaScript-Utils was created by Tony Wallace.