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.2.2 to 1.0.0

38

dist/commonjs/index.js

@@ -33,2 +33,5 @@ "use strict";

* Used for reverse key lookups.
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup
* string key for values and using a plain Object:
* {@link https://jsperf.com/polyfill-map-vs-es6-map-vs-object-with-string-key}
*/

@@ -41,2 +44,4 @@ this.keysByValueMap = new Map();

this.valuesList = new Array(length);
// According to multiple tests found on jsperf.com, a plain for loop is faster than using
// Array.prototype.forEach
for (var index = 0; index < length; ++index) {

@@ -193,2 +198,4 @@ var key = this.keysList[index];

var length = this.length;
// According to multiple tests found on jsperf.com, a plain for loop is faster than using
// Array.prototype.forEach
for (var index = 0; index < length; ++index) {

@@ -213,2 +220,3 @@ var entry = this[index];

var result = new Array(length);
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map
for (var index = 0; index < length; ++index) {

@@ -248,2 +256,3 @@ var entry = this[index];

var result = new Array(length);
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map
for (var index = 0; index < length; ++index) {

@@ -348,3 +357,11 @@ var entry = this[index];

EnumWrapper.prototype.getKeyOrThrow = function (value) {
return this.keysByValueMap.get(this.asValueOrThrow(value));
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map
// for successful lookups.
var result = (value != null) ? this.keysByValueMap.get(value) : undefined;
if (result != null) {
return result;
}
else {
throw new Error("Unexpected value: " + value + ". Expected one of: " + this.getValues());
}
};

@@ -362,4 +379,6 @@ /**

EnumWrapper.prototype.getKeyOrDefault = function (value, defaultKey) {
if (this.isValue(value)) {
return this.keysByValueMap.get(value);
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups.
var result = (value != null) ? this.keysByValueMap.get(value) : undefined;
if (result != null) {
return result;
}

@@ -378,2 +397,5 @@ else {

EnumWrapper.prototype.getValueOrThrow = function (key) {
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of
// reverse lookups on numeric enums.
return this.enumObj[this.asKeyOrThrow(key)];

@@ -390,2 +412,5 @@ };

EnumWrapper.prototype.getValueOrDefault = function (key, defaultValue) {
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of
// reverse lookups on numeric enums.
if (this.isKey(key)) {

@@ -402,3 +427,8 @@ // type cast required to work around TypeScript bug:

* Map of enum object -> EnumWrapper instance.
* Used as a cache for {@link EnumWrapper.getCachedInstance}.
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}).
* NOTE: Performance tests show that object key lookups into a Map (even if it's a slow polyfill) are plenty fast
* for this use case of a relatively small number of items in the map, assuming you don't do something stupid
* like lookup a cached instance within a tight loop. It's also an order of magnitude faster than building
* a unique string key for each object and using a fast native Map with the generated string key:
* {@link https://jsperf.com/map-with-object-keys}
*/

@@ -405,0 +435,0 @@ EnumWrapper.instancesCache = new Map();

@@ -31,2 +31,5 @@ /**

* Used for reverse key lookups.
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup
* string key for values and using a plain Object:
* {@link https://jsperf.com/polyfill-map-vs-es6-map-vs-object-with-string-key}
*/

@@ -39,2 +42,4 @@ this.keysByValueMap = new Map();

this.valuesList = new Array(length);
// According to multiple tests found on jsperf.com, a plain for loop is faster than using
// Array.prototype.forEach
for (var index = 0; index < length; ++index) {

@@ -191,2 +196,4 @@ var key = this.keysList[index];

var length = this.length;
// According to multiple tests found on jsperf.com, a plain for loop is faster than using
// Array.prototype.forEach
for (var index = 0; index < length; ++index) {

@@ -211,2 +218,3 @@ var entry = this[index];

var result = new Array(length);
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map
for (var index = 0; index < length; ++index) {

@@ -246,2 +254,3 @@ var entry = this[index];

var result = new Array(length);
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map
for (var index = 0; index < length; ++index) {

@@ -346,3 +355,11 @@ var entry = this[index];

EnumWrapper.prototype.getKeyOrThrow = function (value) {
return this.keysByValueMap.get(this.asValueOrThrow(value));
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map
// for successful lookups.
var result = (value != null) ? this.keysByValueMap.get(value) : undefined;
if (result != null) {
return result;
}
else {
throw new Error("Unexpected value: " + value + ". Expected one of: " + this.getValues());
}
};

@@ -360,4 +377,6 @@ /**

EnumWrapper.prototype.getKeyOrDefault = function (value, defaultKey) {
if (this.isValue(value)) {
return this.keysByValueMap.get(value);
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups.
var result = (value != null) ? this.keysByValueMap.get(value) : undefined;
if (result != null) {
return result;
}

@@ -376,2 +395,5 @@ else {

EnumWrapper.prototype.getValueOrThrow = function (key) {
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of
// reverse lookups on numeric enums.
return this.enumObj[this.asKeyOrThrow(key)];

@@ -388,2 +410,5 @@ };

EnumWrapper.prototype.getValueOrDefault = function (key, defaultValue) {
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of
// reverse lookups on numeric enums.
if (this.isKey(key)) {

@@ -400,3 +425,8 @@ // type cast required to work around TypeScript bug:

* Map of enum object -> EnumWrapper instance.
* Used as a cache for {@link EnumWrapper.getCachedInstance}.
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}).
* NOTE: Performance tests show that object key lookups into a Map (even if it's a slow polyfill) are plenty fast
* for this use case of a relatively small number of items in the map, assuming you don't do something stupid
* like lookup a cached instance within a tight loop. It's also an order of magnitude faster than building
* a unique string key for each object and using a fast native Map with the generated string key:
* {@link https://jsperf.com/map-with-object-keys}
*/

@@ -403,0 +433,0 @@ EnumWrapper.instancesCache = new Map();

@@ -28,3 +28,8 @@ /**

* Map of enum object -> EnumWrapper instance.
* Used as a cache for {@link EnumWrapper.getCachedInstance}.
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}).
* NOTE: Performance tests show that object key lookups into a Map (even if it's a slow polyfill) are plenty fast
* for this use case of a relatively small number of items in the map, assuming you don't do something stupid
* like lookup a cached instance within a tight loop. It's also an order of magnitude faster than building
* a unique string key for each object and using a fast native Map with the generated string key:
* {@link https://jsperf.com/map-with-object-keys}
*/

@@ -43,2 +48,5 @@ private static readonly instancesCache;

* Used for reverse key lookups.
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup
* string key for values and using a plain Object:
* {@link https://jsperf.com/polyfill-map-vs-es6-map-vs-object-with-string-key}
*/

@@ -45,0 +53,0 @@ private readonly keysByValueMap;

2

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

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

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

### Lookup value by key
See also:
- [Wrapped enums are Map-Like](#wrapped-enums-are-map-like)
```ts

@@ -170,0 +168,0 @@ // type: RGB

@@ -31,3 +31,8 @@ /**

* Map of enum object -> EnumWrapper instance.
* Used as a cache for {@link EnumWrapper.getCachedInstance}.
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}).
* NOTE: Performance tests show that object key lookups into a Map (even if it's a slow polyfill) are plenty fast
* for this use case of a relatively small number of items in the map, assuming you don't do something stupid
* like lookup a cached instance within a tight loop. It's also an order of magnitude faster than building
* a unique string key for each object and using a fast native Map with the generated string key:
* {@link https://jsperf.com/map-with-object-keys}
*/

@@ -49,2 +54,5 @@ private static readonly instancesCache = new Map<object, EnumWrapper>();

* Used for reverse key lookups.
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup
* string key for values and using a plain Object:
* {@link https://jsperf.com/polyfill-map-vs-es6-map-vs-object-with-string-key}
*/

@@ -215,2 +223,4 @@ private readonly keysByValueMap = new Map<V, keyof T>();

// According to multiple tests found on jsperf.com, a plain for loop is faster than using
// Array.prototype.forEach
for (let index = 0; index < length; ++index) {

@@ -349,2 +359,4 @@ const key = this.keysList[index];

// According to multiple tests found on jsperf.com, a plain for loop is faster than using
// Array.prototype.forEach
for (let index = 0; index < length; ++index) {

@@ -371,2 +383,3 @@ const entry = this[index];

// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map
for (let index = 0; index < length; ++index) {

@@ -411,2 +424,3 @@ const entry = this[index];

// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map
for (let index = 0; index < length; ++index) {

@@ -587,3 +601,11 @@ const entry = this[index];

public getKeyOrThrow(value: V | null | undefined): keyof T {
return this.keysByValueMap.get(this.asValueOrThrow(value));
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map
// for successful lookups.
const result = (value != null) ? this.keysByValueMap.get(value) : undefined;
if (result != null) {
return result;
} else {
throw new Error(`Unexpected value: ${value}. Expected one of: ${this.getValues()}`);
}
}

@@ -646,4 +668,7 @@

public getKeyOrDefault(value: V | null | undefined, defaultKey?: keyof T | string): string | undefined {
if (this.isValue(value)) {
return this.keysByValueMap.get(value);
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups.
const result = (value != null) ? this.keysByValueMap.get(value) : undefined;
if (result != null) {
return result;
} else {

@@ -662,2 +687,5 @@ return defaultKey;

public getValueOrThrow(key: string | null | undefined): T[keyof T] {
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of
// reverse lookups on numeric enums.
return this.enumObj[this.asKeyOrThrow(key)];

@@ -711,2 +739,5 @@ }

public getValueOrDefault(key: string | null | undefined, defaultValue?: T[keyof T] | V): V | undefined {
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of
// reverse lookups on numeric enums.
if (this.isKey(key)) {

@@ -713,0 +744,0 @@ // type cast required to work around TypeScript bug:

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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