Skip to content

Commit

Permalink
Add naive_jq
Browse files Browse the repository at this point in the history
  • Loading branch information
PRO-2684 committed Nov 19, 2024
1 parent 213a75c commit f84c189
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Some code snippets that's hard to categorize. See READMEs under subfolders for d
- `GM_config`: Simple config lib for Tampermonkey scripts.
- `greasyfork_enhance`: Enhance your experience at Greasyfork.
- `hover_card`: Get Github hovercard for given repo.
- `naive_jq`: A naive implementation of `jq` in Node.js, only for quick testing.
- `purlfy_for_tm`: The ultimate URL purifier - Tampermonkey version.
- `sanitify_header_id`: Sanitify/Sanitize given string (header id) so that it is (hopefully) free of emojis, with its spaces replaced by `-`.
- `scrollbar_mod`: Customize your scrollbar easily.
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- `GM_config`: 简易的 Tampermonkey 脚本配置库。
- `greasyfork_enhance`: 增进 Greasyfork 浏览体验。
- `hover_card`: 获取 Github 仓库的悬停卡片。
- `naive_jq`: 一个简单的 `jq` 的 Node.js 实现,仅用于快速测试。
- `purlfy_for_tm`: 终极 URL 净化器 - Tampermonkey 版本。
- `sanitify_header_id`: 格式化给定字符串(标题 ID),使其不含表情符号,空格被 `-` 替代。
- `scrollbar_mod`: 轻松定制你的滚动条。
Expand Down
38 changes: 38 additions & 0 deletions naive_jq/README.md
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
```
38 changes: 38 additions & 0 deletions naive_jq/README_CN.md
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
```
39 changes: 39 additions & 0 deletions naive_jq/jq.js
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);

0 comments on commit f84c189

Please sign in to comment.