dress

missing javascript prototypes(experimental)

npm install dress
14 downloads in the last week
15 downloads in the last month

Dress is a javascript helper library which includes basic functional programming methods and commonly used shortcuts. You can use Dress for your browser application or your node.js application.

Note: This library is still experimental, please do not use this in a production project for now. Many methods may change or delete until the beta or stable version release.

Arrays Objects Strings Numbers
$map $pick $starts $times
$$map $$pick $ends $date
$first $keys $capitalize
$last $values $titleize
$find $invert $integer
$contains $$invert $number
$max $each $date
$min $pairs $parse
$sort $functions
$$sort $merge
$sample $$merge
$samples $omit
$samples $$omit
$each $defaults
$where $$defaults
$$where $clone
$reject $tap
$$reject $isFunction
$take $isArray
$$take $isEmpty
$drop $isString
$$drop $isNumber
$invoke $isBoolean
$$invoke $isDate
$pluck $json
$$pluck
$group
$count
$shuffle
$$shuffle

Installation

Node.js

To use Dress in your node project, first install the module.

npm install dress

After npm installation run this command in the beggining of your project.(e.g. app.js)

require('dress');

Browser

Download index.min.js and load in the beggining of head tag.

<script src="../path/index.min.js"></script>

Arrays

$map

[1, 2, 3].$map(function(item){ return item + 1 });

=> [2, 3, 4]

$$map

array = [1, 2, 3]

array.$$map(function(item){ return item + 1 })

console.log(array)

=> [2, 3, 4]

$first

[5, 3, 2, 6].$first()

=> 5

$last

[5, 3, 2, 6].$last()

=> 6

$find

people = [
    { name: 'Albert Einstein', birthPlace: 'Ulm, Germany' },
    { name: 'Nikola Tesla', birthPlace: 'Smiljan, Croatia' }
]

people.$find(function(item){
    return item.name == 'Nikola Tesla'
}).birthPlace

=> "Smiljan, Croatia"

$contains

[1, 4, 9, 3].$contains(3)

=> true

[1, 4, 9, 3].$contains(2)

=> false

$max

[5, 3, -2, 11, -3].$max()

=> 11

$min

[5, 3, -2, 11, -3].$min()

=> -3

$sort

[5, 3, -2, 11, -3].$sort()

=> [-3, -2, 3, 5, 11]

array = [{ a: 1, b: 6}, { a: 9, b: 3}, { b: 7, c: 1}]

array.$sort(function(item){
    return item.b
})

=> [{ a: 9, b: 3}, { a: 1, b: 6}, { b: 7, c: 1}]

$$sort

array = [5, 3, -2, 11, -3]

array.$$sort()

console.log(array)

=> [-3, -2, 3, 5, 11]

$sample

[5, 3, -2, 11, -3].$sample()

=> 11

[5, 3, -2, 11, -3].$sample()

=> 3

$samples

[5, 3, -2, 11, -3].samples(2)

=> [-3, 5]

[5, 3, -2, 11, -3].samples()

=> [11]

$$samples

array = [5, 3, -2, 11, -3]

array.$$samples(3)

console.log(array)

=> [11, -2]

$each

array = [5, 3, -2]

array.$each(function(item){
    console.log(item)
})

=> 5

=> 3

=> -2

$where

array = [
  { a: 1, b: 2 },
  { a: 2, c: 8},
  { a: -5, c: 8}
]

array.$where({ c: 8 })

=> [{ a: 2, c: 8 }, { a: -5, c: 8 }]

array.$where({ a: 2, c: 8 })

=> [{ a: 2, c: 8 }]

$$where

array = [
  { a: 1, b: 2 },
  { a: 2, c: 8},
  { a: -5, c: 8}
]

array.$$where({ a: -5 })

console.log(array)

=> [{ a: -5, c: 8 }]

$reject

[1, 2, 3, 4, 5, 6].$reject(function(item){
    return item % 2 == 0
})

=> [1, 3, 5]

$$reject

array = [1, 2, 3, 4, 5, 6]

array.$$reject(function(item){
    return item % 2 == 0
})

console.log(array)

=> [1, 3, 5]

$take

[4, 2, 8, 3].take(2)

=> [4, 2]

$$take

array = [4, 2, 8, 3]

array.$$take(3)

console.log(array)

=> [4, 2, 8]

$drop

[4, 2, 8, 3].drop(2)

=> [8, 3]

$$drop

array = [4, 2, 8, 3]

array.$$drop(1)

console.log(array)

=> [2, 8, 3]

$invoke

[[1, 4, 2], [3, 2, 5]].$invoke('$drop', 2)

=> [[2], [5]]

$$invoke

array = [[1, 4, 2], [3, 2, 5]]

array.$$invoke('$drop', 2)

console.log(array)

=> [[2], [5]]

$pluck

[{ a: 1, b: 2 }, { a: 7, b: 6 }, { a: 9 }].$pluck('a')

=> [1, 7, 9]

$$pluck

array = [{ a: 1, b: 2 }, { a: 7, b: 6 }, { a: 9 }]

array.$$pluck('a')

console.log(array)

=> [1, 7, 9]

$group

array = [1.7, 4.3, 4.9]

array.$group(function(item){ return Math.floor(item) })

=> { '1': [1.7], '4': [4.3, 4.9]}

