Skip to content

Commit

Permalink
ci(docs): 添加文档自动生成功能
Browse files Browse the repository at this point in the history
同时修改了API的注释
  • Loading branch information
anotherso1a committed Oct 22, 2019
1 parent e157ff8 commit 9eb5a98
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 27 deletions.
140 changes: 119 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,160 @@

## API

### Array
### shuffle

#### shuffle
数组洗牌,返回顺序随机的新数组(浅拷贝)

洗牌算法,用于随机打乱数组
#### 参数

|类型|参数名|描述|
|:-:|:-:|:-:|
|Array|arr|需要洗牌的数组,纯数字|


#### 返回值

|类型|描述|
|:-:|:-:|
|Array|打乱后的新数组|


#### 例子

```js
afl.shuffle([1,2,3]) //[2,1,3]
afl.shuffle([1,2,3]) //[3,2,1]
afl.shuffle([1]) //[1]
```

### Function
### throttle

节流函数(连续触发时第一次立即触发,之后每 delay ms 执行一次)

#### 参数

|类型|参数名|描述|
|:-:|:-:|:-:|
|Function|fn|需要节流的方法|
|Number|delay|单位:ms,每次执行函数的间隔|


#### 返回值

#### throttle
|类型|描述|
|:-:|:-:|
|Function|被节流后的函数|

节流函数

#### 例子

```js
let count = {n:0}
let thottled = afl.throttle(count=> ++count.n,500)
setTimeout(()=>{
for(let i = 0; i < 10 ; i++){
thottled(count)
}
console.log(count.n)//1
for(let i = 0; i < 10 ; i++){
thottled(count)
}
console.log(count.n)//1
},1000)
```

### Object
### object2map

对象转map

#### 参数

|类型|参数名|描述|
|:-:|:-:|:-:|
||obj|obj 需要转换成map的对象|


#### 返回值

#### object2map
|类型|描述|
|:-:|:-:|
|Map|转换后的Map|

对象转为Map

#### 例子

```js
afl.object2map({
a: 1,
b: 2
a: 1,
b: 2
}) //Map(2) {"a" => 1, "b" => 2}
```

#### objectDig
### objectDig

从对象中找到特定key的值

#### 参数

|类型|参数名|描述|
|:-:|:-:|:-:|
|Object|obj||
|string|target||


查找对象中特定key值
#### 返回值

|类型|描述|
|:-:|:-:|
||{}|


#### 例子

```js
afl.objectDig({a:{b:{c:{d:4}}}},'d') //4
```

### String
### parseQuery

将链接search转换为对象,可以用原生api: URLSearchParams

#### 参数

|类型|参数名|描述|
|:-:|:-:|:-:|
|String|str|search字符串,不包含?|

#### parseQuery && stringifyQuery

序列化或解析search字符串
#### 返回值

|类型|描述|
|:-:|:-:|
|Object|参数对象|


#### 例子

```js
afl.parseQuery('a=1&b=2') //{a: "1", b: "2"}
```

### stringifyQuery

将对象转换成search字符串(不带问号)

#### 参数

|类型|参数名|描述|
|:-:|:-:|:-:|
|Object|obj|要被转换成search字符串的对象|


#### 返回值

|类型|描述|
|:-:|:-:|
|String|字符串|


#### 例子

```js
afl.stringifyQuery({a:1,b:2}) //a=1&b=2
```

Expand Down
105 changes: 105 additions & 0 deletions README.old.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 函数库

## Usage

```html
<script src="js/afl.min.js"></script>
<script>
afl.shuffle([1,2,3]) //[2,1,3]
afl.object2map({
a: 1,
b: 2
}) //Map(2) {"a" => 1, "b" => 2}
afl.parseQuery('a=1&b=2') //{a: "1", b: "2"}
afl.stringifyQuery({a:1,b:2}) //a=1&b=2
afl.objectDig({a:{b:{c:{d:4}}}},'d') //4
</script>
```

## 描述

初步打算封装一些自己造的轮子

## API

### Array

#### shuffle

洗牌算法,用于随机打乱数组

```js
afl.shuffle([1,2,3]) //[2,1,3]
```

