Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esm version of Mockjs #454

Open
wants to merge 65 commits into
base: refactoring
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
2144e88
初始化仓库
KonghaYao Jun 9, 2021
0263c26
提交基本解构
KonghaYao Jun 9, 2021
b809aa2
大部分 Random 函数的 esm 化
KonghaYao Jun 9, 2021
67495e7
修改全部文件为 esm 导入方式
KonghaYao Jun 10, 2021
3c2eda1
添加 test 文件夹
KonghaYao Jun 10, 2021
2e6c4dd
esm 基础完成
KonghaYao Jun 12, 2021
1d8927f
解决 Number 方面的问题:全面使用 lodash 提供的 random 函数
KonghaYao Jun 12, 2021
cb0c634
color 转换的库 使用 ‘color’ 替代
KonghaYao Jun 12, 2021
eac8560
修改部分
KonghaYao Jun 12, 2021
2f4be38
添加 build 文件
KonghaYao Jun 12, 2021
1424471
添加 README
KonghaYao Jun 12, 2021
bf26870
添加 License 和部分说明
KonghaYao Jun 12, 2021
9eaf7c3
bug
KonghaYao Jun 12, 2021
ac23b25
0.9.1
KonghaYao Jun 12, 2021
5d283ed
更新 README
KonghaYao Jun 12, 2021
56e10a6
修复 float BUG
KonghaYao Jun 13, 2021
2821e7b
0.9.2
KonghaYao Jun 13, 2021
6adb97c
添加 fetch 的代理
KonghaYao Jun 14, 2021
e069eee
0.9.3
KonghaYao Jun 14, 2021
c328130
使用继承代理的方式实现 XHR 的代理
KonghaYao Jun 14, 2021
74b0741
更新npm 版本
KonghaYao Jun 14, 2021
7e9af90
0.9.4
KonghaYao Jun 14, 2021
69ed904
修复 xhr 的一个 bug
KonghaYao Jun 15, 2021
b3c6f52
0.9.5
KonghaYao Jun 15, 2021
05ab2ce
2021/6/16
KonghaYao Jun 16, 2021
7564b86
0.9.6
KonghaYao Jun 16, 2021
ae9df78
修复 XHR 对外部 URL 请求不返回数据的情况
KonghaYao Jun 16, 2021
7d19bf8
2021/6/19
KonghaYao Jun 19, 2021
5f13836
修复完传入参数错误 BUG
KonghaYao Jun 25, 2021
0de7310
0.9.9
KonghaYao Jun 25, 2021
de8dc3a
fix: 修复 esm 版本和 iife 版本
KonghaYao Jun 30, 2021
eece575
0.9.10
KonghaYao Jun 30, 2021
7bcff09
style: 修复文件以适应 eslint 和 prettier
KonghaYao Jun 30, 2021
98ade7c
fix: 修复英文句子没空格
KonghaYao Jun 30, 2021
d143cf4
0.9.11
KonghaYao Jun 30, 2021
a71290e
feat: 对 node 适配完成
KonghaYao Jun 30, 2021
b52fb04
docs: 添加mockjs 引入的说明
KonghaYao Jun 30, 2021
cdc6fca
0.9.12
KonghaYao Jun 30, 2021
6b44798
1.0.0
KonghaYao Jun 30, 2021
49caa24
fix: 修复 XHR 的适配性错误
KonghaYao Jul 1, 2021
db515c7
1.0.1
KonghaYao Jul 1, 2021
dfaae7f
teat(schema): 通过 schema 测试
KonghaYao Jan 30, 2024
0cb891f
chore: null
KonghaYao Jan 30, 2024
1875c5e
fix(random): 修复 random
KonghaYao Jan 30, 2024
40b30cf
fix(random): 完成 103 个测试
KonghaYao Jan 30, 2024
7f63b6f
refactor(xhr): xhr 重构
KonghaYao Jan 30, 2024
43196c0
test(all): 修复大量 bug
KonghaYao Feb 4, 2024
c8f93a7
test(all): 修复 boolean 值问题
KonghaYao Feb 4, 2024
66c4e9b
test(valid2): 通过 valid2 测试
KonghaYao Feb 4, 2024
b87ffb4
build(vite): 调整打包方式
KonghaYao Feb 5, 2024
7b00a66
test(xhr): 完成 xhr 测试
KonghaYao Feb 5, 2024
e3d7593
chore(actions): 添加github action
KonghaYao Feb 5, 2024
dbb3f95
fix(workflow): pnpm 缺失
KonghaYao Feb 5, 2024
32a543f
fix(coverage): 修复 coverage 的数据
KonghaYao Feb 5, 2024
750c69e
fix(coverage): lcov 文件生成
KonghaYao Feb 5, 2024
a1f6dec
refactor(random): 注册式依赖
KonghaYao Feb 5, 2024
1f17c9e
chore(version): 2.1.0
KonghaYao Feb 5, 2024
2b8eda6
fix(array): 修复 array 数据过大问题
KonghaYao Feb 8, 2024
f1ce1c9
refactor: change random base
KonghaYao Mar 14, 2024
440c0e8
feat(game-random): 迁移 game-random
KonghaYao Mar 14, 2024
baa89b0
feat(game-random): control random result
KonghaYao Mar 14, 2024
777093b
test(seed): 测试种子随机数
KonghaYao Mar 14, 2024
415a751
feat: 使用 game-random 稳定输出
KonghaYao Mar 15, 2024
123b57d
style: 样式格式化
KonghaYao Mar 15, 2024
be9d81b
chore: 2.3.0
KonghaYao Mar 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# editorconfig.org
# /.editorconfig

