This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: iterate over substitutions
- Loading branch information
Showing
1 changed file
with
40 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,52 @@ | ||
require("IsTermInString") | ||
|
||
-- Cleanup spaces where none should be | ||
local function CleanupSpaces(string) | ||
local cleaned = string | ||
if IsTermInString(', ', cleaned) then | ||
cleaned = cleaned.gsub(cleaned, ', ', ',') | ||
end | ||
if IsTermInString('; ', cleaned) then | ||
cleaned = cleaned.gsub(cleaned, '; ', ';') | ||
end | ||
return cleaned | ||
-- Remove all whitespaces after delimeters | ||
local function stripWhitespaces(traffic_sign) | ||
local stripped = string.gsub(traffic_sign, ', ', ',') | ||
stripped = string.gsub(stripped, '; ', ';') | ||
return stripped | ||
end | ||
|
||
-- * @desc Cleanup the `traffic_sign=*` tag | ||
-- * @returns string | ||
function SanitizeTrafficSign(traffic_sign_value) | ||
if traffic_sign_value == nil then | ||
--- Cleanup the `traffic_sign=*` tag | ||
--- @param traffic_sign string | ||
--- @returns string | ||
function SanitizeTrafficSign(traffic_sign) | ||
if traffic_sign == nil then | ||
return nil | ||
end | ||
|
||
-- Rename | ||
-- Docs: gsub with "^" targets beginning of string | ||
if traffic_sign_value == "no" then | ||
if traffic_sign == "no" or traffic_sign == 'none' then | ||
return "none" | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'DE: ') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE: ', 'DE:')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'DE2') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE2', 'DE:2')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'DE1') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE1', 'DE:1')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'D:') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^D:', 'DE:')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'de:') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^de:', 'DE:')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'DE.') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE.', 'DE:')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, '2') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^2', 'DE:2')) | ||
end | ||
if osm2pgsql.has_prefix(traffic_sign_value, '10') then | ||
return CleanupSpaces(string.gsub(traffic_sign_value, '^10', 'DE:10')) | ||
end | ||
|
||
-- Allows | ||
if osm2pgsql.has_prefix(traffic_sign_value, 'DE:') then | ||
return CleanupSpaces(traffic_sign_value) | ||
-- Docs: patterns with "^" target beginning of string | ||
|
||
-- This is the correct tagging, all traffic signs should start with DE: | ||
if string.find(traffic_sign, '^DE:%S') then | ||
return stripWhitespaces(traffic_sign) | ||
end | ||
if traffic_sign_value == "none" then | ||
return traffic_sign_value | ||
|
||
local substitutions = { | ||
['^DE: '] = 'DE:', | ||
['^DE.'] = 'DE:', | ||
['^D:'] = 'DE:', | ||
['^D%.'] = 'DE:', | ||
['^de:'] = 'DE:', | ||
['^DE1'] = 'DE:1', | ||
['^DE2'] = 'DE:2', | ||
['^2'] = 'DE:2', | ||
['^1'] = 'DE:1', | ||
-- These patterns could handle all the above in a more generalized way | ||
-- ['^DE?[:.]%s?'] = 'DE:', | ||
-- ['^de?[:.]%s?'] = 'DE:' | ||
-- ['^DE(%d)'] = 'DE:%1' | ||
-- ['^(%d)'] = 'DE:%1' | ||
} | ||
for pattern, substitude in pairs(substitutions) do | ||
local val, n = string.gsub(traffic_sign, pattern, substitude) | ||
if n > 0 then | ||
-- TODO: add to todo list | ||
return stripWhitespaces(val) | ||
end | ||
end | ||
|
||
-- Disallow everything else | ||
-- Discard everything else | ||
return nil | ||
end |