6

I am looking for a function that can tell me to which data type a string might be converted. Examples:

"28.98" results in float (. as separator)

"44.332,95" results in float (, as separator)

"29/04/14" results in date (should work internationally -> different date formats)

"34.524" results in int (. as delimited)

"all the rest" results in string

Ideally also (these are subclasses of string):

"[email protected]" results in e-mail

"+49/2234/234567" results in phone

Is there a (open source) libary can can do such thing?

Thanks!

7
  • How would you parse something like that: 1,2,3,444.555.666,777,888,+92/12/12/12/12/12/12.12? Commented May 27, 2013 at 14:57
  • 1
    Since this does not match anything else, it would be a string. Commented May 27, 2013 at 15:02
  • Why it doesn't match anything else? You mean, the whole string should match some type, or tokens should be separated by whitespace? Your requirements exclude using commas or slashes as separators... Commented May 27, 2013 at 15:07
  • 1
    Yes, I always consider the whole string as one element. And no, I don't want to limit the imput / exclude some characters. Commented May 27, 2013 at 15:09
  • OK, but with dates you generally can't. 01/02/03 could be 1st Feb 2003 or 3rd Feb 2001, but Americans could see 2nd Jan 2003 or 2nd Mar 2001 (they are often using middle-indian for dates) Commented May 27, 2013 at 15:12

2 Answers 2

6

There you have it. Not a library, unhealthy amount of regular expressions, but it works with your examples. If you need other things to be matched, please add more examples. Open to critique or requirements in the comments.

function getType(str){
    if (typeof str !== 'string') str = str.toString();
    var nan = isNaN(Number(str));
    var isfloat = /^\d*(\.|,)\d*$/;
    var commaFloat = /^(\d{0,3}(,)?)+\.\d*$/;
    var dotFloat = /^(\d{0,3}(\.)?)+,\d*$/;
    var date = /^\d{0,4}(\.|\/)\d{0,4}(\.|\/)\d{0,4}$/;
    var email = /^[A-za-z0-9._-]*@[A-za-z0-9_-]*\.[A-Za-z0-9.]*$/;
    var phone = /^\+\d{2}\/\d{4}\/\d{6}$/g;
    if (!nan){
        if (parseFloat(str) === parseInt(str)) return "integer";
        else return "float";
    }
    else if (isfloat.test(str) || commaFloat.test(str) || dotFloat.test(str)) return "float";
    else if (date.test(str)) return "date";
    else {
        if (email.test(str)) return "e-mail";
        else if (phone.test(str)) return "phone";
        else return "string";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

in this case just lazy evaluate everything. here you test every regex but in most case won't use more than half of it.
So let's create a library
-2

Has been a while since I worked with JavaScript frameworks, but what you are working on is rather simple. You can do it yourself, by checking if the logical differences exist in your string, the way you are presenting them here. For example, you can use the indexOf() JavaScript function to check if an @ sign exists in your string. If you have both a dot and a comma, means that you get a floating point number. Lastly, the difference you want between 28.98 and 34.524 cannot be presented in any way, since the . is always a floating point mark for numbers, meaning that 34.524 is a float for both human and computer.

Hope it helps - probably not with the library you were asking for though!

indexOf() function in w3schools.com

2 Comments

I am aware that it should not be too hard to implement this by myself - but in order to cover (nearly) all cases (esp. for the date), it seems like a lot of work. By the way: the . is not always a float point mark; in Germany for example the , would be used for that.
You didn't mention that you want multilingual and localisation services. And what you do in Germany is a left-over from the Greek's manipulation on the arabic number representation! The international usage goes with dot as a decimal sign and comma for thousand separation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.