diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-07-07 20:10:13 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-07-07 20:10:13 +0200 |
commit | 37ca6fe6d63f748904a18ee5213f52e642980ab5 (patch) | |
tree | 51c08ee78ac844bee99144e2dfe55f7f4eae490b /modules | |
parent | d6aa68ae0da29aff23fdd5b08dd4e73564d15630 (diff) |
luci-base: luci.js: add isObject(), toArray() and sortedKeys() helper
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/luci.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 1e1c38bbe..93ecbac15 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -811,6 +811,56 @@ }, + /* Data helpers */ + isObject: function(val) { + return (val != null && typeof(val) == 'object'); + }, + + sortedKeys: function(obj, key, sortmode) { + if (obj == null || typeof(obj) != 'object') + return []; + + return Object.keys(obj).map(function(e) { + var v = (key != null) ? obj[e][key] : e; + + switch (sortmode) { + case 'addr': + v = (v != null) ? v.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g, + function(m0, m1) { return ('000' + m1.toLowerCase()).substr(-4) }) : null; + break; + + case 'num': + v = (v != null) ? +v : null; + break; + } + + return [ e, v ]; + }).filter(function(e) { + return (e[1] != null); + }).sort(function(a, b) { + return (a[1] > b[1]); + }).map(function(e) { + return e[0]; + }); + }, + + toArray: function(val) { + if (val == null) + return []; + else if (Array.isArray(val)) + return val; + else if (typeof(val) == 'object') + return [ val ]; + + var s = String(val).trim(); + + if (s == '') + return []; + + return s.split(/\s+/); + }, + + /* HTTP resource fetching */ get: function(url, args, cb) { return this.poll(null, url, args, cb, false); |