chasefleming / enum-xyz Goto Github PK
View Code? Open in Web Editor NEWFlexible JavaScript enums using ES6 Proxies.
Flexible JavaScript enums using ES6 Proxies.
Files are currently using CommonJS for Jest. Convert to ES Modules and get Jest running with them.
Allow options to be passed into enum creation. e.g. For String
let lowercase be an option.
With the current syntax, there is no way to create an enum with say a key of foo
, but a value of bar
. There is also no way to create heterogenous enums (although those cases are much more rare). It might make sense to have an enum that could support this and also lends itself to being more easily exportable (as discussed here) if a developer chooses to use this syntax. Caveat being, there should be a way for the destructured syntax to also access these methods even if the syntax is more clunky (developer choice...or they may start one way in a single file and later expand on it and need more functionality).
Example syntax could be:
const ENVIRONMENTS = Enum.set({
DEV: 'development',
PROD: 'production'
})
// Could have methods of this later e.g. ENVIRONMENTS.keys()
Later methods accessible from this will show the value of this over an object. And could also internally freeze the values for more benefits over an object.
When creating multiple numeric enums consecutively, the index does not reset to zero for the new enum. This leads to unexpected behavior where the new enum starts from where the previous one left off.
Declare a numeric enum:
const { A, B, C } = Enum.Numeric;
Declare another numeric enum:
const { D, E } = Enum.Numeric;
Expected Behavior:
D
should have a value of 0
.
Actual Behavior:
D
starts from where C
left off.
Currently, if you export like this:
// enums.js
export const { Summer, Autumn, Winter, Spring } = Enum.String
Importing would then require you to import
// other file
import { Summer, Autumn, Winter, Spring } from './enums.js'
If you have many enums, this could be tedious and you may want to scope them to a name like Seasons
which you could then call each item on. Ideally, it would look something like this:
import { Seasons } from './enums.js'
Seasons.Summer // 'Summer'
This may take some design changes. Curious to spark a conversation here on approach.
Currently there is no way to access keys or values of an enum. This ties somewhat back to the exporting issue in that there needs to be a way to group enum items together in order to access methods off of them.
Ideal solution would include methods for:
.keys()
.values()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.