New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

xiter

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xiter

Extended iterables such as `Set`.

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
3
50%
Maintainers
1
Weekly downloads
 
Created
Source

xiter - Extended iterables

Build Status Coverage Status npm version Downloads

Extensions of ES6 iterables such as Set.

Problem

Many iterables (such as Set, for instance) lack methods supporting common iterator operations (such as map, filter, every, etc.).

Array is the only iterable that currently provides all these methods. Consequently, in order to perform one of these operations on a non-Array iterable, one must convert the iterable into an Array, perform the operation, and convert it back to the original type of iterable.

For example, suppose you had the following Set and mapper function:

const original: Set<string> = ...;
function mapper(s: string): number { ... }

Ideally, you would be able to write:

const mapped = original.map(mapper);

...but in reality, you have to write:

const mapped = new Set(Array.from(original).map(mapper));

Notice how much more readable the former is. If your codebase frequently performs these iterator operations, readability improvements like these will quickly add up.

Unfortunately, the former currently throws an error, because the native Set does not provide a map method.

Solution

xiter provides thorougly tested and documented extensions of native iterables that provide these methods.

These extensions are fully backwards-compatible, allowing you to confidently replace any native iterables with their extended counterparts without fear of breaking your codebase.

Usage

npm install --save xiter
import { XSet } from "xiter";

const a = new XSet(["x", "y", "z"]);
const b = new XSet([8, "z", false]);

function printSet(s: Set<any>): void {
  console.log(Array.from(s));
}

// XSets behave like native Sets...

console.log(a instanceof Set); // true
console.log(a.has("z"), a.has("w")); // true, false;
console.log(a.delete("z"), a.delete("z")); // true, false
a.add("z");
console.log(a.size); // 3

// ...but have additional methods you would expect all iterables to have (e.g., map)...

printSet(a.map(s => s.toUpperCase())); // ["X", "Y", "Z"]
printSet(b.filter(x => "number" === typeof x)); // [8]
console.log(b.find(x => "number" === typeof x); // 8
console.log(b.some(x => "number" === typeof x); // true
console.log(b.every(x => "number" === typeof x); // false

// See API Docs for the rest of the methods.

API Docs

Docs can be found here.

("Bonus") class-specific methods

As a bonus, some extensions provide additional class-specific methods for your convenience. For example, XSet provides methods implementing common set operations, such as union and isSubsetOf.

These methods, despite being completely unrelated to solving the original problem of providing iterator operations, were included as a part of this library because the author believed there is a reasonable chance that users of the given iterables would want to perform these class-specific operations in addition to the iterator operations.

Roadmap

  • XSet provided as of v1.0.0
  • XMap
  • XWeakSet?
  • XWeakMap?

License

MIT

Copyright (c) 2019 Kyle Lin

Keywords

typescript

FAQs

Package last updated on 04 Dec 2019

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