-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b45c484
Showing
3 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
# JS Object Key Information | ||
|
||
This is a 0dep JS module/class that provides 'uniqueness' information about the keys within an array of objects. Particularly useful in searching / sorting / filtering applications or for general object comparison. | ||
|
||
## Installation | ||
```sh | ||
user@machine> npm i unique-key-js -S | ||
``` | ||
|
||
**- or -** | ||
|
||
simply include `UniqueKey.js` in your project and src/package/import/require it as necessary. | ||
|
||
## Usage | ||
|
||
`UniqueKey.js` exports a factory class as a module, `UniqueKey` | ||
|
||
```js | ||
const UniqueKey = require('./UniqueKey') | ||
console.log('More Detailed Key Info : ', UniqueKey.getInfoAboutKeys(people)) | ||
console.log('Keys with Unique Values : ', UniqueKey.getKeysWithUniqueValues(people)) | ||
``` | ||
|
||
## Example | ||
```js | ||
const UniqueKey = require('./UniqueKey') | ||
|
||
const people = [{ | ||
"department":"Content Management", | ||
"deskphone" : "+1-234-456-7890", | ||
"displayname" : "Sherell Marcusen", | ||
"email" : "[email protected]", | ||
"employee_id" : "5819", | ||
"firstname" : "Sherell ", | ||
"lastname" : "Marcusen", | ||
"location" : "LAX", | ||
"mobile" : "+1-123-234-3456", | ||
"picture" : "http://lorempixel.com/400/400/people/1/", | ||
"title" : "Content Manager" | ||
}, | ||
{ | ||
"department" : "Content Management", | ||
"deskphone" : "+1-234-456-5678", | ||
"displayname" : "Dian Chough", | ||
"email" : "[email protected]", | ||
"employee_id" : "5790", | ||
"firstname" : "Dian ", | ||
"lastname" : "Chough", | ||
"location" : "LAX", | ||
"mobile" : "+1-123-234-5678", | ||
"picture" : "http://lorempixel.com/400/400/people/7/", | ||
"title" : "Content Manager" | ||
}, | ||
{ | ||
"department": "Content Management", | ||
"deskphone" : "+1-234-456-6789", | ||
"displayname" : "Ernie Dangler", | ||
"email" : "[email protected]", | ||
"employee_id" : "5979", | ||
"firstname" : "Ernie ", | ||
"lastname" : "Dangler", | ||
"mobile" : "+1-123-234-4567", | ||
"picture" : "http://lorempixel.com/400/400/people/3/", | ||
"title" : "Content Management Supervisor" | ||
}] | ||
|
||
console.log('More Detailed Key Info : ', UniqueKey.getInfoAboutKeys(people)) | ||
console.log('Keys with Unique Values : ', UniqueKey.getKeysWithUniqueValues(people)) | ||
``` | ||
|
||
**Example Output** | ||
|
||
Given the array of objects : | ||
```javascript | ||
[{ | ||
"department":"Content Management", | ||
"deskphone" : "+1-234-456-7890", | ||
"displayname" : "Sherell Marcusen", | ||
"email" : "[email protected]", | ||
"employee_id" : "5819", | ||
"firstname" : "Sherell ", | ||
"lastname" : "Marcusen", | ||
"location" : "LAX", | ||
"mobile" : "+1-123-234-3456", | ||
"picture" : "http://lorempixel.com/400/400/people/1/", | ||
"title" : "Content Manager" | ||
}, | ||
{ | ||
"department" : "Content Management", | ||
"deskphone" : "+1-234-456-5678", | ||
"displayname" : "Dian Chough", | ||
"email" : "[email protected]", | ||
"employee_id" : "5790", | ||
"firstname" : "Dian ", | ||
"lastname" : "Chough", | ||
"location" : "LAX", | ||
"mobile" : "+1-123-234-5678", | ||
"picture" : "http://lorempixel.com/400/400/people/7/", | ||
"title" : "Content Manager" | ||
}, | ||
{ | ||
"department": "Content Management", | ||
"deskphone" : "+1-234-456-6789", | ||
"displayname" : "Ernie Dangler", | ||
"email" : "[email protected]", | ||
"employee_id" : "5979", | ||
"firstname" : "Ernie ", | ||
"lastname" : "Dangler", | ||
"mobile" : "+1-123-234-4567", | ||
"picture" : "http://lorempixel.com/400/400/people/3/", | ||
"title" : "Content Management Supervisor" | ||
}] | ||
``` | ||
a response would look like | ||
```javascript | ||
{ unique_keys: | ||
[ 'department', | ||
'deskphone', | ||
'displayname', | ||
'email', | ||
'employee_id', | ||
'firstname', | ||
'lastname', | ||
'mobile', | ||
'picture', | ||
'title', | ||
'location' ], | ||
department: | ||
{ is_unique: false, | ||
all_values: | ||
[ 'Content Management', | ||
'Content Management', | ||
'Content Management' ], | ||
unique_values: [ 'Content Management' ] }, | ||
deskphone: | ||
{ is_unique: true, | ||
all_values: [ '+1-234-456-6789', '+1-234-456-5678', '+1-234-456-7890' ], | ||
unique_values: [ '+1-234-456-6789', '+1-234-456-5678', '+1-234-456-7890' ] }, | ||
displayname: | ||
{ is_unique: true, | ||
all_values: [ 'Ernie Dangler', 'Dian Chough', 'Sherell Marcusen' ], | ||
unique_values: [ 'Ernie Dangler', 'Dian Chough', 'Sherell Marcusen' ] }, | ||
email: | ||
{ is_unique: true, | ||
all_values: | ||
[ '[email protected]', | ||
'[email protected]', | ||
'[email protected]' ], | ||
unique_values: | ||
[ '[email protected]', | ||
'[email protected]', | ||
'[email protected]' ] }, | ||
employee_id: | ||
{ is_unique: true, | ||
all_values: [ '5979', '5790', '5819' ], | ||
unique_values: [ '5979', '5790', '5819' ] }, | ||
firstname: | ||
{ is_unique: true, | ||
all_values: [ 'Ernie ', 'Dian ', 'Sherell ' ], | ||
unique_values: [ 'Ernie ', 'Dian ', 'Sherell ' ] }, | ||
lastname: | ||
{ is_unique: true, | ||
all_values: [ 'Dangler', 'Chough', 'Marcusen' ], | ||
unique_values: [ 'Dangler', 'Chough', 'Marcusen' ] }, | ||
mobile: | ||
{ is_unique: true, | ||
all_values: [ '+1-123-234-4567', '+1-123-234-5678', '+1-123-234-3456' ], | ||
unique_values: [ '+1-123-234-4567', '+1-123-234-5678', '+1-123-234-3456' ] }, | ||
picture: | ||
{ is_unique: true, | ||
all_values: | ||
[ 'http://lorempixel.com/400/400/people/3/', | ||
'http://lorempixel.com/400/400/people/7/', | ||
'http://lorempixel.com/400/400/people/1/' ], | ||
unique_values: | ||
[ 'http://lorempixel.com/400/400/people/3/', | ||
'http://lorempixel.com/400/400/people/7/', | ||
'http://lorempixel.com/400/400/people/1/' ] }, | ||
title: | ||
{ is_unique: false, | ||
all_values: | ||
[ 'Content Management Supervisor', | ||
'Content Manager', | ||
'Content Manager' ], | ||
unique_values: [ 'Content Management Supervisor', 'Content Manager' ] }, | ||
location: | ||
{ is_unique: false, | ||
all_values: [ 'LAX', 'LAX' ], | ||
unique_values: [ 'LAX' ] } } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* | ||
* @class | ||
* @classdesc Factory class that exposes utility method for object comparison within an array of objects. | ||
* @exports {Class} uniqueKey | ||
*/ | ||
class UniqueKey { | ||
|
||
/** | ||
* Gets detailed comparison information from an array of objects. | ||
* | ||
* @static | ||
* @param {Object} an array of objects to be compared. | ||
* @returns {Object} Returns a verbose informational object with propertes : | ||
* is_unique {Bool}, all_values {Array} and unique_values {Array} | ||
* for any key identified in any of the objects within the argument array | ||
*/ | ||
static getInfoAboutKeys(objArr) { | ||
let key_info = { "unique_keys" : [] } | ||
for (var i = objArr.length - 1; i >= 0; i--) { | ||
const prop = objArr[i] | ||
for (const key in prop) { | ||
const value = prop[key] | ||
if (!key_info[key]) { | ||
key_info[key] = { | ||
"is_unique" : true, | ||
"all_values" : [], | ||
"unique_values" : [] | ||
} | ||
key_info[key].all_values.push(value) | ||
key_info[key].unique_values.push(value) | ||
key_info.unique_keys.push(key) | ||
} else { | ||
if (key_info[key].all_values.includes(value)){ | ||
key_info[key].is_unique = false | ||
} else { | ||
key_info[key].unique_values.push(value) | ||
} | ||
key_info[key].all_values.push(value) | ||
} | ||
} | ||
} | ||
return key_info | ||
} | ||
|
||
/** | ||
* Gets an array of keys where each object has a unique valueue. | ||
* | ||
* @static | ||
* @param {Object} an array of objects to be compared. | ||
* @returns {Array} Returns an array of keys. | ||
*/ | ||
static getKeysWithUniqueValues(objArr) { | ||
const unique_keys = this.getInfoAboutKeys(objArr).unique_keys | ||
return unique_keys | ||
} | ||
} | ||
|
||
|
||
module.exports = UniqueKey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "unique-key-js", | ||
"version": "1.0.0", | ||
"description": "Object comparison utility that provides uniqueness information about the keys within an array of objects.", | ||
"main": "UniqueKey.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": { | ||
"name": "Anthony Chavez", | ||
"url": "https://github.com/artisgarbage/" | ||
}, | ||
"license": "ISC", | ||
"keywords": [ | ||
"javascript", | ||
"objects", | ||
"0dep", | ||
"node", | ||
"npm" | ||
], | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/artisgarbage/unique-key-js.git" | ||
} | ||
} |