Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
is-equally-spaced
Advanced tools
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.
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).
npm install --save is-equally-spaced
yarn add is-equally-spaced
This package is written in TypeScript and is designed to be compatible with NodeJS 6+. 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;
import isEquallySpaced, { EquallySpacedResult, IsEquallySpaced } from 'is-equally-spaced';
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);
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:
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.
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%.
FAQs
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 npm package is-equally-spaced receives a total of 2 weekly downloads. As such, is-equally-spaced popularity was classified as not popular.
We found that is-equally-spaced demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.