Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"dependencies": {
"axios": "^0.21.4"
},
"devDependencies": {
"jest": "^29.7.0"
},
"scripts": {
"test": "jest"
}
}
19 changes: 19 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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,
};
27 changes: 27 additions & 0 deletions scripts/utils.test.js
Original file line number Diff line number Diff line change
@@ -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' });
});
});