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

goku-array

v0.0.3

Published

Extended array

Downloads

371

Readme

Build Status Coverage Statusnpm version

Goku Array

Utility class that extends Array, works with node 10

Install

npm i goku-array

OR

yarn add goku-array

Utilities

unique()

import GokuArray from 'goku-array';

const arrayOfPrimitives = [1, 2, 2, 3, 3, 4, 1];
const arrayOfObjects = [
  {
    id: 1,
    text: 'sample'
  },
  {
    id: 1,
    text: 'sample'
  },
  {
    id: 2,
    text: 'sample'
  },
  {
    id: 1,
    text: 'sample'
  },
];

  // make an array of primitives unique

  const arr = new GokuArray(arrayOfPrimitives);
  const newUniqueArray = arr.unique() // [1, 2, 3, 4]

 // make an array of objects unique passing a function if given the right field to filter
  const arr = new GokuArray(arrayOfObjects);
  let newUniqueArray = arr.unique(({ id }) => id) // [{ id: 1, text: 'sample' }, { id: 2, text: 'sample' }]

  newUniqueArray = arr.unique(({ text }) => text) // [{ id: 1, text: 'sample' }]

diff()

const arrayOne = [1, 2, 2, 3, 3, 4, 1];
const arrayTwo = [1, 3, 7, 2, 5];

// find the difference in 2 arrays
// result is an object

  const arr = new GokuArray(arrayOne);
  const objDiff = arr.diff(arrayTwo)
  
  console.log(objDiff)
  
// {
//   additional: [7, 5],
//   missing: [4],
//   common: [1, 2, 3]
// }

missingFrom()

const arrayOne = [1, 2, 2, 3, 3, 4, 1];
const arrayTwo = [1, 3, 7, 2, 5];

// find the missing item in the second array
  const arr = new GokuArray(arrayOne);
  const arrDiff = arr.missingFrom(arrayTwo)
  console.log(arrDiff)
  // [4]

asyncMap()

function timeout(ms) {
  return new Promise(resolve => setTimeout(resolve(true), ms * 1000));
}

const simpleArray = [1, 2, 3]

// await for the promises to resolve'

  const arr = new GokuArray(simpleArray)
  const newArr = await arr.asyncMap(item => timeout(item))
  console.log(newArray)
  // [true, true, true]

asycnFilter()

function timeout(ms) {
  return new Promise(resolve => setTimeout(resolve(ms), ms * 1000))
}

const simpleArray = [1, 2, 3]

// await for the promises to resolve
  const arr = new GokuArray(simpleArray)
  const newArr = await arr.asyncFilter(async item => {
    const obj = await timeout(item)
    return obj === item
  })
  console.log(newArr)
  // [1, 2, 3]

groupBy(key)

const arrayOfMixed = [1, 2, { id: 'one', text: 'same' }, 3, 3, 4, 1]
const arrayOfObjects = [
  {
    id: 'one',
    text: 'sample #1'
  },
  {
    id: 'one',
    text: 'sample #2'
  },
  {
    id: 'one',
    text: 'sample #3'
  },
  {
    id: 'two',
    text: 'sample #4'
  },
  {
    __id: 'two',
    text: 'sample #4'
  }
]

let arr = new GokuArray(arrayOfMixed)
let newGroupedArray = arr.groupBy('id')
  // newGroupedArray equals to {
  //   "one": [
  //     {
  //       "id": "one",
  //       "text": "same",
  //     },
  //   ],
  // })

  arr = new GokuArray(arrayOfObjects)
  newGroupedArray = arr.groupBy('id')
  // newGroupedArray equals to {
  //   "one": [
  //     {
  //       "id": "one",
  //       "text": "same",
  //       "text": "sample #1",
  //     }, {
  //       "id": "one",
  //       "text": "sample #2",
  //     },
  //     {
  //       "id": "one",
  //       "text": "sample #3",
  //     },
  //   ],
  //   "two": [
  //     {
  //       "id": "two",
  //       "text": "sample #4",
  //     },
  //   ],
  // }

sortItems()

You may sort numbers, strings and objects

const arrayOfNumbers = [2, 1, 3]
const arrayOfStrings = ['a', 'c', 'b']
const arrayOfObjects = [{ id: 1, text: 'a' }, { id: 3, text: 'c' }, { id: 2, text: 'b' }]

