Skip to content

Commit

Permalink
feat: add support for nested arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Dec 9, 2018
1 parent 148a558 commit 86773fb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
]
},
"dependencies": {
"ajv": "^6.5.5",
"ajv": "^6.6.1",
"es6-error": "^4.1.1",
"nearley": "^2.15.1",
"roarr": "^2.11.8"
"roarr": "^2.11.9"
},
"description": "A declarative function composition and evaluation engine.",
"devDependencies": {
"@babel/cli": "^7.1.5",
"@babel/core": "^7.1.6",
"@babel/node": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/node": "^7.2.0",
"@babel/plugin-transform-flow-strip-types": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/register": "^7.0.0",
"@babel/types": "^7.1.6",
"@babel/types": "^7.2.0",
"ava": "^1.0.0-rc.2",
"babel-plugin-istanbul": "^5.1.0",
"babel-plugin-transform-export-default-name": "^2.0.4",
"coveralls": "^3.0.2",
"eslint": "^5.9.0",
"eslint-config-canonical": "^13.0.0",
"flow-bin": "^0.86.0",
"eslint": "^5.10.0",
"eslint-config-canonical": "^15.0.1",
"flow-bin": "^0.87.0",
"flow-copy-source": "^2.0.2",
"husky": "^1.1.4",
"husky": "^1.2.0",
"nyc": "^13.1.0",
"semantic-release": "^15.12.0",
"semantic-release": "^15.12.4",
"sinon": "^7.1.1"
},
"engines": {
Expand Down
8 changes: 7 additions & 1 deletion src/factories/createQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const createQuery = (denormalizedQuery: DenormalizedQueryType): QueryType => {
operator: 'PIPELINE'
});
}
} else if (Array.isArray(maybeExpression)) {
const children = createQuery(maybeExpression);

commands.push({
margeChildren: children
});
} else {
const adoption = maybeExpression;

Expand All @@ -49,7 +55,7 @@ const createQuery = (denormalizedQuery: DenormalizedQueryType): QueryType => {
}

commands.push({
children
namedChildren: children
});
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ const play = (instructions, startValue, subroutines, bindle: Object, handleResul

const nextOperator: OperatorType | null = instructions[index] && instructions[index].operator || null;

if (instruction.children) {
if (instruction.namedChildren) {
const children = {};

const childrenNames = Object.keys(instruction.children);
const childrenNames = Object.keys(instruction.namedChildren);

for (const childName of childrenNames) {
children[childName] = play(instruction.children[childName], result, subroutines, bindle, handleResult);
children[childName] = play(instruction.namedChildren[childName], result, subroutines, bindle, handleResult);
}

const remainingInstructions = instructions.slice(index);

return play(remainingInstructions, children['0'] ? children['0'] : children, subroutines, bindle, handleResult);
return play(remainingInstructions, children, subroutines, bindle, handleResult);
} else if (instruction.margeChildren) {
let value = result;

value = play(instruction.margeChildren, value, subroutines, bindle, handleResult);

const remainingInstructions = instructions.slice(index);

return play(remainingInstructions, value, subroutines, bindle, handleResult);
}

const lastResult = result;
Expand Down
15 changes: 10 additions & 5 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export type OperatorType = 'PIPELINE' | 'AGGREGATE_PIPELINE';

export type DenormalizedQueryType =
QueryInstructionType |
$ReadOnlyArray<QueryInstructionType | QueryChildrenType> |
QueryChildrenType;
QueryChildrenType |
$ReadOnlyArray<DenormalizedQueryType>;

export type SubroutineInstructionType = {|
+subroutine: string,
Expand All @@ -24,14 +24,19 @@ export type OperatorInstructionType = {|
+operator: OperatorType
|};

export type AdoptionInstructionType = {|
+children: {
export type MergeAdoptionInstructionType = {|
+margeChildren: QueryType
|};

export type NamedAdoptionInstructionType = {|
+namedChildren: {
+[key: string]: QueryType
}
|};

export type InstructionType =
AdoptionInstructionType |
MergeAdoptionInstructionType |
NamedAdoptionInstructionType |
SubroutineInstructionType |
OperatorInstructionType;

Expand Down
8 changes: 4 additions & 4 deletions test/pianola/factories/createQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('converts simple object command into "adopt" subroutine (string expression)
operator: 'PIPELINE'
},
{
children: {
namedChildren: {
baz: [
{
subroutine: 'baz0',
Expand Down Expand Up @@ -140,7 +140,7 @@ test('converts simple object command into "adopt" subroutine (string expression)
operator: 'PIPELINE'
},
{
children: {
namedChildren: {
baz: [
{
subroutine: 'baz',
Expand All @@ -149,7 +149,7 @@ test('converts simple object command into "adopt" subroutine (string expression)
],
qux: [
{
children: {
namedChildren: {
quux: [
{
subroutine: 'quux',
Expand Down Expand Up @@ -196,7 +196,7 @@ test('converts simple object command into "adopt" subroutine (array expression)'
operator: 'PIPELINE'
},
{
children: {
namedChildren: {
baz: [
{
subroutine: 'baz0',
Expand Down

0 comments on commit 86773fb

Please sign in to comment.