coffee script switch without break - javascript

Is it possible to use switch in coffeescript without break?
switch code switch (code) {
when 37 then case 37: break;
when 38 then -> case 38: break;
when 39 then case 39: break;
when 40 case 40:
... ...
I thought this will work but failed:
switch code
when 37 then continue
when 38 then continue -> not valid
when 39 then continue
when 40
...

Not really. From the docs:
Switch statements in JavaScript are a bit awkward. You need to remember to break at the end of every case statement to avoid accidentally falling through to the default case. CoffeeScript prevents accidental fall-through, and can convert the switch into a returnable, assignable expression. The format is: switch condition, when clauses, else the default case.
What you can do, though, is specify several values in a case, if they are to be treated equally:
switch day
when "Mon" then go work
when "Tue" then go relax
when "Thu" then go iceFishing
when "Fri", "Sat"
if day is bingoDay
go bingo
go dancing
when "Sun" then go church
else go work

You can use line continuation to help with this. For example:
name = 'Jill'
switch name
when 'Jill', \
'Joan', \
'Jess', \
'Jean'
$('#display').text 'Hi!'
else
$('#display').text 'Bye!'
Check it out in action here.

It's totally possible, just use a classic javascript and pass it through with backtics
`
switch (code) {
case 37:
case 38:
case 39:
case 40:
// do the work of all four
default:
//default
}
`

Old question already, but if you place the commas on the next line, it works as expected, without the backslash line continuation showed by #Ron Martinez
switch code
when 37
, 38
, 39
, 40
console.log "Some Number"
else
console.log "Default"
Which will compile to:
switch (code) {
case 37:
case 38:
case 39:
case 40:
return console.log("Some Number");
default:
return console.log("Default");
}

Related

Why switch false always return first case?

I'm trying to understand switch behaviour when it deals with false.
let grade = 65;
switch(false){
case grade >= 90:
console.log(grade);
console.log("You did great!");
break;
case grade >= 80:
console.log("You did good!");
break;
default:
console.log(grade, "is not a letter grade!");
I don't understand why grade will always hit first case in the code above
I was expecting none of the case being fulfilled because of switch(false), and there should be no console log printed.
swtich compares the expression in the case with the value passed to switch.
(grade >= 90) === false
I strongly recommend only using simple values in cases and using if / else for more complex logic like you have here. Putting expressions in cases is unintuative.
You need to check against the opposite of the expression, because of the double negation.
switch (false) {
case !(grade >= 90): // code
}
vs
switch (true) {
case grade >= 90: // code
}
From MDN:
The switch statement evaluates an expression, matching the
expression's value against a series of case clauses, and executes
statements after the first case clause with a matching value, until a
break statement is encountered. The default clause of a switch
statement will be jumped to if no case matches the expression's value.
More details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
It's because, it compares passed value (false) with case equation (which is also false in first case), so false === false is true (Yes it's ===, not ==), so it's going into the first case.

case:27(ESC) doesn't work with microsoft edge

As the title say's, the other cases work in google chrome, firefox, iexplore and opera. Does someone know why this is and what the correct code for edge is so that the case would work and go to another page (that's what #messagebtn does).
Further i want to thank everyone for their time.
document.addEventListener("keyup",function(e){
var key = e.which||e.keyCode;
switch(key){
//enter
case 13:
document.getElementById("messagebtn").click();
break;
//space
case 32:
document.getElementById("messagebtn").click();
break;
//escape
case 27:
document.getElementById("messagebtn").click();
break;
}
});

How to differentiate key event from numpad and barcode reader?

I'm working on a software that is expected to use a barcode reader. Everything worked fine till a customer started using a new barcode reader and some bindings we got in the numpad keyboard started triggering which did not happened before (we actually tried various barcode readers).
I'm pretty sure it can be fixed from the barcode reader configuration but most of our customers are small shops with no knowledge about stuff like that. So it would be really important for us to fix it from the code.
Here's a mixup code between what I got and what I would like to acomplish, obviouslyI'm ignoring parts of the original code to make it easy readable.
$('body').keydown(function(key){
var keyCode = key.keyCode;
switch(keyCode){
case 96:
case 97:
case 98:
case 99:
case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 110:
if(not_actually_from_keyboard_but_barcode){
//step out
}
else{
doNumPadStuff(keyCode);
}
break;
});
Would appreciate any kind of solution or work around to the problem.
Maybe the below helps
The numeric keypad may produce different keycodes depending on the NumLock state.
$(document).keyup(function(e) {
/* OPTIONAL: Only if you want other elements to ignore event */
e.preventDefault();
e.stopPropagation();
var keyCode = (e.keyCode ? e.keyCode : e.which);
debuger //check what keyCode is saying. to get this press f12 to get browser debugger.
switch(keyCode){
case 96: //numpad 0
case 97: //numpad 1
case 98: //numpad 2
case 99: //numpad 3
case 100: //numpad 4
case 101: //numpad 5
case 102: //numpad 6
case 103: //numpad 7
case 104: //numpad 8
case 105: //numpad 9
case 110: //numpad .
if(not_actually_from_keyboard_but_barcode){
//step out
}
else{
doNumPadStuff(keyCode);
}
break;
});

How to validate currency value by Locale?

We have a big web application, that has portals in different countries.
In these countries the money value is inserted differently.
Exists a solution which can validate the money value by locale?
i would recommend the http://openexchangerates.github.io/money.js/ this would provide you the live currency conversion rates.
for the formatting purpose look into AccountingJs https://josscrowcroft.github.io/accounting.js/
you can provide a dropdown next to the currency text box. so user can select the approperiate currency and can fill the value.
and in backend you need to get both value. entered value by user and the currecy selected.
based on location, you can change the selected currency by default.
"We have a big web application, that has portals in different countries"
I highly recommend setting up localization with Globalize. After setting it up correctly, all you have to do is change the locale of the user and all the locale dependent formatting (e.g currency, date, number, units, etc.) fixes itself.
If you want to use it just for currency, see their currency module: Globalize Currency Module
I've implemented it now via an own function:
/*
* checks if a given money value is a correct one via locale language
* */
function isMoney(moneyValue) {
var currentSelectedLanguage = $('#currentSelectedLanguage').val();
switch (currentSelectedLanguage)
{
// checks for , as separator and . as decimal separator e.g.: 1,234,567,890.98 - also allows without , as separator e.g.: 1234567890.98
case "en":
return /(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(\.[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for . as separator and , as decimal separator e.g.: 1.234.567.890,98 - also allows without . as separator e.g.: 1234567890,98
case "de":
case "el":
case "es":
case "hr":
case "ro":
case "sl":
case "sr":
return /(?=.)^\$?(([1-9][0-9]{0,2}(\.[0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for space as separator and , as decimal separator e.g.: 1 234 567 890,98, also allows without space as separator e.g.: 1234567890,98
case "bg":
case "cs":
case "hu":
case "pl":
case "ru":
case "sk":
case "uk":
return /(?=.)^\$?(([1-9][0-9]{0,2}( [0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for . as separator and , as decimal separator e.g.: 1.234.567.890,98 - also allows without . as separator e.g.: 1234567890,98
default:
return /(?=.)^\$?(([1-9][0-9]{0,2}(\.[0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
}
}
Only downside is, that I've to extend it on my own if there is a new locale in our application

CRM 2011 Contact Address Set up for Both Domestic and Internationl

Great community here!
I am currently working within a 2011 CRM system that has a new requirement of the end users updating Contact Addresses from domestic (US) addressing to International. The system was originally set up for just Domestic addresses.
My problems are with the Telephone field and zip code. Both have custom javascript to format the integers in the field and ensure length compliance.
Now, is it possible to manipulate the JavaScript below to handle the both domestic and international phone numbers?
Below is the function that is called on an onchange event:
function checkPhoneNumberFormat(obj) {
var phoneNumber = obj.getEventSource().getValue();
if (phoneNumber != null) {
var sTmp = phoneNumber.replace(/[^0-9]/g, "");
switch (sTmp.length) {
case 10:
obj.getEventSource().setValue("(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4));
break;
default:
alert("Phone must contain 10 numbers.");
break;
}
}
}
Somewhat new to java scripting, just looking for some feed back, on how to manipulate this code. It would be great if it had the ability to auto format 9 digit as well as 14 digit phone numbers. And only allow numbers of those lengths to be passed through on the save.
Any comments or ideas are greatly appreciated.
Thank You!
Yes Pawel, this is pretty simple to do:
put another case statement like this (this means do nothing, but don't throw that alert up when the length is 14, since you don't want to do any special formatting)
case 14:
break;
Then in your onsave - you can do the same thing but block saving in the "Default" case.
default:
alert("Phone number must contain 10 or 14 digits");
executionObj.getEventArgs().preventDefault();
break;
One thing to note about phone numbers is that often there can be extensions, this doesn't seem to be handled by your code, but maybe that is not relevant to your application.

Categories

Resources