Key + Enum = kenum
A TypeScript-first utility for creating enum-like objects with namespaced keys using template literals. Perfect for action types, constants, and configuration objects.
- 🚀 Zero dependencies - Pure TypeScript implementation
- 📝 Type-safe - Full TypeScript support with proper type definitions
- 🎯 Template literals - Clean, readable syntax using ES6 template strings
- 🔧 Value assignment - Support for custom values with
KEY = valuesyntax - 🌐 Namespaced - Automatic prefixing to avoid key collisions
- ✨ Interpolation - Full support for variable interpolation
npm install kenum
# or
yarn add kenumimport kenum from 'kenum';
const actions = kenum.USER`
FETCH
CREATE
UPDATE
DELETE
`;
console.log(actions);
// Output:
// {
// FETCH: 'USER/FETCH',
// CREATE: 'USER/CREATE',
// UPDATE: 'USER/UPDATE',
// DELETE: 'USER/DELETE'
// }const apiEndpoints = kenum.API`
USERS
POSTS
COMMENTS
`;
// Result: { USERS: 'API/USERS', POSTS: 'API/POSTS', COMMENTS: 'API/COMMENTS' }const entity = 'PRODUCT';
const action = 'SYNC';
const constants = kenum.STORE`
${entity}_CREATE
${entity}_UPDATE
BATCH_${action}
`;
// Result: {
// PRODUCT_CREATE: 'STORE/PRODUCT_CREATE',
// PRODUCT_UPDATE: 'STORE/PRODUCT_UPDATE',
// BATCH_SYNC: 'STORE/BATCH_SYNC'
// }Assign custom values using the = syntax:
const httpStatus = kenum.HTTP`
SUCCESS = 200
NOT_FOUND = 404
SERVER_ERROR = 500
`;
// Result: {
// SUCCESS: 'HTTP/SUCCESS:200',
// NOT_FOUND: 'HTTP/NOT_FOUND:404',
// SERVER_ERROR: 'HTTP/SERVER_ERROR:500'
// }const config = kenum.APP`
API_URL = https://api.example.com
TIMEOUT = 5000
DEBUG_MODE
PRODUCTION_MODE
`;
// Result: {
// API_URL: 'APP/API_URL:https://api.example.com',
// TIMEOUT: 'APP/TIMEOUT:5000',
// DEBUG_MODE: 'APP/DEBUG_MODE',
// PRODUCTION_MODE: 'APP/PRODUCTION_MODE'
// }const userActions = kenum.USER`
FETCH_PENDING
FETCH_SUCCESS
FETCH_FAILURE
CREATE_PENDING
CREATE_SUCCESS
CREATE_FAILURE
`;
// Perfect for Redux reducers:
// {
// FETCH_PENDING: 'USER/FETCH_PENDING',
// FETCH_SUCCESS: 'USER/FETCH_SUCCESS',
// FETCH_FAILURE: 'USER/FETCH_FAILURE',
// // ... etc
// }Creates a kenum function for the specified namespace.
Parameters:
namespace(string): The namespace prefix for all keys
Returns: A template literal function that creates the enum object
When using the = syntax, the resulting value follows the pattern:
'namespace/KEY:value'
For regular keys without assignment:
'namespace/KEY'
Kenum is written in TypeScript and provides full type safety:
import kenum, { KenumResult } from 'kenum';
const myEnum: KenumResult = kenum.NAMESPACE`
KEY1
KEY2 = customValue
`;
// myEnum is typed as { [key: string]: string }- Prevents typos: Centralized constant definitions
- Namespace isolation: Avoid key collisions across different modules
- Readable syntax: Template literals are more readable than object literals
- Consistent formatting: Automatic namespace prefixing ensures consistency
- IDE support: Full IntelliSense and autocomplete support
Before (traditional approach):
const USER_ACTIONS = {
FETCH: 'USER/FETCH',
CREATE: 'USER/CREATE',
UPDATE: 'USER/UPDATE',
DELETE: 'USER/DELETE'
};After (with kenum):
const USER_ACTIONS = kenum.USER`
FETCH
CREATE
UPDATE
DELETE
`;- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Add tests for your changes
- Ensure tests pass (
yarn test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
ISC License - see the LICENSE file for details.