A collection of methods to be used as underscore mixins

Downloads in past


010.1.49 years ago10 years agoMinified + gzip package size for underscore-mixins in KB


Underscore mixins
Build Status NPM version Coverage Status Dependency Status devDependency Status
A collection of methods to be used as underscore mixins

Table of Contents

.deepClone .prettify .percentage .stringifyQuery .parseQuery .batchList

Getting Started

_ = require 'underscore'
_.mixin require('underscore-mixins')



Returns a deep clone of the given object
obj = {...} # some object with nested values
cloned = _.deepClone(obj)


Returns a pretty-print formatted JSON string.
obj = foo: 'bar'
pretty = _.prettify(obj) # you can pass the indentation value as optional 2nd argument (default 2)
# =>
# "{
#   "foo": "bar"
# }"

If the argument is not a JSON object, the argument itself is returned (also for Error instances)


Returns the percentage of the given values
value = _.percentage(30, 500)
# => 6


Returns a URL query string from a key-value object
params =
  where: encodeURIComponent('name = "Foo"')
  staged: true
  limit: 100
  offset: 2
# => 'where=name%20%3D%20%22Foo%22&staged=true&limit=100&offset=2'


Returns a key-value JSON object from a query string
Note that all values are parsed as string

query = 'where=name%20%3D%20%22Foo%22&staged=true&limit=100&offset=2'
# => {where: 'name%20%3D%20%22Foo%22', staged: 'true', limit: '100', offset: '2'}

You can pass a hasUniqueParams as second argument to determine the parsing strategy in case of multiple parameters with the same key:
  • true (default): same parameter key will be overridden
  • false: same parameters values will be put in an array

query = 'foo=bar1&foo=bar2'
# => {foo: ['bar1', 'bar2']}


Transform a given list in a new nested list of single list elements (batches) given max size. Useful if you need to process some elements on a list, but not all together.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
_.batchList(list, 3)
# => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0]]


In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt. More info here


Releasing a new version is completely automated using the Grunt task grunt release.
grunt release // patch release
grunt release:minor // minor release
grunt release:major // major release


Copyright (c) 2014 SPHERE.IO Licensed under the MIT license.