Comparing version 1.0.2 to 1.0.3
@@ -27,3 +27,7 @@ /* global define, __DEV__ */ | ||
coalesce: function(obj, field) { | ||
if (field in obj || field in Object.values(obj)) { | ||
if ( | ||
obj && | ||
typeof obj === 'object' && | ||
(field in obj || field in Object.values(obj)) | ||
) { | ||
return field; | ||
@@ -42,3 +46,3 @@ } | ||
} else { | ||
throw new Error('Invalid Enum'); | ||
throw new Error('Invalid Enum.'); | ||
} | ||
@@ -45,0 +49,0 @@ }, |
{ | ||
"name": "Enumjs", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A simple function for creating enum object in javascript", | ||
@@ -12,3 +12,5 @@ "main": "index.js", | ||
}, | ||
"bugs": "https://github.com/ryan953/enumjs/issues", | ||
"scripts": { | ||
"pretest": "flow check", | ||
"test": "jest" | ||
@@ -25,3 +27,4 @@ }, | ||
"devDependencies": { | ||
"jest": "^14.1.0", | ||
"flow-bin": "^0.76.0", | ||
"jest": "^22.1.4", | ||
"object.entries": "^1.0.3", | ||
@@ -28,0 +31,0 @@ "object.values": "^1.0.3" |
@@ -1,3 +0,5 @@ | ||
## Enumjs | ||
## Enumjs | ||
[![Current Version](https://img.shields.io/npm/v/Enumjs.svg)](https://www.npmjs.com/package/Enumjs) [![Build Status](https://travis-ci.org/ryan953/Enumjs.svg?branch=master)](https://travis-ci.org/ryan953/Enumjs) [![Greenkeeper badge](https://badges.greenkeeper.io/ryan953/Enumjs.svg)](https://greenkeeper.io/) | ||
Create an enum from a object of key/values. | ||
@@ -8,3 +10,4 @@ | ||
```sh | ||
npm install enumjs --save | ||
# Capitalization matters! | ||
npm install Enumjs --save | ||
``` | ||
@@ -27,3 +30,3 @@ | ||
const Suits = Enum({ | ||
const Suits = Enum.make({ | ||
HEART: 'HEART', | ||
@@ -36,3 +39,3 @@ DIAMOND: 'DIAMOND', | ||
Your new Enum is a plain object that's been frozen so no one can edit the fields or values on it. It is enumerable, immutable, and you can the `in` operator, `hasOwnProperty()` just like normal. | ||
Your new Enum is a plain object that's been frozen so no one can edit the fields or values on it. It is enumerable, immutable, and you can use the `in` operator, `hasOwnProperty()` just like normal. | ||
@@ -42,3 +45,3 @@ ```js | ||
-> true | ||
console.log(Suits.hasOwnProperty('HEART') | ||
console.log(Suits.hasOwnProperty('HEART')) | ||
-> true | ||
@@ -78,16 +81,9 @@ console.log(Suits.HEART) | ||
type SuitType = $Keys<typeof Suits>; | ||
``` | ||
If your values are not the same as the keys then you'll probably just want to type out the flow type as a union of values. | ||
If your values are not the same as the keys then you can use `$Keys` and `$Values` to differentiate between the two sets: | ||
```js | ||
const Suits = Enum({ | ||
HEART: 'suit_hearts', | ||
DIAMOND: 'suit_diamonds', | ||
SPADE: 'suit_spades', | ||
CLUB: 'suit_clubs', | ||
}); | ||
type SuitType = | ||
'suit_diamonds' | 'suit_diamonds' | | ||
'suit_spades' | 'suit_clubs'; | ||
type SuitType = $Keys<typeof Suits>; | ||
type SuitValue = $Values<typeof Suits>; | ||
``` | ||
@@ -103,7 +99,3 @@ | ||
For Tests: | ||
Similarly the test file also requires `Object.keys`. | ||
`Array.isArray`: see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill. | ||
`Object.keys`: see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill. This is only used in `dedupe.js`. | ||
Unit Tests: | ||
The tests also requires [`Object.keys`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
36106
8
964
4
96