underscore-mixins

0.1.4 • Public • Published

SPHERE.IO icon

Underscore mixins

NPM

Build Status NPM version Coverage Status Dependency Status devDependency Status

A collection of methods to be used as underscore mixins

Table of Contents

Getting Started

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

Documentation

_.deepClone

Returns a deep clone of the given object

obj = {...} # some object with nested values 
cloned = _.deepClone(obj)

_.prettify

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)

_.percentage

Returns the percentage of the given values

value = _.percentage(30500)
# => 6 

_.stringifyQuery

Returns a URL query string from a key-value object

params =
  where: encodeURIComponent('name = "Foo"')
  staged: true
  limit: 100
  offset: 2
_.stringifyQuery(params)
# => 'where=name%20%3D%20%22Foo%22&staged=true&limit=100&offset=2' 

_.parseQuery

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'
_.parseQuery(query)
# => {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']} 

_.batchList

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 = [1234567890]
_.batchList(list3)
# => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0]] 

Contributing

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

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

License

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

Readme

Keywords

none

Package Sidebar

Install

npm i underscore-mixins

Weekly Downloads

1,219

Version

0.1.4

License

none

Last publish

Collaborators

  • emmenko