get() possibility to provide params

This commit is contained in:
Sebastian Hildebrandt 2020-11-12 07:58:48 +01:00
parent 5a4e222339
commit fc7769e548
7 changed files with 93 additions and 20 deletions

View File

@ -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) |

View File

@ -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

View File

@ -203,6 +203,60 @@ si.get(valueObject).then(data => console.log(data));</code></pre class="example"
</tbody>
</table>
<p>The key names of the <span class="code">valueObject</span> must be exactly the same as the representing function within systeminformation.</p>
<h3>Providing parameters to the get() function</h3>
<p>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:</p>
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Function</th>
<th>Result object</th>
<th>Linux</th>
<th>BSD</th>
<th>Mac</th>
<th>Win</th>
<th>Sun</th>
<th>Comments</th>
</tr>
</thead>
<tr>
<td>si.get(valueObject,cb)</td>
<td>{...}</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>example with parameters:<br>value in paretheses goes as parameter<br>to the given function:</td>
</tr>
<tr class="example">
<td></td>
<td colspan="7">
<h5>Example</h5>
<pre><code class="js">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));</code></pre class="example">
<pre class="example">
{
processLoad: {
pids: [
640, 643, 654,
655, 656, 657,
658, 659
],
cpu: 0.63
}
}
</pre>
</tr>
</tbody>
</table>
<h2>Get All At Once</h2>
<p>The following three functions <span class="code">si.getStaticData()</span>, <span class="code">si.getDynamicData()</span> and <span class="code">si.getAllData()</span> will return most of the available data in a single result object:</p>
<table class="table table-sm table-bordered table-striped">

View File

@ -83,6 +83,11 @@
</tr>
</thead>
<tbody>
<tr>
<th scope="row">4.30.0</th>
<td>2020-11-11</td>
<td><span class="code">get()</span> added possibility to provide parameters</td>
</tr>
<tr>
<th scope="row">4.29.3</th>
<td>2020-11-09</td>

View File

@ -168,7 +168,7 @@
<img class="logo" src="assets/logo.png">
<div class="title">systeminformation</div>
<div class="subtitle"><span id="typed"></span></div>
<div class="version">Current Version: <span id="version">4.29.3</span></div>
<div class="version">Current Version: <span id="version">4.30.0</span></div>
<button class="btn btn-light" onclick="location.href='https://github.com/sebhildebrandt/systeminformation'">View on Github <i class=" fab fa-github"></i></button>
</div>
<div class="down">

View File

@ -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,6 +339,7 @@ function get(valueObject, callback) {
result[key] = data[i];
} else {
const keys = valueObject[key].replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
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 = [];
@ -353,6 +362,9 @@ function get(valueObject, callback) {
});
result[key] = partialRes;
}
} else {
result[key] = {};
}
}
i++;
}

View File

@ -490,7 +490,7 @@ function countLines(lines, startingWith) {
}
function sanitizeShellString(str) {
let result = str;
let result = str || '';
result = result.replace(/>/g, '');
result = result.replace(/</g, '');
result = result.replace(/\*/g, '');