Skip to content

Commit

Permalink
new url_join version
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf758 committed Apr 20, 2023
1 parent 5a5f8b4 commit b425feb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
31 changes: 31 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest
from v3io.common.helpers import url_join

class Test(unittest.TestCase):
def test_url_join(self):
self.assertEqual(url_join('a','b') , 'a/b') # add just exactly one '/' between parts
self.assertEqual(url_join('/','a','b') , '/a/b') #
self.assertEqual(url_join('/','a','/b'), '/a/b') #
self.assertEqual(url_join('/','/a','b') , '/a/b') #
self.assertEqual(url_join('/','/a','/b'), '/a/b') #
self.assertEqual(url_join('/','/a/','b'), '/a/b') #
self.assertEqual(url_join('/','/a/','/b'), '/a/b') #
self.assertEqual(url_join('a','b'), 'a/b') # keep suffix '/' exist/not-exist invariant
self.assertEqual(url_join('a','b/'), 'a/b/') #
self.assertEqual(url_join('a','b//'), 'a/b//') #
self.assertEqual(url_join('a','b//', '/'), 'a/b//') # suffix '/' count (if > 0) may change (but we don't care)

24 changes: 16 additions & 8 deletions v3io/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from functools import reduce


def _join_slash(left, right):
return left.rstrip("/") + "/" + right.lstrip("/")


def url_join(*parts):
"""join parts by pairs with a single slash, leaving left-part leading and right-part trailing slashes."""
return reduce(_join_slash, parts) if parts else ""
result = ""
slash_suffix = False
for part_index, part in enumerate(parts):
if part == '':
continue
# add slash prefix before part if:
# 1. slash suffix did not exit in prev part
# 2. slash prefix does not exit in this part
# 3. part is not the first
if not slash_suffix and part[0] != '/' and part_index != 0:
result += '/' + part
else:
# if slash suffix existed in prev trim slash prefix from this part
result += part if not slash_suffix else part.lstrip("/")
slash_suffix = True if part[-1] == '/' else False
return result

0 comments on commit b425feb

Please sign in to comment.