Skip to content

Commit

Permalink
It's HoseJS
Browse files Browse the repository at this point in the history
  • Loading branch information
deptno committed May 20, 2018
0 parents commit a2e3f2c
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
node_modules/
**/*.js
log.json
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.ts
*.json
*.lock
.gitignore
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

:tada:
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Bonggyun Lee

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# HoseJS
[![npm](https://img.shields.io/npm/dt/hosejs.svg?style=for-the-badge)](https://www.npmjs.com/package/hosejs)

![hosejs](https://github.com/deptno/hosejs/raw/master/asset/hosejs.gif)

> Required node > v10
Transform JSON data with just **JavaScript** in terminal

`jq`? javascript is clearly better option for people already use javascript.

## Options

- `--file` option provide you to apply pre-defined script first
- `--tab` if result is Object, print JSON format with specified tab size (default: 2)

## Install

```
npm -g install hosejs
```

## command

`j` or `js`


## usage

```
$ cat some.json | j '_.map(x => x.timestamp)'
$ cat some.json | j '_.map(x => x.timestamp)' --tab 4
$ cat some.json | j '_.map(x => x.timestamp)' --file pre.js --tab 2
$ cat some.json | j '_.map(x => x.timestamp)' > some.json
$ cat some.json | j '[_].map(x => x.some_property + '👍')[0]'
$ cat some.json | j '_.map(x => new Date(x.timestamp).toISOString())'
$ cat some.json | j --file preload.js '_.map(x => x.timestamp)'
$ http https://swapi.co/api/people/ | j 'Object.keys(_)'
$ http https://swapi.co/api/people/ | j '_.count'
http https://swapi.co/api/people/ | j '_.results.map(x => x.name)'
```

## License

MIT
Binary file added asset/hosejs.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env node

import {chomp, chunksToLinesAsync} from '@rauschma/stringio'
import {createContext, runInContext} from 'vm'
import * as meow from 'meow'
import * as fs from 'fs'
import {promisify} from 'util'

const program = meow(`
HoseJS
Transform JSON with javascript.
Usage
$ cat some.json | j '[_].map(x => x.some_property)[0]'
$ cat some.json | j '_.map(x => new Date(x.timestamp).toISOString())'
$ cat some.json | j --file preload.js '.map(x => x.timestamp)'
Alias
js
Options
--file, -f use javascript file first
--tab, -t JSON tab space (default: 2)
`, {
flags: {
file: {type: 'string', alias: 'f'},
tab : {type: 'string', alias: 't', default: '2'},
}
})

async function main() {
const {input, flags} = program
const readFile = promisify(fs.readFile)
const stream = process.stdin as any
const lines = []

if (flags.file) {
input.unshift((await readFile(flags.file)).toString())
}

for await (const line of chunksToLinesAsync(stream)) {
lines.push(chomp(line))
}

try {
const _ = JSON.parse(lines.join(''))
const sandbox = {_}

createContext(sandbox)
runInContext(input.map(c => `_ = ${c}`).join(';'), sandbox)

print(sandbox._, parseInt(flags.tab))
} catch (e) {
console.error('🚫', e)
process.exit(1)
}
}

function print(output, tab: number) {
if (typeof output === 'object') {
return console.log(JSON.stringify(output, null, tab))
}
console.log(output)
}

main()
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "hosejs",
"version": "1.0.0",
"description": "javascript pipe, jq alternative",
"main": "index.js",
"bin": {
"j": "index.js",
"js": "index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/deptno/hosejs.git"
},
"author": "Bonggyun Lee <deptno@gmail.com>",
"license": "MIT",
"dependencies": {
"@rauschma/stringio": "^1.4.0",
"meow": "^5.0.0"
},
"devDependencies": {
"@types/meow": "^4.0.1",
"@types/node": "^10.1.0",
"typescript": "^2.8.3"
},
"bugs": {
"url": "https://github.com/deptno/hosejs/issues"
},
"homepage": "https://github.com/deptno/hosejs#readme",
"directories": {
"lib": "lib"
},
"keywords": [
"javascript",
"jq",
"terminal",
"eval",
"cli",
"pipe",
"js"
]
}
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs"
}
}
Loading

0 comments on commit a2e3f2c

Please sign in to comment.