root = true

[*]

indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: CI Pipeline

# trigger build when pushing, or when creating a pull request
on: [push, pull_request]

jobs:
build:
# run build on latest ubuntu
runs-on: ubuntu-latest

steps:
# this will check out the current branch (https://github.com/actions/checkout#Push-a-commit-using-the-built-in-token)
- uses: actions/checkout@v3
# installing Node
- name: Use Node.js 18.16.0
uses: actions/setup-node@v3
with:
# this will use the latest Node 16 version
node-version: 18.16.0
# install dependencies using clean install to avoid package lock updates
- run: npm i -g pnpm
- run: pnpm i
- run: pnpm coverage

# Save coverage report in Coveralls
- name: Coveralls
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
bak
.DS_Store
.*.swp
.idea
node_modules
bower_components
dist
coverage
PLAN.md
npm-debug.log
9 changes: 0 additions & 9 deletions .jshintrc

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./src/mock/regexp/parser.js
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 120,
"tabWidth": 4,
"useTabs": false,
"endOfLine": "auto",
"singleQuote": true,
"semi": true,
"trailingComma": "all",
"bracketSpacing": true
}
6 changes: 0 additions & 6 deletions .spmignore

This file was deleted.

13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

137 changes: 0 additions & 137 deletions CHANGELOG.md

This file was deleted.

28 changes: 27 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
// esm 版本编写人

MIT License

Copyright (c) 2020 动中之动

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.

// 原始代码编写人

The MIT License (MIT)

