Javascript : allow function to return two string value - coffeescript

I have a JavaScript function that return a comma character for decimal input :
getLocalDecimalSeparator: ->
$translate.instant( 'numbers.decimal_separator' )
the translate is defined like that :
numbers:
decimal_separator: ","
I want my inputs accept dot character also for decimal numbers, I thought about adding an regExp or return the two characters in one function.
I tried this :
getLocalDecimalSeparator: ->
['.' , ',']
but it didn't work !

Related

Twincat 3.0 How to convert string to ASCII code and vice-versa?

Is there any function block which allows conversion of string to ASCII code and vice versa in TwinCAT?
I found this function f_ToCHR, but it only converts one character at a time and for converting the whole string, I would need to put it in a for loop, which would not be optimal.
Is there any function that could do the whole string conversion, not character by character?
You could create a UNION with a string and a byte array. This will put them in the same memory space, and since a string is simply a series of ASCII bytes, the individual character values will end up in each array element.
TYPE testUnion
UNION
stTest : STRING;
arTest : ARRAY[0..79] OF BYTE;
END_UNION
END_TYPE

Parse string to arguments

I have a character vector like so:
string = 'a(0:2), b(3), c(rand(4, 5)*0.1)';
I'd like to use this char array as input arguments to a function. The arguments would then be:
a(0:2)
b(3)
c(rand(4, 5)*0.1)
How can I parse the string into those input arguments?
At first glance, one could split the string with the ', ' separator, but it would fail for the third argument obviously.
A simple solution is using split as following:
expressions = split(string, "), ");
Then add ")" at the end of each string in expressions.

How can I convert a single Character type to uppercase?

All I want to do is convert a single Character to uppercase without the overhead of converting to a String and then calling .uppercased(). Is there any built-in way to do this, or a way for me to call the toupper() function from C without any bridging? I really don't think I should have to go out of my way for something so simple.
To call the C toupper() you need to get the Unicode code point of the Character. But Character has no method for getting its code point (a Character may consist of multiple code points), so you have to convert the Character into a String to obtain any of its code points.
So you really have to convert to String to get anywhere. Unless you store the character as a UnicodeScalar instead of a Character. In this case you can do this:
assert(unicodeScalar.isASCII) // toupper argument must be "representable as an unsigned char"
let uppercase = UnicodeScalar(toupper(CInt(unicodeScalar.value)))
But this isn't really more readable than simply using String:
let uppercase = Character(String(character).uppercased())
just add this to your program
extension Character {
//converts a character to uppercase
func convertToUpperCase() -> Character {
if(self.isUppercase){
return self
}
return Character(self.uppercased())
}
}

Autohotkey: Splitting a concatenated string into string and number

I am using an input box to request a string from the user that has the form "sometext5". I would like to separate this via regexp into a variable for the string component and a variable for the number. The number then shall be used in a loop.
The following just returns "0", even when I enter a string in the form "itemize5"
!n::
InputBox, UserEnv, Environment, Please enter an environment!, , 240, 120
If ErrorLevel
return
Else
FoundPos := RegExMatch(%UserEnv%, "\d+$")
MsgBox %FoundPos%
retur
n
FoundPos, as its name implies, contains the position of the leftmost occurrence of the needle. It does not contain anything you specifically want to match with your regex.
When passing variable contents to a function, don't enclose the variable names in percent signs (like %UserEnv%).
Your regex \d+$ will only match numbers at the end of the string, not the text before it.
A possible solution:
myText := "sometext55"
if( RegExMatch(myText, "(.*?)(\d+)$", splitted) ) {
msgbox, Text: %splitted1%`nNumber: %splitted2%
}
As described in the docs, splitted will be set to a pseudo-array (splitted1, splitted2 ...), with each element containing the matched subpattern of your regex (the stuff that is in between round brackets).

Format String to truncate a number to a specific number of digits

Is there a format string to truncate a number to a specific number of digits?
For example, any number greater than 5 digits i would like to truncate to 3 digits.
132456 -> 132
5000000 -> 500
#Erik : Format specifiers like %2d are specific to a language? I actually want to use it in javascript
Pseudo-Code
Function returning a String, receiving a String representing a Number as a parameter
IF the String has more than 5 characters
RETURN a substring containing the first 3 characters.
ELSE
RETURN the string received as a parameter
END IF
END Function
I assume you refer to printf format strings. I couldn't find anything that will truncate an integer argument (i.e. %d). But you can specify the maximum length of a string by referring to a string format string and specifying lengths via "%<MinLength>.<MaxLength>s".
So in your case you could turn your number arguments into strings and then use "%3.3s".