-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into priyanshu-kun/#1608
Signed-off-by: priyanshu-kun <priyanshushrama709@gmail.com>
- Loading branch information
Showing
11 changed files
with
149 additions
and
33 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
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
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,48 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import parseQuery from './parseQuery'; | ||
|
||
describe('parseQuery', () => { | ||
it('should parse a query string with no options', () => { | ||
const queryString = 'foo=bar&baz=qux'; | ||
const expected = { foo: 'bar', baz: 'qux' }; | ||
expect(parseQuery(queryString)).toEqual(expected); | ||
}); | ||
|
||
it('should parse a query string with options', () => { | ||
const queryString = 'foo[]=bar&foo[]'; | ||
const options = { arrayFormat: 'bracket' }; | ||
const expected = { foo: ['bar', ''] }; | ||
expect(parseQuery(queryString, options)).toEqual(expected); | ||
}); | ||
|
||
it('should handle null values in the query string', () => { | ||
const queryString = 'bar=&empty&baz'; | ||
const expected = { bar: '', empty: null, baz: null }; | ||
expect(parseQuery(queryString)).toEqual(expected); | ||
}); | ||
|
||
it('should handle multiple values for the same key', () => { | ||
const queryString = 'colors=red&colors=blue&colors=green'; | ||
const expected = { colors: ['red', 'blue', 'green'] }; | ||
expect(parseQuery(queryString)).toEqual(expected); | ||
}); | ||
|
||
it('should return an empty object for an empty query string', () => { | ||
const queryString = ''; | ||
const expected = {}; | ||
expect(parseQuery(queryString)).toEqual(expected); | ||
}); | ||
}); |
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,37 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import queryString, { ParseOptions } from 'query-string'; | ||
|
||
interface IParsedQuery { | ||
[key: string]: string | null | (string | null)[]; | ||
} | ||
|
||
function parseQuery(query: string, options?: ParseOptions): { [key: string]: string | string[] } { | ||
const parsed: IParsedQuery = queryString.parse(query, options); | ||
|
||
const result: { [key: string]: string | string[] } = {}; | ||
|
||
Object.keys(parsed).forEach(key => { | ||
if (Array.isArray(parsed[key])) { | ||
result[key] = (parsed[key] as string[]).map((item: string | null) => item || ''); | ||
} else { | ||
result[key] = parsed[key] as string | string[]; | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
export default parseQuery; |
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