Porting the Branch-Name Helper
The team’s git hooks are moving to JavaScript, so in this lesson, you ported most of one developer’s Python helper, line by line, down to the branch and commit lines. Each line kept what it does and changed its string calls, and where JavaScript had no matching call, the port built one. Here’s the Python helper with PAY-2291’s header:
header = (
"PAY-2291: Refund emails show the wrong currency: "
"EUR shown as USD"
)
key, title = header.split(":", 1)
title = title.strip().rstrip(".")
words = title.lower().replace(":", "").split()
branch = f"fix/{key.lower()}-{'-'.join(words[:6])}"
subject = f"{key}: {title}"[:50]
number = key.split("-")[1]
print(f"Branch: {branch}")
print(f"Commit: {subject}")
print(f"Ticket number: {number}")
print(f"Needs a second reviewer: {'currency' in title.lower()}")
Each line of the port does the same job with JavaScript’s string methods:
split(":", 1)doesn’t carry over, because JavaScript’s limit keeps one piece, as Splitting on the First Colon showed.indexOf()finds the first colon, at position 8, as Python’sfind()does.slice()takes the key before it, andslice(colon + 1)takes the title from one past it, second colon included.trim()doesstrip()’s job.trimEnd(".")ignores its argument, as Writing the Methods JavaScript Doesn’t Have showed. So anifchecksendsWith(".")beforeslice(0, -1)drops the last character. Theifmay reassigntitle, so it’s alet, not aconst. Like Python’sremovesuffix("."), that drops one full stop whererstrip(".")drops them all, which is enough here.toLowerCase()stands in forlower(), andreplaceAll()forreplace(), which in JavaScript stops at the first match, and a title can hold more than one colon.split(" ", 6)keeps the first six words in an array, JavaScript’s version of a list. The limit that brokesplit(":", 1)is what you want here: six pieces, Python’ssplit()and[:6]in one call. It assumes one space between words, which is right for these tidy headers, but Python’ssplit()treats a run of spaces as one gap, which is why PAY-2330’s doubled spaces took extra work in Turn Titles Into Branch Names.- Each f-string becomes a template literal: backticks in place of the
fand its quotes, and a dollar sign before each brace. The array joins itself withwords.join("-"), since strings have nojoin()method, andslice(0, 50)cuts the subject where Python has[:50].