-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
34 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |