[Enterprise Search] Add parseQueryParams helper (#83750) (#83842)

* [Enterprise Search] Add parseQueryParams helper

This PR migrates part of the ent-search queryParams util, `parseQueryParams` for use in Workplace Search.

`setQueryParams` was no a part of this PR because it is only used one time in App Search and a better alternative might be available for that use-case

* Remove mock

* Actually test functionality of query-string

* Add test for array

* Better test name
This commit is contained in:
Scotty Bollinger 2020-11-19 14:59:56 -06:00 committed by GitHub
parent 064fd8ef84
commit 02706ffaf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View file

@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
export { parseQueryParams } from './query_params';

View file

@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { parseQueryParams } from './';
describe('parseQueryParams', () => {
it('parse query strings', () => {
expect(parseQueryParams('?foo=bar')).toEqual({ foo: 'bar' });
expect(parseQueryParams('?foo[]=bar&foo[]=baz')).toEqual({ foo: ['bar', 'baz'] });
});
});

View file

@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import queryString from 'query-string';
export const parseQueryParams = (search: string) =>
queryString.parse(search, { arrayFormat: 'bracket' });