d0p3t / fivem-js

Javascript and Typescript wrapper for the FiveM natives API
https://d0p3t.nl
Other
143 stars 57 forks source link

StringToArray doesn't seem to work #18

Closed vincentducorps closed 5 years ago

vincentducorps commented 5 years ago

What is the purpose of this function?

https://github.com/d0p3t/fivem-js/blob/70c8dcff4ca95ccdfd9a049333e2f1c0246d0b11/src/utils/String.ts#L4-L12

If I use this method:

Cfx.Screen.DisplayHelpTextThisFrame('message')

I have this error : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length (Probably due to the param stringsNeeded in : new Array<string>(stringsNeeded);)

But even corrected, this function seems to act strangely... (return input twice)

If you only need to split word by word you can use:

function StringToArray(string) {
    return string.trim().split(' ');
}
d0p3t commented 5 years ago

What is the purpose of this function?

The purpose of this function is to split an input string into an array of strings where each string does is not longer than 99 characters. This is needed for certain GTA natives, which do not support string longer than 99 characters. This results in, for example, cut off notification texts.

I have this error : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length (Probably due to the param stringsNeeded in : new Array<string>(stringsNeeded);)

You're correct! This was a mistake in the function. The stringsNeeded variable is not always an integer. I will fix this by simply applying a Math.ceil() to it.

But even corrected, this function seems to act strangely... (return input twice)

Also correct! You are right, when a string is less than 99 characters long, it will return inputString.length / 99 + 1, which will always be 2 (when rounded). I will fix this by checking if the length of the input string is less than 99.

I will address this on the development branch. It will be pushed to master as soon as I finish some other development features.

Thank you for the issue report!

d0p3t commented 5 years ago

Fixed in the development branch.

This will ship with version 1.3 (next)