@js-bits/enumerate
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -8,2 +8,5 @@ /* eslint-disable import/no-duplicates */ | ||
/** | ||
* Accepts a string containing a list of keys and returns an object with corresponding enumerated properties | ||
*/ | ||
declare function Enumerate(list: TemplateStringsArray, ...names: unknown[]): EnumType; | ||
@@ -14,7 +17,30 @@ declare function Enumerate(type: Converter, separator?: SeparatorType): (list: TemplateStringsArray) => EnumType; | ||
declare namespace Enumerate { | ||
/** | ||
* Type-safe analog of {@link Enumerate | enumerate()} tag function | ||
*/ | ||
export const ts: EnumConstructor; | ||
/** | ||
* Check if the given object is a enum or not | ||
*/ | ||
export const isEnum: (value: unknown) => boolean; | ||
/** | ||
* Lower casing string converter | ||
*/ | ||
export const LowerCase: Converters.LowerCase; | ||
/** | ||
* Upper casing string converter | ||
*/ | ||
export const UpperCase: Converters.UpperCase; | ||
/** | ||
* Prefixing string converter | ||
*/ | ||
export const Prefix: Converters.Prefix; | ||
/** | ||
* Incremental number converter | ||
*/ | ||
export const Increment: Converters.Increment; | ||
@@ -21,0 +47,0 @@ } |
{ | ||
"name": "@js-bits/enumerate", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Easy to use, Symbol-based enum implementation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# Easy to use, Symbol-based enum implementation | ||
Just list values you need and `enumerate` tag function will create an object with corresponding keys and unique values for your convenience. | ||
Just list keys you need and `enumerate` tag function will create an object with corresponding properties and unique values for your convenience. | ||
@@ -159,3 +159,3 @@ ## Installation | ||
You can also check if a certain object is a enum or not. | ||
You can also check if the given object is a enum or not. | ||
@@ -171,11 +171,11 @@ ```javascript | ||
<img src="./images/intellisense1.png" _width="450"> | ||
<img src="./images/intellisense1.png" width="500"> | ||
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. | ||
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. | ||
Compare | ||
<img src="./images/intellisense2.png" _width="350"> | ||
<img src="./images/intellisense2.png" width="350"> | ||
@@ -186,21 +186,23 @@ versus | ||
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. | ||
For example | ||
```javascript | ||
console.log(enumerate.ts('ZERO ONE TWO', Number)); // Enum { ZERO: 0, ONE: 1, TWO: 2 } | ||
enumerate.ts('ZERO ONE TWO', Number); // Enum { ZERO: 0, ONE: 1, TWO: 2 } | ||
``` | ||
is the same as | ||
does exactly the same as | ||
```javascript | ||
console.log(enumerate(Number)`ZERO ONE TWO`); // Enum { ZERO: 0, ONE: 1, TWO: 2 } | ||
enumerate(Number)`ZERO ONE TWO`; // Enum { ZERO: 0, ONE: 1, TWO: 2 } | ||
``` | ||
but allows TypeScript to recognize the result type. | ||
but it allows TypeScript to recognize the result type. | ||
<img src="./images/intellisense4.png" _width="750"> | ||
<img src="./images/intellisense4.png" width="750"> | ||
The reason of why we cannot use `enumerate` directly 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. | ||
## 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. | ||
- Requires TypeScript 4.8+ for best type safety features support. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
384879
1351
206