How to enter cents in ext Js 4 number field? - extjs4

when trying to enter 12.80 in my number field, the last 0 get disappeared. How can i display the number field??

It is expected behavior. If you want a custom implementation, you have to work a round the parseFloat. Because that (native) function will strip trailing decimal zeros (and leading zeros).
e.g. parseFloat("00100.00100") results in 100.001
The only way around, is by overriding at least these two functions:
parseValue : function(value) {
//provide own implementation
console.log('parseValue', value);
return value;
},
valueToRaw: function(value) {
//provide own implementation
console.log('valueToRaw', value);
return value;
}
Here is the source

Related

Any difference between using this to access the watcher's value and using the argument?

This is not particularily important, but I am trying to learn Vue.
Is there any difference between which of these two values is used? They should always be the same, right?
For example:
watch: {
myVar(value) {
console.log('this.myVar, value, this.myVar === value', this.myVar, value, this.myVar === value)
}
}
Is the inclusion of this argument just for convenience/readability? Or is there some reason one would use one or the other?
this.myVar and value are the same in this case. The use of value is tiny bit more efficient because it doesn't require to access this property again, and it's shortened to one-letter variable in minified build. More importantly, it can be named for readability or brevity, and can be complemented with another parameter:
myVar(value, oldValue) {
...
In case a watcher is asynchronous, value captures the original value, while this.myVar contains the current value, which may be desirable or not depending on a case:
async myVar(value, oldValue) {
...
console.log(this.myVar !== value);

The least amount of letters in a list of Palindromes

So the question is giving a BIG string, break it up, find the palindromes and then find the shortest length within those sets of palindromes. Here's the code
Main Function
fun main(){
val bigArray = "Simple, given a string of words, return the length of acdca the " +
"shortest valav words String will never be empty and you do not need dad to account for different data types."
println(leastP(bigArray))
}
The Custom Function
fun leastP(s: String): Int {
val sSplit = listOf(s.split(""))
val newArray = listOf<String>()
for (i in sSplit){
for (j in i.indices){
if (isPalindrome3(i[j])) newArray.plus(j)
}
}
return newArray.minOf { it.length }
}
private fun isPalindrome3(s: String): Boolean {
var i = 0
var j = s.length -1
while (i < j){
if (s[i++].lowercaseChar() != s[j--].lowercaseChar()) return false
}
return true
}
}
I get this error
Not sure whats going on or where I messed up. Any help is appreciated.
In addition to the array problem identified in Tenfour04's answer, the code has an additional problem:
split("") splits the string into individual characters, not just individual words. 
If you debug it, you'll find that isPalindrome3() is being called first on an empty string, then on "S", then on "i", and so on.
That's because the empty string "" matches at every point in the input.
The easiest fix is to call split(" "), which will split it at space characters.
However, that might not do exactly what you want, for several reasons: it will include empty strings if the input has runs of multiple spaces; it won't split at other white space characters such as tabs, newlines, non-breaking spaces, en spaces, etc.; and it will include punctuation such as commas and full stops. Splitting to give only words is harder, but you might try something like split(Regex("\\W") to include only letters, digits, and/or underscores. (You'll probably want something more sophisticated to include hyphens and apostrophes, and ensure that accented letters etc. are included.)
There's a further issue that may or may not be a problem: you don't specify a minimum length for your palindromes, and so words like a match. (As do empty strings, if the split produces any.) If you don't want the result to be 0 or 1, then you'll also have to exclude those.
Also, the code is currently case-sensitive: it would not count "Abba" as a palindrome, because the first A is in upper case but the last a isn't. If you wanted to check case-insensitively, you'd have to handle that.
As mentioned in a comment, this is the sort of thing that should be easy to test and debug. Short, self-contained functions with no external dependencies are pretty easy to write unit tests for. For example:
#Test fun testIsPalindrome3() {
// These should all count as palindromes:
for (s in listOf("abcba", "abba", "a", "", "DDDDDD"))
assertTrue(isPalindrome3(s))
// But these shouldn't:
for (s in listOf("abcbb", "Abba", "a,", "abcdba"))
assertFalse(isPalindrome3(s))
}
A test like that should give you a lot of confidence that the code actually works. (Especially because I've tried to include corner cases that would spot all the ways it could fail.) And it's worth keeping unit tests around once written, as they can verify that the code doesn't get broken by future changes.
And if the test shows that the code doesn't work, then you have to debug it! There are many approaches, but I've found printing out intermediate values (whether using a logging framework or simply println() calls) to be the simplest and most flexible.
And for reference, all this can be rewritten much more simply:
fun String.leastP() = split(Regex("\\W"))
.filter{ it.length >= 2 && it.isPalindrome() }
.minOfOrNull{ it.length }
private fun String.isPalindrome() = this == reversed()
Here both functions are extension functions on String, which makes them a bit simpler to write and to call. I've added a restriction to 2+ characters. And if the input is empty, minOfOrNull() returns null instead of throwing a NoSuchElementException.
That version of isPalindrome() isn't quite as efficient as yours, because it creates a new temporary String each time it's called. In most programs, the greater simplicity will win out, but it's worth bearing in mind. Here's one that's longer but as efficient as in the question:
private fun String.isPalindrome()
= (0 until length / 2).all{ i -> this[i] == this[length - i - 1]}
Your newArray is a read-only list. When you call plus on it, the function does not modify the original list (after all, it is read-only). The List.plus() function returns a new list, which you are promptly discarding by not assigning it to any variable or property.
Then it crashes because it is unsafe to call minOf on an empty list.
Two different ways to fix this:
Make the newArray variable a var and replace newArray.plus(j) with newArray += j. The += operator, when used on a read-only list that is assigned to a mutable var variable, calls plus() on it and assigns the result back to the variable.
Initialize newArray as a MutableList using mutableListOf() and replace newArray.plus(j) with newArray += j. The += operator, when used with a MutableList, calls add() or addAll() on the MutableList, so it directly changes the original instance.
I didn’t check any of your logic. I’m only answering the question about why it’s crashing.
But as Gidds points out, the logic can be simplified a ton to achieve the same thing you’re trying to do using functions like filter(). A few odd things you’re doing:
Putting the result ofstring.split("") in a list for no reason
Using "" to split your string so it’s just a list of one-character Strings instead of a list of words. And you’re ignoring punctuation.
Filling newArray with indices so minOf will simply give you the first index that corresponded with being a palindrome, so it will always be 0.
Here’s how I might write this function (didn’t test it):
fun leastP(s: String): Int {
return s.split(" ")
.map { it.filter { c -> c.isLetter() } }
.filter { isPalindrome3(it) }
.minOfOrNull { it.length } ?: 0
}

create Flag Enum from int

I have an interesting situation, we have a settings database that store enum values as an int, the back end knows how to handle this but on our mobile app I'm struggle to figure how how to reproduce this.
currently I have the following:
enum class ProfileDisplayFlags(val number:Int)
{
Address(0x01),
PhoneNumber(0x02),
Email(0x04),
TwitterHandler(0x08),
GithubUsername(0x10),
Birthday(0x20)
}
for example, if I get the setting value from the database of 3, it should return on the app that I want to display Address & PhoneNumber.
I'm lost on how I can do this, I found a solution on finding a single value but I need to be able to get multiple back.
Each flag value is a different unique bit set to one, so you can filter by which ones are not masked (bitwise AND) to zero by the given flags.
companion object {
fun matchedValues(flags: Int): List<ProfileDisplayFlags> =
values().filter { it.number and flags != 0 }
}
To convert back, you can use bitwise OR on all of them.
fun Iterable<ProfileDisplayFlags>.toFlags(): Int =
fold(0) { acc, value -> acc or value.number }

How to pass variable content to a parameter value in dart/flutter?

I may be missing something obvious, but I'm having trouble passing a variable to a parameter or widget in flutter/dart. For example, let's say I have some variable:
String col = 'red';
and I want to pass this to a color parameter to get the equivalent of
color: Colors.red
The difficult thing is that any way I try to pass the value ends up passing a string (which isn't accepted), including trying to pass just the value of col directly or trying to build a function that returns Colors.col.
I think what I need is something like a function like
setColor(String str) {
return Colors.str;
}
but, as you might expect, this returns "The getter 'str' isn't defined for the type 'Colors'." (And similarly for:
setColor(String str) {
return Colors.$str;
}
I know one option is to create a function using a bunch of if's, like
setColor(String str) {
if (str==red) return Colors.red;
if (str==blue) return Colors.blue;
if (str==green) return Colors.green;
etc.
}
but I'd prefer a more elegant option if one is available.
EDIT: It looks like this isn't quite as easy as I'd hoped (see answers and comments to answers below).
The library Supercharged is your best solution. You can try it this way (Hex text or HTML color):
"#ff00ff".toColor(); // pink
"ff0000".toColor(); // red
"00f".toColor(); // blue
"red".toColor(); // red (HTML color name)
"deeppink".toColor(); // deep pink (HTML color name)
Of course, this function depends on extension. Extension methods, introduced in Dart 2.7.
Edit:
extension MainAxisAlignmentExtension on String {
MainAxisAlignment get mainAxis {
switch (this.toUpperCase()) {
case "BETWEEN":
return MainAxisAlignment.spaceBetween;
case "AROUND":
return MainAxisAlignment.spaceAround;
case "EVENLY":
return MainAxisAlignment.spaceEvenly;
case "CENTER":
return MainAxisAlignment.center;
case "START":
return MainAxisAlignment.start;
case "END":
return MainAxisAlignment.end;
default:
return MainAxisAlignment.start;
}
}
}
print("Between".mainAxis);
print("AROUND".mainAxis);
Instead of passing Color as String, you can use type Color
Color col = Colors.red;
If you want to continue using String to store color you can use the hex value
String col = "#FF5733";
And use a function to convert it to color
Color hexToColor(String code) {
return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
}
Example:
Color newColor = hexToColor(col);
Hope this is what you are looking for!
You can pass colors from one widget to another by declaring color as a variable.
final Color redColor=Colors.red;
Then you can pass it to a widget as an argument (Passing color to a widget):
Widget something(Color redColor){
Text("It Works!",style: TextStyle(color: redColor))
}
If you want to pass it from one stateless or stateful widget to another:
class DetailScreen extends StatelessWidget {
// Declare a field that holds the color.
final Color redColor;
// In the constructor, require a Color.
DetailScreen({Key key, #required this.redColor}) : super(key: key);
// Your widget build and other things here
}
Then get the value by calling:
Text("It Works!",style: TextStyle(color: widget.redColor))

How to avoid if else or switch case whe dealing with enums?

I have a member variable that tells units for a value I have measured like centimeters,kilometers,seconds,hours etc.
Now these are enums,
When I display a corresponding string, I have created a method that returns corresponding string for these enums.
Unlike Java, enums here cant have other properties associated with them.
So I have to explicitly do a if-else-if chain or a switch case to return the correct string.
I am new to Objective C. any good practice that I should be following in such scenarios ?
afaik Objective-C enums are just old-school C enums... so maybe you can use an integer value for them?
I guess if your enum values started at 0 and increased you could use some sort of array access:
const char *distanceUnitToString2(enum DistanceUnit unit)
{
const char *values[] = {
"cm",
"m",
"km"
};
// do some sanity checking here
// ...
return values[unit];
}
But this feels a little flaky to me. What if you have negative values, or you are using bitmask-style enum values like 1 << 8? You are going to end up using a very large array.
You also could use a switch and improve it a little with a macro. Something like this:
const char *distanceUnitToString(enum DistanceUnit unit)
{
#define CASE(UNIT, STRING) case (UNIT): return (STRING)
switch (unit) {
CASE(kCentimeters, "cm");
CASE(kMeters, "m");
CASE(kKiloMeters, "km");
default:
// should not get here
assert(0);
break;
}
#undef CASE
}
But you don't really save that much vs. not using the macro.
Martin James's comment is the right answer. And use a definition of the enum like:
enum units { cm = 0, m, km };
that way you can be sure that your enum translates to the correct index values.