mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
added a bunch of useful utils functions
This commit is contained in:
@@ -272,6 +272,87 @@
|
||||
return env;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// get all the url params in a single key/value hash
|
||||
params: function(options) {
|
||||
var a, hash = {}, params;
|
||||
|
||||
options = options || {};
|
||||
options.skipToType = options.skipToType || {};
|
||||
|
||||
if (options.url) {
|
||||
a = utils.urlToLocation(options.url);
|
||||
}
|
||||
params = (a ? a.search : window.location.search).substring(1).split("&");
|
||||
|
||||
params.forEach(function(param) {
|
||||
var val = param.split('='),
|
||||
key = decodeURI(val[0]),
|
||||
value = options.skipToType[key] ? decodeURI(val[1]) : utils.toType(decodeURI(val[1]));
|
||||
|
||||
if (key)
|
||||
hash[key] = value;
|
||||
});
|
||||
return hash;
|
||||
},
|
||||
|
||||
param: function(key) {
|
||||
return this.params()[key];
|
||||
},
|
||||
|
||||
urlToLocation: function(url) {
|
||||
var a = document.createElement('a');
|
||||
a.href = url;
|
||||
return a;
|
||||
},
|
||||
|
||||
// return boolean if string 'true' or string 'false', or if a parsable string which is a number
|
||||
// also supports JSON object and/or arrays parsing
|
||||
toType: function(str) {
|
||||
var type = typeof str;
|
||||
if (type !== 'string') {
|
||||
return str;
|
||||
} else {
|
||||
var nb = parseFloat(str);
|
||||
if (!isNaN(nb) && isFinite(str))
|
||||
return nb;
|
||||
if (str === 'false')
|
||||
return false;
|
||||
if (str === 'true')
|
||||
return true;
|
||||
|
||||
try {
|
||||
str = JSON.parse(str);
|
||||
} catch (e) {}
|
||||
|
||||
return str;
|
||||
}
|
||||
},
|
||||
|
||||
// Safely get/set chained properties on an object
|
||||
// set example: utils.props(A, 'a.b.c.d', 10) // sets A to {a: {b: {c: {d: 10}}}}, and returns 10
|
||||
// get example: utils.props(A, 'a.b.c') // returns {d: 10}
|
||||
// get example: utils.props(A, 'a.b.c.foo.bar') // returns undefined without throwing a TypeError
|
||||
// credits to github.com/gkindel
|
||||
props: function(obj, props, value) {
|
||||
if(obj === undefined)
|
||||
obj = window;
|
||||
if(props == null)
|
||||
return undefined;
|
||||
var i = props.indexOf('.');
|
||||
if( i == -1 ) {
|
||||
if(value !== undefined)
|
||||
obj[props] = value;
|
||||
return obj[props];
|
||||
}
|
||||
var prop = props.slice(0, i),
|
||||
newProps = props.slice(i + 1);
|
||||
|
||||
if(props !== undefined && !(obj[prop] instanceof Object) )
|
||||
obj[prop] = {};
|
||||
|
||||
return util.props(obj[prop], newProps, value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user