array = ['this', 'is', 'test']

array.$group('length')

=> { '2' : ['is'], '4': ['this', 'test'] }

$count

array = [1, 2, 3, 4, 5]

array.$count(function(item){
    return item % 2 == 0 ? 'even': 'odd';
})

=> { odd: 3, even: 2 }

$shuffle

[1, 2, 3, 4, 5].$shuffle()

=> [3, 5, 1, 4, 2]

[1, 2, 3, 4, 5].$shuffle()

=> [2, 4, 3, 5, 1]

$$shuffle

array = [1, 2, 3, 4, 5]

array.$$shuffle()

console.log(array)

=> [4, 1, 2, 5, 3]

Objects

$pick

({ a: 1, b: 2, c: 3, d: 4 }).$pick('a', 'd')

=> { a: 1, d: 4 }

$$pick

object = { a: 1, b: 2, c: 3, d: 4 }

object.$$pick('a', 'd')

console.log(object)

=> { a: 1, d: 4 }

$keys

({ a: 1, b: 2, c: 3, d: 4 }).$keys()

=> ['a', 'b', 'c', 'd']

$values

({ a: 1, b: 2, c: 3, d: 4 }).$values()

=> [1, 2, 3, 4]

$invert

({ a: 1, b: 2, c: 3 }).$invert()

=> { '1': 'a', '2': 'b', '3': 'c' }

$$invert

object = { a: 1, b: 2, c: 3 }

object.$$invert()

console.log(object)

=> { '1': 'a', '2': 'b', '3': 'c' }

$each

object = { a: 1, b: 2, c: 3 }

result = []

object.$each(function(k, v){ result.push(object[k]) })

console.log(result)

=> [1, 2, 3]

$pairs

({ a: 1, b: 2, c: 3 }).$pairs()

=> [['a', 1], ['b', 2], ['c', 3]]

$functions

object = { a: 1, b: 2, c: function(){ return 'I am the function'; } }

object.$functions()

=> ['c']

$merge

({ a: 1, b: 2 }).$merge({ c: 3, d: 4 })

=> { a: 1, b: 2, c: 3, d: 4 }

$$merge

object = { a: 1, b: 2 }

object.$$merge({ c: 3, d: 4 })

console.log(object)

=> { a: 1, b: 2, c: 3, d: 4 }

$omit

({ a: 1, b: 2, c: 3 }).$omit('b', 'c')

=> { a: 1 }

({ a: 1, b: 2, c: 3 }).$omit(['a', 'c'])

=> { b: 2 }

$$omit

object = { a: 1, b: 2, c: 3 }

object.$$omit('a', 'b')

console.log(object)

=> { c: 3 }

$defaults

({ a: 1, b: 2, c: 3 }).$defaults({ a: 6, e: 9 })

=> { a: 1, b: 2, c: 3, e: 9 }

$$defaults

object = { a: 1, b: 2, c: 3 }

object.$$defaults({ a: 6, e: 9 })

console.log(object)

=> { a: 1, b: 2, c: 3, e: 9 }

$clone

object = { a: 1, b: 2 }

object2 = object.$clone()

console.log(object2)

=> { a: 1, b: 2 }

console.log(object == object2)

=> false

$tap

object = { a: 1, b: 2 }

result = false

fn = function(){ result = true }

object.$defaults({ c: 1 }).$tap(fn).$omit('b')

=> { a: 1, c: 1 }

console.log(result)

=> true

$isFunction

(function(){ }).$isFunction()

=> true

({ name: 'Walter White' }).$isFunction()

=> false

$isArray

[].$isArray()

=> true

(function(){ }).$isArray()

=> false

$isEmpty

({}).$isEmpty()

=> true

({ a: 1 }).$isEmpty()

=> false

$isString

"woo".$isString()

=> true

[].$isString()

=> false

$isNumber

(4).$isNumber()

=> true

({}).$isNumber()

=> false

$isBoolean

(true).$isBoolean()

=> true

("true").$isBoolean()

=> false

$isDate

(new Date).$isDate()

=> true

("Date").$isDate()

=> false

$json

{ a: 12, b: 'hey' }.$json()

=> '{"a":12,"b":"hey"}'

[1, 2, 3].$json()

=> '[1,2,3]'

Strings

$starts

('This is a test string').$starts('This is')

=> true

('This is a test string').$starts('this is')

=> false

$ends

('This is a test string').$ends('test string')

=> true

('This is a test string').$ends('test str')

=> false

$capitalize

'nikola tesla'.$capitalize()

=> 'Nikola tesla'

$titleize

'nikola tesla'.$titleize()

=> 'Nikola Tesla'

$integer

'2014'.$integer()

=> 2014

'2.43'.$integer()

=> 2

$number

'2.12'.$number()

=> 2.12

$date

'10.10.2010 10:11:12'.$date().getTime()

=> 1286694672000

$parse

'{ "a": 12, "b": "test" }'.$parse()

=> { a: 12, b: 'test' }

Numbers

$times

result = []

(5).$times(function(index){ result.push(index) })

console.log(result)

=> [0, 1, 2, 3, 4]

$date

(1286658123123).$date()

=> Sun Oct 10 2010 00:02:03 GMT+0300 (EEST)

License

The MIT License (MIT)

Copyright (c) 2014 Ali Davut

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