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

@js-bits/enumerate

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@js-bits/enumerate - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

images/intellisense1.png

14

index.d.ts

@@ -6,13 +6,7 @@ /* eslint-disable import/no-duplicates */

type EnumKeyType = string | symbol;
type EnumValueType = string | symbol | number;
type EnumType = { [key: string]: string | symbol | number } & { [key: symbol]: boolean };
declare function Enumerate(list: TemplateStringsArray, ...names: unknown[]): { [key: EnumKeyType]: EnumValueType };
declare function Enumerate(
type: Converter,
separator?: SeparatorType
): (list: TemplateStringsArray) => { [key: EnumKeyType]: EnumValueType };
declare function Enumerate(
separator?: SeparatorType
): (list: TemplateStringsArray) => { [key: EnumKeyType]: EnumValueType };
declare function Enumerate(list: TemplateStringsArray, ...names: unknown[]): EnumType;
declare function Enumerate(type: Converter, separator?: SeparatorType): (list: TemplateStringsArray) => EnumType;
declare function Enumerate(separator?: SeparatorType): (list: TemplateStringsArray) => EnumType;

@@ -19,0 +13,0 @@ declare namespace Enumerate {

/* eslint-disable max-classes-per-file */
// @ts-nocheck
/**

@@ -62,3 +63,2 @@ * @type {Map<Function, Function>}

let converter;
// @ts-ignore
const valueConverter = CONVERTERS.get(enumType);

@@ -76,3 +76,2 @@ if (valueConverter) {

/** @type {object} */
// @ts-ignore
const result = Array.from(values).reduce(converter, this);

@@ -120,3 +119,2 @@

// @ts-ignore
const enumerate = (...args) => {

@@ -138,3 +136,2 @@ if (args.length > 3 || (Array.isArray(args[0]) && (args[0].length > 1 || typeof args[0][0] !== 'string'))) {

// @ts-ignore
return (...rest) => enumerate(...rest, type, separator);

@@ -148,3 +145,5 @@ }

// @ts-ignore
/**
* @type {import('./types/types').EnumConstructor}
*/
enumerate.ts = (list, ...args) => enumerate([list], ...args);

@@ -151,0 +150,0 @@

@@ -295,2 +295,3 @@ import enumerate from './index.js';

test('should have generated values', () => {
// @ts-ignore
const enumerateTens = enumerate((acc, item) => {

@@ -311,2 +312,3 @@ acc[item] = Object.keys(acc).length * 10;

test('should have generated values', () => {
// @ts-ignore
const enumerateUpperCase = enumerate((acc, item) => {

@@ -313,0 +315,0 @@ acc[item.toUpperCase()] = item;

{
"name": "@js-bits/enumerate",
"version": "1.0.3",
"version": "1.0.4",
"description": "Easy to use, Symbol-based enum implementation",

@@ -43,3 +43,3 @@ "keywords": [

"dependencies": {
"@js-bits/typedef-utils": "^1.0.0"
"@js-bits/typedef-utils": "^1.0.3"
},

@@ -54,3 +54,4 @@ "devDependencies": {

"rimraf": "^5.0.1",
"rollup": "^3.23.0"
"rollup": "^3.23.0",
"typescript": "^4.8.4"
},

@@ -57,0 +58,0 @@ "engines": {

@@ -166,4 +166,39 @@ # Easy to use, Symbol-based enum implementation

## Type-safety and IntelliSense (code completion)
The package includes a TypeScript Declaration File and supports VS Code IntelliSense features.
<img src="./images/intellisense1.png" _width="450">
But there is one caveat. In order to achieve full type safety you have to use a bit different syntax. Unfortunately.
The reason is that there is a long-standing TypeScript [issue](https://github.com/microsoft/TypeScript/issues/33304) with [TemplateStringArray](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_es5_d_.templatestringsarray.html) being incorrectly typed and, as result, not being able to be parameterized.
Compare
<img src="./images/intellisense2.png" _width="350">
versus
<img src="./images/intellisense3.png" _width="750">
So, instead of using `enumerate` directly as a [tag function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) you can use `enumerate.ts` function.
```javascript
console.log(enumerate.ts('ZERO ONE TWO', Number)); // Enum { ZERO: 0, ONE: 1, TWO: 2 }
```
is the same as
```javascript
console.log(enumerate(Number)`ZERO ONE TWO`); // Enum { ZERO: 0, ONE: 1, TWO: 2 }
```
but allows TypeScript to recognize the result type.
<img src="./images/intellisense4.png" _width="750">
## Notes
- Be careful adding new items to an existing numeric enum. Always append them to the end of the list to avoid changing previous item values.
- Requires TypeScript 4.8+ for most type safety features support.

@@ -9,2 +9,3 @@ {

"isolatedModules": true,
"noImplicitAny": true,
"types": ["jest"],

@@ -11,0 +12,0 @@ "typeRoots": ["./node_modules/@types"],

@@ -281,3 +281,4 @@ import enumerate from '../index.js';

test('should have generated values', () => {
const Enum = enumerate.ts('CODE1 CODE2 CODE3', (/** @type {object} */ acc, /** @type {string} */ item) => {
// @ts-ignore
const Enum = enumerate.ts('CODE1 CODE2 CODE3', (acc, item) => {
acc[item] = Object.keys(acc).length * 10;

@@ -296,2 +297,3 @@ return acc;

test('should have generated values', () => {
// @ts-ignore
const enumerateUpperCase = enumerate((acc, item) => {

@@ -298,0 +300,0 @@ acc[item.toUpperCase()] = item;

@@ -31,2 +31,2 @@ /* eslint-disable @typescript-eslint/no-unused-vars */

separator?: RegExp | string
) => EnumType<Options, Type>;
) => EnumType<Options, Type> & { [key: symbol]: boolean };

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