Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Branch-Name Helper

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:

Language: Python
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’s find() does. slice() takes the key before it, and slice(colon + 1) takes the title from one past it, second colon included. trim() does strip()’s job.
  • trimEnd(".") ignores its argument, as Writing the Methods JavaScript Doesn’t Have showed. So an if checks endsWith(".") before slice(0, -1) drops the last character. The if may reassign title, so it’s a let, not a const. Like Python’s removesuffix("."), that drops one full stop where rstrip(".") drops them all, which is enough here.
  • toLowerCase() stands in for lower(), and replaceAll() for replace(), 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 broke split(":", 1) is what you want here: six pieces, Python’s split() and [:6] in one call. It assumes one space between words, which is right for these tidy headers, but Python’s split() 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 f and its quotes, and a dollar sign before each brace. The array joins itself with words.join("-"), since strings have no join() method, and slice(0, 50) cuts the subject where Python has [:50].

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 The payments team’s git hooks are moving to JavaScript, so one developer’s branch-name helper has to move too. Here’s her Python script, with PAY-2291’s header pasted in at the top.

00:15 Run it, and Python prints four lines: the branch, the commit subject, the ticket number, and whether the ticket needs a second reviewer. The port keeps what each line does and changes its string calls.

00:29 You’ll port the lines that build the branch and the commit subject, and leave the other two for the exercise after this video.

00:37 In JavaScript, the header carries over as two strings, joined with a + sign. A fast port would keep split(":", 1), but as Splitting on the First Colon showed, JavaScript’s limit of 1 keeps one piece.

00:52 So indexOf() finds the first colon, as Python’s find() would, at position 8. slice() takes the key before it, and a second slice() keeps the title from one past it, second colon included.

01:05 trim() drops the space at the front.

01:09 Python’s rstrip(".") strips the full stop from the end. JavaScript has no method for that: trimEnd(".") ignores its argument, as Writing the Methods JavaScript Doesn’t Have showed.

01:22 So an if checks whether the title ends with a full stop. Only then does slice(0, -1) cut off the last character. That’s why title is a let, not a const: the if may reassign it.

01:37 Like Python’s removesuffix("."), it drops one full stop, not all of them: enough here.

01:45 The words line chains three calls, and toLowerCase() lowercases the title, as lower() did. JavaScript’s replace() changes only the first match, and a title can have two colons, so the port uses replaceAll(), as Replacing Every Match With replaceAll() showed.

02:03 Then split(" ", 6) cuts at each space and keeps six words in an array, JavaScript’s list. It’s the limit that broke split(":", 1), here doing Python’s split() and [:6] in one call.

02:18 It assumes one space between words, fine for tidy headers, but Python’s split() treats a run of spaces as one gap, which is why, in Turn Titles Into Branch Names, PAY-2330 took extra work.

02:33 The branch line turns the f-string into a template literal: backticks in place of the f and its quotes, and a dollar sign before each brace.

02:42 The array joins its words with hyphens. Strings have no join() in JavaScript, so Python’s "-".join() would fail, as Joining Words Into a Branch Name showed.

02:54 The subject line builds a second template literal, and slice(0, 50) cuts it to 50 characters, as [:50] does.

03:03 The two print lines become console.log() calls, and the other two wait for the exercise. Then node --check reads the file without running it.

03:13 It prints nothing, so there’s no syntax error.

03:17 Now run both programs. JavaScript prints Python’s branch, character for character. The commit subject matches too, cut inside EUR at 50 characters. Python’s last two lines are still to port.

03:35 Now paste PAY-2304’s header into both files. Its title ends with a full stop. Does that full stop make it into the commit subject? Your turn: pause the video and predict.

03:54 JavaScript answers no: there’s no full stop at the end. The if dropped it, and with a subject under 50 characters, slice(0, 50) had nothing to cut.

04:06 The branch matches too.

04:09 The port kept what each line does and built the calls JavaScript lacks. For the first colon, use indexOf() and slice either side. For a trailing full stop, check endsWith() before you slice.

04:24 For six words from a single-spaced title, split with a limit and join the array.

04:30 Next up is the exercise Finish the Branch-Name Helper. There, you’ll add the ticket number and second-reviewer lines, so the JavaScript prints what Python prints for each ticket.

Become a Member to join the conversation.