forked from PRO-2684/gadgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Naive jq | ||
|
||
A naive implementation of `jq` in Node.js, only for quick testing. | ||
|
||
## Usage | ||
|
||
```sh | ||
node jq.js [options] [input] | ||
``` | ||
|
||
### Options | ||
|
||
- `-p, --path <path>`: Specify the JSON path to extract | ||
- `-h, --help`: Display this help message and exit | ||
|
||
### Positional Arguments | ||
|
||
- `input`: JSON data. If input is not provided, the script will read from standard input. | ||
|
||
## Examples | ||
|
||
### Read from command-line arguments | ||
|
||
```sh | ||
node jq.js -p .data '{"data": "hello"}' | ||
``` | ||
|
||
### Read from standard input | ||
|
||
```sh | ||
cat example.json | node jq.js -p .data | ||
``` | ||
|
||
### Display help message | ||
|
||
```sh | ||
node jq.js --help | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 简易 jq | ||
|
||
一个在 Node.js 中实现的简易 `jq`,仅用于快速测试。 | ||
|
||
## 使用方法 | ||
|
||
```sh | ||
node jq.js [options] [input] | ||
``` | ||
|
||
### 选项 | ||
|
||
- `-p, --path <path>`: 指定要提取的 JSON 路径 | ||
- `-h, --help`: 显示此帮助信息并退出 | ||
|
||
### 位置参数 | ||
|
||
- `input`: JSON 数据。如果未提供输入,脚本将从标准输入读取。 | ||
|
||
## 示例 | ||
|
||
### 从命令行参数读取 | ||
|
||
```sh | ||
node jq.js -p .data '{"data": "hello"}' | ||
``` | ||
|
||
### 从标准输入读取 | ||
|
||
```sh | ||
cat example.json | node jq.js -p .data | ||
``` | ||
|
||
### 显示帮助信息 | ||
|
||
```sh | ||
node jq.js --help | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// A naive implementation of jq in Node.js, only for quick testing | ||
|
||
const fs = require("fs"); | ||
const { parseArgs } = require("node:util"); | ||
|
||
const options = { | ||
path: { | ||
type: "string", | ||
short: "p" | ||
}, | ||
help: { | ||
type: "boolean", | ||
short: "h" | ||
} | ||
}; | ||
const args = parseArgs({ options, allowPositionals: true, args: process.argv.slice(2) }); | ||
|
||
if (args.values.help) { | ||
console.log(`Usage: node jq.js [options] [input] | ||
Options: | ||
-p, --path <path> Specify the JSON path to extract | ||
-h, --help Show this help message and exit | ||
If no input is provided, the script will read from stdin.`); | ||
process.exit(0); | ||
} | ||
|
||
const path = args.values.path ?? ""; | ||
// console.log(args); | ||
|
||
const str = args.positionals[0] ?? fs.readFileSync(0, "utf-8"); | ||
const data = JSON.parse(str.trim()); | ||
// console.log(data); | ||
|
||
let result = data; | ||
for (const part of path.split(".").slice(1)) { | ||
result = result?.[part]; | ||
} | ||
console.log(result); |