Merge pull request #1048 from sharetribe/fix-empty-slug

Fix empty slug
This commit is contained in:
Vesa Luusua 2019-03-20 13:26:03 +02:00 committed by GitHub
commit ce9d61adc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 4 deletions

View file

@ -14,6 +14,8 @@ way to update this template, but currently, we follow a pattern:
## Upcoming version 2019-XX-XX
- [fix] A listing title that contained only stripped-off characters caused bugs in slug / pathName
generation. [#1048](https://github.com/sharetribe/flex-template-web/pull/1048)
- [change] Removed Node-engine setup from package.json. Fixed version was causing problems for quite
many in their first FTW installation. Note: when troubleshooting your Heroku installation, you
might want to reintroduce engine setup.

View file

@ -20,8 +20,11 @@ const toPathByRouteName = (nameToFind, routes) => {
/**
* Shorthand for single path call. (```pathByRouteName('ListingPage', routes, { id: uuidX });```)
*/
export const pathByRouteName = (nameToFind, routes, params = {}) =>
toPathByRouteName(nameToFind, routes)(params);
export const pathByRouteName = (nameToFind, routes, params = {}) => {
const hasEmptySlug = params && params.hasOwnProperty('slug') && params.slug === '';
const pathParams = hasEmptySlug ? { ...params, slug: 'no-slug' } : params;
return toPathByRouteName(nameToFind, routes)(pathParams);
};
/**
* Find the matching routes and their params for the given pathname

View file

@ -55,7 +55,7 @@ export const createSlug = str => {
text = text.replace(new RegExp(`[${set.from}]`, 'gi'), set.to);
});
return encodeURIComponent(
const slug = encodeURIComponent(
text
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w-]+/g, '') // Remove all non-word chars
@ -63,6 +63,8 @@ export const createSlug = str => {
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
);
return slug.length > 0 ? slug : 'no-slug';
};
/**

View file

@ -18,7 +18,7 @@ const COMMA = encodeURIComponent(',');
describe('urlHelpers', () => {
describe('parseFloatNum()', () => {
it('handles empty string (returns "")', () => {
expect(createSlug('')).toEqual('');
expect(createSlug('')).toEqual('no-slug');
});
it('handles space characters', () => {