// Numbers
// array of numbers ascending
let arr = new GokuArray(arrayOfNumbers)
arr.sortItems()
// [1, 2, 3]

arr = new GokuArray(arrayOfNumbers)
arr.sortItems({ ordering: 'ASC' })
// [1, 2, 3]


// array of numbers descending'
let arr = new GokuArray(arrayOfNumbers)
arr.sortItems({ ordering: 'DESC' })
// [3, 2, 1]


// Strings
// array of strings ascending
let arr = new GokuArray(arrayOfStrings)
arr.sortItems()
// ['a', 'b', 'c']

arr = new GokuArray(arrayOfStrings)
arr.sortItems({ ordering: 'ASC' })
// ['a', 'b', 'c']


// array of strings descending
let arr = new GokuArray(arrayOfStrings)
arr.sortItems({ ordering: 'DESC' })
// ['c', 'b', 'a']

// Objects
// array of objects ascending by field type of number
let arr = new GokuArray(arrayOfObjects)
arr.sortItems({ field: 'id' })
// [{ id: 1 }, { id: 2 }, { id: 3 }]

arr = new GokuArray(arrayOfObjects)
arr.sortItems({ ordering: 'ASC', field: 'id' })
// [{ id: 1 }, { id: 2 }, { id: 3 }]

// array of objects descending by field type of number
let arr = new GokuArray(arrayOfObjects)
arr.sortItems({ ordering: 'DESC', field: 'id' })
// [{ id: 3 }, { id: 2 }, { id: 1 }]


// array of objects ascending by field type of string
let arr = new GokuArray(arrayOfObjects)
arr.sortItems({ field: 'text' })
// [{ text: 'a' }, { text: 'b' }, { text: 'c' }]

arr = new GokuArray(arrayOfObjects)
arr.sortItems({ ordering: 'ASC', field: 'text' })
// [{ text: 'a' }, { text: 'b' }, { text: 'c' }]

// array of objects descending by field type of strings
let arr = new GokuArray(arrayOfObjects)
arr.sortItems({ ordering: 'DESC', field: 'text' })
// [{ text: 'c' }, { text: 'b' }, { text: 'a' }]

toArray()

const array = [1, 2, 2, 3, 3, 4, 1];

// return plain array
const arr = new GokuArray(array);
arr.toArray() // will return native Array and pass deep equality check for Array

Inheritance

GokuArray inherits all Array built-in methods

const arrayOfPrimitives = [1, 2, 2, 3, 3, 4, 1];

const gokuArr = new GokuArray(arrayOfPrimitives);
console.log(Array.isArray(gokuArr)) // true


const arr = new GokuArray(arrayOfPrimitives);
const newArr = arr.filter(item => item === 1)
console.log(newArray) // [1, 1]

Chain methods

const array = [1, 2, 2, 3, 3, 4, 1];

// chain own methods
const arr = new GokuArray(array);
const uniqueSortedPlainArray = arr.unique().sortItems().toArray() // works

// chain onwn methods and array build-in methods
const arr = new GokuArray(array);
const uniqueSortedPlainArray = arr.unique().map(i => i).filter(i => !!i).sortItems().sort().toArray().sort() // works

Preserve built in methods

Default

const arr = new GokuArray(array);
// .map(), .filter(), .sort(), etc. => GokuArray
// .asyncMap(), .asyncFilter, .unique(), .missingFrom() => GokuArray
// .diff() => Object
// .toArray() => Array
arr.unique().map(i => i).sortItems() // works
arr.unique().map(i => i) // works
arr.unique().toArray().map(i => i) // works
arr.unique().toArray().unique(i => i) // does not work

Preserving built-in methods

const arr = new GokuArray(array, true);
// .map(), .filter(), .sort(), etc. => Array
// .asyncMap(), .asyncFilter, .unique(), .missingFrom() => GokuArray
// .diff() => Object
// .toArray() => Array
arr.unique().map(i => i).sortItems() // does not work
arr.unique().map(i => i) // works
arr.unique().toArray().map(i => i) // works
arr.unique().toArray().unique(i => i) // does not work

Thanks To