JavaScript utilities for arrays.
Install
Install with npm:
npm i arr --save-dev
Usage
var utils = ;
API
filterType
Filter array
, returning only the values of the given type
.
array
{Array}type
{String}: Native type, e.g.string
,object
returns
: {Boolean}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c'; utils;//=> [{a: 'b'}, {c: 'd'}]
firstIndex
Return the first index of the given type
, or -1
if not found.
array
{Array}type
{String}: Native type, e.g.string
,object
returns
: {Boolean}
utils;//=> 4
findFirst
Return the first index of the given type
, or -1
if not found.
array
{Array}type
{String}: Native type, e.g.string
,object
returns
: {Boolean}
utils;//=> 4
lastIndex
Return the first index of the given type
, or -1
if not found.
array
{Array}type
{String}: Native type, e.g.string
,object
returns
: {Boolean}
utils;//=> 4
findLast
Return the first index of the given type
, or -1
if not found.
array
{Array}type
{String}: Native type, e.g.string
,object
returns
: {Boolean}
utils;//=> 4
hasType
Filter array
, returning only the numbers.
array
{Array}index
{Array}: Optionally specify the index of the number to return, otherwise all numbers are returned.returns
{Array}: Array of numbers or empty array.
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c'; utils;//=> [1, 2]
numbers
Filter array
, returning only the numbers.
array
{Array}index
{Array}: Optionally specify the index of the number to return, otherwise all numbers are returned.returns
{Array}: Array of numbers or empty array.
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c'; utils;//=> [1, 2]
strings
Filter array
, returning only the strings.
array
{Array}index
{Array}: Optionally specify the index of the string to return, otherwise all strings are returned.returns
{Array}: Array of strings or empty array.
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c'; utils;//=> ['a', 'b', 'c']
objects
Filter array
, returning only the objects.
array
{Array}index
{Array}: Optionally specify the index of the object to return, otherwise all objects are returned.returns
{Array}: Array of objects or empty array.
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c'; utils;//=> [{a: 'b'}, {c: 'd'}]
functions
Filter array
, returning only the functions.
array
{Array}index
{Array}: Optionally specify the index of the function to return, otherwise all functions are returned.returns
{Array}: Array of functions or empty array.
var {};var {};var arr = 'a' a: 'b' 1 one 'b' 2 c: 'd' two 'c'; utils;//=> [one, two]
arrays
Filter array
, returning only the arrays.
array
{Array}index
{Array}: Optionally specify the index of the array to return, otherwise all arrays are returned.returns
{Array}: Array of arrays or empty array.
var arr = 'a' 'aaa' 1 'b' 'bbb' 2 c: 'd' 'c'; utils;//=> [['aaa'], ['bbb']]
first
Return the first element in array
, or, if a callback is passed, return the first value for which the returns true.
array
{Array}returns
: {*}
var arr = 'a' a: 'b' 1 one 'b' 2 c: 'd' two 'c'; utils;//=> 'a' utils;//=> {a: 'b'} utils;//=> {a: 'b'}
last
Return the last element in array
, or, if a callback is passed, return the last value for which the returns true.
array
{Array}returns
: {*}
// `one` and `two` are functionsvar arr = 'a' a: 'b' 1 one 'b' 2 c: 'd' two 'c'; utils;//=> 'c' utils;//=> two utils;//=> {c: 'd'}
lastOfType
Get the last element in array
of the given type
.
array
{Array}type
{String}: The native type to get.returns
: {*}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> 2
firstOfType
Get the first element in array
of the given type
.
array
{Array}type
{String}: The native type to get.returns
: {*}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> 1
lastIsType
Returns true
if the last element in array
is the given type
.
array
{Array}type
{String}: The native type to check.returns
: {Boolean}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> false
firstIsType
Returns true
if the first element in array
is the given type
.
array
{Array}type
{String}: The native type to check.returns
: {Boolean}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> true
firstString
Get the first string in array
.
array
{Array}returns
: {String}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> 'a'
lastString
Get the last string in array
.
array
{Array}returns
: {String}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> 'c'
firstFunction
Get the first function in array
.
array
{Array}returns
: {Function}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> 'a'
lastFunction
Get the last function in array
.
array
{Array}returns
: {Function}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> 'a'
firstNumber
Get the first number in array
.
array
{Array}returns
: {Function}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> '1'
lastNumber
Get the last number in array
.
array
{Array}returns
: {Function}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> '2'
firstObject
Get the first object in array
.
array
{Array}returns
: {Object}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> {a: 'b'}
lastObject
Get the last object in array
.
array
{Array}returns
: {Object}
var arr = 'a' a: 'b' 1 'b' 2 c: 'd' 'c';;//=> {c: 'd'}
Author
Jon Schlinkert
Other javascript/node.js utils
Other projects that I maintain:
- assemble
- verb
- less.js
- handlebars-helpers
- arr
- arr-diff
- array-last
- array-slice
- array-sum
- arrayify-compact
- compact-object
- delete
- for-in
- for-own
- has-any
- has-value
- is-number
- is-plain-object
- mixin-deep
- mixin-object
- object-length
- omit-empty
- reduce-object
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on November 03, 2014.