Skip to content

Commit

Permalink
feat: add parse2Object fun
Browse files Browse the repository at this point in the history
  • Loading branch information
yunnysunny committed Sep 24, 2023
1 parent db3ed32 commit f911146
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Expand Up @@ -2,6 +2,10 @@ module.exports = {
plugins: [
// 'eslint-plugin-tsdoc',
],
'parserOptions': {
'ecmaVersion': 2018,
'requireConfigFile': false
},
extends: [
'eslint:recommended',
],
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -46,7 +46,7 @@ aaa bbb "ccc xxx"
const rv2 = parse(table2);
console.log(JSON.stringify(rv2));
```
Will print `{"lines":[["aaa","bbb","ccc xxx"],["111","222 yyy","333"],["222 zzz","777","888"],["333 888","444 000","444"]],"firstLine":["key1 000","444"]],"firstLine":["key1","key2","key3"]}`
Will print `{"lines":[["aaa","bbb","ccc xxx"],["111","222 yyy","333"],["222 zzz","777","888"],["333 888","444 000","444"]],"firstLine":["key1","key2","key3"]}`

## License

Expand Down
12 changes: 11 additions & 1 deletion index.d.ts
@@ -1,9 +1,19 @@

interface ParseOptions {
/**
* @defaultValue [' ', '/t']
*/
splitChars?: string[]
}
interface ReadTable {
parse(output: string): {
parse(output: string, options?: ParseOptions): {
fistLine: string[],
lines: string[][]
}
parseObject(output: string, options?: ParseOptions) : {
fistLine: string[],
records: Map<string, string>[]
}
}
declare const readTable: ReadTable
export default readTable
36 changes: 31 additions & 5 deletions index.js
Expand Up @@ -3,14 +3,19 @@ const SPACE_CHARS = [
'\t',
];
const QUOTATION_CHAR = '"';
function isSpace(char) {
return SPACE_CHARS.indexOf(char) !== -1 ? char : null;
function isSpace(char, splitChars) {
return splitChars.indexOf(char) !== -1 ? char : null;
}
/**
* Parsing table like output to object array
* @param {string} output
* @param {ParseOptions} options
*/
exports.parse = function(output) {
exports.parse = function(output, options) {
options = {
splitChars: SPACE_CHARS,
...(options || {})
};
const len = output.length;
let firstLine = [];
let currentLine = [];
Expand Down Expand Up @@ -62,7 +67,7 @@ exports.parse = function(output) {
firstLine = currentLine.splice(0);
}
break;
case isSpace(char): // get a space
case isSpace(char, options.splitChars): // get a space
if ((quotationStart && !quotationEnd)) {
currentField += char;
}
Expand All @@ -84,7 +89,7 @@ exports.parse = function(output) {
lastChar = char;
break;
default:
if (isSpace(lastChar)) {
if (isSpace(lastChar, options.splitChars)) {
if ((quotationStart)) {
// skip
} else if (quotationEnd) {
Expand All @@ -106,4 +111,25 @@ exports.parse = function(output) {
lines,
firstLine
};
};
/**
* Parsing table like output to object array
* @param {string} output
*/
exports.parse2Object = function(output, options) {
const parsed = exports.parse(output, options);
const { firstLine, lines } = parsed;
const keyLen = firstLine.length;
if (lines.length === 0) {
return { firstLine, records: [] };
}
const records = lines.map(function doLoop(line) {
const record = new Map();
for (let i = 0; i < keyLen; i++) {
const key = firstLine[i];
record.set(key, line[i]);
}
return record;
});
return { firstLine, records };
};
8 changes: 8 additions & 0 deletions test/basic.test.js
@@ -1,5 +1,8 @@
const { parse } = require('..');
const { expect } = require('chai');
const fs = require('fs');
const path = require('path');
const { parse2Object } = require('..');
describe('basic test', function () {
it('plain field', function () {
const table = `
Expand Down Expand Up @@ -35,6 +38,11 @@ aaa bbb "ccc xxx"
'firstLine': ['key1', 'key2', 'key3']
});
});
it('process test', function() {
const content = fs.readFileSync(path.join(__dirname, 'resources/process.txt'));
const rv = parse2Object(content.toString(), {splitChars: ['\t']});
expect(rv.records.length).to.gt(0);
});
});


