Skip to content

Commit c8c6af8

Browse files
committed
added getTupleValuesByProperty
1 parent a2359f7 commit c8c6af8

17 files changed

Lines changed: 116 additions & 259 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
### [0.0.5] - 2025-06-09
9+
10+
### Added
11+
- Added getTupleValuesByProperty
12+
13+
### Fixed
14+
- KeyName conflict will show the error on the right line
15+
816
## [0.0.4] - 2025-06-06
917

1018
### Added

dist/butterKeyedEnum.d.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@
4646
* @throws {Error} If the `tupleFactory` does not return a tuple that includes all keys, a compile-time type error will occur.
4747
*/
4848
export declare function ButterKeyedEnum<KeyName extends string = "key", const T extends {
49-
[K in keyof T]: KeyName extends keyof T[K] ? never : Record<string, any>;
49+
[K in keyof T]: KeyName extends keyof T[K] ? {
50+
[K2 in keyof T[K]]: K2 extends KeyName ? {
51+
error: "You must not include the keyName in the inner objects";
52+
value: never;
53+
} : any;
54+
} & Record<string, any> : Record<string, any>;
5055
} = {
5156
[key: string]: any;
5257
}, TTuple extends [T[keyof T], ...T[keyof T][]] = [T[keyof T], ...T[keyof T][]], TResult extends [T[keyof T], ...T[keyof T][]] = TTuple>(enumObject: T, options: {
@@ -83,7 +88,34 @@ export declare function ButterKeyedEnum<KeyName extends string = "key", const T
8388
* @type {TTuple} The tuple of enum values in the order defined by tupleFactory
8489
*/
8590
readonly tuple: TResult;
86-
readonly mapTuple: <U>(fn: (value: TResult[number], index: number, array: TResult) => U) => { [K in keyof TResult]: U; };
91+
/**
92+
* Maps a property of the tuple to an array of values
93+
*
94+
* @example
95+
* ```typescript
96+
* const colorsEnum = ButterKeyedEnum({
97+
* green: {
98+
* emoji: '🟩',
99+
* hex: '#00FF00',
100+
* },
101+
* red: {
102+
* emoji: '🟥',
103+
* hex: '#FF0000',
104+
* },
105+
* }, {
106+
* tupleFactory: (enumObject) => [
107+
* enumObject.green,
108+
* enumObject.red,
109+
* ]
110+
* })
111+
*
112+
* colorsEnum.getTupleValuesByProperty('emoji') // ['🟩', '🟥']
113+
* ```
114+
*
115+
* @param property The property to map
116+
* @returns An array of values from the tuple
117+
*/
118+
readonly getTupleValuesByProperty: <TProperty extends keyof TResult[number]>(property: TProperty) => { [TIndex in keyof TResult]: TResult[TIndex][TProperty]; };
87119
/**
88120
* Gets a value by key
89121
*

dist/butterKeyedEnum.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
exports.ButterKeyedEnum = ButterKeyedEnum;
77
const deep_freeze_es6_1 = __importDefault(require("deep-freeze-es6"));
8-
const mapTuple_1 = require("./mapTuple");
98
/**
109
* Butter Keyed Enum
1110
*
@@ -73,7 +72,36 @@ function ButterKeyedEnum(enumObject, options) {
7372
* @type {TTuple} The tuple of enum values in the order defined by tupleFactory
7473
*/
7574
tuple: $tuple,
76-
mapTuple: (fn) => (0, mapTuple_1.mapTuple)($tuple, fn),
75+
/**
76+
* Maps a property of the tuple to an array of values
77+
*
78+
* @example
79+
* ```typescript
80+
* const colorsEnum = ButterKeyedEnum({
81+
* green: {
82+
* emoji: '🟩',
83+
* hex: '#00FF00',
84+
* },
85+
* red: {
86+
* emoji: '🟥',
87+
* hex: '#FF0000',
88+
* },
89+
* }, {
90+
* tupleFactory: (enumObject) => [
91+
* enumObject.green,
92+
* enumObject.red,
93+
* ]
94+
* })
95+
*
96+
* colorsEnum.getTupleValuesByProperty('emoji') // ['🟩', '🟥']
97+
* ```
98+
*
99+
* @param property The property to map
100+
* @returns An array of values from the tuple
101+
*/
102+
getTupleValuesByProperty(property) {
103+
return $tuple.map((value) => value[property]);
104+
},
77105
/**
78106
* Gets a value by key
79107
*

dist/butterTupleEnum.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export declare function ButterTupleEnum<const T extends readonly string[]>(tuple
2525
* @type {T} The tuple of strings
2626
*/
2727
tuple: T;
28-
mapTuple: <U>(fn: (value: T[number], index: number, tuple: T) => U) => { [K in keyof T]: U; };
2928
/**
3029
* The enum object
3130
*

dist/butterTupleEnum.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
exports.ButterTupleEnum = ButterTupleEnum;
77
const deep_freeze_es6_1 = __importDefault(require("deep-freeze-es6"));
8-
const mapTuple_1 = require("./mapTuple");
98
/**
109
* Butter Tuple Enum
1110
*
@@ -36,7 +35,6 @@ function ButterTupleEnum(tuple) {
3635
* @type {T} The tuple of strings
3736
*/
3837
tuple: $tuple,
39-
mapTuple: (fn) => (0, mapTuple_1.mapTuple)($tuple, fn),
4038
/**
4139
* The enum object
4240
*

dist/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './butterKeyedEnum';
22
export * from './butterTupleEnum';
3-
export * from './mapTuple';

dist/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
1616
Object.defineProperty(exports, "__esModule", { value: true });
1717
__exportStar(require("./butterKeyedEnum"), exports);
1818
__exportStar(require("./butterTupleEnum"), exports);
19-
__exportStar(require("./mapTuple"), exports);

dist/mapTuple.d.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

dist/mapTuple.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "butter-enums",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Typesafe specialized enums for TypeScript - Smooth like butter",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)