Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Falsy truthy and fix doov function #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/doov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function matchAny(...values: BooleanFunction[]): BooleanFunction {
return new BooleanFunction(new NaryMetadata(values.map(value => value.metadata), MATCH_ANY), (obj, ctx) => {
return values.some(value => {
const v = value.get(obj, ctx);
return v != null ? v : false;
return Boolean(v);
});
});
}
Expand All @@ -140,7 +140,7 @@ export function matchAll(...values: BooleanFunction[]): BooleanFunction {
return new BooleanFunction(new NaryMetadata(values.map(value => value.metadata), MATCH_ALL), (obj, ctx) => {
return values.every(value => {
const v = value.get(obj, ctx);
return v != null ? v : false;
return Boolean(v);
});
});
}
Expand All @@ -149,7 +149,7 @@ export function matchNone(...values: BooleanFunction[]): BooleanFunction {
return new BooleanFunction(new NaryMetadata(values.map(value => value.metadata), NONE_MATCH), (obj, ctx) => {
return values.every(value => {
const v = value.get(obj, ctx);
return v != null ? !v : false;
return !Boolean(v);
});
});
}
Expand All @@ -158,7 +158,7 @@ export function count(...values: BooleanFunction[]): NumberFunction {
return new NumberFunction(new NaryMetadata(values.map(value => value.metadata), COUNT), (obj, ctx) => {
return values.filter(value => {
const v = value.get(obj, ctx);
return v != null ? v : false;
return Boolean(v);
}).length;
});
}
Expand All @@ -167,7 +167,7 @@ export function sum(...values: NumberFunction[]): NumberFunction {
return new NumberFunction(new NaryMetadata(values.map(value => value.metadata), SUM), (obj, ctx) => {
return values.reduce((previous, value) => {
const v = value.get(obj, ctx);
return v != null ? previous + v : previous;
return v !== undefined && v !== null && !isNaN(v) ? previous + v : previous;
}, 0);
});
}
Expand Down
16 changes: 15 additions & 1 deletion src/dsl/lang/BooleanFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { condition, Function } from './Function';
import { Context } from '../Context';
import { ContextAccessor } from '../ContextAccessor';
import { UnaryMetadata } from '../meta/UnaryMetadata';
import { AND, NOT, OR } from './DefaultOperators';
import { AND, IS_FALSY, IS_TRUTHY, NOT, OR } from './DefaultOperators';
import { BinaryMetadata } from '../meta/BinaryMetadata';
import { ValueMetadata } from '../meta/ValueMetadata';

Expand All @@ -11,6 +11,20 @@ export class BooleanFunction extends Function<boolean> {
return new BooleanFunction(accessor.metadata, accessor.get, accessor.set);
}

public isFalsy(): BooleanFunction {
return new BooleanFunction(
new UnaryMetadata(this.metadata, IS_FALSY),
condition(this, false, (expr: boolean) => !Boolean(expr), true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

);
}

public isTruthy(): BooleanFunction {
return new BooleanFunction(
new UnaryMetadata(this.metadata, IS_TRUTHY),
condition(this, false, (expr: boolean) => Boolean(expr), false)
);
}

public not(): BooleanFunction {
return new BooleanFunction(
new UnaryMetadata(this.metadata, NOT),
Expand Down
2 changes: 2 additions & 0 deletions src/dsl/lang/DefaultOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const VALIDATE: Operator = { readable: 'validate' };
export const NOT: Operator = { readable: 'not' };
export const AND: Operator = { readable: 'and' };
export const OR: Operator = { readable: 'or' };
export const IS_FALSY: Operator = { readable: 'is falsy' };
export const IS_TRUTHY: Operator = { readable: 'is truthy' };
export const THEN: Operator = { readable: 'then' };
export const ELSE: Operator = { readable: 'else' };
export const FUNCTION: Operator = { readable: 'function' };
Expand Down
23 changes: 23 additions & 0 deletions test/dsl/lang/BooleanFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const falseFunction = DOOV.lift(BooleanFunction, false);
const nullField = DOOV.lift(BooleanFunction, null as any);
const trueField = DOOV.boolean(DOOV.field<boolean, Model>('user', 'b'));
const undefinedField = DOOV.boolean(DOOV.field<boolean, Model>('user', 'a'));
const falsyField = DOOV.boolean(DOOV.field<boolean, Model>('user', 'birth'));

beforeEach(() => {
model = new Model();
user = new User(1);
user.name = 'test';
user.b = true;
user.birth = undefined;
model.user = user;
});

Expand Down Expand Up @@ -210,4 +212,25 @@ describe('boolean function with left null with short circuit', () => {
it('not null', () => {
expect(nullField.not().get(model, new DefaultContext(true))).toEqual(false);
});

it('is falsy', () => {
expect(falsyField.isFalsy().get(model, new DefaultContext(true))).toEqual(true);
expect(
trueFunction
.not()
.isFalsy()
.get(model, new DefaultContext(true))
).toEqual(true);
});

it('is truthy', () => {
const context = new DefaultContext(true);
expect(trueFunction.isTruthy().get(model, new DefaultContext(true))).toEqual(true);
expect(
falsyField
.not()
.isTruthy()
.get(model, context)
).toEqual(true);
});
});