Calling str Methods by Their JavaScript Names

With .slice(), the port of ticket_branch.py can cut the commit subject down to 50 characters. The helper’s other lines lean on the str methods you use without thinking: strip() to tidy the title, lower() for the branch name, and in to spot a ticket that mentions currency.

JavaScript strings have the same everyday toolkit, under longer camelCase names, and the methods chain into one cleaning step as Python’s do. Once you know the names, most of these lines port almost word for word. Only the currency check switches from an operator to a method.

Clean the Title in One Line

The ticket header is still "PAY-2291: Refund emails show the wrong currency: EUR shown as USD". After the helper splits it at the first colon, the title starts with the space that followed the colon. Porting that split is a lesson of its own, so the snippets here start from its two pieces.

In Python, the helper strips the title. For the branch name, it then lowercases the title and the key:

Language: Python
key = "PAY-2291"
raw_title = " Refund emails show the wrong currency: EUR shown as USD"

title = raw_title.strip()
print(title.lower())
print(key.lower())

In the JavaScript port, strip() becomes .trim() and lower() becomes .toLowerCase():

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

Become a Member to join the conversation.