šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

typed-set

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

typed-set

Typed Set structure with no dependencies

0.2.0
latest
Source
npm
Version published
Maintainers
1
Created
Source

typed-set

NPM version Build Status

Motivation

Unlike Typescript, type checking will be done at runtime.

Difference from the native Set

An array of initial values cannot be passed during initialization.

Installation

Install with npm:

$ npm install --save typed-set

Usage

TypedSet only works with the following function signatures

f(value: any) => boolean

Notice: if the function returns a non-boolean value, the result of the check will be skipped

const TypedSet = require("typed-set");

class TestClass {}

// you can omit validation
let classicSet = new TypedSet();

// you can pass expected type as string for typeof comparison
let stringTypedSet = new TypedSet("string");

// you can pass expected class for instanceof comparison
let classTypedSet = new TypedSet(TestClass);

// you can pass function with type comparison
let anotherStringTypedSet = new TypedSet((value) => typeof value === "string");

// you can pass function with instanceof comparison
let objectTypedSet = new TypedSet((value) => value instanceof TestClass);

// you can pass function with any conditions
let typedSet = new TypedSet((value) => [1, 2, 3].includes(value));

// then use like a classic Set
typedSet.add(1);
typedSet.add(2);
typedSet.add(3);

// and catch Error when incompatible type added
try {
    typedSet.add("some incompatible type");
} catch (e) {
    console.log(e.message); // "Incompatible type of value" 
}

List of predefined TypedSet classes for primitive types

  • TypedSet.TypedBigIntSet
  • TypedSet.TypedBooleanSet
  • TypedSet.TypedFunctionSet
  • TypedSet.TypedNumberSet
  • TypedSet.TypedStringSet
  • TypedSet.TypedSymbolSet

License

The typed-set package is open-sourced software licensed under the MIT license.

Keywords

set

FAQs

Package last updated on 13 Jul 2023

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