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

sbarr

v1.1.1

Published

Standard Javascript Workaround for Arrays and Arrays of Objects.

Downloads

3

Readme

README.md

Sbarr

This repository contains utility functions for manipulating arrays of objects. It provides methods for finding objects by key-value pairs, determining the index of an object based on a key, and sorting arrays of objects by multiple properties.

Installation

To use these utility functions in your project, you can install them via npm:

npm install sbarr

Usage

findObjectByKey(array, key, value)

This function finds an object within an array of objects by a specific key-value pair.

Parameters:

  • array: The array of objects to search through.
  • key: The key to search for.
  • value: The value associated with the key.

Returns:

  • Returns the first object found with the specified key-value pair. If no object is found, it returns null.

Example:

const { findObjectByKey } = require('sbarr');

const array = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Alice' },
    { id: 3, name: 'Bob' }
];

const result = findObjectByKey(array, 'name', 'Alice');
console.log(result); // Output: { id: 2, name: 'Alice' }

parentIndexOfObjKey(array, attr)

This function determines the index of the first object containing a specific attribute.

Parameters:

  • array: The array of objects to search through.
  • attr: The attribute to search for.

Returns:

  • Returns the index of the first object containing the specified attribute.

Example:

const { parentIndexOfObjKey } = require('sbarr');

const array = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Alice' },
    { id: 3, name: 'Bob' }
];

const result = parentIndexOfObjKey(array, 'name');
console.log(result); // Output: 0

sortByProperties(array, properties)

This function sorts an array of objects by multiple properties.

Parameters:

  • array: The array of objects to be sorted.
  • properties: An array of strings representing the properties to sort by. Prefix a property with '-' to sort in descending order.

Returns:

  • Returns the sorted array of objects.

Example:

const { sortByProperties } = require('sbarr');

const array = [
    { name: 'John', date: '2023-05-15' },
    { name: 'Alice', date: '2022-10-20' },
    { name: 'Bob', date: '2024-01-08' }
];

const sortedArray = sortByProperties(array, ['name', '-date']);
console.log(sortedArray);
/* Output:
[
    { name: 'Alice', date: '2022-10-20' },
    { name: 'Bob', date: '2024-01-08' },
    { name: 'John', date: '2023-05-15' }
]
*/

To sort in ascending order, simply list the property without any prefix. To sort in descending order, prefix the property with '-'.