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

ts-enum-util

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-enum-util - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

2

package.json
{
"name": "ts-enum-util",
"version": "0.0.6",
"version": "0.0.7",
"description": "TypeScript Enum Utilities",

@@ -5,0 +5,0 @@ "repository": {

@@ -15,3 +15,3 @@ [![npm version](https://img.shields.io/npm/v/ts-enum-util.svg)](https://www.npmjs.com/package/ts-enum-util)

- [Installation](#installation)
- [Usage Example](#usage-example)
- [Usage Examples](#usage-examples)
- [Requirements](#requirements)

@@ -51,5 +51,7 @@ - [General Concepts](#general-concepts)

### Usage Example
A quick example of some of `ts-enum-util`'s capabilities.
### Usage Examples
Several small examples `ts-enum-util`'s capabilities to give you a quick overview of what it can do.
Pay special attention to the comments indicating the compile-time type of various results. See [Specific Typing](#specific-typing) for more about data types.
```ts

@@ -59,5 +61,5 @@ import {$enum} from "ts-enum-util";

enum RGB {
R,
G,
B
R = "r",
G = "g",
B = "b"
}

@@ -70,23 +72,27 @@

// type: RGB[]
// value: [0, 1, 2]
// value: ["r", "g", "b"]
const values = $enum(RGB).getValues();
// type: ["R" | "G" | "B", RGB][]
// value: [["R", "r"], ["G", "g"], ["B", "b"]]
const entries = $enum(RGB).getEntries();
// type: "R" | "G" | "B"
// value: "G"
const key = $enum(RGB).getKey(1);
const key = $enum(RGB).getKey("g");
// throws: Error("Unexpected value: 42. Expected one of: 0,1,2")
const key2 = $enum(RGB).getKey(42);
// throws: Error("Unexpected value: blah. Expected one of: r,g,b")
const key2 = $enum(RGB).getKey("blah");
// type: "R" | "G" | "B" | undefined
// value: undefined
const key3 = $enum(RGB).getKeyOrDefault(42);
const key3 = $enum(RGB).getKeyOrDefault("blah");
// type: "R" | "G" | "B"
// value: "R"
const key4 = $enum(RGB).getKeyOrDefault(42, "R");
const key4 = $enum(RGB).getKeyOrDefault("blah", "R");
// type: string
// value: "BLAH!"
const key4 = $enum(RGB).getKeyOrDefault(42, "BLAH!");
const key4 = $enum(RGB).getKeyOrDefault("blah", "BLAH!");

@@ -98,2 +104,56 @@ // A wrapped enum is iterable!

}
declare const str: string;
// returns true if 'str' is a valid key of RGB
if($enum(RGB).isKey(str)) {
// isKey() is a type guard
// type of 'str' in here is ("R" | "G" | "B")
}
// type: "R" | "G" | "B"
// throws error if 'str' is not a valid key for RGB
const key5 = $enum(RGB).asKey(str);
// type: "R" | "G" | "B" | undefined
// value is undefined if 'str' is not a valid key for RGB
const key6 = $enum(RGB).asKeyOrDefault(str);
// type: "R" | "G" | "B"
// value is "G" if 'str' is not a valid key for RGB
const key6 = $enum(RGB).asKeyOrDefault(str, "G");
// returns true if 'str' is a valid value of RGB
if($enum(RGB).isValue(str)) {
// isValue() is a type guard
// type of 'str' in here is RGB
}
// type: RGB
// throws error if 'str' is not a valid value for RGB
const value = $enum(RGB).asValue(str);
// type: RGB | undefined
// value is undefined if 'str' is not a valid value for RGB
const value2 = $enum(RGB).asValueOrDefault(str);
// type: RGB | undefined
// value is RGB.G if 'str' is not a valid value for RGB
const value3 = $enum(RGB).asValueOrDefault(str, RGB.G);
// iterate all entries in the enum
$enum(RGB).forEach((value, key, rgbRef) => {
// type of value is RGB
// type of key is "R" | "G" | "B"
// rgbRef is a reference to RGB, type is typeof RBG
});
// Convert all entries of the enum to an array of mapped values
// value: ["R: r", "G: g", "B: b"]
const mapped = $enum(RGB).map((value, key, rgbRef) => {
// type of value is RGB
// type of key is "R" | "G" | "B"
// rgbRef is a reference to RGB, type is typeof RBG
return `${key}: ${value}`;
});
```

@@ -138,9 +198,9 @@

## Reference
!!! WORK IN PROGRESS / INCOMPLETE !!!
See the source code or the distributed `index.d.ts` file for complete details of method signatures/overloads, detailed method/param documentation, etc.
The following reference is a work in progress.
### Terminology
Throughout this references, the following aliases for types will be used:
Throughout this reference, the following aliases for types will be used:
* `EnumLike`: An enum-like object type. See [Enum-Like Object](#enum-like-object).

@@ -147,0 +207,0 @@ * `KeyType`: The type of the enum's keys. This is usually a string literal union type of the enum's names, but may also simply be `string` if an `EnumWrapper` was created for an object whose possible property names are not known at compile time.

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