chore: run prettier
This commit is contained in:
parent
9cdbbc3a00
commit
155d7bb876
76 changed files with 788 additions and 550 deletions
|
@ -1,79 +1,86 @@
|
|||
import extractSearchFilters from '@/helpers/extractSearchFilters';
|
||||
|
||||
type TestCase = [ string, 0 | Record<string, string[]> ];
|
||||
type TestCase = [string, 0 | Record<string, string[]>];
|
||||
|
||||
describe('@/helpers/extractSearchFilters.ts', function () {
|
||||
const cases: TestCase[] = [
|
||||
[ '', {} ],
|
||||
[ 'hello world', {} ],
|
||||
[ 'bar:xyz foo:abc', { bar: [ 'xyz' ], foo: [ 'abc' ] } ],
|
||||
[ 'hello foo:abc', { foo: [ 'abc' ] } ],
|
||||
[ 'hello foo:abc world another bar:xyz hodor', { foo: [ 'abc' ], bar: [ 'xyz' ] } ],
|
||||
[ 'foo:1 foo:2 foo: 3 foo:4', { foo: [ '1', '2', '4' ] } ],
|
||||
[ ' foo:123 foo:bar:123 foo: foo:string', { foo: [ '123', 'bar:123', 'string' ] } ],
|
||||
[ 'foo:1 bar:2 baz:3', { foo: [ '1' ], bar: [ '2' ] } ],
|
||||
[ 'hello "world this" is quoted', {} ],
|
||||
[ 'hello "world foo:123 is" quoted', {} ],
|
||||
[ 'hello foo:"this is quoted" bar:"this \\"is deeply\\" quoted" world foo:another', {
|
||||
foo: [ 'this is quoted', 'another' ],
|
||||
bar: [ 'this "is deeply" quoted' ],
|
||||
} ],
|
||||
['', {}],
|
||||
['hello world', {}],
|
||||
['bar:xyz foo:abc', { bar: ['xyz'], foo: ['abc'] }],
|
||||
['hello foo:abc', { foo: ['abc'] }],
|
||||
['hello foo:abc world another bar:xyz hodor', { foo: ['abc'], bar: ['xyz'] }],
|
||||
['foo:1 foo:2 foo: 3 foo:4', { foo: ['1', '2', '4'] }],
|
||||
[' foo:123 foo:bar:123 foo: foo:string', { foo: ['123', 'bar:123', 'string'] }],
|
||||
['foo:1 bar:2 baz:3', { foo: ['1'], bar: ['2'] }],
|
||||
['hello "world this" is quoted', {}],
|
||||
['hello "world foo:123 is" quoted', {}],
|
||||
[
|
||||
'hello foo:"this is quoted" bar:"this \\"is deeply\\" quoted" world foo:another',
|
||||
{
|
||||
foo: ['this is quoted', 'another'],
|
||||
bar: ['this "is deeply" quoted'],
|
||||
},
|
||||
],
|
||||
];
|
||||
|
||||
it.each(cases)('should return expected filters: [%s]', function (input, output) {
|
||||
expect(extractSearchFilters(input, [ 'foo', 'bar' ])).toStrictEqual({
|
||||
expect(extractSearchFilters(input, ['foo', 'bar'])).toStrictEqual({
|
||||
filters: output,
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow modification of the default parameter', function () {
|
||||
expect(extractSearchFilters('hello world', [ 'foo' ], { defaultFilter: 'default_param', returnUnmatched: true })).toStrictEqual({
|
||||
expect(
|
||||
extractSearchFilters('hello world', ['foo'], { defaultFilter: 'default_param', returnUnmatched: true }),
|
||||
).toStrictEqual({
|
||||
filters: {
|
||||
default_param: [ 'hello world' ],
|
||||
default_param: ['hello world'],
|
||||
},
|
||||
});
|
||||
|
||||
expect(extractSearchFilters('foo:123 bar', [ 'foo' ], { defaultFilter: 'default_param' })).toStrictEqual({
|
||||
expect(extractSearchFilters('foo:123 bar', ['foo'], { defaultFilter: 'default_param' })).toStrictEqual({
|
||||
filters: {
|
||||
foo: [ '123' ],
|
||||
foo: ['123'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[ '', {} ],
|
||||
[ 'hello world', { '*': [ 'hello world' ] } ],
|
||||
[ 'hello world foo:123 bar:456', { foo: [ '123' ], bar: [ '456' ], '*': [ 'hello world' ] } ],
|
||||
[ 'hello world foo:123 another string', { foo: [ '123' ], '*': [ 'hello world another string' ] } ],
|
||||
['', {}],
|
||||
['hello world', { '*': ['hello world'] }],
|
||||
['hello world foo:123 bar:456', { foo: ['123'], bar: ['456'], '*': ['hello world'] }],
|
||||
['hello world foo:123 another string', { foo: ['123'], '*': ['hello world another string'] }],
|
||||
])('should return unmatched parameters: %s', function (input, output) {
|
||||
expect(extractSearchFilters(input, [ 'foo', 'bar' ], { returnUnmatched: true })).toStrictEqual({
|
||||
expect(extractSearchFilters(input, ['foo', 'bar'], { returnUnmatched: true })).toStrictEqual({
|
||||
filters: output,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[ '', {} ],
|
||||
[ 'hello world', { '*': [ 'hello', 'world' ] } ],
|
||||
[ 'hello world foo:123 bar:456', { foo: [ '123' ], bar: [ '456' ], '*': [ 'hello', 'world' ] } ],
|
||||
[ 'hello world foo:123 another string', { foo: [ '123' ], '*': [ 'hello', 'world', 'another', 'string' ] } ],
|
||||
['', {}],
|
||||
['hello world', { '*': ['hello', 'world'] }],
|
||||
['hello world foo:123 bar:456', { foo: ['123'], bar: ['456'], '*': ['hello', 'world'] }],
|
||||
['hello world foo:123 another string', { foo: ['123'], '*': ['hello', 'world', 'another', 'string'] }],
|
||||
])('should split unmatched parameters: %s', function (input, output) {
|
||||
expect(extractSearchFilters(input, [ 'foo', 'bar' ], {
|
||||
returnUnmatched: true,
|
||||
splitUnmatched: true,
|
||||
})).toStrictEqual({
|
||||
expect(
|
||||
extractSearchFilters(input, ['foo', 'bar'], {
|
||||
returnUnmatched: true,
|
||||
splitUnmatched: true,
|
||||
}),
|
||||
).toStrictEqual({
|
||||
filters: output,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([ true, false ])('should return the unsplit value (splitting: %s)', function (split) {
|
||||
const extracted = extractSearchFilters('hello foo:123 bar:123 world', [ 'foo' ], {
|
||||
it.each([true, false])('should return the unsplit value (splitting: %s)', function (split) {
|
||||
const extracted = extractSearchFilters('hello foo:123 bar:123 world', ['foo'], {
|
||||
returnUnmatched: true,
|
||||
splitUnmatched: split,
|
||||
});
|
||||
expect(extracted).toStrictEqual({
|
||||
filters: {
|
||||
foo: [ '123' ],
|
||||
'*': split ? [ 'hello', 'bar:123', 'world' ] : [ 'hello bar:123 world' ],
|
||||
foo: ['123'],
|
||||
'*': split ? ['hello', 'bar:123', 'world'] : ['hello bar:123 world'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,13 +7,13 @@ interface Options<D extends string = string> {
|
|||
returnUnmatched?: boolean;
|
||||
}
|
||||
|
||||
const extractSearchFilters = <T extends string, D extends string = '*'> (
|
||||
const extractSearchFilters = <T extends string, D extends string = '*'>(
|
||||
str: string,
|
||||
params: Readonly<T[]>,
|
||||
options?: Options<D>,
|
||||
): QueryBuilderParams<T> | QueryBuilderParams<D> | QueryBuilderParams<T & D> => {
|
||||
const opts: Required<Options<D>> = {
|
||||
defaultFilter: options?.defaultFilter || '*' as D,
|
||||
defaultFilter: options?.defaultFilter || ('*' as D),
|
||||
splitUnmatched: options?.splitUnmatched || false,
|
||||
returnUnmatched: options?.returnUnmatched || false,
|
||||
};
|
||||
|
@ -30,12 +30,12 @@ const extractSearchFilters = <T extends string, D extends string = '*'> (
|
|||
} else if (!params.includes(filter)) {
|
||||
unmatched.push(segment);
|
||||
} else {
|
||||
filters.set(filter, [ ...(filters.get(filter) || []), value ]);
|
||||
filters.set(filter, [...(filters.get(filter) || []), value]);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.returnUnmatched && str.trim().length > 0) {
|
||||
filters.set(opts.defaultFilter as any, opts.splitUnmatched ? unmatched : [ unmatched.join(' ') ]);
|
||||
filters.set(opts.defaultFilter as any, opts.splitUnmatched ? unmatched : [unmatched.join(' ')]);
|
||||
}
|
||||
|
||||
if (filters.size === 0) {
|
||||
|
|
|
@ -2,14 +2,14 @@ import splitStringWhitespace from '@/helpers/splitStringWhitespace';
|
|||
|
||||
describe('@/helpers/splitStringWhitespace.ts', function () {
|
||||
it.each([
|
||||
[ '', [] ],
|
||||
[ 'hello world', [ 'hello', 'world' ] ],
|
||||
[ ' hello world ', [ 'hello', 'world' ] ],
|
||||
[ 'hello123 world 123 $$ s ', [ 'hello123', 'world', '123', '$$', 's' ] ],
|
||||
[ 'hello world! how are you?', [ 'hello', 'world!', 'how', 'are', 'you?' ] ],
|
||||
[ 'hello "foo bar baz" world', [ 'hello', 'foo bar baz', 'world' ] ],
|
||||
[ 'hello "foo \\"bar bar \\" baz" world', [ 'hello', 'foo "bar bar " baz', 'world' ] ],
|
||||
[ 'hello "foo "bar baz" baz" world', [ 'hello', 'foo bar', 'baz baz', 'world' ] ],
|
||||
['', []],
|
||||
['hello world', ['hello', 'world']],
|
||||
[' hello world ', ['hello', 'world']],
|
||||
['hello123 world 123 $$ s ', ['hello123', 'world', '123', '$$', 's']],
|
||||
['hello world! how are you?', ['hello', 'world!', 'how', 'are', 'you?']],
|
||||
['hello "foo bar baz" world', ['hello', 'foo bar baz', 'world']],
|
||||
['hello "foo \\"bar bar \\" baz" world', ['hello', 'foo "bar bar " baz', 'world']],
|
||||
['hello "foo "bar baz" baz" world', ['hello', 'foo bar', 'baz baz', 'world']],
|
||||
])('should handle string: %s', function (input, output) {
|
||||
expect(splitStringWhitespace(input)).toStrictEqual(output);
|
||||
});
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
*/
|
||||
export default (str: string): string[] => {
|
||||
let quoted = false;
|
||||
const parts = [ '' ] as string[];
|
||||
const parts = [''] as string[];
|
||||
|
||||
for (const char of (str.trim().match(/\\?.|^$/g) || [])) {
|
||||
for (const char of str.trim().match(/\\?.|^$/g) || []) {
|
||||
if (char === '"') {
|
||||
quoted = !quoted;
|
||||
} else if (!quoted && char === ' ') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue