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

array-jar

v1.0.2

Published

A utility package for enhanced array methods with additional features and robust error handling.

Downloads

5

Readme

array-jar

array-jar is a JavaScript utility library for arrays that provides a collection of functions to simplify common array operations. It includes methods for safely accessing array elements, removing duplicates, finding intersections, and more. This library supports compatibility with older browsers by using polyfills and ES5 syntax.

Features

  • Safe array access with bounds checking
  • Safe method calls for various array methods
  • Compatible with older browsers

Installation

You can install array-jar via npm.

From npm

If the package is published on npm, you can install it directly:

npm install array-jar

Usage

Importing the Package

const arrayUtils = require("array-jar");

Available Methods

safeGet(arr, index)

Safely access array elements with bounds checking.

  • Parameters:

    • arr (Array): The array to access.
    • index (number): The index to access.
  • Returns: The element at the specified index or undefined if out of bounds.

Example:

console.log(arrayUtils.safeGet([1, 2, 3], 1)); // Output: 2
console.log(arrayUtils.safeGet([1, 2, 3], 10)); // Output: undefined

unique(arr)

Remove duplicates from an array.

  • Parameters:

    • arr (Array): The array to process.
  • Returns: A new array with duplicates removed.

Example:

console.log(arrayUtils.unique([1, 2, 2, 3, 4, 5, 5])); // Output: [1, 2, 3, 4, 5]

intersect(arr1, arr2)

Get the intersection of two arrays.

  • Parameters:

    • arr1 (Array): The first array.
    • arr2 (Array): The second array.
  • Returns: An array containing elements present in both arrays.

Example:

console.log(arrayUtils.intersect([1, 2, 3], [2, 3, 4])); // Output: [2, 3]

Array Method Wrappers

The package also provides wrappers for common array methods:

  • forEach(arr, callback, thisArg)
  • map(arr, callback, thisArg)
  • filter(arr, callback, thisArg)
  • reduce(arr, callback, initialValue)
  • reduceRight(arr, callback, initialValue)
  • every(arr, callback, thisArg)
  • some(arr, callback, thisArg)
  • find(arr, callback, thisArg)
  • findIndex(arr, callback, thisArg)
  • sort(arr, compareFn)
  • reverse(arr)
  • concat(arr, ...items)
  • join(arr, separator)
  • slice(arr, start, end)
  • splice(arr, start, deleteCount, ...items)
  • push(arr, ...items)
  • pop(arr)
  • shift(arr)
  • unshift(arr, ...items)
  • includes(arr, valueToFind, fromIndex)
  • indexOf(arr, searchElement, fromIndex)
  • lastIndexOf(arr, searchElement, fromIndex)
  • copyWithin(arr, target, start, end)
  • fill(arr, value, start, end)
  • toString(arr)
  • toLocaleString(arr)

Example:

const arr = [1, 2, 3, 4, 5];

// Using forEach
arrayUtils.forEach(arr, function (item) {
  console.log(item);
});

// Using map
const doubled = arrayUtils.map(arr, function (item) {
  return item * 2;
});
console.log(doubled); // Output: [2, 4, 6, 8, 10]

Error Handling

Ensure you handle errors appropriately when using the methods:

try {
  console.log(arrayUtils.safeGet([1, 2, 3], "not-a-number")); // This will throw an error
} catch (error) {
  console.error("Error:", error.message);
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the ISC License - see the LICENSE file for details.


Feel free to customize the README as needed for your project!