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

generic-slice

v0.0.1

Published

Array.prototype.slice.call()-like method for TypedArrays, Buffers and Arguments

Downloads

86

Readme

generic-slice

A poly fill for slice that uniformly and efficiently implements Array-like semantics for the following classses of objects:

  • Typed Arrays
  • Node.JS-style Buffers()
  • Regular JS Arrays
  • Arguments
  • Any other array-like object (ie has a length and array accessors)

Install

npm install generic-slice

Example

var slice = require("generic-slice")

var x = new Float32Array(4)
x[0] = 1
x[1] = 2
x[2] = 3
x[3] = 4

var y = slice(x, 0, 2)

// Now:
//      y = [ 1, 2 ]
//      x = [ 1, 2, 3, 4 ]


y[0] = 1000

// Now:
//      y = [ 1000, 2 ]
//      x = [ 1, 2, 3, 4 ]

require("generic-slice")(array[, sliceBegin, sliceEnd])

A generic polyfill for slicing array-like objects.

  • array is a typed array, buffer, Array, or Arguments object
  • sliceBegin is an optional argument giving the start of the slice. Negative values change counting to end of array. (Default: 0)
  • sliceEnd is an optional argument giving the end of the slice. Negative values wrap around. (Default: array.length)

Returns A copy of the array from the range [sliceBegin, sliceEnd)

Why make this?

Because:

  • slice got dropped from the typed array specification (and the replacement method, .subarray() merely returns a view and does not make a copy)
  • Node.JS's Buffer's slice method does not make a copy like Array's slice does, and so it has inconsistent semantics.
  • Arguments doesn't have a slice method either
  • Using Array.prototype.slice.call doesn't properly preserve the types of things like Buffers or typed arrays
  • Also Array.prototype.slice.call can be slower than an appropriate implementation. For example, to clone a typed array you can slice the underlying ArrayBuffer assuming that you are careful about how you perform indexing.

Is it thoroughly tested?

All of the major edge cases should be tested. If you find a bug, please open an issue or send a pull request.

Credits

(c) 2013 Mikola Lysenko. MIT License