Skip to content

Commit

Permalink
feat(typeEvaluator): add support for string::startsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed May 7, 2024
1 parent af958db commit 6b51180
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
type: 'string',
}
}
case 'string.startsWith': {
const strTypeNode = walk({node: node.args[0], scope})
const prefixTypeNode = walk({node: node.args[1], scope})
return mapConcrete(strTypeNode, scope, (strNode) => {
if (strNode.type !== 'string') {
return {type: 'null'}
}

return mapConcrete(prefixTypeNode, scope, (prefixNode) => {
if (prefixNode.type !== 'string') {
return {type: 'null'}
}

return {type: 'boolean'}
})
})
}
default: {
return {type: 'unknown'}
}
Expand Down
37 changes: 37 additions & 0 deletions test/typeEvaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,43 @@ t.test('function: references', (t) => {
t.end()
})

t.test('function: string::startsWith', (t) => {
const query = `*[_type == "author" && string::startsWith(name, "george")] {
"stringStartsWith": string::startsWith(name, "george"),
"numberStartsWith": string::startsWith(age, "foo"),
"stringStartsWithNumber": string::startsWith(name, 123),
}`
const ast = parse(query)
const res = typeEvaluate(ast, schemas)
t.strictSame(res, {
type: 'array',
of: {
type: 'object',
attributes: {
stringStartsWith: {
type: 'objectAttribute',
value: {
type: 'boolean',
},
},
numberStartsWith: {
type: 'objectAttribute',
value: {
type: 'null',
},
},
stringStartsWithNumber: {
type: 'objectAttribute',
value: {
type: 'null',
},
},
},
},
})
t.end()
})

function findSchemaType(name: string): TypeNode {
const type = schemas.find((s) => s.name === name)
if (!type) {
Expand Down

0 comments on commit 6b51180

Please sign in to comment.