Cannot send email using switch statement - javascript

I'm using node mailer to send out the emails and have followed a youtube tutorial, but I did some changes. I want to the user to send an email to any of the 31 people the he/she will choose, and so we will call the number assigned to people being chosen is classNum.
When I assign an email to the to parameter, the code works, but if I remove it and use the switch statement, it give the 500 error.
const sendMail = (email, subject, text,classNum, cb) => {
const mailOptions = {
from: email,
subject,
text,
to: " "
};
switch(classNum) {
case 25:
mailOptions.to = 'thisIsAnEmail#gmail.com'
break;
}
This is probably the chunk of code that has a problem, please tell me if I need to add/post more code from the source code.

A switch expression uses strict equality
With the debugging info I asked for in the comments, you are passing in a string and you are comparing it against a number. So "25" === 25 is false.
You either need to make it a string
switch(classNum) {
case "25":
or convert classNum to a number
switch(Number(classNum)) {
case 25:

As #epascarello indicated, you have a string in classNum, not a number; as a result, you "case" doesn't match. You can fix this one of two ways:
Make sure classNum is a number, rather than a string (probably better, since classNum implies a number in the variable name) by using classNum = Number(classNum); OR
change your switch case to be a string: case '25':
I think option 1 is probably better, again because the variable name implies a number value.

Related

can not set limit on regex

I have this function called vatValidate, which is used to validate VAT format of user inputs. Currently it performs validation for two countries, Austria and Italy as default case. For each, I specified the expected user input sequence for related VAT format with regular expression.
function vatValidate() {
let vatFormat;
let countryCode = document.getElementById('countries').value;
switch (countryCode) {
case 'Austria':
countryCode = 'AT';
vatFormat = /[U]{1}[0-9]{8}/;
break;
default:
countryCode = 'IT';
vatFormat = /[0-9]{11}/;
}
let vatNumber = document.getElementById('pivaid').value;
let vat = countryCode + vatNumber;
if (vatFormat.test(vat)) {
console.log('Correct');
} else {
console.log('Error');
}
}
vatValidate()
If user input matches the predefined sequence, the function logs true else false. It works fine, but the problem is, that the regex code I defined, does not enforce the length of the sequence. As I have studied to do so, for instance for Austria, I have to define the regex using ^ and $ resulting in: /^[U]{1}[0-9]{8}$/
Apparently this should work just fine, as I verified it in regex101.com and can be seen below:
Now the problem is, that as soon as I add ^ and $ in my code, it won't work any longer and it just logs an error! My development environment is Laravel and this code is executed in a script tag inside a blade.
the problem is solved for the case of Austria with THIS REGEX expression:
/^[A]{1}[T]{1}[U]{1}[0-9]{8}$/

What is the least issue prone way to separate the first set of numbers from a string?

For context, I have a system that asks a user to give a rating between 1-10. Afterwards, I give them the option to leave any additional notes they would like to leave.
Unfortunately what seems to happen half of the time is that a customer will leave notes along with the initial 1-10 rating. With the interface I am using I have no way of restricting the input to numbers only.
The best solution I would be going for is to simply separate the number from the statement into two separate variables or into an array[0-1].
So basically I have
var responseA = (customer input here)
Then I would need to remove any additional comments they put into the first response
responseA = responseA.someMagic() //This is where I need help
And continue to deal with the remaining interactions and store them as such
var responseB = responseA[1] + ". " + (customer input 2 here)
This will take the second portion of the first response that includes notes and combines those notes with the second response which contains the second portion of notes. If it's possible to have some sort of way to detect that there were notes in the first place that would also be good to avoid
". Thank you."
as opposed to
"Thank you."
Because of the concatenation of the first string in situations it is not necessary.
edit: After rereading this it still seems confusing if you don't know exactly what is going on. Let me show you a real world example:
Q1: Please give a rating between 1-10:
"10! You were very helpful, thank you!"
Q2: If you have any notes you may leave them here:
"Very helpful."
This means that when the system that deals with the ratings from question 1 receives additional strings, it won't count it as a number. I want to remove everything after the 10 and add it to Q2's response to not lose any of their response, but also not store it in the wrong place.
Here is a possibility, using regex to check and split apart the first value:
const combineResponses = (q1, q2) => {
const matches = q1.match(/^(\d*)\W*(.*)/)
return matches[2] ? {
rating: matches[1],
comment: [matches[2], q2].join('. ')
} : {
rating: matches[1],
comment: q2
}
}
console.log(combineResponses(
"10! You were very helpful, thank you!",
"Very helpful."
))
console.log(combineResponses(
"10",
"Very helpful."
))
console.log(combineResponses(
"oops",
"Very helpful."
))
It does nothing to check that the rating is in the right range. Nor does it handle, say 7.4, although it would be easy enough to modify for the latter.
But if you can find a way to separate your initial input in a cleaner way, it would definitely be better to avoid this.
If you are sure the string will always begin with a number for the rating, you could call parseInt() on the string to find out what the number is. If there is a number, you could then use indexOf() to find the first time it appears and cut the string there, otherwise just assume the whole thing was a string.
const userInputs = ['1 thank you', '10thanks', ' 5. random text', 'no rating'];
let length = 1;
let index;
let rating;
let response;
for (const i of userInputs) {
rating = parseInt(i);
if (rating) {
if (rating > 9) {
length = 2;
}
index = i.indexOf(rating);
response = i.substring(index + length).trim();
} else {
response = i;
}
console.log(`${i}, rating: ${rating}, response: ${response}`);
}

How to compare string variable using JavaScript

I am trying to compare the variable using javascipt:
response value: ""test#gmail.com""
response value i am getting it from server.
var str1="test#gmail.com"
var str2 =response;
if(str1===str2)
{
//
}
However not getting the proper result.
any idea on how to compare them ?
There are a few ways to achieve your goal:
1) You can remove all " from the response when doing your equality check:
if(str1===str2.replace(/['"]+/g, ''))
{
//
}
2) Change your server code to not include ". Doing so, would mean that your Javascript will not need to change.
3) Last option, add " to your str1:
var str1='"test#gmail.com"'
var str2 =response;
if(str1===str2)
{
//
}
Obviously I don't know enough about your requirements to tell you which one you should do, but my suggestion would be choice #2 because I think it's strange to return an email address wrapped in quotes, otherwise I would recommend #1.
You are trying to compare '""test#gmail.com""' with 'test#gmail.com'. They would never be equal.
Actually ""test#gmail.com"" is not a valid string. It might have been represented as '""test#gmail.com""' and "test#gmail.com" is a valid string (Same as 'test#gmail.com').

indexOf within Switch

I have a Javascript-based bot for a Xat chatroom which also acts as an AI. I've recently decided to redo the AI part of it due to it becoming an absolutely massive chain of else if statements, becoming nearly impossible to work with.
I did some research and came up with a new idea of how to handle responses. I'll give you the code segment first:
function msgSwitch(id,msgRes) {
var botResponse = [];
switch (msgRes) {
case (msgRes.indexOf("hi") !=-1):
botResponse.push("HELLO. ");
case (msgRes.indexOf("how are you") !=-1):
botResponse.push("I AM FINE. ")
case (msgRes.indexOf("do you like pie") !=-1):
botResponse.push("I CAN'T EAT. THANKS, ASSHAT. ")
default:
respond (botResponse);
spamCount(id);
break;
}
}
The idea here is to check msgRes (the user's input) and see how many cases it matches. Then for each match, it'll push the response into the botResponse array, then at the end, it'll reply with all the messages in that array.
Example
User Msg: Hi! How are you?
msgRes: hi how are you
Bot Matches:
hi > pushes HELLO. to array
how are you > pushes I AM FINE. to array
Bot Responds: HELLO. I AM FINE.
This in turn saves me the trouble of having to write an if for each possible combination.
However, after looking into it some more, I'm not sure if it's possible use indexOf inside of a switch. Does anyone know of a way around this or have a better idea for handling responses in the same manner?
EDIT:
To Avoid the XY Problem (To clarify my problem)
I need a clean alternative to using a massive chain of else if statements. There are going to be hundreds of word segments that the bot will respond to. Without the ability for it to keep searching for matches, I'd have to write a new else if for every combination.
I'm hoping for a way to have it scan through every statement for a match, then combine the response for each match together into a single string.
EDIT 2:
I should also add that this is being ran on Tampermonkey and not a website.
you just need to compare to true instead of msgRes (since cases use === comparison), and use break to prevent the annoying fall-though of the switch behavior:
function msgSwitch(id,msgRes) {
var botResponse = [];
switch (true) {
case (msgRes.indexOf("hi") !=-1):
botResponse.push("HELLO. "); break;
case (msgRes.indexOf("how are you") !=-1):
botResponse.push("I AM FINE. "); break;
case (msgRes.indexOf("do you like pie") !=-1):
botResponse.push("I CAN'T EAT. THANKS, ASSHAT. "); break;
default:
respond (botResponse);
spamCount(id);
break;
}
}
This is a perfectly valid logical forking pattern, known as an "overloaded switch". A lot of folks might not realize that each case: is an expression, not just a value, so you could even put an IIFE in there if needed...
My two cents for the gist of what you're trying to do:
function msgSwitch(id, msgRes) {
var seed = {'hi': 'HELLO. ', 'how are you': 'I AM FINE'};
var botResponse = [];
for (var key in seed) {
if (msgRes.indexOf(key) !== -1) {
botResponse.push(seed[key]);
}
}
}
In my opinion it is easier to change this program as you only have to edit the seed if you have more responses in the future. You can even stash the seed on some json file and read it (via ajax) so the program does not need to be changed if there are additional messages.

How to test valid UUID/GUID?

How to check if variable contains valid UUID/GUID identifier?
I'm currently interested only in validating types 1 and 4, but it should not be a limitation to your answers.
Currently, UUID's are as specified in RFC4122. An often neglected edge case is the NIL UUID, noted here. The following regex takes this into account and will return a match for a NIL UUID. See below for a UUID which only accepts non-NIL UUIDs. Both of these solutions are for versions 1 to 5 (see the first character of the third block).
Therefore to validate a UUID...
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i
...ensures you have a canonically formatted UUID that is Version 1 through 5 and is the appropriate Variant as per RFC4122.
NOTE: Braces { and } are not canonical. They are an artifact of some systems and usages.
Easy to modify the above regex to meet the requirements of the original question.
HINT: regex group/captures
To avoid matching NIL UUID:
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
If you want to check or validate a specific UUID version, here are the corresponding regexes.
Note that the only difference is the version number, which is explained in 4.1.3. Version chapter of UUID 4122 RFC.
The version number is the first character of the third group : [VERSION_NUMBER][0-9A-F]{3} :
UUID v1 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v2 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v3 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v4 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v5 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
regex to the rescue
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test('01234567-9ABC-DEF0-1234-56789ABCDEF0');
or with brackets
/^\{?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}‌​\}?$/
If you are using Node.js for development, it is recommended to use a package called Validator. It includes all the regexes required to validate different versions of UUID's plus you get various other functions for validation.
Here is the npm link: Validator
var a = 'd3aa88e2-c754-41e0-8ba6-4198a34aa0a2'
v.isUUID(a)
true
v.isUUID('abc')
false
v.isNull(a)
false
If you use the uuid package, this package brings a boolean validation function where it tells you if a uuid is valid or not.
Example:
import { validate as isValidUUID } from 'uuid';
if (!isValidUUID(tx.originId)) {
return Promise.reject('Invalid OriginID');
}
thanks to #usertatha with some modification
function isUUID ( uuid ) {
let s = "" + uuid;
s = s.match('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
if (s === null) {
return false;
}
return true;
}
Beside Gambol's answer that will do the job in nearly all cases, all answers given so far missed that the grouped formatting (8-4-4-4-12) is not mandatory to encode GUIDs in text. It's used extremely often but obviously also a plain chain of 32 hexadecimal digits can be valid.[1] regexenh:
/^[0-9a-f]{8}-?[0-9a-f]{4}-?[1-5][0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$/i
[1] The question is about checking variables, so we should include the user-unfriendly form as well.
Why are there dashes in a .NET GUID? - Stack Overflow plus Accepted answer
Test and validate a GUID (guid.us)
Guid.ToString Method (String) (MSDN)
All type-specific regexes posted so far are failing on the "type 0" Nil UUID, defined in 4.1.7 of the RFC as:
The nil UUID is special form of UUID that is specified to have all 128 bits set to zero: 00000000-0000-0000-0000-000000000000
To modify Wolf's answer:
/^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$/i
Or, to properly exclude a "type 0" without all zeros, we have the following (thanks to Luke):
/^(?:[0-9a-f]{8}-?[0-9a-f]{4}-?[1-5][0-9a-f]{3}-?[89ab][0-9a‌​-f]{3}-?[0-9a-f]{12}‌​|00000000-0000-0000-‌​0000-000000000000)$/‌​i
if you use the uuid package, you can import the validate and pass the id into it
const { v4: uuidv4, validate } = require('uuid');
const { id } = request.params;
validate(id) ? true : false;
I think Gambol's answer is almost perfect, but it misinterprets the RFC 4122 § 4.1.1. Variant section a bit.
It covers Variant-1 UUIDs (10xx = 8..b), but does not cover Variant-0 (0xxx = 0..7) and Variant-2 (110x = c..d) variants which are reserved for backward compatibility, so they are technically valid UUIDs. Variant-4 (111x = e..f) is indeed reserved for future use, so they are not valid currently.
Also, 0 type is not valid, that "digit" is only allowed to be 0 if it's a NIL UUID (like mentioned in Evan's answer).
So I think the most accurate regex that complies with current RFC 4122 specification is (including hyphens):
/^([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[0-9a-d][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i
^ ^^^^^^
(0 type is not valid) (only e..f variant digit is invalid currently)
A slightly modified version of the above answers written in a more concise way. This will validate any GUID with hyphens (however easily modified to make hyphens optional). This will also support upper and lower case characters which has become the convention regardless of the specification:
/^([0-9a-fA-F]{8})-(([0-9a-fA-F]{4}\-){3})([0-9a-fA-F]{12})$/i
The key here is the repeating part below
(([0-9a-fA-F]{4}\-){3})
Which simply repeats the 4 char patterns 3 times
If someone is using yup , JavaScript schema validator library, This functionality can be achieved with below code.
const schema = yup.object().shape({
uuid: yup.string().uuid()
});
const isValid = schema.isValidSync({uuid:"string"});
Use the .match() method to check whether String is UUID.
public boolean isUUID(String s){
return s.match("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
}
A good way to do it in Node is to use the ajv package (https://github.com/epoberezkin/ajv).
const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true, useDefaults: true, verbose: true });
const uuidSchema = { type: 'string', format: 'uuid' };
ajv.validate(uuidSchema, 'bogus'); // returns false
ajv.validate(uuidSchema, 'd42a8273-a4fe-4eb2-b4ee-c1fc57eb9865'); // returns true with v4 GUID
ajv.validate(uuidSchema, '892717ce-3bd8-11ea-b77f-2e728ce88125'); // returns true with a v1 GUID
Versions 1 to 5, without using a multi-version regex when version is omitted.
const uuid_patterns = {
1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
};
const isUUID = (input, version) => {
if(typeof input === "string"){
if(Object.keys(uuid_patterns).includes(typeof version === "string" ? version : String(version))){
return uuid_patterns[version].test(input);
} else {
return Object.values(uuid_patterns).some(pattern => pattern.test(input));
}
}
return false;
}
// Testing
let valid = [
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
];
let invalid = [
'',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx',
'A987FBC94BED3078CF079141BA07C9F3',
'934859',
'987FBC9-4BED-3078-CF07A-9141BA07C9F3',
'AAAAAAAA-1111-1111-AAAG-111111111111',
];
valid.forEach(test => console.log("Valid case, result: "+isUUID(test)));
invalid.forEach(test => console.log("Invalid case, result: "+isUUID(test)));
I added a UUID validator to Apache Commons Validator. It's not yet been merged, but you can vote for it here:
https://github.com/apache/commons-validator/pull/68
I have this function, but it's essentially the same as the accepted answer.
export default function isUuid(uuid: string, isNullable: boolean = false): boolean {
return isNullable
? /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid)
: /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
}
I think a better way is using the static method fromString to avoid those regular expressions.
id = UUID.randomUUID();
UUID uuid = UUID.fromString(id.toString());
Assert.assertEquals(id.toString(), uuid.toString());
On the other hand
UUID uuidFalse = UUID.fromString("x");
throws java.lang.IllegalArgumentException: Invalid UUID string: x

Categories