### Function

#### throttle

节流函数

```js
let count = {n:0}
let thottled = afl.throttle(count=> ++count.n,500)
setTimeout(()=>{
for(let i = 0; i < 10 ; i++){
thottled(count)
}
console.log(count.n)//1
},1000)
```

### Object

#### object2map

对象转为Map

```js
afl.object2map({
a: 1,
b: 2
}) //Map(2) {"a" => 1, "b" => 2}
```

#### objectDig

查找对象中特定key值

```js
afl.objectDig({a:{b:{c:{d:4}}}},'d') //4
```

### String

#### parseQuery && stringifyQuery

序列化或解析search字符串

```js
afl.parseQuery('a=1&b=2') //{a: "1", b: "2"}
afl.stringifyQuery({a:1,b:2}) //a=1&b=2
```

## 项目说明

**1.** `commit`提交规范,请使用`npm run commit` 进行提交
参数如下:

- feat:新增功能;
- fix:修复bug;
- docs:修改文档;
- refactor:代码重构,未新增任何功能和修复任何bug;
- build:改变构建流程,新增依赖库、工具等(例如webpack修改);
- style:仅仅修改了空格、缩进等,不改变代码逻辑;
- perf:改善性能和体现的修改;
- chore:非src和test的修改;
- test:测试用例的修改;
- ci:自动化流程配置修改;
- revert:回滚到上一个版本;
scope:【可选】用于说明commit的影响范围
subject:commit的简要说明,尽量简短

## 下一步计划

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"cfb": "sh scripts/feature.sh c",
"mfb": "sh scripts/feature.sh m",
"dfb": "sh scripts/feature.sh d",
"docs": "node ./scripts/createDocs/index.js",
"build": "npm run pr && npm run test test && webpack"
},
"keywords": [
Expand Down
57 changes: 57 additions & 0 deletions scripts/createDocs/analysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
let parttern = /\/\*[\s\S]+?\*\//g

function removeSign(e) {
return e.replace(/@\w+?(\s|\b)/g, '').trim()
}

function parseStatment(e) {
let type, name, desc
try {
type = e.match(/\{.+?\}/g)[0].replace(/[{}]/g, '')
} catch { type = "" }
try {
name = e.replace(/\{.+?\}/g, '').match(/[a-zA-Z\d]+/g)[0]
} catch { name = '' }
try {
desc = e.replace(/(\{.+?\})\s+?[a-zA-Z\d]*/, '').trim()
} catch { desc = "" }
return { type, name, desc }
}

function format(content) {
if (!content) return null
let name, desc, params, returns, example
let texts = content
.split(/\n+/)
.map(e => e.replace(/(\/\*|\*\/|\*)/g, '').trim())
.filter(e => e)
name = texts.filter(e => e.startsWith('@name')).map(removeSign).join('\n')
desc = texts.filter(e => e.startsWith('@description')).map(removeSign).join('\n')
params = texts.filter(e => e.startsWith('@param')).map(removeSign).map(parseStatment)
returns = texts.filter(e => e.startsWith('@returns')).map(removeSign).map(parseStatment)
let index = texts.findIndex(e => e.startsWith('@example'))
example = ~index ?
texts.slice(index + 1).join('\n') :
''

return {
name,
desc,
params,
returns,
example
}
}


function getNotes(content) {
try {
let notes = content.match(parttern)[0]
return format(notes)
} catch (err) {
console.log(err)
return ''
}
}

module.exports = getNotes
23 changes: 23 additions & 0 deletions scripts/createDocs/getFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const join = require('path').join;

async function getJsonFiles(filePath) {
let jsonFiles = [];

function findJsonFile(path) {
let files = fs.readdirSync(path);
files.forEach(item => {
let fPath = join(path, item);
let stat = fs.statSync(fPath);
if (stat.isDirectory() === true) {
findJsonFile(fPath);
}
if (stat.isFile() === true) {
jsonFiles.push(fPath);
}
});
}
findJsonFile(filePath);
return jsonFiles
}
module.exports = getJsonFiles
Loading

0 comments on commit 9eb5a98

Please sign in to comment.