Minimum javascript string value for use in backbone comparator - javascript

I want a minimum string value to use in my comparator. Assume that my validation prevents name from being the empty string.
This appears to be working correctly. Are there any values of "name" for which it will fail?
S.FileList = Backbone.Collection.extend
model: S.File
comparator: (file) ->
# We add display files alphabetically, but with meta.file at the top.
if file.get("name") == "meta.file"
return ""
return file.get("name")

Assuming your validation prevents name from being the empty string, and enforces it being a string: Yes, this will work. "" < str where str is any string other than "".
Again, you've got to make sure that typeof name is 'string', because while
"" < "0"
is true,
"" < 0
is false.

Related

Finding variable types from a string

Pardon if this question has already been answered however I'm struggling to find the any answers to it.
I'm looking to see if I can convert variable types to a string in the code below.
input = prompt('Type something please', 'your input here')
alert(input + ' is a ' + typeof input)
i.e. if the user were to type 1 typeof would return number, or if the user were to enter true it would return a boolean
You can run the input through a series of parseInt, parseFloat and
parseBool
functions.
Whenever you get a valid result, return it.
Something similar to:
if (parseInt(input) != NaN) {
return "int"
}
if (parseFloat(input) != NaN) {
return "float"
}
Generally, all inputs per your example will return a string careless of what they entered or intended to enter. We could however build a few logics to check if what they entered is; Strings (Alphabets only) or an integer (numbers only) or any other ones per a few other logics you could base your checks on.
One of the quickest ways to check if an input contains a number or not;
isNaN(input) // this returns true if the variable does NOT contain a valid number
eg.
isNaN(123) // false
isNaN('123') // false
isNaN('1e10000') // false (This translates to Infinity, which is a number)
isNaN('foo') // true
isNaN('10px') // true
you could try regex (which is not always ideal but works)
var input = "123";
if(num.match(/^-{0,1}\d+$/)){
//return true if positive or negative
}else if(num.match(/^\d+\.\d+$/)){
//return true if float
}else{
// return false neither worked
}
You could also use the (typeof input) but this will be more convenient if your user is going to enter an expected set of entries
var input = true;
alert(typeof input);
// This eg will return bolean
Let me know if this helps.

Replace affects all strings in an array

I am having some issues about replacing. I am fetching some integers from DB. And if the number fetched is just 1 then I replace it with "empty", but when I do that, it affects all numbers that have 1 in them.
Here is what I am doing, first calling toString and then replace
<tr v-if="moreIndex < one.length"  v-for="moreIndex in morePro">
<td>{{one[moreIndex].products}}</td>
<td>{{priceSep(one[moreIndex].price).toString().replace("1", "empty")}}</td>
</tr>
So I am changing "1" to empty but if any other data has includes 1 then output look like this...
5empty98
How can I fix this problem?
You can check the length with Conditional (ternary) operator
.:
<td>{{priceSep(one[moreIndex].price).toString().length == 1 ? priceSep(one[moreIndex].price).toString().replace("1", "empty") : priceSep(one[moreIndex].price).toString()}}</td>
May be, you can try directly set the value if the value is 1:
<td>{{priceSep(one[moreIndex].price).toString() == "1" ? "empty" : priceSep(one[moreIndex].price).toString()}}</td>
You can check the length of the string before replacing it
{{priceSep(one[moreIndex].price).toString().length > ?
priceSep(one[moreIndex].price).toString() :
priceSep(one[moreIndex].price).toString().replace("1", "empty")
}}
You can use RegExp:
{{priceSep(one[moreIndex].price).toString().replace(/^1$/, "empty")}}

understanding this javascript function code

function isBlank(s){
var len = s.length
var i
for(i=0; i<len; ++i) {
if(s.charAt(i)!= " ") return false
}
return true
}
I am totally new to JavaScript and coding. Please can someone explain me how is this code working.
I know that it is being used to check whether a input box has some value or not, but I know no further.
question update....
See, in the above code, the for loop runs and if the string is not blank it returns false.
Now for loop ends and browser reads the next line which is-- returns true--. So is not the function finally returning true. No matter if there was a return false in the middle.
It is looping through the string s and checking if each character is a space. If there are characters other than spaces, then the function returns false, because the string is not blank. If the string is empty or only contains spaces, then it returns true because the string is blank.
function isBlank(s){ // it is a function named 'isBlank' that accept one parameter, that the parameter is something passed from the outside
var len = s.length // Assign the length of parameter 's' into a local variable 'len'
var i // Declare a new local variable 'i'
for(i=0;i<len;++i) { // This is a 'loop', you can google it
if(s.charAt(i)!= " ") return false // if any character inside the parameter 's' is not an empty space, that means it isn't blank, so return false
}
return true; // If code reach this line that means 's' is either with 0 length or all characters of it are an empty space
}
By using the above function:
alert(isBlank("123")); // false
alert(isBlank("")); // true
alert(isBlank(" ")); //true
The function checks whether the string is empty or not.
for(i=0;i<len;++i) { // iterates through the string
if(s.charAt(i)!= " ") // checks whether character at index i of string s is not equal to " ".
return false
}
It iterates through the string and returns false if any character is not equal to " ".s.charAt(i) returns the character at index i of the string s.
If the condition is not satisfied for every character then it returns true.

RegExp filter in JavaScript (Angular.js)

I want to custom filter in Angular.js.
In case an object has name == null, and I insert "u" to filter-> it returns obj, which name == null because re.test(null)=true, but others characters are returning false. Can you tell me why? How can I prevent this case?
You need to check if obj.name is also defined before testing it with regexp:
$scope.searchFilter = function(obj) {
var re = new RegExp($scope.searchText, 'i');
return !$scope.searchText || (obj.name && re.test(obj.name)) || re.test(obj.age.toString());
};
Otherwise, null casted to String type yields "null" and it's matched of course.
Demo: http://jsfiddle.net/26fZb/232/

Why does var combined = null + "" has a value?

Probably a very basic question from a confused javascript noob...
Why do
var hasthisvalue = null;
if (hasthisvalue)
print("hasthisvalue hs value");
and
var hasthatvalue = "";
if (hasthatvalue)
print("hasthatvalue has value");
don't print anything, but if I combine these two
var combined = "hasthisvalue" + "hasthatvalue";
if (combined)
print ("combined has value");
it does?
Or more directly:
var combined = null + "";
if (combined)
print ("combined has value");
Why does "combined" has a value if I only add two variables that don't have values? What am I missing?
When you compare them separately, each converts to false in the if check. When you combine them, the null becomes the string "null", so their concatenation is the string "null", which does not convert to false
The first 2 examples are situations where the values are "falsy". These values are equal to false during a loose comparison:
null
undefined
blank string
boolean false
the number 0
NaN
Other values not in this list are "truthy" and are equal to true on a loose comparison.
The third case, you can try in the console. null+'' becomes a string: "null" and is therefore truthy.

Categories

Resources