File tree Expand file tree Collapse file tree 4 files changed +161
-0
lines changed
solution/2700-2799/2704.To Be Or Not To Be Expand file tree Collapse file tree 4 files changed +161
-0
lines changed Original file line number Diff line number Diff line change 50
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
51
52
52
``` ts
53
+ type ToBeOrNotToBe = {
54
+ toBe: (val : any ) => boolean ;
55
+ notToBe: (val : any ) => boolean ;
56
+ };
57
+
58
+ function expect(val : any ): ToBeOrNotToBe {
59
+ return {
60
+ toBe : (toBeVal : any ) => {
61
+ if (val !== toBeVal ) {
62
+ throw new Error (' Not Equal' );
63
+ }
64
+ return true ;
65
+ },
66
+ notToBe : (notToBeVal : any ) => {
67
+ if (val === notToBeVal ) {
68
+ throw new Error (' Equal' );
69
+ }
70
+ return true ;
71
+ },
72
+ };
73
+ }
74
+
75
+ /**
76
+ * expect(5).toBe(5); // true
77
+ * expect(5).notToBe(5); // throws "Equal"
78
+ */
79
+ ```
53
80
81
+ ### ** JavaScript**
82
+
83
+ ``` js
84
+ /**
85
+ * @param {string} val
86
+ * @return {Object}
87
+ */
88
+ var expect = function (val ) {
89
+ return {
90
+ toBe : function (expected ) {
91
+ if (val !== expected) {
92
+ throw new Error (' Not Equal' );
93
+ }
94
+ return true ;
95
+ },
96
+ notToBe : function (expected ) {
97
+ if (val === expected) {
98
+ throw new Error (' Equal' );
99
+ }
100
+ return true ;
101
+ },
102
+ };
103
+ };
104
+
105
+ /**
106
+ * expect(5).toBe(5); // true
107
+ * expect(5).notToBe(5); // throws "Equal"
108
+ */
54
109
```
55
110
56
111
<!-- tabs:end -->
Original file line number Diff line number Diff line change 43
43
### ** TypeScript**
44
44
45
45
``` ts
46
+ type ToBeOrNotToBe = {
47
+ toBe: (val : any ) => boolean ;
48
+ notToBe: (val : any ) => boolean ;
49
+ };
50
+
51
+ function expect(val : any ): ToBeOrNotToBe {
52
+ return {
53
+ toBe : (toBeVal : any ) => {
54
+ if (val !== toBeVal ) {
55
+ throw new Error (' Not Equal' );
56
+ }
57
+ return true ;
58
+ },
59
+ notToBe : (notToBeVal : any ) => {
60
+ if (val === notToBeVal ) {
61
+ throw new Error (' Equal' );
62
+ }
63
+ return true ;
64
+ },
65
+ };
66
+ }
67
+
68
+ /**
69
+ * expect(5).toBe(5); // true
70
+ * expect(5).notToBe(5); // throws "Equal"
71
+ */
72
+ ```
46
73
74
+ ### ** JavaScript**
75
+
76
+ ``` js
77
+ /**
78
+ * @param {string} val
79
+ * @return {Object}
80
+ */
81
+ var expect = function (val ) {
82
+ return {
83
+ toBe : function (expected ) {
84
+ if (val !== expected) {
85
+ throw new Error (' Not Equal' );
86
+ }
87
+ return true ;
88
+ },
89
+ notToBe : function (expected ) {
90
+ if (val === expected) {
91
+ throw new Error (' Equal' );
92
+ }
93
+ return true ;
94
+ },
95
+ };
96
+ };
97
+
98
+ /**
99
+ * expect(5).toBe(5); // true
100
+ * expect(5).notToBe(5); // throws "Equal"
101
+ */
47
102
```
48
103
49
104
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } val
3
+ * @return {Object }
4
+ */
5
+ var expect = function ( val ) {
6
+ return {
7
+ toBe : function ( expected ) {
8
+ if ( val !== expected ) {
9
+ throw new Error ( 'Not Equal' ) ;
10
+ }
11
+ return true ;
12
+ } ,
13
+ notToBe : function ( expected ) {
14
+ if ( val === expected ) {
15
+ throw new Error ( 'Equal' ) ;
16
+ }
17
+ return true ;
18
+ } ,
19
+ } ;
20
+ } ;
21
+
22
+ /**
23
+ * expect(5).toBe(5); // true
24
+ * expect(5).notToBe(5); // throws "Equal"
25
+ */
Original file line number Diff line number Diff line change
1
+ type ToBeOrNotToBe = {
2
+ toBe : ( val : any ) => boolean ;
3
+ notToBe : ( val : any ) => boolean ;
4
+ } ;
5
+
6
+ function expect ( val : any ) : ToBeOrNotToBe {
7
+ return {
8
+ toBe : ( toBeVal : any ) => {
9
+ if ( val !== toBeVal ) {
10
+ throw new Error ( 'Not Equal' ) ;
11
+ }
12
+ return true ;
13
+ } ,
14
+ notToBe : ( notToBeVal : any ) => {
15
+ if ( val === notToBeVal ) {
16
+ throw new Error ( 'Equal' ) ;
17
+ }
18
+ return true ;
19
+ } ,
20
+ } ;
21
+ }
22
+
23
+ /**
24
+ * expect(5).toBe(5); // true
25
+ * expect(5).notToBe(5); // throws "Equal"
26
+ */
You can’t perform that action at this time.
0 commit comments