From fc7769e548cbee1c4b5d56d8166f5f80715b3e10 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 12 Nov 2020 07:58:48 +0100 Subject: [PATCH] get() possibility to provide params --- CHANGELOG.md | 3 ++- README.md | 1 + docs/general.html | 54 +++++++++++++++++++++++++++++++++++++++++++++++ docs/history.html | 5 +++++ docs/index.html | 2 +- lib/index.js | 46 +++++++++++++++++++++++++--------------- lib/util.js | 2 +- 7 files changed, 93 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df35446..6e4e41e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,8 @@ For major (breaking) changes - version 3 and 2 see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | -| 4.29.2 | 2020-11-09 | `blockdevices()` catch errors adapted for just one line | +| 4.30.0 | 2020-11-12 | `get()` possibility to provide params | +| 4.29.3 | 2020-11-09 | `blockdevices()` catch errors adapted for just one line | | 4.29.2 | 2020-11-09 | `blockdevices()` catch errors | | 4.29.1 | 2020-11-08 | `cpu()`, `system()` better parsing Raspberry Pi revision codes | | 4.29.0 | 2020-11-08 | `fsSize()` correct fs type detection macOS (HFS, APFS, NFS) | diff --git a/README.md b/README.md index a7c1385..6ad891c 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ si.cpu() (last 7 major and minor version releases) +- Version 4.30.0: `get()` added possibility to provide parameters - Version 4.29.0: `fsSize()` correct fs type detection macOS (HFS, APFS, NFS) - Version 4.28.0: `graphics()` added deviceName (Windows) - Version 4.27.0: `observe()` added observe / watch function diff --git a/docs/general.html b/docs/general.html index b6f7e50..9019200 100644 --- a/docs/general.html +++ b/docs/general.html @@ -203,6 +203,60 @@ si.get(valueObject).then(data => console.log(data));

The key names of the valueObject must be exactly the same as the representing function within systeminformation.

+

Providing parameters to the get() function

+

Now you can also provide parameters to get() functions (where needed). Just pass the parameters in parentheses right after the wanted keys: have a look at the folloging example:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionResult objectLinuxBSDMacWinSunComments
si.get(valueObject,cb){...}XXXXXexample with parameters:
value in paretheses goes as parameter
to the given function:
+
Example
+
const si = require('systeminformation');
+
+// define all values, you want to get back
+// here the value in paretheses goes as a parameter
+// to the processLoad function
+
+valueObject = {
+  processLoad: 'pids, cpu (postgres)'
+}
+si.get(valueObject).then(data => console.log(data));
+
+{
+  processLoad: {
+  pids: [
+    640, 643, 654,
+    655, 656, 657,
+    658, 659
+  ],
+    cpu: 0.63
+  }
+}
+
+

Get All At Once

The following three functions si.getStaticData(), si.getDynamicData() and si.getAllData() will return most of the available data in a single result object:

diff --git a/docs/history.html b/docs/history.html index a5530a5..4b5c158 100644 --- a/docs/history.html +++ b/docs/history.html @@ -83,6 +83,11 @@ + + + + + diff --git a/docs/index.html b/docs/index.html index 12ba2b1..3bf0eae 100644 --- a/docs/index.html +++ b/docs/index.html @@ -168,7 +168,7 @@
systeminformation
-
Current Version: 4.29.3
+
Current Version: 4.30.0
diff --git a/lib/index.js b/lib/index.js index 91045df..3c77372 100755 --- a/lib/index.js +++ b/lib/index.js @@ -320,7 +320,15 @@ function get(valueObject, callback) { process.nextTick(() => { const allPromises = Object.keys(valueObject) .filter(func => ({}.hasOwnProperty.call(exports, func))) - .map(func => exports[func]()); + .map(func => { + const params = valueObject[func].substring(valueObject[func].lastIndexOf('(') + 1, valueObject[func].lastIndexOf(')')); + const funcWithoutParams = func.split('(')[0]; + if (params) { + return exports[funcWithoutParams](params) + } else { + return exports[funcWithoutParams]('') + } + }); Promise.all(allPromises).then(data => { const result = {}; @@ -331,27 +339,31 @@ function get(valueObject, callback) { result[key] = data[i]; } else { const keys = valueObject[key].replace(/,/g, ' ').replace(/ +/g, ' ').split(' '); - if (Array.isArray(data[i])) { - // result is in an array, go through all elements of array and pick only the right ones - const partialArray = []; - data[i].forEach(element => { + if (data[i]) { + if (Array.isArray(data[i])) { + // result is in an array, go through all elements of array and pick only the right ones + const partialArray = []; + data[i].forEach(element => { + const partialRes = {}; + keys.forEach(k => { + if ({}.hasOwnProperty.call(element, k)) { + partialRes[k] = element[k]; + } + }); + partialArray.push(partialRes); + }); + result[key] = partialArray; + } else { const partialRes = {}; keys.forEach(k => { - if ({}.hasOwnProperty.call(element, k)) { - partialRes[k] = element[k]; + if ({}.hasOwnProperty.call(data[i], k)) { + partialRes[k] = data[i][k]; } }); - partialArray.push(partialRes); - }); - result[key] = partialArray; + result[key] = partialRes; + } } else { - const partialRes = {}; - keys.forEach(k => { - if ({}.hasOwnProperty.call(data[i], k)) { - partialRes[k] = data[i][k]; - } - }); - result[key] = partialRes; + result[key] = {}; } } i++; diff --git a/lib/util.js b/lib/util.js index 0286a57..a986bad 100644 --- a/lib/util.js +++ b/lib/util.js @@ -490,7 +490,7 @@ function countLines(lines, startingWith) { } function sanitizeShellString(str) { - let result = str; + let result = str || ''; result = result.replace(/>/g, ''); result = result.replace(/
4.30.02020-11-11get() added possibility to provide parameters
4.29.3 2020-11-09