{"id":28754220,"url":"https://github.com/zeroasterisk/npm-s-enum","last_synced_at":"2025-06-17T01:07:59.270Z","repository":{"id":35312644,"uuid":"39574121","full_name":"zeroasterisk/npm-s-enum","owner":"zeroasterisk","description":"Super Enums factory for config lists, etc, with helper functions, packaged for Meteor","archived":false,"fork":false,"pushed_at":"2016-08-26T04:12:17.000Z","size":23,"stargazers_count":9,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-08T22:05:41.966Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zeroasterisk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-23T15:07:16.000Z","updated_at":"2018-04-20T08:45:05.000Z","dependencies_parsed_at":"2022-09-18T19:43:08.611Z","dependency_job_id":null,"html_url":"https://github.com/zeroasterisk/npm-s-enum","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/zeroasterisk/npm-s-enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2Fnpm-s-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2Fnpm-s-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2Fnpm-s-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2Fnpm-s-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeroasterisk","download_url":"https://codeload.github.com/zeroasterisk/npm-s-enum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2Fnpm-s-enum/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260269440,"owners_count":22983646,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-17T01:07:58.001Z","updated_at":"2025-06-17T01:07:59.257Z","avatar_url":"https://github.com/zeroasterisk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Super Enums Factory: `SEnum(list)`\n\nEnums are great, for reducing a list of values, keys, labels, etc\n\n\u003e In computer programming, an enumerated type (also called enumeration or enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members or enumerators of the type.\n\u003e https://en.wikipedia.org/wiki/Enumerated_type\n\n\u003e You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: \"permanent\", \"temp\", \"apprentice\"), or flags (\"execute now\", \"defer execution\").\n\u003e http://stackoverflow.com/a/4709224/194105\n\nYou end up with `values` which are smaller, so less goes into your database, and\nless \"over the wire\" via DDP.\n\nThe translation from `values` to `labels` is all in very small application code,\ncached on the client.\n\n## Install\n\n```\nmeteor add zeroasterisk:s-enum\n```\n\n## Terms\n\n* `key` should be a **camelCase** string selector for any node.\n * if you provide one, we don't do anything to it\n * if you do not provide one, we attempt to translate `label` to camelCase\n* `value` can be **anything**... usually a number or string or function\n * if you don't provide one, we attempt to create a numerical value incrementing\n   from `0`\n* `label` should be a **human readable string**\n * if you don't provide one, we use key\n* `nodes` the internal array of all added items.\n * Each node has at least a `key`, a `value`, and a `label` and can have any other properties.\n\n\n## Usage Example: Days of the Week (basics)\n\nFirst you create your enum object... this can happen anywhere\n\n``` js\nvar Days = SEnum([\n  \"Sunday\",\n  \"Monday\",\n  \"Tuesday\",\n  \"Wednesday\",\n  \"Thursday\",\n  \"Friday\",\n  \"Saturday\"\n]);\n\n// now you can access:\nDays.values() === [0, 1, 2, 3, 4, 5, 6];\nDays.keys() === [\"sunday\", \"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\"];\nDays.labels() === [\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"];\nDays.options() === {0: \"Sunday\", 1: \"Monday\", 2: \"Tuesday\", 3: \"Wednesday\", 4: \"Thursday\", 5: \"Friday\", 6: \"Saturday\"};\n\n// and via aliases on the value\nDays[0].label === \"Sunday\";\nDays[0].key === \"sunday\";\nDays[0].value === 0;\n\n// and via aliases on the key\nDays.sunday.label === \"Sunday\"\nDays.sunday.key === \"sunday\"\nDays.sunday.value === 0;\n\n// and use a get() to return any specific field, from value or key\nDays.get(\"sunday\", \"value\") === 0;\nDays.get(0, \"label\") === \"Sunday\";\nDays.get(\"not-found\", \"value\", \"defaultValue\") === \"defaultValue\";\n\n// or you have access to the full list of all internal \"nodes\"\n_.pluck(_.filter(Days.nodes, function(node){ return node.value % 2 == 0; }), 'label') === [Sunday, Tuesday, Thursday, Saturday]\n```\n\n## Usage Example: Status\n\nFirst you create your enum object... this can happen anywhere, but in this\nexample we are going to think about attaching this to a Collection Object.\n\n``` js\nTasks.Statuses = SEnum([\n  { key: \"submitted\", value: 1,  label: \"Submitted\",\n    icon: \"fa fa-check\" },\n  { key: \"accepted\",  value: 2,  label: \"Accepted\",\n    icon: \"fa fa-check-circle\" },\n  { key: \"completed\", value: 31, label: \"All Done Yo\",\n    icon: \"fa fa-check-square\", finished:  true }\n]);\n\n// NOTE in additiion to the basics above, you can get to any field configured\n\nTasks.Statuses.get(\"submitted\", \"icon\") === \"fa fa-check\";\nTasks.Statuses.get(1, \"icon\") === \"fa fa-check\";\nTasks.Statuses.get(31, \"finished\") === true;\nTasks.Statuses.get(1, \"finished\") === undefined;\nTasks.Statuses.get(1, \"finished\", \"defaultValue\") === \"defaultValue\";\n\n// and via aliases on the value\nTasks.Statuses[1].icon === \"fa fa-check\";\nTasks.Statuses.submitted.icon === \"fa fa-check\";\n\n// or you have access to the full list of all internal \"nodes\"\n_.pluck(_.filter(Tasks.Statuses.nodes, function(node){ return node.value % 2 == 0; }), 'label') === [\"Accepted\"]\n```\n\n#### Integrating into Meteor\n\nYou can setup a transform, translating all `keys` \u0026 `labels` into `values`\n\n``` js\n// allow all values\nallowedValues: Tasks.Statuses.values(),\n// or allow all values AND all keys [1, 2, 31, \"submitted\", \"accepted\", \"completed\"]\nallowedValues: Tasks.Statuses.values().concat(Tasks.Statuses.keys()),\n```\n\nIf you want to, you can setup a\n[Mongo.Collection transform](http://docs.meteor.com/#/full/mongo_collection)\nThis will allow a user to pass in any `value` or `key`, and verify that it's got\na valid, `value`; if not it changes to `0`.\n\n``` js\nTasks = new Mongo.Collection('tasks', {\n  transform:  function(doc) {\n    if (_.has(doc, \"status\")) {\n      doc.status = Tasks.Statuses.value(doc.status, 0);\n    }\n    return doc;\n  }\n});\n```\n\nIf you're using autoform/schemas you can setup `autoValue` something like the\nfollowing.\n\nThis will allow a user to pass in any `value` or `key`, and verify that it's got\na valid, `value`; if not it unsets... (you could choose not to unset, and\ninstead get validation errors based on `allowedValues`).\n\n``` js\nstatus: {\n  type: Number,\n  optional: true,\n  autoValue: function() {\n    var autoFormField = this.field(\"status\");\n    if (!autoFormField.isSet) {\n      this.unset();\n      return;\n    }\n    var status = Tasks.Statuses.value(autoFormField.value);\n    if (typeof status === \"undefined\") {\n      this.unset();\n      return;\n    }\n    return status;\n  }\n}\n```\n\nAnd for any template, you can easily translate\n`value` -\u003e `label`\nor any `value` -\u003e any other field...\n\nThis allows some fancy and simple comparable translations.\n\n``` js\nTemplate.Example.helpers({\n\n  // value -\u003e label\n  status: function() {\n    return Tasks.Statuses.label(this.status);\n  },\n\n  // value -\u003e \u003ci\u003e font awesome icon (with default=?)\n  statusIcon: function() {\n    var icon = Tasks.Statuses.get(this.status, \"icon\");\n    if (!icon) {\n      icon = \"fa fa-question\";\n    }\n    return '\u003ci class=\"' + icon '\"\u003e\u003c/i\u003e';\n  },\n\n  // value -\u003e \u003cspan class=\"label {{finished?}}\"\u003e\u003ci\u003e text\n  statusLabelAndIcon: function() {\n    var label = Tasks.Statuses.label(this.status, \"Unknown\");\n    var icon = Tasks.Statuses.get(this.status, \"icon\");\n    if (!icon) {\n      icon = \"fa fa-question\";\n    }\n    var finished = Tasks.Statuses.get(this.status, \"finished\");\n    var className = finished ? 'success' : 'default';\n    return '\u003cspan class=\"label label-' + className + '\"\u003e' +\n      '\u003ci class=\"' + icon '\"\u003e\u003c/i\u003e ' + label +\n      '\u003c/span\u003e';\n  }\n});\n```\n\n## TODO / Roadmap\n\n- [x] Construct support for full nodes, simple `[value, ...]` lists, and `{value: lable, ...}` lists\n- [x] Object helper methods: `keys(), values(), labels()`\n- [x] Object helper methods: `get(), value(), label()`\n- [x] Fully tested\n- [x] README w/ real world usage examples\n- [ ] Meteor ui helper (maybe as extra optional package) `{{SEnum status Tasks.Statuses}}`\n- [ ] split to non Meteor npm package format\n- [ ] maybe remove requirement of underscorejs??\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroasterisk%2Fnpm-s-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeroasterisk%2Fnpm-s-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroasterisk%2Fnpm-s-enum/lists"}