Skip to content

Commit

Permalink
feat: add and and or statment constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
johngeorgewright committed Sep 21, 2023
1 parent be2aee2 commit f3981fb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/query/and.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Statement } from './condition'
import { GTStatement } from './gt'
import { Comparable } from './is'
import { LTStatement } from './lt'
import { NotStatement } from './not'
import { OrStatement } from './or'

type AndStatementValueItem<T extends Comparable> =
| NotStatement<T>
| GTStatement
| LTStatement
| OrStatement<T>

type AndStatementValue<T extends Comparable> = AndStatementValueItem<T>[]

export class AndStatement<T extends Comparable> extends Statement<
AndStatementValue<T>
> {
override statement(prop: string): string {
return `(${this.value
.map((value) => `${value.statement(prop)}`)
.join(' AND ')})`
}
}

export function And<T extends Comparable>(values: AndStatementValue<T>) {
return new AndStatement(values)
}
2 changes: 2 additions & 0 deletions src/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export * from './and'
export * from './gt'
export * from './in'
export * from './is'
export * from './like'
export * from './lt'
export * from './not'
export * from './null'
export * from './or'
export * from './pql'
35 changes: 35 additions & 0 deletions src/query/or.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { AndStatement } from './and'
import { Statement } from './condition'
import { GTStatement } from './gt'
import { Comparable, Is, IsStatement } from './is'
import { LTStatement } from './lt'
import { NotStatement } from './not'

type OrStatementValueItem<T extends Comparable> =
| IsStatement<T>
| NotStatement<T>
| GTStatement
| LTStatement
| AndStatement<T>

type OrStatementValue<T extends Comparable> = OrStatementValueItem<T>[]

type OrValue<T extends Comparable> = (OrStatementValueItem<T> | T)[]

export class OrStatement<T extends Comparable> extends Statement<
OrStatementValue<T>
> {
constructor(values: OrValue<T>) {
super(
values.map((value) => (value instanceof Statement ? value : Is(value))),
)
}

override statement(prop: string): string {
return `(${this.value.map((value) => value.statement(prop)).join(' OR ')})`
}
}

export function Or<T extends Comparable>(values: OrValue<T>) {
return new OrStatement(values)
}
20 changes: 19 additions & 1 deletion test/pql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GT, In, Like, Not, Null, pql } from '../src'
import { And, GT, In, LT, Like, Not, Null, Or, pql } from '../src'
import { Creatives } from '../src/service/v202308/creativeservice'
import { LineItems } from '../src/service/v202308/lineitemservice'

Expand Down Expand Up @@ -127,6 +127,14 @@ test('ands', () => {
},
}),
).toBe("WHERE name = 'foo' AND previewUrl = 'bar'")

expect(
pql<LineItems>({
where: {
creationDateTime: And([GT('2001'), LT('2022')]),
},
}),
).toBe("WHERE (creationDateTime > '2001' AND creationDateTime < '2022')")
})

test('ors', () => {
Expand All @@ -146,4 +154,14 @@ test('ors', () => {
).toBe(
"WHERE (name = 'foo' AND previewUrl = 'bar') OR (id = 123 AND advertiserId = 333)",
)

expect(
pql<LineItems>({
where: {
creationDateTime: Or(['2023', And([GT('2001'), LT('2022')])]),
},
}),
).toBe(
"WHERE (creationDateTime = '2023' OR (creationDateTime > '2001' AND creationDateTime < '2022'))",
)
})

0 comments on commit f3981fb

Please sign in to comment.