Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions JavaScript/2-between.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
// Implementation

const between = (str, quotes) => {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can make in more flexible

Suggested change
const between = (str, quotes) => {
const between = (str, start, end) => {

let i = str.indexOf(quotes[0]);
const start = quotes[0];
const end = quotes[1];
let i = str.indexOf(start);
if (i === -1) return '';
str = str.substring(i + 1);
if (quotes[1]) {
i = str.indexOf(quotes[1]);
const line = str.substring(i + 1);
if (end) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect than end argument always present, so if is not needed

i = line.indexOf(end);
if (i === -1) return '';
str = str.substring(0, i);
return line.substring(0, i);

@nechaido nechaido Oct 22, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to implement this function in a way, that we only create a substring once in each case.
Current implementation extracts substring twice if we provide end quote.

}
return str;
};
Expand Down