From c5876bd67315f6d1e8d9c43f558f7b07bf4fe303 Mon Sep 17 00:00:00 2001 From: Marwan Sbitani Date: Tue, 26 Nov 2024 12:19:19 -0500 Subject: [PATCH 1/5] Rewrite house number increment logic --- RapidHouseNumbers.js | 87 ++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 60 deletions(-) diff --git a/RapidHouseNumbers.js b/RapidHouseNumbers.js index 77c8b61..2ea192a 100644 --- a/RapidHouseNumbers.js +++ b/RapidHouseNumbers.js @@ -5,9 +5,9 @@ // ==UserScript== // @name WME Rapid House Numbers -// @description A House Number script with its controls in the House Number mini-editor. It injects the next value in a sequence into each new HN. To support different regions, house numbers may be [0-9]+, [0-9]+[a-z]+, or [0-9]+-[0-9]+. +// @description A House Number script with its controls in the House Number mini-editor. It injects the next value in a sequence into each new HN. All house number formats are supported. // @namespace https://github.com/WazeDev -// @version 2.9 +// @version 3.0 // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/ // @copyright 2017-2024, kjg53 // @author kjg53, WazeDev (2023-?), SaiCode (2024-?) @@ -52,12 +52,9 @@ { version: "2.7", message: "Minor version check fix." }, { version: "2.8", message: "Changelog UI enhancements." }, { version: "2.9", message: "Bug fixing." }, + { version: "3.0", message: "Support any house number format." }, ]; - const ALL_DIGITS = /^[0-9]+$/; - const DIG_ALPHA = /^([0-9]+)([A-Z]$)/i; - const DIG_DASH_DIG = /^([0-9]+-)([0-9]+)$/; - const ONE = 49; const NINE = 57; const NUMPAD1 = 97; @@ -337,71 +334,41 @@ } function injectHouseNumber(newHouseNumber) { - const incElm = $("input.rapidHN.increment"); - - let inc; - if (oneTimeIncrement) { - inc = oneTimeIncrement; - oneTimeIncrement = undefined; - } else { - inc = parseInt(incElm.val(), 10); - } + let increment = oneTimeIncrement ?? Number($("input.rapidHN.increment").val()); + oneTimeIncrement = undefined; const nextElement = $("input.rapidHN.next").filter(":visible"); const next = nextElement.val(); - if (ALL_DIGITS.test(next)) { - // Inject next HN into WME - // newHouseNumber.val(next).change(); - setNativeValue(newHouseNumber[0], next); - - const n = parseInt(next, 10); - - nextElement.val(n + inc); - } else if (DIG_ALPHA.test(next)) { - // Inject next HN into WME - // newHouseNumber.val(next).change(); - setNativeValue(newHouseNumber[0], next); - - const digAlpha = next.match(DIG_ALPHA); - const curLet = digAlpha[2]; - let min; - let max; - if (curLet >= "a" && curLet <= "z") { - min = "a".codePointAt(0); - max = "z".codePointAt(0); - } else if (curLet >= "A" && curLet <= "Z") { - min = "A".codePointAt(0); - max = "Z".codePointAt(0); - } else { - return; - } + // Inject next HN into WME + setNativeValue(newHouseNumber[0], next); - let nxtLet = curLet.codePointAt(0) + inc; - // if we need to wrap the letter - if (nxtLet > max) { - // Increment the numeric portion - digAlpha[1] = `${parseInt(digAlpha[1], 10) + 1}`; + const nextParts = next.match(/[0-9]+|[a-z]|[A-Z]|\S/g); - // wrap the letter - nxtLet -= max; - nxtLet += min - 1; - } - digAlpha[2] = String.fromCodePoint(nxtLet); + for (const [index, part] of nextParts.reverse().entries()) { + if (!Number.isNaN(Number(part))) { + nextParts[index] = (Number(part) + increment).toString().padStart(part.length, '0'); + break; + } - nextElement.val(digAlpha[1] + digAlpha[2]); - } else if (DIG_DASH_DIG.test(next)) { - // Inject next HN into WME - // newHouseNumber.val(next).change(); - setNativeValue(newHouseNumber[0], next); + if (/[a-z]/i.test(part)) { + let nextLetter = part.codePointAt(0) + (increment % 26); - const digDig = next.match(DIG_DASH_DIG); + increment = Math.floor(increment/26); - // Increment the numeric portion - digDig[2] = `${parseInt(digDig[2], 10) + inc}`; + if ((/[a-z]/.test(part) && nextLetter > 'z'.codePointAt(0)) || + (/[A-Z]/.test(part) && nextLetter > 'Z'.codePointAt(0))) { + nextLetter -= 26; + increment++; + } - nextElement.val(digDig[1] + digDig[2]); + nextParts[index] = String.fromCodePoint(nextLetter); + + if (!increment) break; + } } + + nextElement.val(nextParts.reverse().join('')); } function nonZero(input) { From 095b6c130a8a6a2ea19d41989a3addbff4a6d2b1 Mon Sep 17 00:00:00 2001 From: Marwan Sbitani Date: Tue, 26 Nov 2024 12:40:38 -0500 Subject: [PATCH 2/5] Remove redundant next field --- RapidHouseNumbers.js | 44 +------------------------------------------- 1 file changed, 1 insertion(+), 43 deletions(-) diff --git a/RapidHouseNumbers.js b/RapidHouseNumbers.js index 2ea192a..701daf9 100644 --- a/RapidHouseNumbers.js +++ b/RapidHouseNumbers.js @@ -168,20 +168,12 @@ window.localStorage.getItem("rapidHNincrement") || 2 ).toString(); - // NOTE: We have two input.rapidHN.next fields because the type property cannot be modified. We, instead, create two fields - // then use a function, updateRapidHNnextVisibility, to determine which one is currently visible. $(addHouseNumberNode).after(`
Next #
- -
-
- - 1,2,3 -
@@ -193,19 +185,9 @@
`); rapidHNtoolbarButton = addHouseNumberNode.nextSibling; - updateRapidHNnextVisibility(false); - + enableDisableControls(rapidHNtoolbarButton, W.map.getZoom() < 18); - $("button#current-input-type").click(() => { - let nextInputType = window.localStorage.getItem("rapidHNnextInputType") || "number"; - - nextInputType = { number: "text", text: "number" }[nextInputType]; - - window.localStorage.setItem("rapidHNnextInputType", nextInputType); - updateRapidHNnextVisibility(true); - }); - // if the key is released blur so that you can type to add a house number rather than see it appended to the next value. $("input.rapidHN.next").keyup(evt => { if (evt.which === 13) { @@ -448,30 +430,6 @@ return secondary; } - function updateRapidHNnextVisibility(showTooltip) { - const nextInputType = window.localStorage.getItem("rapidHNnextInputType") || "number"; - const inputs = $("input.rapidHN.next"); - - inputs.hide(); - const nextInput = inputs.filter(`[type='${nextInputType}']`); - nextInput.show(); - - $("button#current-input-type").text( - { number: "1", text: "A" }[nextInputType], - ); - - if (showTooltip) { - // hide both tooltips - ["number", "text"].forEach(type => { - const tooltip = $(`span#rapidHN-input-is-${type}`); - tooltip.hide(); - }); - - const tooltip = $(`span#rapidHN-input-is-${nextInputType}`); - tooltip.show(); - } - } - rapidHNBootstrap(); })(); From 84f64090a0220aa1f4d4472f76b5f447bc6ba55d Mon Sep 17 00:00:00 2001 From: Marwan Sbitani Date: Tue, 26 Nov 2024 12:56:50 -0500 Subject: [PATCH 3/5] Fix return key blur --- RapidHouseNumbers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RapidHouseNumbers.js b/RapidHouseNumbers.js index 701daf9..d14d54f 100644 --- a/RapidHouseNumbers.js +++ b/RapidHouseNumbers.js @@ -191,7 +191,7 @@ // if the key is released blur so that you can type to add a house number rather than see it appended to the next value. $("input.rapidHN.next").keyup(evt => { if (evt.which === 13) { - this.blur(); + evt.target.blur(); } }); From b2907552f5e55d74b1165879f6d0170800169220 Mon Sep 17 00:00:00 2001 From: Marwan Sbitani Date: Tue, 26 Nov 2024 13:10:25 -0500 Subject: [PATCH 4/5] Remove superfluous nonZero method --- RapidHouseNumbers.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/RapidHouseNumbers.js b/RapidHouseNumbers.js index d14d54f..07160ae 100644 --- a/RapidHouseNumbers.js +++ b/RapidHouseNumbers.js @@ -179,7 +179,7 @@
Increment
- +
@@ -202,7 +202,7 @@ $("div.rapidHN-control input").on("change", () => { const controls = $("div.rapidHN-control"); const rapidHNenabled = $("input.rapidHN.next", controls).filter(":visible").val() - && nonZero($("input.rapidHN.increment", controls)); + && Number($("input.rapidHN.increment", controls)) > 0; if (rapidHNenabled) { if (houseNumbersObserver === undefined) { @@ -353,11 +353,6 @@ nextElement.val(nextParts.reverse().join('')); } - function nonZero(input) { - const i = parseInt(input.val(), 10); - return !isNaN(i) && i !== 0; - } - // Type 1-9 instead of 'h' to specify a one-time increment that be applied after the current "next" value is added to the map function rapidAccelerator(event) { // if not in house number editor mode, return From 03670aa66bce4650f1ae1f57fe509d2a62b89120 Mon Sep 17 00:00:00 2001 From: Marwan Sbitani Date: Tue, 26 Nov 2024 14:07:07 -0500 Subject: [PATCH 5/5] Add missing val() --- RapidHouseNumbers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RapidHouseNumbers.js b/RapidHouseNumbers.js index 07160ae..d2bcb9c 100644 --- a/RapidHouseNumbers.js +++ b/RapidHouseNumbers.js @@ -202,7 +202,7 @@ $("div.rapidHN-control input").on("change", () => { const controls = $("div.rapidHN-control"); const rapidHNenabled = $("input.rapidHN.next", controls).filter(":visible").val() - && Number($("input.rapidHN.increment", controls)) > 0; + && Number($("input.rapidHN.increment", controls).val()) > 0; if (rapidHNenabled) { if (houseNumbersObserver === undefined) {