Skip to content

Commit 276431c

Browse files
First version ready for publishing
1 parent 992087b commit 276431c

File tree

11 files changed

+269
-9
lines changed

11 files changed

+269
-9
lines changed

.babelrc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"presets": ["@babel/preset-typescript"],
3-
"plugins": ["@babel/plugin-transform-modules-umd"],
4-
"filename": "array-of-objects.js",
5-
"sourceFileName": "array-of-objects.ts",
6-
"filenameRelative": "array-of-objects.ts",
3+
"plugins": [
4+
"@babel/plugin-transform-modules-umd",
5+
"@babel/plugin-proposal-class-properties",
6+
"@babel/plugin-proposal-object-rest-spread"
7+
],
78
"moduleRoot": "src"
89
}

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# for `yarn link` development
2+
coverage
3+
node_modules
4+
lib

.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
browser: true,
5+
node: true,
6+
es6: true
7+
},
8+
parser: "@typescript-eslint/parser",
9+
parserOptions: {
10+
"ecmaVersion": 2020,
11+
"sourceType": "module"
12+
},
13+
plugins: ["@typescript-eslint/eslint-plugin"],
14+
extends: [
15+
"plugin:prettier/recommended",
16+
"eslint:recommended",
17+
"plugin:@typescript-eslint/recommended"
18+
],
19+
rules: {
20+
"no-console": "off",
21+
"no-unused-vars": "off",
22+
"spaced-comment": "off",
23+
"camelcase": "off"
24+
}
25+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
node_modules
22
yarn.lock
33
package-lock.json
4+
coverage
5+
yarn-error.log
46

57
# MAC generated files
68
**/.DS_Store

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"printWidth": 100
4+
}

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Array of Objects
2+
3+
Useful functions to help working with Javascript Array of Objects
4+
5+
## Usage
6+
7+
### Install the package
8+
9+
package.json
10+
```
11+
{
12+
"dependencies": {
13+
"viktor-maksimov/array-of-objects": "latest"
14+
}
15+
}
16+
```
17+
18+
After that (with Yarn):
19+
```
20+
yarn install
21+
```
22+
23+
Or with NPM:
24+
```
25+
npm install
26+
```
27+
28+
And finally you can use it in your project:
29+
```
30+
import { getUniqueValues } from "array-of-objects"
31+
32+
const arrayOfObjects = [
33+
{
34+
name: "John"
35+
},
36+
{
37+
name: "James"
38+
},
39+
{
40+
name: "John"
41+
}
42+
]
43+
44+
const uniqueNames = getUniqueValues(arrayOfObjects, "name")
45+
```
46+
47+
## Functions
48+
49+
### getUniqueValues(arrayOfObjects, propertyName)
50+
51+
This function returns an array containing only the unique values of a specific property in the objects. It accepts as parameters first the ArrayOfObjects and second - a string of the property name.
52+
53+
## Contribution
54+
55+
Everybody can contribute
56+
57+
### Useful commands (examples with yarn)
58+
59+
Build your code:
60+
```
61+
yarn build
62+
```
63+
64+
Run tests:
65+
```
66+
yarn test
67+
```
68+
69+
Generate test coverage:
70+
```
71+
yarn test:coverage
72+
```
73+
74+
Run ESLint:
75+
```
76+
yarn lint
77+
```
78+
79+
### Important
80+
81+
Please before opening a PR for this package - run tests and eslint and fix the errors in your code.
82+
Also for new functions - please add tests.
83+
84+
## License
85+
86+
MIT

lib/array-of-objects.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(function (global, factory) {
2+
if (typeof define === "function" && define.amd) {
3+
define([], factory);
4+
} else if (typeof exports !== "undefined") {
5+
factory();
6+
} else {
7+
var mod = {
8+
exports: {}
9+
};
10+
factory();
11+
global.arrayOfObjects = mod.exports;
12+
}
13+
})(this, function () {
14+
"use strict";
15+
16+
(function (global, factory) {
17+
if (typeof define === "function" && define.amd) {
18+
define(["exports"], factory);
19+
} else if (typeof exports !== "undefined") {
20+
factory(exports);
21+
} else {
22+
var mod = {
23+
exports: {}
24+
};
25+
factory(mod.exports);
26+
global.arrayOfObjects = mod.exports;
27+
}
28+
})(void 0, function (_exports) {
29+
"use strict";
30+
31+
Object.defineProperty(_exports, "__esModule", {
32+
value: true
33+
});
34+
_exports.getUniqueValues = getUniqueValues;
35+
36+
function getUniqueValues(arrayOfObjects, propertyName) {
37+
const output = [];
38+
arrayOfObjects.map(obj => {
39+
if (obj[propertyName] !== undefined) {
40+
if (output.find(val => obj[propertyName] === val) === undefined) {
41+
output.push(obj[propertyName]);
42+
}
43+
}
44+
});
45+
return output;
46+
}
47+
});
48+
});

