Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed between function #946

Merged
merged 4 commits into from
Jan 14, 2023
Merged
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: 8 additions & 4 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,18 @@ var _ = Mavo.Functions = {
between: $.extend((haystack, from, to, tight) => {
[haystack, from, to] = [str(haystack), str(from), str(to)];

var i1 = from? haystack[tight? "lastIndexOf" : "indexOf"](from) : -1;
var i2 = haystack[tight? "indexOf" : "lastIndexOf"](to);
let fromIndex = from? haystack[tight? "lastIndexOf" : "indexOf"](from) : 0;
let toIndex = to? haystack[tight? "indexOf" : "lastIndexOf"](to) : haystack.length;

if (from && i1 === -1 || i2 === -1) {
if (fromIndex === -1 || toIndex === -1) {
return "";
}

return haystack.slice(i1 + 1, i2 === -1 || !to? haystack.length : i2);
if (tight && toIndex <= fromIndex){
return haystack.slice(toIndex + to.length, fromIndex);
}

return haystack.slice(fromIndex + from.length, toIndex);
Comment on lines +619 to +623
Copy link
Member

@DmitrySharabin DmitrySharabin Jan 13, 2023

Choose a reason for hiding this comment

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

But what if from and/or to are not provided (either of them can be an empty string)? They are optional by design (and you took it into account in your first commit). Moreover, the fromlast() and tofirst() functions are built on top of the optionality of from and to.

E.g., With this change, fromlast("foo.bar.baz", ".") returns foo.bar instead of baz, and tofirst("foo.bar.baz", ".") returns bar.baz instead of foo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed!

Copy link
Member

Choose a reason for hiding this comment

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

Awesome! LGTM. Thanks!

}, {
multiValued: true
}),
Expand Down