Skip to content

Instantly share code, notes, and snippets.

@stfsy
Last active July 30, 2021 19:21
Show Gist options
  • Select an option

  • Save stfsy/d548140fdeff6e78dddb0affae0a275b to your computer and use it in GitHub Desktop.

Select an option

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