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

is-equally-spaced

v0.2.0

Published

IsEquallySpaced is a simple utility function that given an array of numbers, evaluates wether or not every element is equally spaced, i.e. if every subsequent couple of numbers in the array has the same distance.

Downloads

6

Readme

Build Status npm version Downloads

is-equally-spaced

IsEquallySpaced is a simple utility function that given an array of numbers, evaluates wether or not every element is equally spaced, i.e. if every subsequent couple of numbers in the array has the same distance. The best case complexity of this algorithm is O(1) and the worst is O(n).


Installing

  • with npm:
npm install --save is-equally-spaced
  • with yarn
yarn add is-equally-spaced

Typings

This package is written in TypeScript. The following types are exported:

export interface EquallySpacedResult {
  /**
   * It's the distance (approximated to the precision you need) between the first two
   * elements of the array.
   */
  distance: number;
  /**
   * isEqual is true if and only if every subsequent couple of items in the array
   * has the same distance (approximated to the precision you need) from the next one.
   */
  isEqual: boolean;
}
export type IsEquallySpaced = (arr: number[], precision?: number) => EquallySpacedResult;

How to import

import isEquallySpaced, { EquallySpacedResult, IsEquallySpaced } from 'is-equally-spaced';

Usage

Just take a look at the signature of the method:

/**
 * Given an array of numbers, evaluates wether or not every element is equally spaced, i.e.
 * if every subsequent couple of numbers in the array has the same distance.
 * The best case complexity of this algorithm is O(1) and the worst is O(n).
 * @param arr Array of numbers, which can be integer or floats
 * @param precision The number of digits after the decimal points to consider in order
 *                  to evaluate two subsequent distances as equal.
 *                  This is only useful when dealing with floats, and defaults to 8.
 */
const isEquallySpaced: IsEquallySpaced = (arr, precision = 8);

Example

Consider the following array (with indexes in the upper row, and values in the bottom row). It's equally spaced since every subsequent couple, having indexes, (0,1), (1,2), (2,3) and values (0,0.44), (0.44,0.88), (0.88,1.33) has the same distance: 0.44.

| 0 | 1 | 2 | 3 | | ---- | ---- | ---- | ---- | | 0 | 0.44 | 0.88 | 1.33 |

The situation above translates to the following code:

const arr: number[] = [0, 0.44, 0.88, 1.33];
console.log(isEquallySpaced(arr, 2)); // { distance: 0.44, isEqual: true }

Hovever, if we set 0 as precision, the obtained result will be quite different:

const arr: number[] = [0, 0.44, 0.88, 1.33];
console.log(isEquallySpaced(arr, 0)); // { distance: 0, isEqual: true }

This is due to the fact that the distance is evaluated in the following way:

  • isEqual = true;
  • d0,1 = 0.44 - 0 = 0.44; --> approximated to 0
  • distance = d0,1;
  • d1,2 = 0.88 - 0.44 = 0.44; --> approximated to 0, which is == d0,1
  • d2,3 = 1.33 - 0.88 = 0.44; --> approximated to 0, which is == d1,2

which gives { distance: 0, isEqual: true }

Please take a look at the tests to check out every possible nuance and other example of using this package.

Related packages

  • fixed-math: utility function that converts a decimal number using fixed-point notation, without using the expensive Number.toFixed

Contributing

Of course PRs are welcome! Before contributing, however, please be sure to run npm run test:ci or yarn test:ci, in order to check if the code you wrote respects the linting conventions and if it doesn't break any test. Please try to keep the unit test code coverage at 100%.