From 37ca6fe6d63f748904a18ee5213f52e642980ab5 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 7 Jul 2019 20:10:13 +0200 Subject: [PATCH] luci-base: luci.js: add isObject(), toArray() and sortedKeys() helper Signed-off-by: Jo-Philipp Wich --- .../htdocs/luci-static/resources/luci.js | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) 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); -- 2.25.1