-
Notifications
You must be signed in to change notification settings - Fork 0
/
propertyParser.js
43 lines (39 loc) · 1.16 KB
/
propertyParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Basic parser for URL properties
* @author Miller Medeiros
* @version 0.1.0 (2011/12/06)
* MIT license
*/
define(function(){
var rProps = /([\w-]+)\s*:\s*(?:(\[[^\]]+\])|([^,]+)),?/g, //match "foo:bar" and "lorem:[ipsum,dolor]" capturing name as $1 and val as $2 or $3
rArr = /^\[([^\]]+)\]$/; //match "[foo,bar]" capturing "foo,bar"
function parseProperties(str){
var match, obj = {};
while (match = rProps.exec(str)) {
obj[ match[1] ] = typecastVal(match[2] || match[3]);
}
return obj;
}
function typecastVal(val){
if (rArr.test(val)){
val = val.replace(rArr, '$1').split(',');
} else if (val === 'null'){
val = null;
} else if (val === 'false'){
val = false;
} else if (val === 'true'){
val = true;
} else if (val === '' || val === "''" || val === '""'){
val = '';
} else if (! isNaN(val)) {
//isNaN('') == false
val = +val;
}
return val;
}
//API
return {
parseProperties : parseProperties,
typecastVal : typecastVal
};
});