Skip to content

Commit 425576e

Browse files
Add getEnumValues (#11)
1 parent eb08d35 commit 425576e

File tree

7 files changed

+81
-19
lines changed

7 files changed

+81
-19
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Removed
1515

16+
## [1.1.0] - 2021-10-17
17+
18+
### Added
19+
20+
- `getEnumValues`
21+
22+
### Changed
23+
24+
### Removed
25+
26+
## [1.0.3] - 2021-10-17
27+
28+
### Added
29+
30+
### Changed
31+
32+
- `getTypeof` returns `function` for javascript functions, like `Date`, `String`, `Object`, etc
33+
34+
### Removed
35+
1636
## [1.0.2] - 2021-10-16
1737

1838
### Added

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<img src="https://img.shields.io/coveralls/github/techmmunity/utils/master?style=for-the-badge" alt="Coveralls">
1515
</a>
1616
<a href="https://github.com/techmmunity/utils/actions/workflows/coverage.yml">
17-
<img src="https://img.shields.io/github/workflow/status/techmmunity/utils/tests?label=tests&logo=github&style=for-the-badge" alt="Tests">
17+
<img src="https://img.shields.io/github/workflow/status/techmmunity/utils/Collect%20Coverage?label=tests&logo=github&style=for-the-badge" alt="Tests">
1818
</a>
1919
<a href="https://www.npmjs.com/package/@techmmunity/utils">
2020
<img src="https://img.shields.io/npm/v/@techmmunity/utils.svg?color=CC3534&style=for-the-badge" alt="Npm">
@@ -30,17 +30,6 @@
3030

3131
Package of utils, make in a way to let you import only the functions that you really need, so it doesn't make your project heavier than it needs to be.
3232

33-
| function | description |
34-
| ---------------------- | ---------------------------------------------------------------------- |
35-
| `cleanObj` | Remove undefined and null values from an object |
36-
| `getArrayUniqueValues` | Remove duplicated values of an array (only work with primitive values) |
37-
| `getTypeof` | Fix native "typeof" |
38-
| `hasRequiredEnvVars` | Verify if all env vars are defined |
39-
| `isEmptyArray` | Checks if is an array and it's empty |
40-
| `isEmptyObject` | Checks if is an object and it's empty |
41-
| `isNotEmptyArray` | Checks if is an array and it's NOT empty |
42-
| `isNotEmptyObject` | Checks if is an object and it's NOT empty |
43-
4433
## Install
4534

4635
With Yarn:
@@ -55,8 +44,16 @@ With NPM:
5544
npm i @techmmunity/utils
5645
```
5746

58-
## How to contribute?
47+
# Docs
5948

60-
All the details about contributing to the project are [described here](https://github.com/techmmunity/utils/blob/master/CONTRIBUTING.md).
61-
62-
## Documentation
49+
| function | description |
50+
| ---------------------- | ---------------------------------------------------------------------- |
51+
| `cleanObj` | Remove undefined and null values from an object |
52+
| `getArrayUniqueValues` | Remove duplicated values of an array (only work with primitive values) |
53+
| `getEnumValues` | Return the enum values |
54+
| `getTypeof` | Fix native "typeof" |
55+
| `hasRequiredEnvVars` | Verify if all env vars are defined |
56+
| `isEmptyArray` | Checks if is an array and it's empty |
57+
| `isEmptyObject` | Checks if is an object and it's empty |
58+
| `isNotEmptyArray` | Checks if is an array and it's NOT empty |
59+
| `isNotEmptyObject` | Checks if is an object and it's NOT empty |

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ module.exports = {
1515
resetMocks: true,
1616
coverageThreshold: {
1717
global: {
18-
statements: 96.07,
18+
statements: 96.29,
1919
branches: 92.85,
2020
functions: 100,
21-
lines: 97.5,
21+
lines: 97.61,
2222
},
2323
},
2424
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@techmmunity/utils",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"main": "index.js",
55
"types": "index.d.ts",
66
"license": "Apache-2.0",
@@ -21,6 +21,7 @@
2121
"array",
2222
"lodash",
2323
"ramda",
24+
"get-enum-values",
2425
"empty-array",
2526
"empty-object"
2627
],

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export * from "./lib/clean-obj";
1818

1919
export * from "./lib/get-typeof";
2020
export * from "./lib/get-array-unique-values";
21+
export * from "./lib/get-enum-values";
2122

2223
/**
2324
* ---------------------------------------------------------------------------

src/lib/get-enum-values/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const getEnumValues = <T>(ENUM: any) =>
2+
[...new Set(Object.values(ENUM))] as Array<T>;

src/tests/get-enum-values.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { getEnumValues } from "lib/get-enum-values";
2+
3+
describe("getEnumValues Util", () => {
4+
describe("With enum that key-value match", () => {
5+
it("should return the enum values", () => {
6+
let result: any;
7+
8+
try {
9+
enum TestEnum {
10+
FOO = "FOO",
11+
BAR = "BAR",
12+
}
13+
14+
result = getEnumValues<TestEnum>(TestEnum);
15+
} catch (err: any) {
16+
result = err;
17+
}
18+
19+
expect(result).toStrictEqual(["FOO", "BAR"]);
20+
});
21+
});
22+
23+
describe("With enum that key-value DON'T match", () => {
24+
it("should return the enum values", () => {
25+
let result: any;
26+
27+
try {
28+
enum TestEnum {
29+
FOO = "FOO1",
30+
BAR = "BAR1",
31+
}
32+
33+
result = getEnumValues<TestEnum>(TestEnum);
34+
} catch (err: any) {
35+
result = err;
36+
}
37+
38+
expect(result).toStrictEqual(["FOO1", "BAR1"]);
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)