Comparing version 2.1.1 to 2.2.0-beta.0
@@ -17,12 +17,12 @@ <!-- markdownlint-disable MD009 MD024 --> | ||
> **Note:** this version has a typing issue, please use `v2.1.1` | ||
### Features | ||
- 🔥 Adds `toSelect` method, deprecated ~~`options`~~ method | ||
- 🔥 Adds `toMenu` method, deprecated ~~`menus`~~ method | ||
- 🔥 Adds `toFilter` method, deprecated ~~`filters`~~ method | ||
- 🔥 Adds `toValueMap` method, ~~`valuesEnum`~~ method | ||
- 🔥 Support global extending, you can add custom methods to Enums | ||
- 🔥 Adds `toSelect` method, deprecated _`options`_ method | ||
- 🔥 Adds `toMenu` method, deprecated _`menus`_ method | ||
- 🔥 Adds `toFilter` method, deprecated _`filters`_ method | ||
- 🔥 Adds `toValueMap` method, _`valuesEnum`_ method | ||
- 🔥 Support global extension, custom methods can be added to Enums | ||
> **Note:** this version has typing problem, please use `v2.1.1` | ||
## 2.0.3 | ||
@@ -29,0 +29,0 @@ |
@@ -20,3 +20,4 @@ import { EnumItemClass } from './enum-item'; | ||
super(); | ||
const keys = Object.keys(init); | ||
// exclude number keys with a "reverse mapping" value, it means those "reverse mapping" keys of number enums | ||
const keys = Object.keys(init).filter((k) => { var _a; return !(/^-?\d+$/.test(k) && k === `${(_a = init[init[k]]) !== null && _a !== void 0 ? _a : ''}`); }); | ||
const parsed = keys.map((key) => parseEnumItem(init[key], key, { typeInit: init, keys })); | ||
@@ -28,2 +29,3 @@ keys.forEach((key, index) => { | ||
}); | ||
Object.freeze(keys); | ||
// @ts-expect-error: because use KEYS to avoid naming conflicts in case of 'keys' field name is taken | ||
@@ -151,45 +153,7 @@ this[Object.keys(init).some((k) => k === 'keys') ? KEYS : 'keys'] = keys; | ||
else { | ||
return inferFromNull(key, options); | ||
} | ||
return { value, label }; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function inferFromNull(key, options) { | ||
const { typeInit, keys } = options; | ||
let value; | ||
const label = key; | ||
// If the value is empty, first check the number incrementing enumeration, otherwise use the key as the value | ||
const index = keys.indexOf(key); | ||
const prev = typeInit[keys[index - 1]]; | ||
// Only pure number and empty enumeration will be incremented | ||
if (keys.some((k) => typeInit[k] != null && typeof typeInit[k] !== 'number')) { | ||
value = key; | ||
label = key; | ||
} | ||
else if (index === 0) { | ||
value = 0; | ||
} | ||
else if (typeof prev === 'number') { | ||
value = (prev + 1); | ||
} | ||
else { | ||
// only nulls | ||
let seed = 0; | ||
let count = 0; | ||
// find seed | ||
for (let i = index - 1; i >= 0; i--) { | ||
const val = typeInit[keys[i]]; | ||
count++; | ||
if (typeof val === 'number') { | ||
seed = val; | ||
break; | ||
} | ||
else { | ||
// only nulls | ||
continue; | ||
} | ||
} | ||
value = (seed + count); | ||
} | ||
return { value, label }; | ||
} | ||
//# sourceMappingURL=enum-collection.js.map |
@@ -304,5 +304,6 @@ import type { EnumItemClass } from './enum-item'; | ||
} | ||
export type EnumInit<K extends keyof any = string, V extends EnumValue = EnumValue> = NumberEnumInit<K> | StringEnumInit<K> | StandardEnumInit<K, V> | ValueOnlyEnumInit<K, V> | LabelOnlyEnumInit<K> | CompactEnumInit<K> | AutoIncrementedEnumInit<K>; | ||
export type EnumInit<K extends keyof any = string, V extends EnumValue = EnumValue> = NumberEnumInit<K> | StringEnumInit<K> | StringNumberEnumInit<K> | StandardEnumInit<K, V> | ValueOnlyEnumInit<K, V> | LabelOnlyEnumInit<K> | CompactEnumInit<K> | AutoIncrementedEnumInit<K>; | ||
export type NumberEnumInit<K extends keyof any> = Record<K, number>; | ||
export type StringEnumInit<K extends keyof any> = Record<K, string>; | ||
export type StringNumberEnumInit<K extends keyof any> = Record<K, string | number>; | ||
export type StandardEnumInit<K extends keyof any, V extends EnumValue> = Record<K, StandardEnumItemInit<V>>; | ||
@@ -309,0 +310,0 @@ export type ValueOnlyEnumInit<K extends keyof any, V extends EnumValue> = Record<K, ValueOnlyEnumItemInit<V>>; |
@@ -24,3 +24,4 @@ "use strict"; | ||
super(); | ||
const keys = Object.keys(init); | ||
// exclude number keys with a "reverse mapping" value, it means those "reverse mapping" keys of number enums | ||
const keys = Object.keys(init).filter((k) => { var _a; return !(/^-?\d+$/.test(k) && k === `${(_a = init[init[k]]) !== null && _a !== void 0 ? _a : ''}`); }); | ||
const parsed = keys.map((key) => parseEnumItem(init[key], key, { typeInit: init, keys })); | ||
@@ -32,2 +33,3 @@ keys.forEach((key, index) => { | ||
}); | ||
Object.freeze(keys); | ||
// @ts-expect-error: because use KEYS to avoid naming conflicts in case of 'keys' field name is taken | ||
@@ -156,45 +158,7 @@ this[Object.keys(init).some((k) => k === 'keys') ? index_1.KEYS : 'keys'] = keys; | ||
else { | ||
return inferFromNull(key, options); | ||
} | ||
return { value, label }; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function inferFromNull(key, options) { | ||
const { typeInit, keys } = options; | ||
let value; | ||
const label = key; | ||
// If the value is empty, first check the number incrementing enumeration, otherwise use the key as the value | ||
const index = keys.indexOf(key); | ||
const prev = typeInit[keys[index - 1]]; | ||
// Only pure number and empty enumeration will be incremented | ||
if (keys.some((k) => typeInit[k] != null && typeof typeInit[k] !== 'number')) { | ||
value = key; | ||
label = key; | ||
} | ||
else if (index === 0) { | ||
value = 0; | ||
} | ||
else if (typeof prev === 'number') { | ||
value = (prev + 1); | ||
} | ||
else { | ||
// only nulls | ||
let seed = 0; | ||
let count = 0; | ||
// find seed | ||
for (let i = index - 1; i >= 0; i--) { | ||
const val = typeInit[keys[i]]; | ||
count++; | ||
if (typeof val === 'number') { | ||
seed = val; | ||
break; | ||
} | ||
else { | ||
// only nulls | ||
continue; | ||
} | ||
} | ||
value = (seed + count); | ||
} | ||
return { value, label }; | ||
} | ||
//# sourceMappingURL=enum-collection.js.map |
@@ -304,5 +304,6 @@ import type { EnumItemClass } from './enum-item'; | ||
} | ||
export type EnumInit<K extends keyof any = string, V extends EnumValue = EnumValue> = NumberEnumInit<K> | StringEnumInit<K> | StandardEnumInit<K, V> | ValueOnlyEnumInit<K, V> | LabelOnlyEnumInit<K> | CompactEnumInit<K> | AutoIncrementedEnumInit<K>; | ||
export type EnumInit<K extends keyof any = string, V extends EnumValue = EnumValue> = NumberEnumInit<K> | StringEnumInit<K> | StringNumberEnumInit<K> | StandardEnumInit<K, V> | ValueOnlyEnumInit<K, V> | LabelOnlyEnumInit<K> | CompactEnumInit<K> | AutoIncrementedEnumInit<K>; | ||
export type NumberEnumInit<K extends keyof any> = Record<K, number>; | ||
export type StringEnumInit<K extends keyof any> = Record<K, string>; | ||
export type StringNumberEnumInit<K extends keyof any> = Record<K, string | number>; | ||
export type StandardEnumInit<K extends keyof any, V extends EnumValue> = Record<K, StandardEnumItemInit<V>>; | ||
@@ -309,0 +310,0 @@ export type ValueOnlyEnumInit<K extends keyof any, V extends EnumValue> = Record<K, ValueOnlyEnumItemInit<V>>; |
{ | ||
"name": "enum-plus", | ||
"version": "2.1.1", | ||
"version": "2.2.0-beta.0", | ||
"description": "A drop-in replacement library for enum, tiny and powerful, like native enum but much more than that", | ||
"keywords": [ | ||
"enum", | ||
"typescript-library" | ||
"enumeration", | ||
"typescript", | ||
"localization", | ||
"ssr", | ||
"tool" | ||
], | ||
@@ -9,0 +13,0 @@ "homepage": "https://github.com/shijistar/enum-plus", |
@@ -9,3 +9,3 @@ <!-- markdownlint-disable MD009 --> | ||
[![npm version](https://img.shields.io/npm/v/enum-plus.svg)](https://www.npmjs.com/package/enum-plus) | ||
[![npm version](https://img.shields.io/npm/v/enum-plus.svg?color=red)](https://www.npmjs.com/package/enum-plus) | ||
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/enum-plus)](https://bundlephobia.com/result?p=enum-plus) | ||
@@ -121,4 +121,2 @@ [![npm downloads](https://img.shields.io/npm/dm/enum-plus.svg)](https://www.npmjs.com/package/enum-plus) | ||
````js | ||
Sometimes we need to create an enum dynamically using data returned by an api, in this case, we can use an array to initialize the enum | ||
@@ -142,4 +140,25 @@ | ||
// { value: 3, label: 'Rabbit', key: 'rabbit' } ] | ||
```` | ||
``` | ||
#### Example 6: initialized from native enum (extend native enum with additional methods) | ||
```ts | ||
import { Enum } from 'enum-plus'; | ||
enum init { | ||
Sunday = 0, | ||
Monday, | ||
Tuesday, | ||
Wednesday, | ||
Thursday, | ||
Friday, | ||
Saturday, | ||
} | ||
const Week = Enum(init); | ||
Week.Sunday; // 0 | ||
Week.Monday; // 1 | ||
Week.Saturday; // 6 | ||
Week.label('Sunday'); // Sunday | ||
``` | ||
## API | ||
@@ -758,4 +777,4 @@ | ||
Enum.extend({ | ||
isWeekend() { | ||
return this.value === 0 || this.value === 6; | ||
getLabels(this: ReturnType<typeof Enum>) { | ||
return this.values.map((item) => item.label); | ||
}, | ||
@@ -766,2 +785,4 @@ reversedValues(this: ReturnType<typeof Enum>) { | ||
}); | ||
Week.getLabels(); // ['Sunday', 'Monday'] | ||
``` | ||
@@ -779,3 +800,3 @@ | ||
export interface EnumExtension<T, K, V> { | ||
isWeekend: (value: number) => boolean; | ||
getLabels: () => string[]; | ||
reversedValues: () => EnumItemClass<EnumItemInit<V>, K, V>[]; | ||
@@ -794,2 +815,2 @@ } | ||
If you want to provide more friendly type hints in the extension methods, you may need to use these type parameters. These are all optional, if your extension method is as simple as `isWeekend`, you can completely ignore them | ||
If you want to provide more friendly type hints in the extension methods, you may need to use these type parameters. These are all optional, if your extension method is as simple as `getLabels`, you can completely ignore them |
@@ -1,2 +0,2 @@ | ||
<!-- markdownlint-disable MD009 --> | ||
<!-- markdownlint-disable MD009 MD001 --> | ||
@@ -9,3 +9,3 @@ # enum-plus | ||
[![npm version](https://img.shields.io/npm/v/enum-plus.svg)](https://www.npmjs.com/package/enum-plus) | ||
[![npm version](https://img.shields.io/npm/v/enum-plus.svg?color=red)](https://www.npmjs.com/package/enum-plus) | ||
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/enum-plus)](https://bundlephobia.com/result?p=enum-plus) | ||
@@ -141,2 +141,23 @@ [![npm downloads](https://img.shields.io/npm/dm/enum-plus.svg)](https://www.npmjs.com/package/enum-plus) | ||
#### 示例 6:支持原生枚举初始化,相当于给原生枚举添加一些扩展方法 | ||
```ts | ||
import { Enum } from 'enum-plus'; | ||
enum init { | ||
Sunday = 0, | ||
Monday, | ||
Tuesday, | ||
Wednesday, | ||
Thursday, | ||
Friday, | ||
Saturday, | ||
} | ||
const Week = Enum(init); | ||
Week.Sunday; // 0 | ||
Week.Monday; // 1 | ||
Week.Saturday; // 6 | ||
Week.label('Sunday'); // Sunday | ||
``` | ||
## API | ||
@@ -248,3 +269,3 @@ | ||
生成一个 filters 数组,可以直接传递给 [Ant Design](https://ant-design.antgroup.com/components/table-cn#table-demo-head) Table 组件的列配置,在表头中显示一个下拉筛选框,用来过滤表格数据 | ||
生成一个对象数组,可以直接传递给 [Ant Design](https://ant-design.antgroup.com/components/table-cn#table-demo-head) Table 组件的列配置,在表头中显示一个下拉筛选框,用来过滤表格数据 | ||
@@ -756,4 +777,4 @@ 数据格式为: | ||
Enum.extend({ | ||
isWeekend() { | ||
return this.value === 0 || this.value === 6; | ||
getLabels(this: ReturnType<typeof Enum>) { | ||
return this.values.map((item) => item.label); | ||
}, | ||
@@ -764,2 +785,4 @@ reversedValues(this: ReturnType<typeof Enum>) { | ||
}); | ||
Week.getLabels(); // ['星期日', '星期一'] | ||
``` | ||
@@ -777,3 +800,3 @@ | ||
export interface EnumExtension<T, K, V> { | ||
isWeekend: (value: number) => boolean; | ||
getLabels: () => string[]; | ||
reversedValues: () => EnumItemClass<EnumItemInit<V>, K, V>[]; | ||
@@ -792,2 +815,2 @@ } | ||
如果你希望在扩展方法中提供更友好的类型提示,你或许可能需要使用到这些类型参数。这些都是可选的,如果你的扩展方法像`isWeekend`这样简单,那么你完全可以忽略它们 | ||
如果你希望在扩展方法中提供更友好的类型提示,你或许可能需要使用到这些类型参数。这些都是可选的,如果你的扩展方法像`getLabels`这样简单,那么你完全可以忽略它们 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
187802
37
0
810
2257
1
1