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

arraybrain

v1.0.9

Published

Arrays never been so smart before.

Downloads

34

Readme

A QUICK USE CASE

• For example, you made a get request to your API, and it returned you a bunch of {objects} in {objects} also in {objects} in an [ParentArray] or {ParentObject}.
• Now you want to Sort them by Descending of price values. If price are same then you want to sort "props.id" values by Ascending which lives in second object inside the first object. If also "props.id" values are same then sort "props.anotherProps.anotherProps.id" values by Descending.

INSTALLATION

npm install arraybrain

METHODS & USAGE

// Before start, import the arraybrain first.
import ArrayBrain from 'arraybrain';

Not: How to define a key property ?

Sample key ⇒ 'props.date=date|desc'
Explanation
props.date ⇒ is a simple js object iteration. Select date from prop.
After equal sign "="
date|desc ⇒ the props.date is a date property and I want to sort by descending
function sort(
  arr: any[], // Pass the array that you wan to sort
  direction: Direction, // Define the direction as 'asc' or 'desc'
  valueType: Value, // Define type of values inside the array
  key?: string, // If it is an object, define the key
  alternativeKey?: string // If the defined key's values are same then sort the array based on alternativeKey's value.
): any[];

let arr = ['2011-11-16', '2011-11-13', '2011-11-12'];
let sortedArray = ArrayBrain.sort(arr, 'desc', 'date');

Sort by using one key and if the sorted values are same than sort by using another key.

// Lets define objects inside an array also object inside in objects
let arr = [
  {
    id: 2,
    productName: 'head ligth',
    price: '100',
    props: { id: 2, date: '2011-11-12' }
  },
  {
    id: 3,
    productName: 'tire',
    price: '500',
    props: { id: 3, date: '2011-11-15' }
  },
  {
    id: 1,
    productName: 'bumper',
    price: '500',
    props: { id: 1, date: '2011-11-14' }
  },
  {
    id: 5,
    productName: 'seats',
    price: '800',
    props: { id: 5, date: '2011-11-13' }
  }
];

let sortedArray = sort(
  arr, // Specify the array
  'asc', // Descending or ascending
  'number', // What is its type? 'number' | 'string' | 'date'
  'price', // Compare the 'price' tag
  'props.date=date|desc' // What if the 'price' are same? simply sort based on 'date' prop, also specify 'ascending' or 'descending'
);

Sort the objects with given keys in the props array until the values are not same.

function smartSort(
  arr: any[], // Pass the array that you wan to sort
  keys: string[] // Define compareable keys as much as you want!
): any[];

let arr = [
  {
    id: 2,
    productName: 'head ligth',
    price: '100',
    props: { id: 2, date: '2011-11-12', inProps: { id: 1 } }
  },
  {
    id: 5,
    productName: 'seats',
    price: '500',
    props: { id: 5, date: '2011-11-15', inProps: { id: 8 } }
  },
  {
    id: 1,
    productName: 'bumper',
    price: '500',
    props: { id: 1, date: '2011-11-14', inProps: { id: 3 } }
  },
  {
    id: 3,
    productName: 'tire',
    price: '500',
    props: { id: 3, date: '2011-11-14', inProps: { id: 5 } }
  }
];

let sortedArray = ArrayBrain.smartSort(arr, [
  'price=number|asc', // Sort by prices in ascending
  'props.date=date|desc', // If prices are same then sort by props.date in descending
  'props.inProps.id=number|asc' // If dates are same then sort by props.inProps.ids in ascending
]);

Sort Objects {} and get output as type of sorted Object or sorted Array.

function smartObjectSort = (
  obj: SortObjectParameter, // Pass the object that you want to sort
  keys: string[], // Define compareable keys as much as you want!
  keepKeyAsProp: boolean, // Do you still want to store actual keys ?
  retType?: 'obj' | 'arr' // Return a sorted array or same object as sorted!
): { [key: string]: any } | any[];

// We will sort this testing array, keep reading the example at the end of the line!
let testOject = {
  xxx: {
    id: 2,
    productName: 'head ligth',
    price: '100',
    props: { id: 2, date: '2011-11-12', inProps: { id: 1 } }
  },
  xxy: {
    id: 3,
    productName: 'tire',
    price: '500',
    props: { id: 3, date: '2011-11-14', inProps: { id: 5 } }
  },
  xyy: {
    id: 1,
    productName: 'bumper',
    price: '500',
    props: { id: 1, date: '2011-11-14', inProps: { id: 3 } }
  },
  yyy: {
    id: 5,
    productName: 'seats',
    price: '500',
    props: { id: 5, date: '2011-11-15', inProps: { id: 8 } }
  }
};

// This will return the same object as sorted
let returnAsArray = ArrayBrain.smartObjectSort(
  testOject,
  [
    'price=number|asc', // Sort by number in ascending
    'props.date=date|desc', // If number are same then sort by props.date in descending
    'props.inProps.id=number|asc' // If dates are same then sort by props.inProps.ids in ascending
  ],
  true, // If you also want to keep keys as well inside the objects, then this will create a property as _arraybrainkey: key
  'obj' // This will return the sorted object as object
);

// This will return the object as sorted array
// If you want to keep actual object's key as well, you should pass "true" parameter to the method. It will return the actual key as _arraybrainkey: key
let returnAsObject = ArrayBrain.smartObjectSort(
  testOject, // Pass the object
  [
    'price=number|asc', // Sort by number in ascending
    'props.date=date|desc', // If number are same then sort by props.date in descending
    'props.inProps.id=number|asc' // If dates are same then sort by props.inProps.ids in ascending
  ], // this will compare all those different keys until they are not same
  false, // If you want the actual object's keys as a prop in the output array, change it to 'true', then the actual keys will be stored in the array's object as _arraybrainkey: key
  'arr' // This will return the sorted object as an array
);