Human readable key names for characters - dart

I want to trigger some code if the 'A' key is pressed:
document.on.keyDown.add((event) {
if (event.keyIdentifier == 'U+0041') {
...
}
});
Using the unicode code (U+0041) of the character is not very readable. Is there any method I can use to convert the code to a character or vice versa? I would like to do something like this:
document.on.keyDown.add((event) {
if (event.keyIdentifier == unicodeCode('A')) {
...
}
});

I hope this will help:
document.on.keyPress.add((KeyboardEvent event) {
final String char = new String.fromCharCodes([event.charCode]);
print('Key: $char');
if (event.keyIdentifier == 'U+0041') {
print('$char pressed.');
}
if (char == 'a') {
print('Lowercase "a" has been pressed.');
}
});
Currently the keyDown event doesn't provide the appropriate charCodes.

Related

How to make sure a string of pure whitespace is invalid with a succinct code in Swift

I am making a section with TextFields and Button("Continue") and then use .disable(isValidAddress) modifier to a whole section to disable the button. The code works well, but I am seeking any solution to make it more succinct with no need to write .hasPrefix() or .hasSuffix() to all parameters one by one.
var isValidAddress: Bool {
if name.hasPrefix(" ") || street.hasPrefix(" ") || city.hasPrefix(" ") || country.hasPrefix(" ") {
return false
} else if name.hasSuffix(" ") || street.hasSuffix(" ") || city.hasSuffix(" ") || country.hasSuffix(" ") {
return false
}
return true
}
var isValidAddress: Bool {
[street, name, city, etc..].reduce(true, { result, text in
if text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return false
} else {
return result
}
})
}
You can add them to an array and trim white space and check if empty in a loop
func isValidAddress() -> Bool {
for field in [name, street, city, country] {
if field.trimmingCharacters(in: .whitespaces).isEmpty { return false }
}
return true
}
I used a function here but a computed property works just as well.
If you don't mind moving some complexity into extensions, you can tidy up the call site. This makes it more readable, and less error prone - you can easily add in another field without mistakes for example.
extension String {
func hasPrefixOrSuffix(_ s: String) -> Bool {
hasPrefix(s) || hasSuffix(s)
}
var noSpacesAtEnds: Bool {
!hasPrefixOrSuffix(" ")
}
}
let isValid = [name, street, city, country]
.allSatisfy(\.noSpacesAtEnds)
If you do mean none of them are "all whitespace strings" then:
extension String {
var isNotAllWhitespace: Bool {
!trimmingCharacters(in: .whitespaces).isEmpty
}
}
let isValid = [name, street, city, country]
.allSatisfy(\.isNotAllWhitespace)
One benefit of having named functions for these things is you can test the pieces, so you can write a test to ensure that isNotAllWhitespace works the way you expect. You couldn't if the logic like name.hasPrefix(" ") || street.hasPrefix(" ") is mixed in with your isValidAddress function.

xtext Format comment with AbstractFormatter2,

I am using AbstractFormatter2 with xtext 2.9.2
I want to put the comment in a specific column
My syntax looks something like this
terminal SL_COMMENT : '*' !('\n'|'\r')* ('\r'? '\n')?;
So far, I tried to put multiple spaces before my comment, but this doesn't work either
def dispatch void format(Model model, extension IFormattableDocument document) {
SL_COMMENTRule.prepend[space " "]
model.getEnte.format;
model.getMapset.format;
}
can anyone guide my how to format comments in general then how to put them in a specific column
comment formatting is done by commentreplacers
thus something like the following should work
override createCommentReplacer(IComment comment) {
val EObject grammarElement = comment.getGrammarElement();
if (grammarElement instanceof AbstractRule) {
val String ruleName = grammarElement.getName();
if (ruleName.startsWith("SL")) {
if (comment.getLineRegions().get(0).getIndentation().getLength() > 0) {
return new SinglelineDocCommentReplacer(comment, "//") {
override configureWhitespace(WhitespaceReplacer leading, WhitespaceReplacer trailing) {
leading.getFormatting().space = " ";
}
};
} else {
return new SinglelineCodeCommentReplacer(comment, "//") {
override configureWhitespace(WhitespaceReplacer leading, WhitespaceReplacer trailing) {
leading.getFormatting().space = " ";
}
}
}
}
}
super.createCommentReplacer(comment)
}

How to compare Obj C enum in Swift?

I am using FreeStreamer in Swift and am trying to set the onStateChange block.
audioStream.onStateChange = { (state) in
if state == kFsAudioStreamBuffering {
//blah
}
}
I am getting this error:
Binary operator '==' cannot be applied to operands of type '(FSAudioStreamState)' and 'FSAudioStreamState'
Edit: Still the same error without the parentheses around state in the block params
EDIT: As a temporary fix, state.value == kFsAudioStreamBuffering.value works
try putting a dot (.) before kFsAudioStreamBuffering
something like this:
if state == .kFsAudioStreamBuffering {
//blah
}
UPDATE: Try this instead
audioStream.onStateChange = { state in
if state.value == kFsAudioStreamBuffering.value {
//blah
}
}
It should be something like this to make it work.
self.audioControler?.onStateChange = { (state:FSAudioStreamState) -> Void in
switch state {
case .fsAudioStreamRetrievingURL:

AngularJS: Using If condition in text input

I have an input type as text to select dates and I want that Its default value should be the earliest amongst the JSON data having following structure:
testData=
{
"name":"ABC", transactions:[
{"tranid":01,"trandate":"01/02/2010"},
{"tranid":02,"trandate":"01/02/2012"}
]
}
I want to iterate through transactions and Dispay the earliest date in the text input(i.e 01/02/2010)
Please assist how to do it in angular.
Hi please see here: http://jsbin.com/reyog/1/edit
function finderliest()
{
angular.forEach(testData.transactions, function(transaction){
if($scope.erliest === undefined || transaction.trandate < $scope.erliest ){
$scope.erliest = transaction.trandate;
}
});
};
var len=testData.transactions.trandate.length;
if(len>0)
{
for (i=0;i<len;i++)
{
for (j=1;j<len;j++)
{
var dateone=testData.transactions.trandate[i];
var datetwo=testData.transactions.trandate[j];
//(use this logic or else logic whichever you like) http://www.c-sharpcorner.com/UploadFile/8911c4/how-to-compare-two-dates-using-javascript/
}
}
}

How to split string in View in MVC?

In my View I have this condition:
#if (User.Identity.Name == "abc")
{
... do this!
}
How can I split this string "User.Identity.Name" in View (in MVC) so I can create new condition, something like this:
string last = User.Identity.Name.Substring(User.Identity.Name.LastIndexOf('.') + 1);
if (last == "this")
{
... do this!
}
thanks.
you can do it like this:
#{
var temp= User.Identity.Name.Split('.');
if (temp[temp.Count() - 1] == "this")
{
}
}
or if "." will be only one time in that string then you can hardcode like this:
#{
var temp= User.Identity.Name.Split('.');
if (temp[1] == "this")
{
}
}
see below example , which finds last word of a string after last dot in it.
String str = "abc.def.xyz";
String last = str.substring(str.lastIndexOf('.')+1);
System.out.println(last);
if(last.equals("something")){
//do this
}
else{
//do this
}
here last comes as xyz

Resources