New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

based-array

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

based-array

A correct and based array implementation. 1-based indexing. obviously.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by133.33%
Maintainers
1
Weekly downloads
 
Created
Source

basedArray 1

A correct and based array implementation. 1-based indexing. obviously.

Inspired by Why you're wrong about 0-based indexing by @tjdevries

Install

npm install --save based-array
yarn add based-array
pnpm add based-array

Use

import { BasedArray } from "based-array";

const funkyReduce = (arr: number[]) => {
  return arr.reduce((accum, item, index) => {
    return accum + item * index;
  }, 0);
};

let array = new BasedArray<number>(5).fill(1);
console.log(array[1]);
// 1
console.log(funkyReduce(array));
// 15

array = BasedArray.from([1, 2, 3, 4, 5]);
console.log(array[1]);
// 1
console.log(funkyReduce(array));
// 55

Strict mode

import { BasedArray } from "based-array";

let array = BasedArray.from([1, 2, 3, 4, 5]);
console.log(array[1]);
// 1
console.log(array[0]);
// undefined
// console.warn: you are counting from zero

BasedArray.strict();

array = BasedArray.from([1, 2, 3, 4, 5]);
console.log(array[1]);
// 1
console.log(array[0]);
// Throws error: CountingFromZeroError

Testing

Every based array method is tested against its vanilla counterpart in a thrilling batte over in tests/vanilla-vs.spec.

The example above is tested in tests/readme-example.spec.

Performance

You can run a simple performance test.

➜  based-array git:(main) ✗ npm run perf

Starting performance test with 10,000 items.

Vanilla: 0.809ms
Based: 0.657ms
Vanilla: 0.524ms
Based: 0.548ms

Finished performance test.

FAQs

Package last updated on 12 May 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc