list.js icon indicating copy to clipboard operation
list.js copied to clipboard

Using existing list: How to auto-detect integer and boolean values?

Open fuchsberger opened this issue 7 years ago • 2 comments

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?

fuchsberger avatar Sep 20 '17 17:09 fuchsberger

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.

segheysens avatar Oct 16 '17 01:10 segheysens

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];
		 }

junkym0nk3y avatar May 04 '18 17:05 junkym0nk3y