Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize hash and search strings #891

Merged
merged 1 commit into from
Aug 16, 2021
Merged
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
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