Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

counting

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

counting

Counting

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Counting

Helper function for consuming iterables with a running count, for those cases where you want a counter variable but don't feel like using a for-loop or callback function.

import { counting } from "counting";

API

counting

function* counting<T>(iter: Iterable<T>): IterableIterator<[number, T]>;

Generates a new iterable from the input that yields an index along with each value from the iterable.

Examples

const array = ["a", "b", "c"];
for (const [i, value] of counting(array)) {
  console.log(`${i}: ${value}`);
  // 0: a
  // 1: b
  // 2: c
}

new Map(counting(array)); // Map { 0 => "a", 1 => "b", 2 => "c" }

const map = new Map([["a", "A"], ["b", "B"], ["c", "C"]]);
for (const [i, [key, value]] of counting(map)) {
  console.log(`${i}: ${key} -> ${value}`);
  // 0: a -> A
  // 1: b -> B
  // 2: c -> C
}
for (const [i, key] of counting(map.keys())) {
  console.log(`${i}: ${key}`);
  // 0: a
  // 1: b
  // 2: c
}
for (const [i, value] of counting(map.values())) {
  console.log(`${i}: ${value}`);
  // 0: A
  // 1: B
  // 2: C
}

LICENSE

The MIT license.

FAQs

Package last updated on 10 Dec 2018

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