1.0.0 • Published 8 years ago

slice-like v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

slice-like

Version License Build Coverage Dependencies

Creates an Array from a slice of an Array-like object.

Install

Install via npm:

npm install --save slice-like

Usage

Convert an Array-like object to an Array

Pass any Array-like object an object with a 'length' property and numerical indices in order to convert it to an Array.

const sliceLike = require('slice-like');

var arrayLike = {
  0: 'zero',
  1: 'one',
  length: 2
};

var arr = sliceLike(arrayLike); // => ['zero', 'one'];

An Alternative to Rest Parameters

This module may be used in place of rest parameters in environments where they are unsupported. Pass the arguments object and the index at which the rest parameters start, as follows:

function someFunction(firstParam, secondParam /*, ...restParams */) {
  var restParams = sliceLike(arguments, 2);
  // ...
}

API

sliceLike(arrayLike, [start], [end])

Slices an Array-like object and returns an Array. Slicing behavior is similar to the Array.prototype.slice() method.

Parameters

Returns

Type: Array

The resulting Array, sliced according to the start and/or end arguments if provided.

License

Copyright © 2016 Akim McMath. Licensed under the MIT License.