Socket
Book a DemoInstallSign in
Socket

zip-ts

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zip-ts

Python's zip() function, but for TypeScript

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
1
Maintainers
0
Weekly downloads
 
Created
Source

zip-ts

TypeScript Implementation of Python zip() Function

@latest

Installation

npm

npm i zip-ts

pnpm

pnpm i zip-ts

Usage

You can use zip to combine multiple iterables into a single iterable of tuples:

import { zip } from 'zip-ts';

const numbers = [1, 2, 3];
const letters = ['a', 'b', 'c']; // works same as: const letters = 'abc'

for (const [number, letter] of zip(numbers, letters)) {
  console.log(`${number} - ${letter}`);
}
// Output:
// 1 - a
// 2 - b
// 3 - c

Stop iterating when the shortest input iterable is exhausted:

import { zip } from 'zip-ts';

const shortArray = [1, 2];
const longString = 'abcdef';

for (const [num, char] of zip(shortArray, longString)) {
  console.log(`${num} - ${char}`);
}
// Output:
// 1 - a
// 2 - b

Combine iterables of different types:

import { zip } from 'zip-ts';

const numbers = [1, 2, 3];
const mixed = ['a', 2, { key: 'value' }];

for (const [num, item] of zip(numbers, mixed)) {
  console.log(num, item);
}
// Output:
// 1 'a'
// 2 2
// 3 { key: 'value' }

zip can handle nested iterables as well:

import { zip } from 'zip-ts';

const nestedArray = [[1], [2], [3]];
const chars = 'xyz';

for (const [arr, char] of zip(nestedArray, chars)) {
  console.log(arr, char);
}
// Output:
// [1] 'x'
// [2] 'y'
// [3] 'z'

When one or more of the input iterables is empty, the result is also empty:

import { zip } from 'zip-ts';

const emptyArray: any[] = [];
const numbers = [1, 2, 3];

for (const [a, b] of zip(emptyArray, numbers)) {
  console.log(a, b);
}
// Output:
// (no output, as the zipped iterable is empty)

For more, check out the tests directory.

License

MIT

Keywords

zip

FAQs

Package last updated on 14 Jul 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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.