Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit ee9fa49

Browse files
committed
refactore: start extracting visitors
1 parent 535dd80 commit ee9fa49

File tree

9 files changed

+141
-0
lines changed

9 files changed

+141
-0
lines changed

src/visitors/allVisitor.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import truthy from "../helpers/truthy";
2+
3+
function allVisitor(data, values) {
4+
const scopedData = apply(values[0], data);
5+
const scopedLogic = values[1];
6+
// All of an empty set is false. Note, some and none have correct fallback after the for loop
7+
if( ! scopedData.length) {
8+
return false;
9+
}
10+
for(let i=0; i < scopedData.length; i+=1) {
11+
if( ! truthy( apply(scopedLogic, scopedData[i]) )) {
12+
return false; // First falsy, short circuit
13+
}
14+
}
15+
return true; // All were truthy
16+
}
17+
18+
export default allVisitor;

src/visitors/andVisitor.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import truthy from "../helpers/truthy";
2+
3+
function andVisitor(data, values, current) {
4+
for(let i=0; i < values.length; i++) {
5+
current = apply(values[i], data);
6+
if( ! truthy(current)) {
7+
return current;
8+
}
9+
}
10+
return current; // Last
11+
}
12+
13+
export default andVisitor;

src/visitors/filterVisitor.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import truthy from "../helpers/truthy";
2+
3+
function filterVisitor(data, values) {
4+
const scopedData = apply(values[0], data);
5+
const scopedLogic = values[1];
6+
7+
if ( ! Array.isArray(scopedData)) {
8+
return [];
9+
}
10+
// Return only the elements from the array in the first argument,
11+
// that return truthy when passed to the logic in the second argument.
12+
// For parity with JavaScript, reindex the returned array
13+
return scopedData.filter(function(datum){
14+
return truthy( apply(scopedLogic, datum));
15+
});
16+
}
17+
18+
export default filterVisitor

src/visitors/ifVisitor.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import truthy from '../helpers/truthy';
2+
3+
function ifVisitor(data, values) {
4+
/* 'if' should be called with a odd number of parameters, 3 or greater
5+
This works on the pattern:
6+
if( 0 ){ 1 }else{ 2 };
7+
if( 0 ){ 1 }else if( 2 ){ 3 }else{ 4 };
8+
if( 0 ){ 1 }else if( 2 ){ 3 }else if( 4 ){ 5 }else{ 6 };
9+
10+
The implementation is:
11+
For pairs of values (0,1 then 2,3 then 4,5 etc)
12+
If the first evaluates truthy, evaluate and return the second
13+
If the first evaluates falsy, jump to the next pair (e.g, 0,1 to 2,3)
14+
given one parameter, evaluate and return it. (it's an Else and all the If/ElseIf were false)
15+
given 0 parameters, return NULL (not great practice, but there was no Else)
16+
*/
17+
for(let i = 0; i < values.length - 1; i += 2) {
18+
if( truthy( apply(values[i], data) ) ) {
19+
return apply(values[i+1], data);
20+
}
21+
}
22+
23+
if(values.length === i+1) {
24+
return apply(values[i], data);
25+
}
26+
27+
return null;
28+
};
29+
30+
export default ifVisitor;

src/visitors/mapVisitor.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function mapVisitor(data, values) {
2+
const scopedData = apply(values[0], data);
3+
const scopedLogic = values[1];
4+
5+
if ( ! Array.isArray(scopedData)) {
6+
return [];
7+
}
8+
9+
return scopedData.map(function(datum){
10+
return apply(scopedLogic, datum);
11+
});
12+
}
13+
14+
export default mapVisitor;

src/visitors/noneVisitor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function noneVisitor(data, values) {
2+
const filtered = apply({'filter' : values}, data);
3+
4+
return filtered.length === 0;
5+
}
6+
7+
export default noneVisitor;

src/visitors/orVisitor.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import truthy from "../helpers/truthy";
2+
3+
function orVisitor(data, values, current) {
4+
for(let i=0; i < values.length; i++) {
5+
current = apply(values[i], data);
6+
if( truthy(current) ) {
7+
return current;
8+
}
9+
}
10+
return current; // Last
11+
}
12+
13+
export default orVisitor;

src/visitors/reduceVisitor.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function reduceVisitor(data, values) {
2+
const scopedData = apply(values[0], data);
3+
const scopedLogic = values[1];
4+
const initial = typeof values[2] !== 'undefined' ? values[2] : null;
5+
6+
if ( ! Array.isArray(scopedData)) {
7+
return initial;
8+
}
9+
10+
return scopedData.reduce(
11+
function(accumulator, current){
12+
return apply(
13+
scopedLogic,
14+
{'current':current, 'accumulator':accumulator}
15+
);
16+
},
17+
initial
18+
);
19+
}
20+
21+
export default reduceVisitor;

src/visitors/someVisitor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function someVisitor(data, values) {
2+
const filtered = apply({'filter' : values}, data);
3+
4+
return filtered.length > 0;
5+
}
6+
7+
export default someVisitor;

0 commit comments

Comments
 (0)