rauschma / enumify Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://2ality.com/2020/01/enum-pattern.html
License: MIT License
Home Page: https://2ality.com/2020/01/enum-pattern.html
License: MIT License
Edit: I'm dumb, was misusing the library and thought it was because I wasn't importing it correctly. Closing this.
Hi, I noticed that the correct import statement is not provided in the README for 2.0.0. This is vexing me as I can't seem to import the package at all, and have not been able to find code examples of others successfully using version >= 2.0.0. Here's what I'm trying:
package.json:
{
"dependencies": {
"enumify": "^2.0.0"
}
}
index.js:
import { Enumify } from 'enumify';
class Color extends Enumify {
static RED = new Color();
static BLUE = new Color();
static _ = this.closeEnum();
}
console.log(JSON.stringify(Enumify));
console.log(JSON.stringify(Color.RED.name));
The console output shows that Enumify
and Color.RED.name
are undefined. Using the default import syntax import enumify from 'enumify
returns an empty object.
I'm a bit of a javascript beginner, so I'm not sure if I'm doing something obviously wrong here. I'm running this with nodejs 14.X which I believe supports all the necessary public static field features.
I'm looking for a good solution for enums to use with a Redux app. There's at least a few cases where I'd want to be storing values in my Redux state, but ideally all values in a Redux store should be serializable to allow features like time-travel debugging to work properly. Glancing at the Enumify source, I suspect the instances themselves aren't directly serializable, but could I get away with storing the string values in my state and use enumValueOf
to deserialize them as needed?
Enum can be initialized by an array or by an object. The array form creates the instances in exactly the order specified, but it only supports simple values that can become the property names. The object form supports complex values that add instance properties to each result, but the iteration order is not guaranteed.
It would be nice to have a combined option that was object-based but supported explicit sequencing. For example, passing objects to the array form and pulling out a name
property to use as the instance property name.
You can use the array form with simple names and go back after the fact and merge in instance properties (which may be why the copyProperties helper function is exported), but it's more cumbersome than having sequence-of-object support directly.
hi rauschma
I love this package so much, is there any chance to include flow type support?
Thanks
Hi Axel,
In our project we use Bable, configured in SystemJS mode.
So, we cannot use Enumify installed from npm, because it's transpiled in CommonJS mode.
Can you remove src/
directory from .npmignore
to make possible use original es6 source with custom babel configuration.
Thank you
I tried
class Foo extends Enum {
super()
console.log(this) // { ..., ordinal: 0 }
this.n = this.ordinal + 1 // n = NaN
}
Foo.initEnum(...)
but printing Foo.Bar.n
yields NaN
, looks like ordinal
is not directly accessible, any ideas?
Hi,
Sorry if it not the right place, but can the library be supported with common js module system (adding
'module.exports = Enum' at the end of the script)?
Thanks
Hi,
I am using enumify
1.x in a Vue application. This morning tried updating to enumify
2.x. I had to rewrite the enums with the new syntax, which looks great IMO.
In our case, we have the statuses of tasks in a workflow manager. These statuses are strings, so I used the suggestion of the documentation defining an enum like:
class TaskState extends Enumify {
static running = new TaskState('running');
static succeeded = new TaskState('succeeded');
static _ = this.closeEnum();
constructor(value) {
super();
this.value = value;
}
}
This works fine in the IDE, but when running the Vue CLI build command I get the error "Missing class properties transform", more below:
error in ./src/model/TaskState.model.js
Syntax Error: SyntaxError: /home/kinow/Development/python/workspace/cylc-ui/src/model/TaskState.model.js: Missing class properties transform.
7 | // NOTE: the order of the enum values is important to calculate the group states in the UI. Items at
8 | // the top of the list have preference over the items below in the algorithm.
> 9 | static submitFailed = new TaskState('submit_failed')
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 | static failed = new TaskState('failed')
11 | static expired = new TaskState('expired')
12 | static submitRetrying = new TaskState('submit_retrying')
@ ./src/utils/tasks.js 2:0-48 8:30-39 8:54-63 8:72-81 8:91-100 8:112-121 8:131-140 8:148-157 8:174-183 8:194-203 8:215-224 8:233-242 8:252-261 8:268-277 19:71-80
@ ./src/components/cylc/tree/index.js
@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/vuetify-loader/lib/loader.js??ref--20-0!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/Tree.vue?vue&type=script&lang=js&
@ ./src/views/Tree.vue?vue&type=script&lang=js&
@ ./src/views/Tree.vue
@ ./src/views lazy ^\.\/.*\.vue$ namespace object
@ ./src/router/index.js
@ ./src/main.js
@ multi ./src/main.js
It looks like this could be something in my local environment, possibly with Vue Loader/babel not being happy with the class static fields.
I will dig more later to investigate, but reporting the issue here just in case anybody else has a similar problem, or even a possible workaround.
Likely not an issue in the library? But if I find a workaround that suggests ES2015 users may need some extra configuration in Babel, I'd be happy to provide a PR for the docs and close this issue ๐
Cheers
Bruno
I'm using babel + polyfill. The following works in all browsers (IE11+), but has a showstopper issue in IE9 and 10:
Error: "Object doesn't support property or method 'initEnum' "
Code:
import {Enum} from 'enumify';
export default class Directions extends Enum {}
EventType.initEnum([
'North',
'South',
'East',
'West'
]);
I was thinking that it would be useful if we could get the enum by ordinal value with
Enum.enumValueWithOrdinal(0)
for example.
I suggest MIT
I npm installed enumify, and I went to node_modules/enumify
, and it had this folder structure:
Yet, package.json has:
...
"main": "./src/enumify.js",
...
Which makes it unusable.
Enumify version 1.0.3.
Npm version 3.3.12.
Node version 5.4.1.
I love the concept. But, after playing with it, I ran across a non-starter that I am hoping you have an idea on how to solve.
Undefined Variables
import {Enum} from 'enumify';
class MyType extends Enum {}
MyType.initEnum(['FOO', 'BAR']);
let x;
if(x === MyType.SOME_TYPO) {
console.log('Found X!');
}
Ideally, I'd like this to be a compile time (i.e. babel) or static (i.e. ESLint) check or at the very least get syntax highlighting in Webstorm. Any suggestions?
From what i can gather enumify
has a dependency of babel-preset-es2015
which kind of conflicts with react-native default presets and hence i am unable to use this with react-native currently. More information can be found from here facebook/react-native#5747 (comment). Currently i am forced to locally copy this code in my source code to use this library. Any help would be appreciated. Thanks
Can't use this library via browserify in IE since on line 1, Symbol() is not supported within any flavor of IE.
While one can use babel for polyfill, it seems like a major dependency to take for this small library.
I mean is it necessary to have the boilerplate of creating the class? Cannot we do something like const Color = Enum.create('RED', 'GREEN', 'BLUE')
. Since class is an expression, having factory function like this should work, right?
Enum.create = function(...names) {
const enum = class EnumClass extends Enum {}
enum.initEnum(...names);
return enum;
}
If this isn't possible for some reason, then perhaps something like class Color extends Enum.create('RED', 'GREEN', 'YELLOW') {}
. Although that's not that pretty anymore.
I'd like to depend on this project in a TypeScript app, but I haven't found typings for it here or on DefinitelyTyped. Have types been defined for this? Or am I just missing something obvious?
Thanks
Hi,
For proper Free Open Source Software documentation I need a proper tag or release for 2.0.0 I use in my application.
Could you please create a tag?
It would be very helpful for me.
Thx and Regards,
Peter
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.