Skip to content

Commit

Permalink
Normalize hash and search strings (#813) (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance authored Aug 16, 2021
1 parent 23c37fb commit e62360d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
62 changes: 62 additions & 0 deletions packages/history/__tests__/create-path-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import expect from 'expect';
import { createPath } from 'history';

describe('createPath', () => {
describe('given only a pathname', () => {
it('returns the pathname unchanged', () => {
let path = createPath({ pathname: 'https://google.com' });
expect(path).toBe('https://google.com');
});
});

describe('given a pathname and a search param', () => {
it('returns the constructed pathname', () => {
let path = createPath({
pathname: 'https://google.com',
search: '?something=cool'
});
expect(path).toBe('https://google.com?something=cool');
});
});

describe('given a pathname and a search param without ?', () => {
it('returns the constructed pathname', () => {
let path = createPath({
pathname: 'https://google.com',
search: 'something=cool'
});
expect(path).toBe('https://google.com?something=cool');
});
});

describe('given a pathname and a hash param', () => {
it('returns the constructed pathname', () => {
let path = createPath({
pathname: 'https://google.com',
hash: '#section-1'
});
expect(path).toBe('https://google.com#section-1');
});
});

describe('given a pathname and a hash param without #', () => {
it('returns the constructed pathname', () => {
let path = createPath({
pathname: 'https://google.com',
hash: 'section-1'
});
expect(path).toBe('https://google.com#section-1');
});
});

describe('given a full location object', () => {
it('returns the constructed pathname', () => {
let path = createPath({
pathname: 'https://google.com',
search: 'something=cool',
hash: '#section-1'
});
expect(path).toBe('https://google.com?something=cool#section-1');
});
});
});
6 changes: 5 additions & 1 deletion packages/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,11 @@ export function createPath({
search = '',
hash = ''
}: PartialPath) {
return pathname + search + hash;
if (search && search !== '?')
pathname += search.charAt(0) === '?' ? search : '?' + search;
if (hash && hash !== '#')
pathname += hash.charAt(0) === '#' ? hash : '#' + hash;
return pathname;
}

/**
Expand Down

0 comments on commit e62360d

Please sign in to comment.