Skip to content

Commit

Permalink
feat: add simple query support
Browse files Browse the repository at this point in the history
  • Loading branch information
stanimirovv committed Nov 9, 2023
1 parent 0cbe751 commit fc17891
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stanimirovv/tsoogle",
"version": "1.1.0",
"version": "1.3.0",
"description": "Find functions or methods by approximate signature - return type, argument types or both. Supports optional arguments, rest arguments and partial type checking.",
"main": "dist/index.js",
"bin": "dist/index.js",
Expand Down
18 changes: 18 additions & 0 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { SearchQuery } from './searchQuery.type'
export function parse (input: string): SearchQuery {
input = input.replace(/\s+/g, '')

input = simpleQueryToFullQuery(input)

const fnType = input.split(':')[0]
const kind = calculateKind(fnType)

Expand Down Expand Up @@ -30,6 +32,22 @@ export function parseTypeQuery (input: string): string[] {
return input.replace('{', '').replace('}', '').split('&')
}

function simpleQueryToFullQuery (input: string): string {
if (!input.includes(':') && !input.includes(',')) {
input = `:${input}`
}

if (!input.includes('?') && !input.includes(',')) {
input = `${input}?`
}

if (input.includes(',') && !input.includes(':') && !input.includes('?')) {
return `:*?${input}`
}

return input
}

function calculateKind (match: string): 'function' | 'method' | 'both' {
if (match === '') {
return 'both'
Expand Down
45 changes: 45 additions & 0 deletions tests/lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,49 @@ describe('parseTypeString', () => {
parameterTypes: [['Product'], ['Service', 'number']]
})
})

it('should correctly parse "number"', () => {
const result = parse('number')
expect(result).toEqual({
kind: 'both',
returnTypes: ['number'],
parameterTypes: []
})
})

it('should correctly parse ":number"', () => {
const result = parse(':number')
expect(result).toEqual({
kind: 'both',
returnTypes: ['number'],
parameterTypes: []
})
})

it('should correctly parse "number?"', () => {
const result = parse(':number')
expect(result).toEqual({
kind: 'both',
returnTypes: ['number'],
parameterTypes: []
})
})

it('should correctly parse "number?number"', () => {
const result = parse('number?number')
expect(result).toEqual({
kind: 'both',
returnTypes: ['number'],
parameterTypes: [['number']]
})
})

it('should correctly parse "number,number"', () => {
const result = parse('number,number')
expect(result).toEqual({
kind: 'both',
returnTypes: ['*'],
parameterTypes: [['number'], ['number']]
})
})
})

0 comments on commit fc17891

Please sign in to comment.