Created
August 24, 2012 17:02
-
-
Save terrycojones/3452933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseFluidinfoValue(value){ | |
/* | |
* Parse a string into one of the 5 types of values recognized by | |
* Fluidinfo. | |
* | |
* param: value is a string | |
* | |
* return: a value of type | |
* | |
* null | [string, string, ...] | float | bool | string | |
*/ | |
var numberRegex = /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/; | |
// convert empty string to null | |
if (typeof value === 'undefined' || value === ''){ | |
return null; | |
} | |
// The value is a non-empty string of some sort. Check if it's a | |
// list. (Found within [] and comma separated). | |
if (value.match(/^\s*\[.*\]\s*$/)){ | |
value = trim(value); | |
// Convert to Array | |
var rawArray = value.slice(1, -1).split(','); | |
var val = []; | |
for (var i = 0; i < rawArray.length; i++){ | |
val.push(trim(rawArray[i])); | |
} | |
return val; | |
} | |
if (value.match(numberRegex)){ | |
// A number. | |
return parseFloat(value); | |
} | |
// Boolean / null value? | |
var lower = value.toLowerCase(); | |
if (lower === 'true' || lower === 'false'){ | |
return lower === 'true'; | |
} | |
if (lower === 'null'){ | |
return null; | |
} | |
// A plain old string. | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment