https://github.com/twistezo/ifless
Conditional logic expressed using natural-language operators via JavaScript template literals.
https://github.com/twistezo/ifless
conditional logic template-literals
Last synced: about 1 month ago
JSON representation
Conditional logic expressed using natural-language operators via JavaScript template literals.
- Host: GitHub
- URL: https://github.com/twistezo/ifless
- Owner: twistezo
- License: mit
- Created: 2026-01-22T11:58:21.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2026-02-02T20:05:02.000Z (about 2 months ago)
- Last Synced: 2026-02-03T10:28:02.671Z (about 2 months ago)
- Topics: conditional, logic, template-literals
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@twistezo/ifless
- Size: 91.8 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## ifless



Conditional logic expressed using natural-language operators via JavaScript template literals.
Why? An experimental project for fun and learning, exploring alternative syntax for conditional logic.
## Examples
- [CodeSandbox](https://codesandbox.io/p/sandbox/ifless-fwwn3p) (check console output)
- [Local examples](./examples/)
- [Unit tests](./tests/)
- [Integration tests](./tests/integration/)
- [Tests with JS eval() as comparator](./tests/eval/)
## Usage
`npm install ifless` and use as follows:
```ts
import { when } from '@twistezo/ifless'
// or const { when } = require('@twistezo/ifless')
const user = { active: true }
const isAdmin = false
// classic
if (user.active && !isAdmin) {
console.log('condition met')
}
// with 'when'
when`${user.active} AND NOT ${isAdmin}`(() => {
console.log('condition met')
})
// with context and aliases
when.ctx({
admin: isAdmin,
userActive: user.active,
})`#userActive AND NOT #admin`(() => {
console.log('condition met')
})
// complex example
when.ctx({
a: true,
b: false,
c: false,
})`(#a AND #b) OR (NOT #c AND ${true})`(() => {
console.log('condition met')
})
```
## Limitations
Accepted:
- `${value}`
- `#alias`
- operators `AND`, `OR`, `NOT`
- parentheses `(`, `)`
Not accepted:
- other words
- empty parentheses
- double negations `NOT NOT`, `!!${value}`, `#{!!value}`
- aliases without value in context
- functions in context
## Features
- `AND`, `&&`
- Before:
```ts
if (a && b)
```
- After:
```ts
when.ctx({ a, b })`#a AND #b`(() => {})
```
- `OR`, `||`
- Before:
```ts
if (a || b)
```
- After:
```ts
when.ctx({ a, b })`#a OR #b`(() => {})
```
- `NOT`, `!`
- Before:
```ts
if (!a)
```
- After:
```ts
when.ctx({ a })`NOT #a`(() => {})
```
- Parentheses
- Before:
```ts
if ((a && b) || (!c && (d || e)))
```
- After:
```ts
when.ctx({ a, b, c, d, e })`(#a AND #b) OR (NOT #c AND (#d OR #e))`(() => {})
```
- Short-circuit evaluation
- Before:
```ts
if (a && expensiveCheck())
```
- After:
```ts
when`${a} AND ${() => expensiveCheck()}`(() => {})
```
- Double negation `!!`
- Before:
```ts
if (!!a && !!b)
```
- After:
```ts
when.ctx({ a, b })`#a AND #b`(() => {})
```
- `null`/`undefined` as `false`
- Before:
```ts
if (a != undefiend && b != null)
```
- After:
```ts
when.ctx({ a, b })`#a AND #b`(() => {})
```
## Development
```bash
bun run example # run examples
bun run build # build all formats
bun run test # run all tests
bun run lint # check lint
bun run lint:fix # fix lint & format
bun run typecheck # check types
bunx npm login # login to npm
bun publish # publish to npm
```