Skip to content

Commit 979df0a

Browse files
authored
Merge pull request #19 from Rain120/dev
feat: apply, bind; commit-push.sh; move call instanceof new to primitive
2 parents cf72dbf + f183666 commit 979df0a

24 files changed

+278
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1.0.0 (2020-03-22)
1+
# 1.0.0 (2020-04-09)
22

33

44
### Bug Fixes
@@ -58,9 +58,10 @@
5858
* update netlify status ([013b5ca](https://github.com/Rain120/awesome-javascript-code-implementation/commit/013b5ca5847873adabbff93a89daa41500ccf153))
5959
* update readme for plans ([b427b96](https://github.com/Rain120/awesome-javascript-code-implementation/commit/b427b9667cc97c77df75ef0af56eb84b6b4c3ba6))
6060
* update test.yml -> ci.yml and set command ([43b96b6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/43b96b63fc2788a5ea37fa7567c6a6dced4b875f))
61+
* update vdom-diff docs ([06590c5](https://github.com/Rain120/awesome-javascript-code-implementation/commit/06590c56d0df7cbba68bb705cb7e159f6f765c3a))
6162
* update xmind ([71317c6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/71317c67376bc8196f80e25391adfdc9b2496380))
62-
* update xmind ([408ca68](https://github.com/Rain120/awesome-javascript-code-implementation/commit/408ca685142dc425e90be64a284016d57d580893))
6363
* update xmind ([4256758](https://github.com/Rain120/awesome-javascript-code-implementation/commit/4256758c5fe0a036ce3ececc43f848e48964d7a0))
64+
* update xmind ([408ca68](https://github.com/Rain120/awesome-javascript-code-implementation/commit/408ca685142dc425e90be64a284016d57d580893))
6465
* update xmind ([cb23933](https://github.com/Rain120/awesome-javascript-code-implementation/commit/cb23933f46152c59a7b08f84d7355b35bba25002))
6566
* update: plans; fix: readme bug ([d44e559](https://github.com/Rain120/awesome-javascript-code-implementation/commit/d44e559def7834fcccbb19797a1d29bf5cc4e5b7))
6667
* vdom diff algorithm ([3b7779d](https://github.com/Rain120/awesome-javascript-code-implementation/commit/3b7779dcad7e184027d1bddfa6387224c2f009d3))

commit-push.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
###
2+
# @Author: Rainy
3+
# @Date: 2020-04-09 19:35:20
4+
# @LastEditors: Rainy
5+
# @LastEditTime: 2020-04-09 19:38:19
6+
###
7+
8+
#!/usr/bin/env bash
9+
10+
echo "Deploy to gh-pages"
11+
npm run deploy || exit 1
12+
13+
echo "Deploy to gh-pages"
14+
npm run changelog
15+
16+
echo "Testing for codes"
17+
npm run testing || exit 1
18+
19+
echo "------------ commit push shell run ------------"
20+
21+
read -p "Input your commit msg (*): " msg
22+
read -p "Amend commit(Y, y, default: N): " amend
23+
read -p "Need pull current branch(Y, y, default: N): " pull
24+
25+
len=72
26+
curBranch=$(git branch | awk '/\*/ { print $2; }')
27+
28+
# start reg
29+
msgRegLower='^\s*(feat|fix|docs|style|chore|build|ci|perf|refactor|revert|test|temp|update)\s*\:*\s*'
30+
msgRegUpper='^\s*(Feat|Fix|Docs|Style|Chore|Build|Ci|Perf|Refactor|Revert|Test|Temp|Update)\s*\:*\s*'
31+
32+
# 另一种获取当前 branch_name 的方法
33+
# git symbolic-ref --short HEAD
34+
35+
echo "Log: Cureren branch is ${curBranch}"
36+
37+
if [[ $pull = "Y" || $pull = "y" ]];then
38+
39+
read -p "Need pull branch(default: current branch, eg: it4/mobile): " branch
40+
41+
fi
42+
43+
if [ -n "$msg" ];then
44+
45+
echo "Log: Commit msg: $msg"
46+
47+
if [ ${#msg} -gt $len ];then
48+
echo "Error: msg length must less than $len"
49+
exit 1
50+
fi
51+
52+
if [[ "$msg" =~ $msgRegLower || "$msg" =~ $msgRegUpper ]];then
53+
54+
git add .
55+
56+
lint-staged
57+
58+
if [[ $amend = "Y" || $amend = "y" ]];then
59+
echo "Log: Amending message commit"
60+
git commit --amend -m "$msg" || exit 1
61+
else
62+
echo "Log: Commiting"
63+
git commit -m "$msg" || exit 1
64+
fi
65+
66+
if [[ $pull = "Y" || $pull = "y" ]];then
67+
68+
if [ -n "$branch" ];then
69+
70+
echo "Log: Pulling branch is $branch"
71+
72+
fi
73+
74+
branch=curBranch
75+
76+
echo "Log: Pulling ${branch} from remote"
77+
git pull origin ${branch} || exit 1
78+
79+
else
80+
81+
branch=${curBranch}
82+
83+
echo "Log: Current branch is $branch"
84+
85+
echo "Log: Jump pull branch"
86+
87+
fi
88+
89+
read -p "Auto Push(Y, y, default: Y): " push
90+
91+
if [ -z "${push}" ];then
92+
push='Y'
93+
fi
94+
95+
if [[ $push = "Y" || $push = "y" ]];then
96+
97+
echo "Log: Pushing"
98+
99+
git push origin ${branch} || exit 1
100+
fi
101+
102+
echo "------------ commit push shell end ------------"
103+
104+
else
105+
echo "Error: Commit message start type must be one of [feat, fix, docs, style, chore, build, ci, perf, refactor, revert, test, temp, Feat, Fix, Docs, Style, Chore, Build, Ci, Perf, Refactor, Revert, Test, Temp]"
106+
exit 1
107+
fi
108+
109+
else
110+
111+
echo "Please input commit msg"
112+
exit 1
113+
114+
fi

docs/zh/primitive/apply/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### 原理
2+
3+
调用一个具有给定 `this` 值的函数,以及作为一个数组(或类似数组对象)提供的参数。
4+
5+
::: warning
6+
注意: `call()` 方法的作用和 `apply()` 方法类似,区别就是 `call()` 方法接受的是参数列表,而 `apply()` 方法接受的是一个参数数组。
7+
:::
8+
9+
## 实现代码
10+
11+
<<< @/src/primitive/apply/index.ts
12+
13+
## 参考
14+
15+
[apply](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply)

docs/zh/primitive/bind/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## 原理
2+
3+
创建一个新的函数, 在 `bind()` 被调用时, 这个新函数的 `this` 被指定为 `bind()` 的第一个参数, 而其余参数将作为新函数的参数, 供调用时使用。
4+
5+
特点:
6+
7+
- 改变 `this` 指向
8+
9+
- 返回一个函数
10+
11+
- 可以传入参数
12+
13+
::: tip
14+
Note:
15+
16+
- `new` 的优先级大于 `bind`, 如果 `bind` 绑定后的函数被 `new`了, `this` 会指向当前函数的实例
17+
18+
- 需要保留 **原函数的原型链** 上的属性和方法
19+
:::
20+
21+
## 实现代码
22+
23+
<<< @/src/primitive/bind/index.ts
24+
25+
## 参考
26+
27+
[bind](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)

docs/zh/primitive/call/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## 实现代码
66

7-
<<< @/src/call/index.ts
7+
<<< @/src/primitive/call/index.ts
88

99
## 参考
1010

docs/zh/primitive/instanceof/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
判断实例对象的`__proto__`和构造函数的`prototype`是不是引用同一个地址
44

5-
![instanceof](~@images/src/instanceof/images/instanceof.png)
5+
![instanceof](~@images/src/primitive/instanceof/images/instanceof.png)
66

7-
![prototype](~@images/src/instanceof/images/prototype.png)
7+
![prototype](~@images/src/primitive/instanceof/images/prototype.png)
88

99
## 实现代码
1010

11-
<<< @/src/instanceof/index.ts
11+
<<< @/src/primitive/instanceof/index.ts

docs/zh/primitive/new/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
## 实现代码
1111

12-
<<< @/src/new/index.ts
12+
<<< @/src/primitive/new/index.ts

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"template-docs": "npm run plop model-template-docs",
2424
"docs:dev": "vuepress dev docs",
2525
"docs:build": "vuepress build docs",
26+
"commit-push": "./commit-push.sh",
2627
"deploy": "bash deploy.sh"
2728
},
2829
"keywords": [
@@ -47,7 +48,7 @@
4748
},
4849
"husky": {
4950
"hooks": {
50-
"pre-commit": "npm run deploy && npm run changelog && npm run testing && git add . && lint-staged",
51+
"pre-commit": "lint-staged",
5152
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
5253
}
5354
},

src/call/index.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/primitive/apply/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### 原理
2+
3+
调用一个具有给定 `this` 值的函数,以及作为一个数组(或类似数组对象)提供的参数。
4+
5+
注意: `call()` 方法的作用和 `apply()` 方法类似,区别就是 `call()` 方法接受的是参数列表,而 `apply()` 方法接受的是一个参数数组。
6+
7+
### 参考
8+
9+
[apply](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply)

src/primitive/apply/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: Rainy
3+
* @Date: 2020-04-09 19:02:51
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2020-04-09 19:10:17
6+
*/
7+
8+
import { ObjectMap } from 'types';
9+
10+
// @ts-ignore
11+
Function.prototype._apply = function(context: ObjectMap<any>, arg: any): any {
12+
/* istanbul ignore next */
13+
if (typeof this !== 'function') {
14+
throw new TypeError('Error');
15+
}
16+
17+
/* istanbul ignore next */
18+
context = context ? Object(context) : window
19+
20+
context.fn = this;
21+
22+
const result = context.fn(...arg);
23+
24+
delete context.fn;
25+
26+
return result;
27+
};

src/primitive/bind/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### 原理
2+
3+
创建一个新的函数, 在 `bind()` 被调用时, 这个新函数的 `this` 被指定为 `bind()` 的第一个参数, 而其余参数将作为新函数的参数, 供调用时使用。
4+
5+
特点:
6+
7+
- 改变 `this` 指向
8+
9+
- 返回一个函数
10+
11+
- 可以传入参数
12+
13+
Note:
14+
15+
- `new` 的优先级大于 `bind`, 如果 `bind` 绑定后的函数被 `new`了, `this` 会指向当前函数的实例
16+
17+
- 需要保留 **原函数的原型链** 上的属性和方法
18+
19+
### 参考
20+
21+
[bind](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)

src/primitive/bind/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: Rainy
3+
* @Date: 2020-04-09 19:02:51
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2020-04-09 19:33:41
6+
*/
7+
8+
import { ObjectMap } from 'types';
9+
10+
// @ts-ignore
11+
Function.prototype._bind = function(context: ObjectMap<any>, ...args: any): any {
12+
/* istanbul ignore next */
13+
if (typeof this !== 'function') {
14+
throw new TypeError('Bind must be called with a function');
15+
}
16+
17+
const _context = this;
18+
19+
const fb = function() {
20+
// @ts-ignore
21+
_context.apply(this instanceof fb ? this : context, args.concat(Array.prototype.slice.call(arguments)));
22+
};
23+
24+
fb.prototype = Object.create(_context.prototype);
25+
26+
return fb;
27+
};
File renamed without changes.

src/call/__tests__.ts renamed to src/primitive/call/__tests__.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Rainy
33
* @Date: 2019-11-14 19:25:01
44
* @LastEditors: Rainy
5-
* @LastEditTime: 2019-12-01 11:02:34
5+
* @LastEditTime: 2020-04-09 18:53:26
66
*/
77

88
import '.';

src/primitive/call/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: Rainy
3+
* @Date: 2019-11-14 19:25:01
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2020-04-09 19:00:57
6+
*/
7+
8+
import { ObjectMap } from 'types';
9+
10+
// @ts-ignore
11+
Function.prototype._call = function(context: ObjectMap<any>, ...arg: any): any {
12+
/* istanbul ignore next */
13+
if (typeof this !== 'function') {
14+
throw new TypeError('Error');
15+
}
16+
17+
/* istanbul ignore next */
18+
context = context ? Object(context) : window
19+
20+
context.fn = this;
21+
22+
const result = context.fn(...arg);
23+
24+
delete context.fn;
25+
26+
return result;
27+
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)