#2188 Uri.removeSlash

SlimerDude Mon 16 Sep 2013

To complement sys::Uri.plusSlash what do you think of Uri.removeSlash?

I find myself doing this over and over again. And...

if (uri.toStr.endsWith("/"))
  uri = `${uri.toStr[0..<-1]}` 

...just isn't that elegant!

EDIT: The above code may be simplified to:

if (uri.isDir)
  url = url.parent.plusName(url.name)

which isn't so bad.

brian Mon 16 Sep 2013

Actually that seems unusual to me - wouldn't you typically use parent in that context? When would you want to get a directory URI as a file URI?

SlimerDude Mon 16 Sep 2013

Looking back through my code for examples and context, I think you're right - I was a little trigger happy with the suggestion.

There are generally 2 scenarios when I want to remove a slash, and neither really warrant the extra method:

  1. When rendering a flat file uri for some 3rd party XML, which doesn't like trailing slashes for dirs.
  2. When concatenating 2 URIs:
`/wot/` + `/ever` --> `/ever`

when what I really want is:

`/wot/` + `ever` --> `/wot/ever`

Wisp's modBase and modRel is a good example of this, as modRel always starts with a / (contrary to the docs at web::WebReq.modBase).

Login or Signup to reply.