Copyright (c) 2015 nuysoft
Expand All @@ -18,4 +44,4 @@ 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.
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
91 changes: 69 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,78 @@
# Mock.js
<!-- 模拟请求 & 模拟数据 -->
[![Build Status](https://travis-ci.org/nuysoft/Mock.svg?branch=refactoring)](https://travis-ci.org/nuysoft/Mock)
# Mockjs 重构 项目

<!-- [![Coverage Status](https://coveralls.io/repos/nuysoft/Mock/badge.png?branch=refactoring)](https://coveralls.io/r/nuysoft/Mock?branch=refactoring)
[![NPM version](https://badge.fury.io/js/mockjs.svg)](http://badge.fury.io/js/mockjs)
[![Bower version](https://badge.fury.io/bo/mockjs.svg)](http://badge.fury.io/bo/mockjs)
[![Dependency Status](https://gemnasium.com/nuysoft/Mock.svg)](https://gemnasium.com/nuysoft/Mock)
[![spm package](http://spmjs.io/badge/mockjs)](http://spmjs.io/package/mockjs) -->
| ![](https://img.shields.io/badge/更新-2023/2/5-blue?style=flat-square) | [![Coverage Status](https://coveralls.io/repos/github/KonghaYao/Mock/badge.svg?branch=esm)](https://coveralls.io/github/KonghaYao/Mock?branch=esm) | ![npm version](https://img.shields.io/npm/v/mockjs-ts.svg?style=flat-square) |

Mock.js is a simulation data generator to help the front-end to develop and prototype separate from the back-end progress and reduce some monotony particularly while writing automated tests.
## 简介

The official site: <http://mockjs.com>
Mockjs 是 [Mockjs](http://mockjs.com/) 的 ESM 重构版本,由于 [Mockjs](http://mockjs.com/) 源代码实在太过久远,本人用了一个月时间将 [Mockjs](http://mockjs.com/) 的大部分模块进行细分与简化,使用 Vite 打包,便于广大开发者学习。

## Features
```sh
npm i -D mockjs-ts
```

* Generate simulated data according to the data template
* Provide request/response mocking for ajax requests
* ~~Generate simulated data according to HTML-based templates~~
## 与 [Mockjs](http://mockjs.com/) 的异同点

This library is loosely inspired by Elijah Manor's post [Mocking
Introduction](http://www.elijahmanor.com/2013/04/angry-birds-of-javascript-green-bird.html), [mennovanslooten/mockJSON](https://github.com/mennovanslooten/mockJSON), [appendto/jquery-mockjax](https://github.com/appendto/jquery-mockjax) and [victorquinn/chancejs](https://github.com/victorquinn/chancejs/).
### 同

## Questions?
If you have any questions, please feel free to ask through [New Issue](https://github.com/nuysoft/Mock/issues/new).
1. Mockjs-ts 沿袭 [Mockjs](http://mockjs.com/) 的所有 API,不对任何的 API 进行更改。
2. Mockjs-ts 通过了所有 Mockjs 的测试,API 效果一致
3. 从 1.0.0 版本起对 node 版本也支持使用。

## Reporting an Issue
Make sure the problem you're addressing is reproducible. Use <http://jsbin.com/> or <http://jsfiddle.net/> to provide a test page. Indicate what browsers the issue can be reproduced in. What version of Mock.js is the issue reproducible in. Is it reproducible after updating to the latest version?
### 异

## License
Mock.js is available under the terms of the [MIT License](./LICENSE).
1. 对源代码中的大部分循环遍历逻辑进行了修改。
2. 引用 lodash 和 color 库对源代码中的随机函数和颜色转换等函数进行简化。(原作者当年是没有依靠任何库的,这一点令我很佩服)
3. 支持通过 `registerRandom` 方法注册自定义随机函数。
4. 源代码打包方式改为 rollup 打包,生成 esm 版本,cjs 版本和 iife 版本。
5. 扩充 [Mockjs](http://mockjs.com/) 的功能,比如提供 timestamp 模板等。
6. 更改随机颜色为从 [中国色](http://zhongguose.com) 中抽取一种颜色。 (中国色 数据来自于 http://zhongguose.com)
7. 删除 XHR 代理时的同步行为,这个行为已经不被浏览器所使用。
8. 使用继承代理的方式而不是覆盖 XHR 请求的方式代理 原生 XHR 行为。
9. 代码通过 eslint 和 prettier 进行风格化管理,加强代码的可读性。

## 教程

本项目维持与 [Mockjs](http://mockjs.com/) 一致的 API,故原作者的教程是完全可以实现的。
官方网站:http://mockjs.com/

## 从 Mockjs 迁移到 Mockjs-ts

不用修改任何的 API ,只需要改变 Mockjs 的引用方式即可。

### iife 即浏览器使用

```html
<script src="https://cdn.jsdelivr.net/npm/mockjs-ts/dist/mock.umd.js"></script>
```

### esm 即用 import 导入

```js
import Mock from 'Mock';
import Mock from 'https://cdn.jsdelivr.net/npm/mockjs-ts/dist/mock.mjs';
```

## 新的功能

1. 生成 timestamp

```js
Mock.mock('@timestamp'); // 生成数字型的 timestamp
```

2. 支持 fetch 代理

```js
Mock.mock('fake/1', 'get', {
'timestamp|10': ['@timestamp'],
});
fetch('fake/1')
.then((res) => res.json())
.then(console.log);
```

## 作者的闲聊

Mockjs 无疑是一个非常优秀的开源库,这个开源库在模块化思想还没有开始流行,运用类似 jQuery 的编程结构完成了对 Mockjs 的编写,作者的功底很深厚,几乎每个代码文件都有 500 多行,大量运用函数重载,但是整个 Mockjs 的 bug 出奇的少,足见其对于整个项目的理解。

我想要对 Mockjs 进行修改的原因是我的前端爬虫项目需要一个前端的 API 后台,而 Mockjs 对 fetch 这个已经流行了很久的新 API 没有进行适配!所以我下定决心将 Mockjs 全部使用 esm 模块化的方式对源代码进行了重写,重写过程中发现了很多不严谨的地方也加以了限制,对于某些底层的繁复的代码使用 lodash 的函数进行了替代。完善后的 Mockjs 是完全可以运行在浏览器端的,而且完整地运行相应地函数。
Loading