Jump to content
Search Community

Set Empty Strings

guiferviz test
Moderator Tag

Recommended Posts

Hi all!

 

I've found that empty strings are replaced by an unwanted "0" when using `set`. Is there any easy way to avoid that? I know that sometimes is desirable but in some other situations I just need an empty string.

 

Also, I've realized that an "=" sign on the second position of the string produces a "NaN" in the output. I've found that using a list with only one string element solves this issue. Is there any other way of solving this?

 

Thanks!!

See the Pen vYxyvzR by guiferviz (@guiferviz) on CodePen

  • Thanks 1
Link to comment
Share on other sites

Yeah, since GSAP is all about tweening, it gravitates toward numbers. Your demo actually revealed something about JavaScript execution that I didn't know and it's kind weird: an empty string multiplied by any number gets evaluated as the number 0!

console.log(1 * ""); // 0!

It really should be NaN, at least intuitively. 

 

Anyway, that's what was causing that first issue. I can patch that in the next release.

 

The "=" as the second character is tripping the algorithm that looks for relative values, like "+=20" or "-=20". In all my years of doing this, I've never seen anyone hit an edge case like this. Congratulations :) I can add some extra logic to sense that condition in the next release.

 

In the meantime, there are several solutions available to you: 

 

1) Use TextPlugin

It'd simplify your code quite a bit too:

t.set(node, {text: "1"}, 0)
t.set(node, {text: ""}, 1)
t.set(node, {text: "3"}, 2)
t.set(node, {text: "!= Not equal operator"}, 3)

No proxy needed. 

 

2) Use a simple custom plugin

Here's a fork with a "plain" plugin that's just for setting string values (no animating): 

See the Pen zYZoVzy?editors=0010 by GreenSock (@GreenSock) on CodePen

 

Does that clear things up? 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hi GreenSock!

 

JavaScript is not my first language and I'm discovering weird things every day. It makes me feel better to know that it still surprises those who have been doing it for years :)

 

I'm glad to have been able to contribute something to the project, even if it's an insignificant bug that happens once every 10 years 🤣

 

The TextPlugin is not a solution for me because my use case is based on objects and not HTML nodes, but that custom plugin you wrote perfectly fits my application needs! Thank you so much!!

  • Like 3
Link to comment
Share on other sites

4 hours ago, GreenSock said:

Yeah, since GSAP is all about tweening, it gravitates toward numbers. Your demo actually revealed something about JavaScript execution that I didn't know and it's kind weird: an empty string multiplied by any number gets evaluated as the number 0!


console.log(1 * ""); // 0!

It really should be NaN, at least intuitively. 


This reminds me of this talk.

https://www.destroyallsoftware.com/talks/wat

  • Haha 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...