Given a value of type unknown and a configuration describing if that value must be an integer or float value. I started with this function
function isValueNumber(value: unknown, isAcceptingFloatingPointNumbers: boolean) {
if (Number.isNaN(value)) {
return false;
}
if (!isAcceptingFloatingPointNumbers) {
return Number.isInteger(value);
}
return true;
}
The problem is that when I call the function like so
isValueNumber("this is not a valid number", true)
it still returns true because my check Number.isNaN is not correct ( Is Number.IsNaN() more broken than isNaN() )
Do you have any ideas how to fix this validator function?
This way, the function will return false if not a number :
function isValueNumber(value: unknown, isAcceptingFloatingPointNumbers: boolean) {
if (typeof value !== 'number') {
return false;
}
if (!isAcceptingFloatingPointNumbers) {
return Number.isInteger(value);
}
return true;
}
Related
I'm trying to check if string matches any of the strings saved in database, but with the code I have right now it checks only the first one
My code:
for (const key in keys) {
if (keys[key].key !== hashedQueryKey) {
return "Invalid Key provided.";
} else return true;
}
You should not return if the key does not match as you want to continue comparing keys. Something like:
function queryMatches(keys, hashedQueryKey) {
for (const key in keys) {
if (keys[key].key === hashedQueryKey) {
return true;
}
}
return false;
}
You could use Object.keys and Array.contains() to check if your key is present
if (Object.keys(keys).contains(hashedQueryKey)) {
return true;
} else {
return "Invalid Key provided.";
}
although looking at your code, and being paranoid ... just in case your 'key' property differs from the objects' key, using Object.keys and anyMatch() is safer ...
if (Object.keys(keys).anyMatch(key => keys[key].key === hashedQueryKey)) {
return true;
} else {
return "Invalid Key provided.";
}
if you don't need the message - just true or false , then you can just return the predicate.
ie.
return Object.keys(keys).contains(hashedQueryKey);
I'm working on Ionic 4, and in one of the pages, I need to display a string according to the gender.
So, the function will take the variable gen, and if it is true it will return 'Female' but 'Male' if it is false.
At the moment of implementing it, it only gives me the first case in every value of gen(no matter gen is true or false).
I tried the == operator, the not operator and the ===.
It happens in other function as wells that return values.
displayGender(gen: boolean): string {
console.log(gen);
if (gen) {
console.log(gen);
return 'Female';
} else {
return 'Male';
}
}
I put some logs to see.
For example, in one of my test I sent a false value, the console output false and then false again, meaning it returns 'Female'.
Thank you
There is nothing wrong in your code... just pass the correct argument in the parameter list.
displayGender(gen: boolean): string {
console.log(gen);
if (gen) {
console.log(gen);
return 'Female';
} else {
return 'Male';
}
}
console.log(this.displayGender(false));
As per your code a boolean value is expected. you might be passing string value (if yes, then adjust the logic).
displayGender(gen: boolean): string {
console.log(gen);
if (gen == 'true') {
console.log(gen);
return 'Female';
} else {
return 'Male';
}
}
console.log(this.displayGender('false'));
Maybe the boolean variable is in a string format.
Try this:
displayGender(gen: boolean): string {
console.log(gen);
if (gen == 'true') {
console.log(gen);
return 'Female';
} else {
return 'Male';
}
}
displayGender(gen: boolean): string {
if (gen == "true") {
return 'Female';
} else {
return 'Male';
}
Actually gen is considered as string not boolean. That's why you are facing this issue. Since gen is considered as string so you can simply solve it by comparing it with the string value.
As the title says, I'm trying to build a function that given a string value as its input, it returns the value casted properly.
So far I got this, I think it looks ugly and have the feeling it can be solved in a better way.
Examples:
const smartCast = function (value) {
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}
if (!isNaN(value)) {
return Number(value);
}
return value;
};
console.log(smartCast('ahoy!') === 'ahoy!');
console.log(smartCast('hello there') === 'hello there');
console.log(smartCast('412') === 412);
console.log(smartCast('71.3') === 71.3);
console.log(smartCast('true') === true);
console.log(smartCast('false') === false);
Let me know if there is a case I'm not taking into account.
function cast(value) {
if (typeof value !== 'string')
return value
// Ignore values that would start an array or object.
if (/^\s*[{\[]/.test(value))
return value
try {
// Try to parse as some value.
return JSON.parse(value)
} catch(e) {
// If there's an error, we will assume it is just
// a string.
}
return value
}
console.log(typeof cast('false')) // boolean
console.log(typeof cast('true')) // boolean
console.log(typeof cast('1')) // number
console.log(typeof cast('3.14')) // number
console.log(typeof cast('abc')) // string
console.log(typeof cast('[1,2,3]')) // string
console.log(typeof cast('{"abc":1}')) // string
Take a look at empty string case and date https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Examples
I have the below function which returns another function, where the getFirstPhoneNo() would return a string.
get phones() {
if (this._patientData && this._patientData.getPatientPrimaryAddress) {
return this._patientData.getFirstPhoneNo();
}
return false;
}
Below is my interface for patientData
export interface IPatient {
getFirstPhoneNo: Function
}
What should be my return type for phones? Should it be a type of Ipatient or Function or a Function which returns string
IPatient is defined as such
export interface IPatient {
getFirstPhoneNo: () => () => string
}
which means that getFirstPhoneNo is a function which returns a function which returns a string.
So get phones returns either a boolean or a function which returns a string. This can be translated to a return type of boolean | () => string. This return type is not very useful because it only has properties which both of the types boolean and () => string share.
One possibility would be to change your code like this:
get phones() {
if (this._patientData && this._patientData.getPatientPrimaryAddress) {
return this._patientData.getFirstPhoneNo();
}
return () => '';
}
This changes the interface of get phones to () => () => string and but also allows you to do checks if the phone number is set (because an empty string evaluates to false)
Another easier approach would be to do the method call already in the get phone function and only return the phone number
get phones() {
if (this._patientData && this._patientData.getPatientPrimaryAddress) {
return this._patientData.getFirstPhoneNo()();
}
return null;
}
I have a function defined like this:
var getPhoneNumber = function (list, phoneType) {
if (_.isEmpty(list)) {
return "Not Entered"
};
_.each(list, function(phoneNo){
if (phoneNo.name === phoneType) {
return phoneNo.value;
};
});
return "Not Entered";
}
list is an Array, while phoneType is a String. The problem is the function always returns the value Not Entered even if the list is not empty and has a phoneNo.name equal to phoneType. If I add a console.log in the if it shows that the condition is true and prints the console.log message but still returns Not Entered
return phoneNo.value; doesn't correspond to the function getPhoneNumber, but to the function passes as callback at _.each.
You should try something like this instead:
var getPhoneNumber = function (list, phoneType) {
var value = null;
_.each(list, function(phoneNo){
if (phoneNo.name === phoneType) {
value = phoneNo.value;
}
});
if(value !== null)
return value;
else
return "Not Entered";
}