Expand Down
25 changes: 25 additions & 0 deletions test/resources/process.txt
@@ -0,0 +1,25 @@
Process CPU Private Bytes Working Set PID Description Company Name Command Line GPU
System Idle Process 46.89 60 K 8 K 0
vmmem 9.15 552,064 K 550,928 K 21020
procexp64.exe 8.53 58,172 K 86,124 K 20500 Sysinternals Process Explorer Sysinternals - www.sysinternals.com "D:\greensoft\SysinternalsSuite\procexp64.exe"
msrdc.exe 6.10 184,484 K 66,544 K 68880 远程桌面 Microsoft Corporation msrdc.exe /v:99BDFB01-31EE-4AC4-85A2-AECBBAAF1726 /hvsocketserviceid:F1D7F981-FACB-11E6-BD58-64006A7986D3 /silent /wslg /plugin:WSLDVC_PACKAGE /wslgsharedmemorypath:WSL\99BDFB01-31EE-4AC4-85A2-AECBBAAF1726\wslg "C:\Program Files\WindowsApps\MicrosoftCorporationII.WindowsSubsystemForLinux_1.2.5.0_x64__8wekyb3d8bbwe\wslg.rdp"
System 5.52 192 K 96 K 4
MsMpEng.exe 5.33 507,644 K 386,252 K 5408 Antimalware Service Executable Microsoft Corporation "C:\ProgramData\Microsoft\Windows Defender\Platform\4.18.23080.2006-0\MsMpEng.exe"
explorer.exe 4.36 817,980 K 253,904 K 8564 Windows 资源管理器 Microsoft Corporation C:\WINDOWS\Explorer.EXE
vmwp.exe 2.46 9,648 K 20,052 K 21440
Interrupts 2.14 K K n/a Hardware Interrupts and DPCs
dwm.exe 1.56 232,100 K 164,804 K 1360
dllhost.exe 1.29 5,680 K 11,076 K 13728 COM Surrogate Microsoft Corporation C:\WINDOWS\SYSTEM32\DLLHOST.EXE /PROCESSID:{DFB65C4C-B34F-435D-AFE9-A86218684AA8}
Taskmgr.exe 1.00 180,012 K 163,244 K 41584
ctfmon.exe 0.60 51,196 K 48,072 K 9696
conhost.exe 0.45 6,940 K 13,100 K 30468 控制台窗口主机 Microsoft Corporation \??\C:\WINDOWS\system32\conhost.exe 0x4
audiodg.exe 0.43 5,416 K 9,472 K 72308
conhost.exe 0.38 6,884 K 13,148 K 74100 控制台窗口主机 Microsoft Corporation \??\C:\WINDOWS\system32\conhost.exe 0x4
svchost.exe 0.36 13,776 K 18,872 K 3088 Windows 服务主进程 Microsoft Corporation C:\WINDOWS\system32\svchost.exe -k appmodel -p
wslhost.exe 0.36 1,888 K 9,172 K 36952 Windows Subsystem for Linux Microsoft Corporation "C:\Program Files\WindowsApps\MicrosoftCorporationII.WindowsSubsystemForLinux_1.2.5.0_x64__8wekyb3d8bbwe\wslhost.exe" --vm-id {99bdfb01-31ee-4ac4-85a2-aecbbaaf1726} --handle 2188
wslhost.exe 0.35 2,496 K 11,324 K 49084 Windows Subsystem for Linux Microsoft Corporation "C:\Program Files\WindowsApps\MicrosoftCorporationII.WindowsSubsystemForLinux_1.2.5.0_x64__8wekyb3d8bbwe\wslhost.exe" --vm-id {99bdfb01-31ee-4ac4-85a2-aecbbaaf1726} --handle 1628
csrss.exe 0.32 4,624 K 5,360 K 860
svchost.exe 0.29 56,804 K 55,896 K 4800 Windows 服务主进程 Microsoft Corporation C:\WINDOWS\System32\svchost.exe -k LocalServiceNoNetwork -p
svchost.exe 0.26 25,516 K 30,692 K 964 Windows 服务主进程 Microsoft Corporation C:\WINDOWS\system32\svchost.exe -k DcomLaunch -p
svchost.exe 0.24 5,540 K 9,464 K 3812 Windows 服务主进程 Microsoft Corporation C:\WINDOWS\System32\svchost.exe -k LocalServiceNetworkRestricted -p
svchost.exe 0.22 22,212 K 19,560 K 1184 Windows 服务主进程 Microsoft Corporation C:\WINDOWS\system32\svchost.exe -k RPCSS -p

0 comments on commit f911146

Please sign in to comment.