Skip to content

Commit

Permalink
Clean the code up (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
timendez authored Oct 23, 2018
1 parent 56b45ca commit a5760c7
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:7.10
- image: circleci/node:10.11.0

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
Expand Down
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ LoMatch([1, 2, 3, 2, 2], [1, 3], []);
```
This will return an output of all the Lodash functions and predicate combinations you should use (only one '[smallest](tools/sorting.js#L5)' predicate per function).
```javascript
[ { func: '_.difference', predicates: [ 2 ] },
{ func: '_.differenceBy', predicates: [ 2 ] },
{ func: '_.differenceWith', predicates: [ 2 ] },
{ func: '_.intersection', predicates: [ 1, 3 ] },
{ func: '_.intersectionBy', predicates: [ 1, 3 ] },
{ func: '_.intersectionWith', predicates: [ 1, 3 ] },
{ func: '_.pull', predicates: 2 },
{ func: '_.pullAll', predicates: [ 2 ] },
{ func: '_.pullAllBy', predicates: [ 2 ] },
{ func: '_.pullAllWith', predicates: [ 2 ] },
{ func: '_.pullAt', predicates: [ 0, 2 ] },
{ func: '_.without', predicates: 2 },
{ func: '_.xor', predicates: [ 2 ] },
{ func: '_.xorBy', predicates: [ 2 ] },
{ func: '_.xorWith', predicates: [ 2 ] } ]
[ { func: '_.difference', args: [ 2 ] },
{ func: '_.differenceBy', args: [ 2 ] },
{ func: '_.differenceWith', args: [ 2 ] },
{ func: '_.intersection', args: [ 1, 3 ] },
{ func: '_.intersectionBy', args: [ 1, 3 ] },
{ func: '_.intersectionWith', args: [ 1, 3 ] },
{ func: '_.pull', args: 2 },
{ func: '_.pullAll', args: [ 2 ] },
{ func: '_.pullAllBy', args: [ 2 ] },
{ func: '_.pullAllWith', args: [ 2 ] },
{ func: '_.pullAt', args: [ 0, 2 ] },
{ func: '_.without', args: 2 },
{ func: '_.xor', args: [ 2 ] },
{ func: '_.xorBy', args: [ 2 ] },
{ func: '_.xorWith', args: [ 2 ] } ]
```
This means that you can use something like `_.pull([1, 2, 3, 2, 2], 2)` in order to get `[1, 3]`. LoMatch will return results for both modified arrays and returned arrays. Be sure to read the [Lodash documentation](https://lodash.com/docs/4.17.10) for the function you are going to use.

Expand All @@ -60,7 +60,7 @@ LoMatch({ 'a': [{ 'b': { 'c': 3 } }, 4] }, [3, 4], []);
```
This will return an output of all the Lodash functions and predicate combinations you should use.
```javascript
[ { func: '_.at', predicates: [ 'a[0].b.c', 'a[1]' ] } ]
[ { func: '_.at', args: [ 'a[0].b.c', 'a[1]' ] } ]
```
This means that you can use `_.at({ 'a': [{ 'b': { 'c': 3 } }, 4] }, [ 'a[0].b.c', 'a[1]' ])` in order to get `[3, 4]`. LoMatch will return results for both modified objects and returned objects. Be sure to read the [Lodash documentation](https://lodash.com/docs/4.17.5) for the function you are going to use.

Expand All @@ -75,6 +75,6 @@ LoMatch('fred, barney, & pebbles', ['fred', 'barney', 'pebbles'], []);
```
This will return an output of all the Lodash functions and predicate combinations you should use.
```javascript
[ { func: '_.words', predicates: [] } ]
[ { func: '_.words', args: [] } ]
```
This means that you can use `_.words('fred, barney, & pebbles')` in order to get `['fred', 'barney', 'pebbles']`. Keep in mind strings are primitives and thus cannot be mutated in JavaScript. Be sure to read the [Lodash documentation](https://lodash.com/docs/4.17.5) for the function you are going to use.
41 changes: 21 additions & 20 deletions lomatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ const generate = require('./tools/generation.js');
const test = require('./tools/test.js');
const sort = require('./tools/sorting.js')

function match(input, output, predicates, funcs) {
function match(input, output, args, funcs) {
var matches = [];
var outerLoopCount = 0;
_.forEach(funcs, func => {
if (test.testWithoutPredicate(input, output, func.func)) {
if (test.testWithoutArgs(input, output, func.func)) {
return matches.push({func: func.name});
}

var validPredicates = [];
_.forEach(predicates, predicate => {
var validArgs = [];
_.forEach(args, arg => {
try {
var passingPredicates = test.testWithPredicate(input, output, predicate, func.func);
if (passingPredicates != 'failure') {
validPredicates.push(passingPredicates);
var passingArgs = test.testWithArg(input, output, arg, func.func);
if (passingArgs != 'failure') {
validArgs.push(passingArgs);
}

// Multiple argument functions
if ((func.argCount > 2 || func.argCount === 'infinite') && predicates.length < 1000) {
_.forEach(predicates, secondaryPredicate => {
var passingMultipleArguments = test.testWithMultipleArguments(input, output, predicate, secondaryPredicate, func.func);
if ((func.argCount > 2 || func.argCount === 'infinite') && args.length < 1000) {
_.forEach(args, secondaryArg => {
var passingMultipleArguments = test.testWithMultipleArguments(input, output, arg, secondaryArg, func.func);
if (passingMultipleArguments != 'failure') {
validPredicates.push(passingMultipleArguments);
validArgs.push(passingMultipleArguments);
}
});
}
}
catch(err){}
});

if (!_.isEmpty(validPredicates)) {
matches.push({func: func.name, predicates: _.head(sort.sortValidPredicates(_.uniqWith(validPredicates, _.isEqual)))});
if (!_.isEmpty(validArgs)) {
matches.push({func: func.name, args: _.head(sort.sortValidArgs(_.uniqWith(validArgs, _.isEqual)))});
}

// Take all the iteratee preddies and return them, let the user decide which preddies to use in their thing
// Take all the iteratees and return them, let the user decide which iteratee to use in their thing
if (func.inputType === 'iteratee') {
var iteratees = test.testWithIteratees(input, output, func);
if (!_.isEmpty(iteratees)) {
Expand All @@ -51,29 +51,30 @@ function match(input, output, predicates, funcs) {
function matchLang(input, output) {
var matches = [];
_.forEach(functions.funcs.lang, func => {
if (test.testWithoutPredicate(input, output, func.func)) {
if (test.testWithoutArgs(input, output, func.func)) {
return matches.push({func: func.name});
}
});
return matches;
}

function LoMatch(input, output, predicates) {
function LoMatch(input, output, primers) {
var funcs;

primers = Array.isArray(primers) ? primers : [];
if (Array.isArray(input)) {
predicates = _.concat(predicates, generate.generateArrayPredicates(input, output));
primers = _.concat(primers, generate.generateArrayArgs(input, output));
funcs = functions.funcs.array;
}
else if (_.isString(input)) {
predicates = _.concat(predicates, generate.generateStringPredicates(input, output));
primers = _.concat(primers, generate.generateStringArgs(input, output));
funcs = functions.funcs.string;
}
else if (_.isObjectLike(input)) {
predicates = _.concat(predicates, generate.generateObjectPredicates(input, output));
primers = _.concat(primers, generate.generateObjectArgs(input, output));
funcs = functions.funcs.object;
}
return _.concat(match(input, output, predicates, funcs), matchLang(input, output));
return _.concat(match(input, output, primers, funcs), matchLang(input, output));
}

module.exports = {
Expand Down
Loading

0 comments on commit a5760c7

Please sign in to comment.