项目作者: maxigimenez

项目描述 :
Extend JavaScript classes in base of Ruby methods
高级语言: JavaScript
项目地址: git://github.com/maxigimenez/extend-js-classes.git
创建时间: 2017-01-22T23:47:46Z
项目社区:https://github.com/maxigimenez/extend-js-classes

开源协议:MIT License

下载


Extend JavaScript classes in base of Ruby methods

Array

.first()

Returns the first element of the array. If the array is empty, the first form returns undefined. See also #last for the opposite effect.

  1. var dummy = ['a', 'b', 'c', 'd'];
  2. dummy.first(); #=> `a`

.last()

Returns the last element of self. If the array is empty, the first form returns undefined.

  1. var dummy = ['a', 'b', 'c', 'd'];
  2. dummy.last(); #=> 'd'

.empty()

Returns true if self contains no elements.

  1. [].empty() #=> true

.clear()

Removes all elements from self.

  1. var dummy = ['a', 'b', 'c', 'd'];
  2. dummy.clear(); #=> []

.size()

Alias for .length.

.sample()

Choose a random element or n random elements from the array.

If the array is empty the first form returns undefined.

  1. var dummy = ['a', 'b', 'c', 'd'];
  2. dummy.sample(); #=> 'c'

.compact()

Returns a copy of self with all undefined or null elements removed.

  1. var dummy = ['a', undefined, null, 'd'];
  2. dummy.compact(); #=> ['a', 'd']

.include()

Returns true if the given object is present in self (that is, if any element === object), otherwise returns false.

  1. var dummy = ['a', 'b', 'c', 'd'];
  2. dummy.include('b') #=> true

.take()

Returns first n elements from the array.

  1. var dummy = ['a', 'b', 'c', 'd'];
  2. dummy.take(2) #=> ['a', 'b']

Number

.times()

Iterates the given block int times, passing in values from zero to int - 1.

  1. (5).times(function (i) {
  2. console.log(i);
  3. });
  4. #=> 0 1 2 3 4

.upto()

Iterates the given block, passing in integer values from int up to and including limit.

  1. (1).upto(5, function (i) {
  2. console.log(i);
  3. });
  4. #=> 1 2 3 4 5

.downto()

Iterates the given block, passing decreasing values from int down to and including limit.

  1. (5).downto(1, function (i) {
  2. console.log(i);
  3. });
  4. #=> 5 4 3 2 1