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

orb-array

v1.4.0

Published

Concise Array Programming

Downloads

5

Readme

orb-array

orb-array exposes concise APIs to manipulate arrays.

Installation

Browser Installation. The module is exported as orbarr global variable.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script>

Node Installation

npm install orb-array

APIs

split

It splits an array into the specified number of pieces. When the number of pieces is larger than the input size, it creates empty pieces. It always returns the specified number of pieces. Some examples:

// Midway split is the default behavior.
const items = [1, 2, 3, 4, 5]
const pieces = split(items)
// Output: [[1, 2, 3], [4, 5]]
const items = [1, 2, 3, 4, 5]
const pieces = split(items, 10)
// Output: [[1], [2], [3], [4], [5], [], [], [], [], []]

range

It generates numbers in a given range, starting with 0.

const items = range(5)
// Output: [0, 1, 2, 3, 4]

fill

It generates a range of values using a function.

const items = fill(5, v => v*2) // v is an item index
// Output: [0, 2, 4, 6, 8]

zip

It zips arrays together. When the array sizes vary, the output size is equal to the shortest array.

const items = range(5)
const values = range(10)
const zipped = zip(items, values)
// Output: [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]

reduce

reduce support several operations.

reduce.o reduces an array to an object. It supports customizations using the key and the value functions. Without customizations, key and value are the input array items.

const items = range(5)
const o = reduce.o(items)
// Output: {0:0, 1:1, 2:2, 3:3, 4:4}
const items = range(5)
const o = reduce.o(items, {value: v => v + 2})
// Output: {0:2, 1:3, 2:4, 3:5, 4:6}

reduce.a transforms an array. A value function is applied to the original values. A custom container can be used to store the results. Some examples:

// Simple Example
const items = range(5)
const o = reduce.a(items)
// Output: [0, 1, 2, 3, 4]
// Using value function and a container
const items = [1, 2, 5, 6]
const vfn = v => 2*v
const container = [20]
const o = reduce.a(items, {value: vfn, container})
// Output: [20, 2, 4, 10, 60]

reduce.mul multiplies together all the elements of an array. When the input contains a non-numerical value, the output is NaN. The boolean values are converted to their numerical form (0 or 1).

const items = [1, 2, 5, 6]
const result = reduce.mul(items)
// Output: 60
// It returns 1 for an empty input.
const result = reduce.mul([])
// Output: 1

reduce.rollingmul performs cumulative multiplication. Follow the following examples:

// Rolling Multiplication
const items = [1, 2, 5, 6]
const o = reduce.rollingmul(items)
// Output: [1, 2, 10, 60]

reduce.sum adds the elements together.

// Addition
const items = [1, 2, 3, 4, 6]
const o = reduce.sum(items)
// Output: 16

map

map supports several operations.

map.scale uses the input factor to scale elements.

const items = range(5)
const scaled = map.scale(items, 2)
// Output: [0, 2, 4, 6, 8]

last

last gets the last element from an array without modification.

// Action
const items = ['last-input']
const o = last(items)
// Output: 'last-input'

ranges

ranges simplifies nested iterations over the deep arrays. It creates a list of addresses for individual elements. resolveAddress API resolves them. Follow the following examples:

// Ranges Demonstration
const o = ranges(2, 3)
// Output: [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
// Access Elements
const deepfruits = [['mango'], ['apple']]
const o = ranges(2, 2)
const fruits = o.map((address) => resolveAddress(deepfruits, address)
// Output: ['mango', 'apple']

resolveAddress

resolveAddress resolves the ordered indices in a nested array or the ordered keys in a nested object. The order must follow the parent-child relationship.

// Array Example
const deepfruits = [['mango'], ['apple']]
const o = resolveAddress(deepfruits, [1, 0])
// Output: 'apple'
// Object Example
const fruits = {
  tropical: {
    summer: ['mango', 'orange']
  },
  wild: {
    names: ['berries']
  }
}
const o = resolveAddress(fruits, ['wild', 'names'])
// Output: ['berries]

repeat

repeat repeats an object. The repeat count is configurable.

// Basic Example
const input = {tree: 'tonmayi'}
const o = repeat(input, 2)
// Output: [{tree: 'tonmayi'}, {tree: 'tonmayi'}, {tree: 'tonmayi'}]