juicy

Extending JavaScript's native clases with useful methods

npm install juicy
13 downloads in the last week
53 downloads in the last month

Juicy

Juicy extends JavaScript's Number, Object, Array and String with some useful methods.

Installation

Browsers:

Just include juicy.min.js:

<srcipt src="juicy.min.js"></script>

Node.js:

Install with:

npm install juicy

And,

require 'juicy'

Usage

Function

include
class SomeExtension
    class SomeExtension.ClassMethods
        someClassMethod: ->

    class SomeExtension.InstanceMethods
        someInstanceMethod: ->

class SomeModel

SomeModel.include SomeExtension

SomeModel.someClassMethod()

model = new SomeModel
model.someInstanceMethod()

Array

collect
items = ['a', 'b'].collect (item) ->
    "#{ item }!"

# items = ['a!', 'b!']
combination
['a', 'b', 'c'].combination(1) # [['a'], ['b'], ['c']]

['a', 'b', 'c'].combination(2) # [['a', 'b'], ['c']]
compact
[true, false, 'a', null, 'b', undefined].compacted # [true, false, 'a', 'b']
merge
['a', 'b'].merge ['c', 'd'] # ['a', 'b', 'c', 'd']
count
count = [1, 2, 3, 4].count (item) ->
    item < 3

# count = 2
remove
items = ['a', 'b', 'c', 'a']
items.remove 'a'

# items = ['b', 'c']
removeAt
items = ['a', 'b', 'c']
items.removeAt 0

# items = ['b', 'c']
removeIf
items = ['a', 'b', 'c'].removeIf (item) ->
    item > 'a'

# items = ['a']
drop
items = ['a', 'b', 'c']
items.drop 2

# items = ['c']
dropWhile
items = ['a', 'b', 'c', 'd'].dropWhile (item) ->
    item < 'd'

# items = ['d']
eachIndex
['a', 'b', 'c'].eachIndex (index) ->
    console.log index

# output: 0, 1, 2
eql
first = ['a', ['b']]
second = ['a', ['b']]
third = ['a', 'b']

first.eql(second) # true
second.eql(third) # false
flatten
['a', ['b']].flattened # ['a', 'b']
take
['a', 'b', 'c'].take(2) # ['a', 'b']
takeWhile
items = ['a', 'b', 'c'].takeWhile (item) ->
    item < 'c'

# items = ['a', 'b']
transpose
[rows, columns] = [['a', 'c'], ['b', 'd']].transposed

# rows[0] = 'a', rows[1] = 'b'
# columns[0] = 'c', columns[1] = 'd'
first
items = ['a', 'b']

items.first # 'a'

items.first = 'c'
# items = ['c', 'd']
last
items = ['a', 'b']
items.last # 'b'

items.last = 'c'
# items = ['a', 'c']
shuffle
items = ['a', 'b']

items.shuffled // or items.shuffle()

Object

merge
first=
    a: 1

second=
    b: 2

Object.merge(first, second) # { a: 1, b: 2 }
eql
first=
    a: 1

second=
    a: 1

third=
    b: 2

Object.eql(first, second) # true
Object.eql(second, third) # false

String

capitalize
'hello'.capitalized # 'Hello'
pluralize
'item'.pluralized # 'items'
camelize
'nice\_product'.camelized 'NiceProduct'
underscore
'NiceProduct'.underscored # 'nice\_product'
trim
'   hello  '.trimmed # 'hello'
singularize
'items'.singularized # 'item'
humanize
'nice\_product'.humanized 'Nice product'
dasherize
'nice product'.dasherized # 'nice-product'
titleize
'nice product'.titleized # 'Nice Product'
demodulize
'ActiveController::Metal'.demodulized # 'Metal'
tableize
'author id'.tableized # 'author ids'

Tests

You can run tests on Node.js by running mocha, or in browser by opening test/test.html.

License

(The MIT License)

Copyright (c) 2011 Vadim Demedes sbioko@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

npm loves you