Skip to content

Commit

Permalink
Merge pull request #4 from gastonpereyra/AOE-02-Add-Function-Compare
Browse files Browse the repository at this point in the history
PR - AOE -02 - Add function compare
  • Loading branch information
gastonpereyra committed Sep 13, 2020
2 parents 5876dee + 18cdd46 commit 7ee1344
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 179 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/story.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Story
about: Story to organize development
title: "[STORY]"
title: "[STORY] - AOE - NUMBER -"
labels: enhancement
assignees: gastonpereyra

Expand Down
10 changes: 9 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
---
name: PR
title: "PR - AOE - NUMBER -"
labels: enhancement
assignees: gastonpereyra

---

# :speech_balloon: Pull Request

## :link: Story
Closes #NUMBER

Link [TITLE](https://github.com/gastonpereyra/REPO_NAME/issues/NUMBER)
Link [TITLE](https://github.com/gastonpereyra/are-objects-equals/issues/NUMBER)

## :sparkles: Solution

Expand Down
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.0.0] 2020-13-09
### Added
- Github Actions
- Github Actions
- Compare Function
- Documentation
118 changes: 114 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,134 @@
![Build Status](https://github.com/gastonpereyra/are-objects-equals/workflows/Build%20Status/badge.svg)
[![Coverage Status](https://img.shields.io/coveralls/github/gastonpereyra/are-objects-equals/master.svg)](https://coveralls.io/r/gastonpereyra/are-objects-equals?branch=master)

![npm-are-objects-equals](https://user-images.githubusercontent.com/39351850/93023968-0f2c1400-f5c9-11ea-8e4c-c567dee98b44.png)

## Description
A tool to compare objects easyly
A Function to compare and normalize objects easier

## Installation

```
npm i are-objects-equals
```

## Methods
## Parmas

`areObjectsEquals(objectBase, objectToCompare, options)`

### objectBase and objectToCompare

* Items to compare
* Type: `Object`
* Required

Example

```js
{
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
}
```

### options

* Options to normalize the objects before compare
* Type: `Object`
* Optional

Example

```js
{
fieldsToKeep: ["name", "number"]
}
```

:link: See more in [Object Normalize](https://github.com/gastonpereyra/objects-normalizer#options)


## Usage

### areObjectsEquals(objectBase, objectToCompare)

Will compare the 2 objects without any formatting

```js
const areObjectsEquals = require('are-objects-equals');

const playerSample1 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
};

const playerSample2 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10
}

areObjectsEquals(playerSample1, playerSample2);

/*
output: false
*/

areObjectsEquals(playerSample1, playerSample1);

/*
output:
output: true
*/
```
## Extra

### areObjectsEquals(objectBase, objectToCompare, options)

Will compare objects after normalize them

```js
const areObjectsEquals = require('are-objects-equals');

const playerSample1 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
};

const playerSample2 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10
}

areObjectsEquals(playerSample1, playerSample2, { fieldsToKeep: ["name", "lastname", "clubs", "number"]});

/*
output: true
*/

const playerSample3 = {
name: "Lionel",
lastname: "Messi",
clubs: ["Barcelona"],
number: 10,
stillPlaying: true
}

areObjectsEquals(playerSample1, playerSample3, { fieldsToRemove: ["name", "lastname", "clubs", "stillPlaying"]});

/*
output: true
*/
32 changes: 30 additions & 2 deletions lib/are-objects-equals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
'use strict'
'use strict';

module.exports = class areObjectsEquals {}
const assert = require('assert');
const objectNormalizer = require('objects-normalizer');
const F = require('press-f');

const ERROR_NAME = 'AreObjectsEquals';

/**
* Normalize Objects and returns if both objects are equal
* @param {object} objectBase Base object
* @param {object} objectToCompare Object to Compare
* @param {object} options To normalize objects, FieldsToKeep or FieldsToRemove
* @returns {boolean} True if are equals, false if not
*/
const areObjectsEquals = (objectBase, objectToCompare, options) => {

if(!objectBase || !objectToCompare)
throw new F('No objects to compare', ERROR_NAME);

const [objectFormatted1, objectFormatted2] = objectNormalizer([objectBase, objectToCompare], options);

try {
assert.deepStrictEqual(objectFormatted1, objectFormatted2);
return true;
} catch(error) {
return false;
}
};

module.exports = areObjectsEquals;
7 changes: 0 additions & 7 deletions lib/index.js

This file was deleted.

Loading

0 comments on commit 7ee1344

Please sign in to comment.