npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

arr

v0.1.5

Published

JavaScript utilities for arrays.

Downloads

38

Readme

arr NPM version

JavaScript utilities for arrays.

Install

Install with npm:

npm i arr --save-dev

Usage

var utils = require('arr');

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.filterType(arr, 'object');
//=> [{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.firstIndex(['a', 1, 'b', 2, {c: 'd'}, 'c'], 'object');
//=> 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.firstIndex(['a', 1, 'b', 2, {c: 'd'}, 'c'], 'object');
//=> 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.firstIndex(['a', 1, 'b', 2, {c: 'd'}, 'c'], 'object');
//=> 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.firstIndex(['a', 1, 'b', 2, {c: 'd'}, 'c'], 'object');
//=> 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.numbers(arr);
//=> [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.numbers(arr);
//=> [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.strings(arr);
//=> ['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.objects(arr);
//=> [{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 one = function() {};
var two = function() {};
var arr = ['a', {a: 'b'}, 1, one, 'b', 2, {c: 'd'}, two, 'c'];

utils.functions(arr);
//=> [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.objects(arr);
//=> [['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.first(arr);
//=> 'a'

utils.first(arr, isType('object'));
//=> {a: 'b'}

utils.first(arr, 'object');
//=> {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 functions
var arr = ['a', {a: 'b'}, 1, one, 'b', 2, {c: 'd'}, two, 'c'];

utils.last(arr);
//=> 'c'

utils.last(arr, isType('function'));
//=> two

utils.last(arr, 'object');
//=> {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'];
lastOfType(arr, 'number');
//=> 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'];
firstOfType(arr, 'number');
//=> 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'];
lastIsType(arr, 'number');
//=> 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'];
firstIsType(arr, 'string');
//=> true

firstString

Get the first string in array.

  • array {Array}
  • returns: {String}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
firstString(arr, 'string');
//=> 'a'

lastString

Get the last string in array.

  • array {Array}
  • returns: {String}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
lastString(arr, 'string');
//=> 'c'

firstFunction

Get the first function in array.

  • array {Array}
  • returns: {Function}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
firstFunction(arr, 'function');
//=> 'a'

lastFunction

Get the last function in array.

  • array {Array}
  • returns: {Function}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
lastFunction(arr, 'function');
//=> 'a'

firstNumber

Get the first number in array.

  • array {Array}
  • returns: {Function}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
firstNumber(arr, 'number');
//=> '1'

lastNumber

Get the last number in array.

  • array {Array}
  • returns: {Function}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
lastNumber(arr, 'number');
//=> '2'

firstObject

Get the first object in array.

  • array {Array}
  • returns: {Object}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
firstObject(arr);
//=> {a: 'b'}

lastObject

Get the last object in array.

  • array {Array}
  • returns: {Object}
var arr = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c'];
lastObject(arr);
//=> {c: 'd'}

Author

Jon Schlinkert

Other javascript/node.js utils

Other projects that I maintain:

License

Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license


This file was generated by verb-cli on November 03, 2014.