diff --git a/package.json b/package.json index 2f36932..6885247 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,11 @@ { "dependencies": { "axios": "^0.21.4" + }, + "devDependencies": { + "jest": "^29.7.0" + }, + "scripts": { + "test": "jest" } } diff --git a/scripts/utils.js b/scripts/utils.js new file mode 100644 index 0000000..81e73de --- /dev/null +++ b/scripts/utils.js @@ -0,0 +1,19 @@ +function parseQueryString(queryString) { + if (!queryString) { + return {}; + } + + const pairs = queryString.replace(/^\?/, '').split('&'); + const result = {}; + + for (let i = 0; i < pairs.length; i++) { + const pair = pairs[i].split('='); + result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || ''); + } + + return result; +} + +module.exports = { + parseQueryString, +}; diff --git a/scripts/utils.test.js b/scripts/utils.test.js new file mode 100644 index 0000000..db5258a --- /dev/null +++ b/scripts/utils.test.js @@ -0,0 +1,27 @@ +const { parseQueryString } = require('./utils'); + +describe('parseQueryString', () => { + test('should return an empty object for an empty string', () => { + expect(parseQueryString('')).toEqual({}); + }); + + test('should parse a single key-value pair', () => { + expect(parseQueryString('?foo=bar')).toEqual({ foo: 'bar' }); + }); + + test('should parse multiple key-value pairs', () => { + expect(parseQueryString('?foo=bar&baz=qux')).toEqual({ foo: 'bar', baz: 'qux' }); + }); + + test('should handle a key with no value', () => { + expect(parseQueryString('?foo=')).toEqual({ foo: '' }); + }); + + test('should handle a key with no value and other keys', () => { + expect(parseQueryString('?foo=&baz=qux')).toEqual({ foo: '', baz: 'qux' }); + }); + + test('should handle URI encoded characters', () => { + expect(parseQueryString('?foo=bar%20baz')).toEqual({ foo: 'bar baz' }); + }); +});