Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

liorocks/underscore-mixins

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Underscore Mixins

This is a collection of helpful javascript functions (a.k.a. mixins) for underscore.js library.

Take a look at the List of Methods below to become more familiar with this package.

Installation

underscore-mixins is a Bower package and it can be easily added to your project by running the following command:

bower install underscore-mixins

Add --save flag at the end of the command to save this package as bower dependency.

bower install underscore-mixins --save

Manual Installation

Or simply just download the archive of latest release and add the following lines of code to your html file.

<script src="path/to/jquery.js"></script>
<script src="path/to/underscore.js"></script>
<script src="path/to/underscore-mixins.js"></script>

NOTE: underscore-mixins.js (or underscore-mixins.min.js ) file should be loaded after underscore.js library.

List of Methods

Here is a list of methods, which are included in the underscore-mixins package.

  • _.hash()
  • _.param()
  • _.render()
  • _.toAttrs()
  • _.formatMoney()
  • _.log(), _.warn(), _.error(), _.info()

_.hash(key)

Returns javascript window location object's hash property. If key parameter is passed through this method, it will return true or false, depending on, if that key is current hash or not.

http://example.com              //   _.hash() returns "" (an empty string)
http://example.com/#some-hash   //   _.hash() returns `#some-hash`
http://example.com/#some-hash   //   _.hash('some-hash') returns true
http://example.com/#some-hash   //   _.hash('another-hash') returns false

_.param(name)

Returns parameter value from URL query string.

http://example.com/?page=12&keyword=some-text    //    _.param('page') returns 12
http://example.com/?page=12&keyword=some-text    //    _.param('keyword') returns "some-text"
http://example.com/?page=12&keyword=some-text    //    _.param('another-key') returns null

_.render(template, data)

Compiles javascript templates into html string.

It uses underscore.js's template method, but instead of <%= … %> it uses curly braces {{ .. }}.

_.render("<span>Hello {{name}}</span>", { name: "World!" });   
// returns <span>Hello World!</span>

_.toAttrs(obj)

Converts javascript object into html attributes.

_.toAttrs({ class: 'className', id: 'div-1', style: 'display:none' }); 
// returns class="className" id="div-1" style="display:none"

_.formatMoney(amount, decimalLength, wholeNumberLength, wholeNumberDelimiter, decimalDelimiter)

_.formatMoney(123456.789) // returns "123 457"
_.formatMoney(123456.789, 2) // returns "123 456.79"
_.formatMoney(123456.789, 2, 3, ', ') // returns "123,456.79"
_.formatMoney(123456.789, 2, 3, ',', ',') // returns "123,456,79"

_.log(msg), _.warn(msg), _.error(msg), _.info(msg)

_.log, _.warn, _.error and _.info methods are simply shorthand versions for console.log, console.warn, console.error and console.info methods.

_.log('Hello World');
// returns console.log('Hello World');

_.warn('Hello World');
// returns console.warn('Hello World');

_.error('Hello World');
// returns console.error('Hello World');

_.info('Hello World');
// returns console.info('Hello World');

License

The MIT License

About

⚒ Collection of helpful mixins for underscore.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published