-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
72 additions
and
7 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
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { describe, it} from 'node:test'; | ||
import assert from 'node:assert'; | ||
import { parseQuery } from '../parseQuery.js'; | ||
|
||
describe('parseQuery', () => { | ||
it('should return q same as raw', () => { | ||
const { q, lang } = parseQuery('foo'); | ||
assert.strictEqual(q, 'foo'); | ||
assert.strictEqual(lang, undefined); | ||
}); | ||
it('should return q undefined when raw is just whitespaces', () => { | ||
const { q, lang } = parseQuery(' '); | ||
assert.strictEqual(q, undefined); | ||
assert.strictEqual(lang, undefined); | ||
}); | ||
it('should return q and lang when raw has lang', () => { | ||
const {q, lang} = parseQuery('foo lang:en'); | ||
assert.strictEqual(q, 'foo'); | ||
assert.strictEqual(lang, 'en'); | ||
}); | ||
it('should return q and lang when raw has lang', () => { | ||
const {q, lang} = parseQuery('lang:en lang '); | ||
assert.strictEqual(q, 'lang'); | ||
assert.strictEqual(lang, 'en'); | ||
}); | ||
it('should return q and lang when raw has lang', () => { | ||
const {q, lang} = parseQuery('lang:en'); | ||
assert.strictEqual(q, ''); | ||
assert.strictEqual(lang, 'en'); | ||
}); | ||
|
||
it('should return undefined q when no query at all', () => { | ||
const { q, lang } = parseQuery(); | ||
assert.strictEqual(q, undefined); | ||
assert.strictEqual(lang, undefined); | ||
}); | ||
|
||
it('should return empty q and lang', () => { | ||
const { q, lang} = parseQuery('lang:pl');; | ||
assert.strictEqual(q, ''); | ||
assert.strictEqual(lang, 'pl'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const parseQuery = (rawQuery) => { | ||
if (!rawQuery) { | ||
return { q: undefined, lang: undefined }; | ||
} | ||
|
||
const trimmed = rawQuery.trim(); | ||
let q = trimmed.length > 0 ? trimmed : undefined; | ||
|
||
if (!q) { | ||
return { q }; | ||
} | ||
|
||
const lang = trimmed.match(/lang:(\w{2})/)?.[1]; | ||
if (lang) { | ||
q = q.replace(/lang:\w{2}/, '').trim(); | ||
} | ||
|
||
return { q, lang }; | ||
}; |
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
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