Last active
July 30, 2021 19:21
-
-
Save stfsy/d548140fdeff6e78dddb0affae0a275b to your computer and use it in GitHub Desktop.
Parses the output of unix w command into an JS array containing zero or more Objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| module.exports = (output) => { | |
| const lines = output.split('\n') | |
| const columns = lines[1].split(' ').filter(column => column).map(entry => entry.toLowerCase()) | |
| const values = lines.slice(2).map(line => line.split(' ').filter(entry => entry)) | |
| return values.slice(0, values.length - 1).reduce((context, next) => { | |
| const line = {} | |
| next.forEach((value, index) => { | |
| line[columns[index]] = value | |
| }) | |
| context.push(line) | |
| return context | |
| }, []) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment