Skip to content

Commit

Permalink
Remove logical object and turn it to functional programing
Browse files Browse the repository at this point in the history
  • Loading branch information
torto committed Aug 26, 2019
1 parent 878c3a7 commit 4db8adb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Easy-compare [![Build Status](https://travis-ci.org/torto/easy-compare.svg?branch=master)](https://travis-ci.org/torto/easy-compare) [![Coverage Status](https://coveralls.io/repos/github/torto/easy-compare/badge.svg?branch=master)](https://coveralls.io/github/torto/easy-compare?branch=master) #
# Easy-compare [![Build Status](https://travis-ci.org/torto/easy-compare.svg?branch=master)](https://travis-ci.org/torto/easy-compare) [![Coverage Status](https://coveralls.io/repos/github/torto/easy-compare/badge.svg?branch=master)](https://coveralls.io/github/torto/easy-compare?branch=master) [![install size](https://packagephobia.now.sh/badge?p=easy-compare)](https://packagephobia.now.sh/result?p=easy-compare) #

This library is a way to execute comparison with a JSON pre-defined. It is using the same pattern of Mongodb operators.

Expand Down
25 changes: 8 additions & 17 deletions lib/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,10 @@ const cmpMap = {
$nsize: isNotSameSize,
$regex: isRegexMatch,
$nregex: isNotRegexMatch,
$and: choiceLogicalOperator,
$or: choiceLogicalOperator,
$all: choiceLogicalOperator,
$not: choiceLogicalOperator
}

const logicalMap = {
$and: andLogical,
$or: orLogical,
$all: andLogical,
$not: notLogical
$and: executeLogicalOperator(andLogical),
$or: executeLogicalOperator(orLogical),
$all: executeLogicalOperator(andLogical),
$not: executeLogicalOperator(notLogical)
}

const allOperators = Object.keys(cmpMap)
Expand All @@ -56,12 +49,10 @@ function interatorValues (value, operator, item) {
return cmpMap[item](value, operator[item], item)
}

function choiceLogicalOperator (value, operator, key) {
const keys = Object.keys(operator)
try {
return logicalMap[key](keys, interatorValues, value, operator)
} catch (err) {
return keys.every(interatorValues.bind(null, value, operator))
function executeLogicalOperator (funcLogical) {
return (value, operator, key) => {
const keys = Object.keys(operator)
return funcLogical(keys, interatorValues, value, operator)
}
}

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-compare",
"version": "0.0.8",
"version": "0.0.9",
"description": "Compare values using opertaros like mongodb",
"main": "index.js",
"scripts": {
Expand All @@ -19,7 +19,11 @@
"keywords": [
"mongodb",
"compare",
"json"
"json",
"comparator",
"function",
"operator",
"values"
],
"author": "Guilherme Berghuaser - @torto",
"license": "GPL-3.0",
Expand Down

0 comments on commit 4db8adb

Please sign in to comment.