Skip to content

Commit

Permalink
Added _calcChainableHelper for mvOverlapExpression and mvContainsExpr…
Browse files Browse the repository at this point in the history
…ession (#279)

* added _calcChainableHelper for mvOverlapExpression and mvContainsExpression

* added mvOverlapExpression test

* previous logic for mvContains and mvOverlap was flipped, corrected now; added mvContainsExpression test

* review feedback
  • Loading branch information
mujinss authored Feb 6, 2023
1 parent 2877bc3 commit 3c9160b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/expressions/mvContainsExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { generalArraysEqual } from 'immutable-class';

import { PlywoodValue } from '../datatypes/index';
import { SQLDialect } from '../dialect/baseDialect';

import { ChainableExpression, Expression, ExpressionJS, ExpressionValue } from './baseExpression';
Expand Down Expand Up @@ -58,6 +59,16 @@ export class MvContainsExpression extends ChainableExpression {
return this.mvArray;
}

protected _calcChainableHelper(operandValue: any): PlywoodValue {
const operandArray =
typeof operandValue === 'string'
? [operandValue]
: Array.isArray(operandValue)
? operandValue
: [];
return operandArray.every(element => this.mvArray.includes(element));
}

protected _getSQLChainableHelper(dialect: SQLDialect, operandSQL: string): string {
return dialect.mvContainsExpression(operandSQL, this.mvArray);
}
Expand Down
11 changes: 11 additions & 0 deletions src/expressions/mvOverlapExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { generalArraysEqual } from 'immutable-class';

import { PlywoodValue } from '../datatypes/index';
import { SQLDialect } from '../dialect/baseDialect';

import { ChainableExpression, Expression, ExpressionJS, ExpressionValue } from './baseExpression';
Expand Down Expand Up @@ -58,6 +59,16 @@ export class MvOverlapExpression extends ChainableExpression {
return this.mvArray;
}

protected _calcChainableHelper(operandValue: any): PlywoodValue {
const operandArray =
typeof operandValue === 'string'
? [operandValue]
: Array.isArray(operandValue)
? operandValue
: null;
return operandArray !== null && operandArray.some(element => this.mvArray.includes(element));
}

protected _getSQLChainableHelper(dialect: SQLDialect, operandSQL: string): string {
return dialect.mvOverlapExpression(operandSQL, this.mvArray);
}
Expand Down
48 changes: 48 additions & 0 deletions test/expression/mvContainsExpression.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2012-2015 Metamarkets Group Inc.
* Copyright 2015-2020 Imply Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const { expect } = require('chai');

const plywood = require('../plywood');

const { Expression } = plywood;

describe('MvContainsExpression', () => {
describe('_calcChainableHelper', () => {
it('works with single string', () => {
const mvContainsExpression = Expression._.mvContains([
'thing',
'otherThing',
'otherOtherThing',
]);

expect(mvContainsExpression._calcChainableHelper('thing')).to.equal(true);
expect(mvContainsExpression._calcChainableHelper('not a thing')).to.equal(false);
});

it('works with array of strings', () => {
const mvContainsExpression = Expression._.mvContains([
'thing',
'otherThing',
'otherOtherThing',
]);

expect(mvContainsExpression._calcChainableHelper(['thing', 'otherThing'])).to.equal(true);
expect(mvContainsExpression._calcChainableHelper(['thing', 'not a thing'])).to.equal(false);
});
});
});
49 changes: 49 additions & 0 deletions test/expression/mvOverlapExpression.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2012-2015 Metamarkets Group Inc.
* Copyright 2015-2020 Imply Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const { expect } = require('chai');

const plywood = require('../plywood');

const { Expression } = plywood;

describe('MvOverlapExpression', () => {
describe('_calcChainableHelper', () => {
it('works with single string', () => {
const mvOverlapExpression = Expression._.mvOverlap([
'thing',
'otherThing',
'otherOtherThing',
]);

expect(mvOverlapExpression._calcChainableHelper('thing')).to.equal(true);
expect(mvOverlapExpression._calcChainableHelper('not a thing')).to.equal(false);
});

it('works with array of strings', () => {
const mvOverlapExpression = Expression._.mvOverlap([
'thing',
'otherThing',
'otherOtherThing',
]);

expect(mvOverlapExpression._calcChainableHelper(['thing', 'otherThing'])).to.equal(true);
expect(mvOverlapExpression._calcChainableHelper(['not a thing'])).to.equal(false);
expect(mvOverlapExpression._calcChainableHelper(['thing', 'not a thing'])).to.equal(true);
});
});
});

0 comments on commit 3c9160b

Please sign in to comment.