list.js
list.js copied to clipboard
Using existing list: How to auto-detect integer and boolean values?
Currently getting items from an existing list always stores values as strings. I would like to implement a function that checks for specific data types:
check(value){
// check for specific data types
if value === "true" return true
if value === "false" return false
if value === "" return null
if parseInt(value) !== NaN return parseInt(value)
if parseFloat(value) !== NaN return parseFloat(value)
// otherwise assume it is a string
return value
}
Is something like that possible without altering list.js code? If so, how? Or does list.js already have such a feature implement?
Hi @Sathras! Do you have a specific reason for wanting to store the data in different types? If the reason is sorting, you could pass sort
a custom sortFunction
that parses numbers and compares them.
You can add classname checking and change values type from strings to integers, Like this
this.values = function(newValues, notCreate) {
if (newValues !== undefined) {
for(var name in newValues) {
if(name.includes("int_")){
item._values[name] = parseInt(newValues[name]);
}
else{
item._values[name] = newValues[name];
}