Skip to content

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
deptno committed May 24, 2018
1 parent 8c154e3 commit ab288a5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.0

- inject ramda
- support fp mode, `-r` option

## 1.2.0

- requirement: Node 8 => Node 6
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

[![npm](https://img.shields.io/npm/dt/hosejs.svg?style=for-the-badge)](https://www.npmjs.com/package/hosejs)

> :tada: Data handling with ramda.js (version 1.3 or higher)
![hosejs](https://github.com/deptno/hosejs/raw/master/asset/hosejs.gif)

> Currently, HoseJS reaches 100% code coverage.
> 100% code coverage.
You can transform JSON(or NOT!!) data with just **JavaScript** in terminal.
You can transform JSON(or NOT) data with just **JavaScript** in terminal.

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

## Feature

### Data handling with ramda.js (version 1.3 or higher)

You can use [ramda.js](https://ramdajs.com>)

eg. `http https://swapi.co/api/people/1/ | j -r "props(['name', 'height'])"`
eg. `http https://swapi.co/api/people/1/ | j -ramda "props(['name', 'height'])"`
eg. `http https://swapi.co/api/people/1/ | j "props(['name', 'height'])(_)"`

## Options

Expand Down Expand Up @@ -43,6 +54,10 @@ $ 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)'
$ echo 'not json string \n !!' | j '_.split("\n")'
$ http https://swapi.co/api/people/1/ | j -r "props(['name', 'height'])"
$ http https://swapi.co/api/people/1/ | j -ramda "props(['name', 'height'])"
$ http https://swapi.co/api/people/1/ | j "props(['name', 'height'])(_)"

```

### Inject script file
Expand Down
31 changes: 17 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ const program = meow(`
$ 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)'
Usage #ramda functions
$ cat some.json | j -r "pipe(pluck('timestamp'), head, Date)"
Advanced Usage
visit https://github.com/deptno/hosejs#usage
Alias
j, js
Options
--file, -f use javascript file first
--tab, -t JSON tab space (default: 2)
--ramda, -r write unary function then function is invoked with data automatically
--ramda, -r automatic invoke with 'your_code(_)'
`, {
flags: {
file : {type: 'string', alias: 'f'},
tab : {type: 'string', alias: 't', default: '2'},
ramda: {type: 'boolean', alias: 'r'},
file: {type: 'string', alias: 'f'},
tab : {type: 'string', alias: 't', default: '2'},
ramda : {type: 'boolean', alias: 'r', default: false},
}
})

Expand All @@ -39,21 +41,22 @@ async function main() {
}

try {
const tryCatch = (_: any) => {
try {
return JSON.parse(_)
} catch(e) {
return _
}
}
const coder = R.ifElse(Boolean, R.always(ramdaInvoker), R.always(invoker))(flags.ramda)
const source = await stdin()
const sandbox: any = {...R, R, _: undefined}
const coder = R.ifElse(
Boolean,
R.always(ramdaInvoker),
R.always(invoker),
)(flags.ramda)

R.compose(
R.assoc('_', (R as any).__, sandbox),
R.tryCatch(JSON.parse, R.identity)
const sandbox: any = R.compose(
R.assoc('_', (R as any).__, {...R, R} as any),
tryCatch
)(source)

vm(sandbox, input.map(coder).join(';'))

print(sandbox._, parseInt(flags.tab))
} catch (e) {
console.error('🚫', e)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hosejs",
"version": "1.2.0",
"version": "1.3.0",
"description": "javascript pipe, jq alternative",
"main": "index.js",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const ramdaInvoker = R.compose(
R.intersperse(__, ['_ = ', '(_)']) as any
)
export const invoker = R.concat(`_ = `)

0 comments on commit ab288a5

Please sign in to comment.