Skip to content

Commit

Permalink
feat: object.is
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 committed Dec 2, 2019
1 parent b1d166c commit 437d64a
Show file tree
Hide file tree
Showing 10 changed files with 702 additions and 610 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ We welcome all contributions. You can submit any ideas as [pull requests](https:

- [ ] Object

- [x] is

- [ ] ES6

- [ ] Promise
Expand Down
1,222 changes: 615 additions & 607 deletions plans.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plans.xmind
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Array/isArray/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Rainy
* @Date: 2019-11-14 19:25:01
* @LastEditors: Rainy
* @LastEditTime: 2019-11-21 19:08:54
* @LastEditTime: 2019-12-02 20:16:48
*/

import { isAbsType } from '../../utils/type';
Expand Down
22 changes: 22 additions & 0 deletions src/Object/is/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### 原理

Object.is() 判断两个值是否相同. 如果下列任何一项成立, 则两个值相同:

- 两个值都是 undefined
- 两个值都是 null
- 两个值都是 true 或者都是 false
- 两个值是由相同个数的字符按照相同的顺序组成的字符串
- 两个值指向同一个对象
- 两个值都是数字并且
- 都是正零 +0
- 都是负零 -0
- 都是 NaN
- 都是除零和 NaN 外的其它同一个数字

这种相等性判断逻辑和传统的 `==` 运算不同, `==` 运算符会对它两边的操作数做隐式类型转换(如果它们类型不同), 然后才进行相等性比较, (所以才会有类似 `""` `==` `false` 等于 `true` 的现象), 但 `Object.is` 不会做这种类型转换.

这与 `===` 运算符的判定方式也不一样. `===` 运算符(和`==` 运算符)将数字值 `-0``+0` 视为相等, 并认为 `Number.NaN` 不等于 `NaN`.

### 参考资料

[Object is](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
44 changes: 44 additions & 0 deletions src/Object/is/__tests__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @Author: Rainy
* @Date: 2019-11-14 19:25:01
* @LastEditors: Rainy
* @LastEditTime: 2019-12-02 20:28:56
*/

import { _is } from '.';

test(`is('foo', 'foo') should true`, () => {
expect(_is('foo', 'foo')).toBe(true);
});

test(`is([], []) should false`, () => {
expect(_is([], [])).toBe(false);
});

const u1 = { name: 'Rain120' };
const u2 = { name: 'Rain120' };

test(`is(u1, u1) should true`, () => {
expect(_is(u1, u1)).toBe(true);
});

test(`is(u1, u2) should false`, () => {
expect(_is(u1, u2)).toBe(false);
});

test(`is(null, null) should true`, () => {
expect(_is(null, null)).toBe(true);
});

test(`is(0, -0) should false`, () => {
expect(_is(0, -0)).toBe(false);
});

test(`is(0, +0) should true`, () => {
expect(_is(0, +0)).toBe(true);
});


test(`is(NaN, 0 / 0) should true`, () => {
expect(_is(NaN, 0 / 0)).toBe(true);
});
13 changes: 13 additions & 0 deletions src/Object/is/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* @Author: Rainy
* @Date: 2019-11-14 19:25:01
* @LastEditors: Rainy
* @LastEditTime: 2019-12-02 20:32:37
*/
export function _is(source: any, target: any): boolean {
if (source === target) {
return source !== 0 || 1 / source === 1 / target;
} else {
return source !== source && target !== target;
}
}
4 changes: 2 additions & 2 deletions src/call/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* @Author: Rainy
* @Date: 2019-11-14 19:25:01
* @LastEditors: Rainy
* @LastEditTime: 2019-11-22 11:19:18
* @LastEditTime: 2019-12-02 20:02:34
*/

import { ObjectMap } from '../types/index';
import { ObjectMap } from 'types';

// @ts-ignore
Function.prototype._call = function(context: ObjectMap<any>, ...arg: any): any {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "src/", /* Base directory to resolve non-absolute module names. */
"paths": {
"src/*": ["src/*"],
"types/*": ["types/*"],
"utils/*": ["utils/*"]
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
Expand Down
2 changes: 2 additions & 0 deletions zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

- [ ] Object

- [x] is

- [ ] ES6

- [ ] Promise
Expand Down

0 comments on commit 437d64a

Please sign in to comment.