package.json

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,54 @@
22
"name": "array-of-objects",
33
"version": "0.1.0",
44
"description": "Useful functions to use with array of objects.",
5-
"main": "lib/array-of-objects.ts",
5+
"main": "lib/array-of-objects.js",
66
"repository": "git@github.com:viktor-maksimov/array-of-objects.git",
77
"author": "viktor-maksimov <viktor.slavov.maksimov@gmail.com>",
88
"license": "MIT",
99
"private": false,
10-
"keywords": ["array", "object", "unique"],
10+
"keywords": [
11+
"array",
12+
"object",
13+
"unique"
14+
],
1115
"homepage": "https://github.com/viktor-maksimov/array-of-objects",
1216
"bugs": "https://github.com/viktor-maksimov/array-of-objects/issues",
17+
"contributors": [
18+
"Andras Serfozo <subztep@gmail.com>"
19+
],
1320
"scripts": {
14-
"build": "babel --presets @babel/preset-typescript src/array-of-objects.ts -d lib"
21+
"build": "babel --presets @babel/preset-typescript src/array-of-objects.ts --extensions '.ts' -d ts-compiled-temp; babel --plugins @babel/plugin-transform-modules-umd ts-compiled-temp -d lib; rm -r ts-compiled-temp",
22+
"check-types": "tsc",
23+
"test": "yarn check-types; jest . --passWithNoTests",
24+
"test:coverage": "jest . --coverage",
25+
"lint": "eslint --ext .js,.ts .",
26+
"lint:debug": "eslint --debug --ext .js,.ts .",
27+
"lint:fix": "eslint --fix --ext .js,.ts ."
1528
},
1629
"dependencies": {},
1730
"devDependencies": {
1831
"@babel/cli": "^7.0.0",
1932
"@babel/core": "^7.0.0",
33+
"@babel/plugin-proposal-class-properties": "^7.5.5",
34+
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
2035
"@babel/plugin-transform-modules-umd": "^7.2.0",
21-
"@babel/preset-typescript": "^7.3.3"
36+
"@babel/preset-typescript": "^7.3.3",
37+
"@types/jest": "^24.0.18",
38+
"@typescript-eslint/eslint-plugin": "^2.0.0",
39+
"@typescript-eslint/parser": "^2.0.0",
40+
"babel-jest": "^24.9.0",
41+
"eslint": "^6.2.1",
42+
"eslint-config-prettier": "^6.1.0",
43+
"eslint-config-standard": "^14.0.0",
44+
"eslint-plugin-import": "^2.18.2",
45+
"eslint-plugin-jest": "^22.15.1",
46+
"eslint-plugin-node": "^9.1.0",
47+
"eslint-plugin-prettier": "^3.1.0",
48+
"eslint-plugin-promise": "^4.2.1",
49+
"eslint-plugin-standard": "^4.0.1",
50+
"jest": "^24.9.0",
51+
"ts-jest": "^24.0.2",
52+
"typescript": "^3.6.2",
53+
"prettier": "^1.18.2"
2254
}
2355
}

src/array-of-objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function getUniqueValues(arrayOfObjects: Object[], propertyName): Array<any> {
1+
export function getUniqueValues(arrayOfObjects: Record<string, any>[], propertyName: string): Array<any> {
22
const output: Array<any> = []
33

44
arrayOfObjects.map(obj => {

tests/getUniqueValues.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { getUniqueValues } from "../src/array-of-objects"
2+
3+
const arrayOfObjects = [
4+
{
5+
id: 7,
6+
name: "John",
7+
surname: "Travolta"
8+
},
9+
{
10+
id: 11,
11+
name: "John",
12+
surname: "Doe"
13+
},
14+
{
15+
id: 15,
16+
name: "Jim",
17+
surname: "Beam"
18+
},
19+
{
20+
id: 19,
21+
name: "Johnnie",
22+
surname: "Walker"
23+
}
24+
]
25+
26+
describe("Get Unique Values", () => {
27+
const uniqueNames = getUniqueValues(arrayOfObjects, "name")
28+
const expectedUniqueNames = ["John", "Jim", "Johnnie"]
29+
const notExpectedUniqueNames = ["John", "John", "Jim", "Johnnie"]
30+
31+
it("Equal", () => {
32+
expect(uniqueNames).toEqual(expectedUniqueNames)
33+
})
34+
it("Not equal", () => {
35+
expect(uniqueNames).not.toEqual(notExpectedUniqueNames)
36+
})
37+
})

0 commit comments

Comments
 (0)