Metadata-Version: 2.1
Name: js_code
Version: 2.0.0
Summary: A simple library to run JavaScript code based on Python
Home-page: https://github.com/aduh5821/js_code.git
Author: Aduh
Author-email: aduh73285@gmail.com
License: BSD 2-Clause License
        
        Copyright (c) [year], [your name or your organization]
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
        
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE


# js_code

A simple Python library to run JavaScript code based on Python.

## 特性 / Features

- Execute JavaScript code directly from Python.
- Supports running scripts or single-line JavaScript code.
- Easy integration with Node.js using `execjs`.
- Provides a simple API to interact with JavaScript functions.

- 直接从 Python 执行 JavaScript 代码。
- 支持运行脚本或单行 JavaScript 代码。
- 通过 `execjs` 实现与 Node.js 的简易集成。
- 提供简单的 API 与 JavaScript 函数交互。

## 安装 / Installation

You can install `js_code` using pip:

```bash
pip install js_code
```

## 试例 / Examples

### 1. Python 调用 `js_read` 无参数调用

- **text.js**

```javascript
// 定义一个函数 fn，返回数字 123
function fn() {
    return 123;
};
```

- **Python 代码**

```python
from js_code import js_read

# 读取 JavaScript 文件并调用 fn 函数
js = js_read('text.js')
data = js.call('fn')  # 调用 JavaScript 中的 fn 函数
print(data)  # 输出 123
```

### 2. Python 调用 `js_read` 传参调用

- **text.js**

```javascript
// 定义一个函数 fn，接受两个参数 a 和 b，并返回它们的和
function fn(a, b) {
    return a + b;
};
```

- **Python 代码**

```python
from js_code import js_read

# 读取 JavaScript 文件并调用 fn 函数，传递参数 1 和 2
js = js_read('text.js')
data = js.call('fn', 1, 2)  # 调用 JavaScript 中的 fn 函数并传递参数
print(data)  # 输出 3
```

### 3. Python 调用 `js_read` 头部添加 JavaScript 代码调用

- **text.js**

```javascript
// 定义一个函数 fn，返回变量 a 的值
function fn() {
    return a;
};
```

- **Python 代码**

```python
from js_code import js_read

# 读取 JavaScript 文件，并在文件开头添加 var a = 10;
js = js_read('text.js', 'var a = 10;')
data = js.call('fn')  # 调用 JavaScript 中的 fn 函数
print(data)  # 输出 10
```

### 4. Python 调用 `js_read` 直接在 Python 中调用

- **Python 代码**

```python
# 在 Python 中直接定义 JavaScript 代码
js = js_read('''var a = 10;
function fn() {
    return a;
};''')

# 调用 JavaScript 中的 fn 函数
data = js.call('fn')
print(data)  # 返回 10
```

### 5. Python 调用 `js_run` 无参数调用

- **text.js**

```javascript
// 定义一个变量 a 和一个函数 fn，函数返回变量 a 的值
var a = 10;
function fn() {
    return a;
};

console.log(fn());  // 输出函数调用结果
```

- **Python 代码**

```python
from js_code import js_run

# 调用 js_run 执行 text.js 并返回控制台输出
data = js_run('text.js')
print(data)  # 返回 ['10']，按 
 和 
 切割的列表
```

### 6. Python 调用 `js_run` 传参，Node.js 接收使用

- **text.js**

```javascript
// Node.js 使用 process.argv 获取传递的参数
var arr = process.argv.slice(2);
console.log("接收到的arr", arr[0]);  // 输出第一个参数
console.log("接收到的arr", typeof arr[0]);  // 输出类型

arr = JSON.parse(arr[0]);  // 解析参数为 JSON 对象
console.log("转换后的arr", arr);  // 输出转换后的对象
console.log("转换后的arr", typeof arr);  // 输出对象类型
```

- **Python 代码**

```python
from js_code import js_run

# 调用 js_run 执行 text.js 并传递多个参数
data = js_run('text.js', 1, "2", [3, "4"], {'key': "value"})
print(data)  # 返回按 
 和 
 切割的列表
# ['接收到的arr [1,"2",[3,"4"],{"key":"value"}]', '接收到的arr string', 
# "转换后的arr [ 1, '2', [ 3, '4' ], { key: 'value' } ]", '转换后的arr object']
```

### 7. Python 调用 `js_run` 传参，Node.js 接收使用，并指定切割

- **text.js**

```javascript
// 接收参数并按指定格式输出
var arr = process.argv.slice(2);
console.log(`**接收到的arr${arr[0]}**`);
console.log(`**接收到的arr${typeof arr[0]}**`);

arr = JSON.parse(arr[0]);
console.log(`**转换后的arr${arr}**`);
console.log(`**转换后的arr${typeof arr}**`);
```

- **Python 代码**

```python
from js_code import js_run

# 调用 js_run 执行 text.js 并传递参数，使用 "**" 分隔输出
data = js_run('text.js', 1, "2", [3, "4"], {'key': "value"}, split="**")
print(data)
# 返回按 
 和 
 切割的列表，拼接成字符串后按传入的 ‘**’ 切割
# ['', '接收到的arr[1,"2",[3,"4"],{"key":"value"}]', '', '接收到的arrstring', '', 
# '转换后的arr1,2,3,4,[object Object]', '', '转换后的arrobject', '']
```

## 贡献 / Contributing

We welcome contributions to improve `js_code`. Feel free to open issues and pull requests!
我们欢迎对 js_code 的贡献。欢迎提出问题和提交拉取请求！

