-
Notifications
You must be signed in to change notification settings - Fork 42
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
Utilities for JSON String Array #445
base: master
Are you sure you want to change the base?
Conversation
var string = self.getValue(text.string, vars); | ||
var delimiter = self.getValue(text.delimiter, vars); | ||
var strArr; | ||
if (string && delimiter != undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be the same as (string && delimiter)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it so.
returnValue = JSON.stringify(strArr); | ||
} catch (err) { | ||
console.error(err); | ||
returnValue = "undefined"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"undefined" as a string seems strange. also returnValue
is undefined
if never set. see the beginning of VariableComponent.getValue(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it.
strArr = string.split(delimiter); | ||
returnValue = JSON.stringify(strArr); | ||
} catch (err) { | ||
console.error(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to not include console
messages in production. should use the built in script log that keeps track of script errors while the game is running
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced console.error() with taro.script.errorLog() .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, other conventional StringArray functions have the same problem. Shall I fix them for you, as it will not change engine specifications?
if (Array.isArray(array) == true) { | ||
// If the string passed is a real array | ||
returnValue = array.join(delimiter); | ||
} else if (array !== null && typeof array === 'object') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i feel like this behaviour is contrary to what joinStringArrayByDelimiter
should be doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more specifically: if it's not an array, this case should not be handling it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it.
Long explanation
Actually, you can read a JSON object with getStringArrayElement by configuring index parameters directly from the raw view. Some games (including mine) are using that feature and it's useful for advanced games (I wish this specification does not change later 🥺 ), so I wanted to list all values for JSON objects. But yes, probably I shouldn't **bother to add** the feature for JSON objects in this function, so I removed it.} | ||
break; | ||
|
||
case 'sortStringArray': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alphabetical and numerical can be merged into the same sort.
there should be two separate cases here. one for asc. one for dec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that there should be two cases ( = functions) for this part, one for ascending, and the other for descending, and I should just merge alphabetical and numerical sort? I thought because they work a bit differently, it was better that we had both.
I fixed the behaviors of alphabetical sorts as it wasn't working as I intended.
If you want to integrate numerical and alphabetical anyways, please tell me which to adopt here (I mean, which to remove, numerical or alphabetical)
} | ||
break; | ||
|
||
case 'sortTwoStringArrays': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to just have this case merge two string arrays and then use the sort case after. and perhaps merge two string arrays could just be using concatenate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, looking a bit weird, this function will be very useful to sort things (such as player name or ID) based on their scores... I think it's not possible only with "sortStringArray" function (note that some player name consists of only numbers). As for the array concatenation, I made a new one, "concatenateTwoStringArrays".
case 'sortStringArray': | ||
var string = self.getValue(text.string, vars); | ||
var mode = self.getValue(text.mode, vars); | ||
var returnValue = undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need to redefine returnValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your quick review. I worked on some points, but still I have several topics to discuss.
returnValue = JSON.stringify(strArr); | ||
} catch (err) { | ||
console.error(err); | ||
returnValue = "undefined"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it.
strArr = string.split(delimiter); | ||
returnValue = JSON.stringify(strArr); | ||
} catch (err) { | ||
console.error(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced console.error() with taro.script.errorLog() .
case 'sortStringArray': | ||
var string = self.getValue(text.string, vars); | ||
var mode = self.getValue(text.mode, vars); | ||
var returnValue = undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it.
var string = self.getValue(text.string, vars); | ||
var delimiter = self.getValue(text.delimiter, vars); | ||
var strArr; | ||
if (string && delimiter != undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it so.
} | ||
break; | ||
|
||
case 'sortStringArray': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that there should be two cases ( = functions) for this part, one for ascending, and the other for descending, and I should just merge alphabetical and numerical sort? I thought because they work a bit differently, it was better that we had both.
I fixed the behaviors of alphabetical sorts as it wasn't working as I intended.
If you want to integrate numerical and alphabetical anyways, please tell me which to adopt here (I mean, which to remove, numerical or alphabetical)
} | ||
break; | ||
|
||
case 'sortTwoStringArrays': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, looking a bit weird, this function will be very useful to sort things (such as player name or ID) based on their scores... I think it's not possible only with "sortStringArray" function (note that some player name consists of only numbers). As for the array concatenation, I made a new one, "concatenateTwoStringArrays".
if (Array.isArray(array) == true) { | ||
// If the string passed is a real array | ||
returnValue = array.join(delimiter); | ||
} else if (array !== null && typeof array === 'object') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it.
Utilities for JSON String Array
What I did
I added 4 functions to handle JSON String Array.
Takes two arguments, "string", and "delimiter". Split a string by a delimiter.
It will greatly help people make a command line.
Use cases
You can extract each element by "getStringArrayElement" function later.
Takes two arguments, "string" and "delimiter". Join the JSON String Array by the delimiter. It will still join when it is a JSON Object.
Use Cases
It will help make an ad-hoc CSV list or destruct the JSON String Array to a readable format.
For example, if you want to show the list of contributors stored in a JSON String Array, you can use it effectively.
Moreover, if you want to look at the content of a JSON Array with high readability, it will help a lot.
Takes two arguments, "string", and "mode". Sort the JSON String Array according to the mode. It will still return a sorted array as long as it is a JSON object.
There are four modes. Here's the list of how an array will be converted.
Use Cases
You will need a sort algorithm to decide the winners, thresholds, and so on. In such cases, it will save time to implement a sort algorithm on the taro engine. Moreover, it will work faster than hand-made ones because of lower overhead.
Takes three arguments, "string", "string2", and "mode". Sort both string1 and string2 according to the mode and string, and return the sorted string2.## Utilities for JSON String Array
What I did
I added 4 functions to handle JSON String Array.
Takes two arguments, "string", and "delimiter". Split a string by a delimiter.
It will greatly help people make a command line.
Use cases
You can extract each element by "getStringArrayElement" function later.
Takes two arguments, "string" and "delimiter". Join the JSON String Array by the delimiter. It will still join when it is a JSON Object.
Use Cases
It will help make an ad-hoc CSV list or destruct the JSON String Array to a readable format.
For example, if you want to show the list of contributors stored in a JSON String Array, you can use it effectively.
Moreover, if you want to look at the content of a JSON Array with high readability, it will help a lot.
Takes two arguments, "string", and "mode". Sort the JSON String Array according to the mode. It will still return a sorted array as long as it is a JSON object.
There are four modes. Here's the list of how an array will be converted.
Use Cases
You will need a sort algorithm to decide the winners, thresholds, and so on. In such cases, it will save time to implement a sort algorithm on the taro engine. Moreover, it will work faster than hand-made ones because of lower overhead.
Takes three arguments, "string", "string2", and "mode". Sort both string1 and string2 according to the mode and string, and return the sorted string2.
(Sort string1 according to the mode and return string2 based on the sorted order)
Use Cases
You can obtain the rank of each player in a game with this function as follows:
After this function, you can use "get player from ID" function to access each player to reward them.
(Sort string1 according to the mode and return string2 based on the sorted order)
Use Cases
You can obtain the rank of each player in a game with this function as follows:
After this function, you can use "get player from ID" function to access each player to reward them.