Skip to content

Commit

Permalink
make and and or helpers class based
Browse files Browse the repository at this point in the history
that way, we preserve semantic of them lazily evaluating arguments and able to accept generics
  • Loading branch information
Sergey Astapov committed Aug 31, 2023
1 parent b46c822 commit 2042def
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
23 changes: 17 additions & 6 deletions packages/ember-truth-helpers/src/helpers/and.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import Helper from '@ember/component/helper';
import truthConvert from '../utils/truth-convert.ts';
import type { MaybeTruth } from '../utils/truth-convert.ts';

export default function and<T extends MaybeTruth[]>(...params: [...T]) {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === false) {
return params[i] as boolean;
interface AndSignature<T extends unknown[]> {
Args: {
Positional: T;
};
Return: T[number];
}

export default class AndHelper<T extends unknown[]> extends Helper<
AndSignature<T>
> {
public compute(params: T): T[number] {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === false) {
return params[i];
}
}
return params[params.length - 1];
}
return params[params.length - 1] as boolean;
}
41 changes: 17 additions & 24 deletions packages/ember-truth-helpers/src/helpers/or.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import truthConvert from '../utils/truth-convert.ts';
import type { MaybeTruth } from '../utils/truth-convert.ts';
import Helper from '@ember/component/helper';

export default function or<T extends MaybeTruth[]>(...params: [...T]) {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === true) {
return params[i] as FirstNonFalsy<T>;
interface OrSignature<T extends unknown[]> {
Args: {
Positional: T;
};
Return: T[number];
}

export default class OrHelper<T extends unknown[]> extends Helper<
OrSignature<T>
> {
public compute(params: T): T[number] {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === true) {
return params[i];
}
}
return params[params.length - 1];
}
return params[params.length - 1] as Last<T>;
}

type FirstNonFalsy<T extends any[]> = T extends [infer K]
? K
: T extends [infer A, ...infer R]
? Truthy<A> extends true
? A
: FirstNonFalsy<R>
: never;

type Last<T extends any[]> = T extends [infer K]
? K
: T extends [...infer Q, infer L]
? L
: never;

type Truthy<T> = T extends Falsy ? false : true;

type Falsy = false | 0 | '' | null | undefined | { isTruthy: false } | [];
7 changes: 0 additions & 7 deletions packages/ember-truth-helpers/src/helpers/xor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import truthConvert from '../utils/truth-convert.ts';

export interface XorSignature {
Args: {
Positional: [unknown, unknown];
};
Return: boolean;
}

export default function xor(left: unknown, right: unknown) {
return truthConvert(left) !== truthConvert(right);
}

0 comments on commit 2042def

Please sign in to comment.