Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita655b9a

Browse files
committed
New classes and tests
1 parentdb2c39d commita655b9a

22 files changed

+976
-612
lines changed

‎README.md‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,22 @@ console.log( rules.isSatisfiedBy( 'Hello world' ) ); // false
8888

8989
##Available classes
9090

91-
-`EqualTo( value: any )`
91+
-`SameValueAs( value: any )`: equality of values, not of types, not of instances
92+
-`StrictSameValueAs( value: any )`: equality of values and types, not of instances
93+
-`EqualTo( value: any )`: equality of values or instances, with`==`
94+
-`StrictEqualTo( value: any )`: equality of values and types or of instances, with`===`
95+
-`SameTypeAs( value: any )`: equality of types
9296
-`GreaterThan( value: any )`
9397
-`GreaterThanOrEqualTo( value: any )`
9498
-`LessThan( value: any )`
9599
-`LessThanOrEqualTo( value: any )`
96100
-`Between( min: any, max: any )`
97-
-`In( values: array )`
98-
-`StartsWith( value: string, ignoreCase: boolean = false )`
99-
-`EndsWith( value: string, ignoreCase: boolean = false )`
100-
-`Contains( value: string, ignoreCase: boolean = false )`
101-
-`LengthBetween( min: any, max: any )`
102-
-`Matches( regex: RegExp )`
101+
-`In( values: array )`: inside an array
102+
-`StartsWith( value: string, ignoreCase: boolean = false )`: string starts with
103+
-`EndsWith( value: string, ignoreCase: boolean = false )`: string ends with
104+
-`Contains( value: string, ignoreCase: boolean = false )`: string contains
105+
-`LengthBetween( min: any, max: any )`: string length between two values
106+
-`Matches( regex: RegExp )`: matches a regular expression
103107

104108
All these classes extend the abstract class`Composite`, which in turn implements the interface`Spec`:
105109

@@ -122,27 +126,28 @@ interface Spec< T > {
122126

123127
##Creating your own class
124128

125-
Creating your own classis**very easy**. Just extends*abstract* class`Composite`, like in the following example. Of course, you can also extend one of the aforementioned classes or implement the interface`Spec`*(but why reinventing the wheel, right?)*.
129+
Create your own classby extending the*abstract* class`Composite`, like in the following example. Of course, you can also extend one of the aforementioned classes or implement the interface`Spec`*(but why reinventing the wheel, right?)*.
126130

127131
Let's create a class`DifferentFrom` ...
128132

129133
*...in TypeScript:*
130134
```typescript
131135
import {Composite }from'spec-pattern';
132136

133-
exportclassDifferentFrom<T>extendsComposite<T > {
137+
exportclassDifferentFrom<C,TextendsC|unknown>extendsComposite<C,T > {
134138

135-
constructor(privatevalue:T ) {
139+
constructor(private_value:T ) {
136140
super();
137141
}
138142

139-
isSatisfiedBy(candidate:T ):boolean {
140-
returnthis.value!=candidate;
143+
isSatisfiedBy(candidate:C|T ):boolean {
144+
returnthis._value!=candidate;
141145
}
142146

143147
toString():string {
144-
return'different from'+this.value;
148+
return'different from'+this._value;
145149
}
150+
146151
}
147152
```
148153

‎__tests__/Between.spec.ts‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import{Between}from"..";
2+
3+
describe('Between',()=>{
4+
5+
describe('satisfied by',()=>{
6+
7+
it('min',()=>{
8+
expect(
9+
(newBetween(-1,1)).isSatisfiedBy(-1)
10+
).toBeTruthy();
11+
});
12+
13+
it('max',()=>{
14+
expect(
15+
(newBetween(-1,1)).isSatisfiedBy(1)
16+
).toBeTruthy();
17+
});
18+
19+
it('any value between',()=>{
20+
expect(
21+
(newBetween(-1,1)).isSatisfiedBy(0)
22+
).toBeTruthy();
23+
});
24+
25+
});
26+
27+
28+
describe('not satisfied by',()=>{
29+
30+
it('less than min',()=>{
31+
expect(
32+
(newBetween(-1,1)).isSatisfiedBy(-2)
33+
).toBeFalsy();
34+
});
35+
36+
it('greater than max',()=>{
37+
expect(
38+
(newBetween(-1,1)).isSatisfiedBy(2)
39+
).toBeFalsy();
40+
});
41+
42+
});
43+
44+
45+
describe('composition of',()=>{
46+
47+
describe('not',()=>{
48+
49+
it('satisfied by - inverted logic',()=>{
50+
expect(
51+
(newBetween(-1,1))
52+
.not()
53+
.isSatisfiedBy(0)
54+
).toBeFalsy();
55+
});
56+
57+
});
58+
59+
60+
describe('or',()=>{
61+
62+
it('satisfied by - left side',()=>{
63+
expect(
64+
(newBetween(-1,1))
65+
.or(newBetween(5,7))
66+
.isSatisfiedBy(0)
67+
).toBeTruthy();
68+
});
69+
70+
it('satisfied by - right side',()=>{
71+
expect(
72+
(newBetween(-1,1))
73+
.or(newBetween(5,7))
74+
.isSatisfiedBy(6)
75+
).toBeTruthy();
76+
});
77+
78+
it('not satisfied by - when in none',()=>{
79+
expect(
80+
(newBetween(-3,-1))
81+
.or(newBetween(1,3))
82+
.isSatisfiedBy(0)
83+
).toBeFalsy();
84+
});
85+
86+
});
87+
88+
89+
describe('and',()=>{
90+
91+
it('satisfied by - when in both',()=>{
92+
expect(
93+
(newBetween(-3,0))
94+
.and(newBetween(0,3))
95+
.isSatisfiedBy(0)
96+
).toBeTruthy();
97+
});
98+
99+
it('not satisfied by - when only in left side',()=>{
100+
expect(
101+
(newBetween(-3,0))
102+
.and(newBetween(0,3))
103+
.isSatisfiedBy(-1)
104+
).toBeFalsy();
105+
});
106+
107+
it('not satisfied by - when only in right side',()=>{
108+
expect(
109+
(newBetween(-3,0))
110+
.and(newBetween(0,3))
111+
.isSatisfiedBy(1)
112+
).toBeFalsy();
113+
});
114+
115+
it('not satisfied by - when in node side',()=>{
116+
expect(
117+
(newBetween(-3,0))
118+
.and(newBetween(0,3))
119+
.isSatisfiedBy(4)
120+
).toBeFalsy();
121+
});
122+
123+
});
124+
125+
126+
describe('orNot',()=>{
127+
128+
it('satisfied by - left side',()=>{
129+
expect(
130+
(newBetween(-1,1))
131+
.orNot(newBetween(5,7))
132+
.isSatisfiedBy(0)
133+
).toBeTruthy();
134+
});
135+
136+
it('satisfied by - not in right side',()=>{
137+
expect(
138+
(newBetween(-1,1))
139+
.orNot(newBetween(5,7))
140+
.isSatisfiedBy(8)
141+
).toBeTruthy();
142+
});
143+
144+
it('not satisfied by - right side',()=>{
145+
expect(
146+
(newBetween(-3,-1))
147+
.orNot(newBetween(1,3))
148+
.isSatisfiedBy(2)
149+
).toBeFalsy();
150+
});
151+
152+
});
153+
154+
155+
describe('andNot',()=>{
156+
157+
it('satisfied by - left side and not right',()=>{
158+
expect(
159+
(newBetween(-1,1))
160+
.andNot(newBetween(5,7))
161+
.isSatisfiedBy(0)
162+
).toBeTruthy();
163+
});
164+
165+
it('not satisfied by - in right side',()=>{
166+
expect(
167+
(newBetween(-1,1))
168+
.andNot(newBetween(5,7))
169+
.isSatisfiedBy(6)
170+
).toBeFalsy();
171+
});
172+
173+
it('not satisfied by - node side',()=>{
174+
expect(
175+
(newBetween(-1,1))
176+
.andNot(newBetween(5,7))
177+
.isSatisfiedBy(8)
178+
).toBeFalsy();
179+
});
180+
181+
});
182+
183+
});
184+
185+
});

‎__tests__/Contains.spec.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import{Contains}from"..";
2+
3+
describe('Contains',()=>{
4+
5+
describe('correct',()=>{
6+
7+
it('case sensitive comparison',()=>{
8+
expect(
9+
(newContains('w')).isSatisfiedBy('Hello world')
10+
).toBeTruthy();
11+
});
12+
13+
it('case insensitive comparison',()=>{
14+
expect(
15+
(newContains('W',true)).isSatisfiedBy('hello world')
16+
).toBeTruthy();
17+
});
18+
19+
});
20+
21+
describe('not satisfied by',()=>{
22+
23+
it('value that not exists',()=>{
24+
expect(
25+
(newContains('x')).isSatisfiedBy('Hello world')
26+
).toBeFalsy();
27+
});
28+
29+
it('different case when not insensitive',()=>{
30+
expect(
31+
(newContains('W')).isSatisfiedBy('Hello world')
32+
).toBeFalsy();
33+
});
34+
35+
});
36+
37+
});

‎__tests__/EndsWith.spec.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import{EndsWith}from"..";
2+
3+
describe('EndsWith',()=>{
4+
5+
describe('satisfied by',()=>{
6+
7+
it('case sensitive',()=>{
8+
expect(
9+
(newEndsWith('world')).isSatisfiedBy('Hello world')
10+
).toBeTruthy();
11+
});
12+
13+
it('case insensitive',()=>{
14+
expect(
15+
(newEndsWith('World',true)).isSatisfiedBy('hello world')
16+
).toBeTruthy();
17+
});
18+
19+
});
20+
21+
describe('not satisfied by',()=>{
22+
23+
it('value that is not in the end',()=>{
24+
expect(
25+
(newEndsWith('hello')).isSatisfiedBy('Hello world')
26+
).toBeFalsy();
27+
});
28+
29+
it('different case when not insensitive',()=>{
30+
expect(
31+
(newEndsWith('World')).isSatisfiedBy('Hello world')
32+
).toBeFalsy();
33+
});
34+
35+
});
36+
37+
});

‎__tests__/EqualTo.spec.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import{EqualTo}from"..";
2+
3+
describe('EqualTo',()=>{
4+
5+
describe('satisfied by',()=>{
6+
7+
it('same value, both integer numbers',()=>{
8+
expect(
9+
(newEqualTo(0)).isSatisfiedBy(0)
10+
).toBeTruthy();
11+
});
12+
13+
it('same value, number and string',()=>{
14+
expect(
15+
(newEqualTo(0)).isSatisfiedBy('0')
16+
).toBeTruthy();
17+
});
18+
19+
it('same value, string and number',()=>{
20+
expect(
21+
(newEqualTo('0')).isSatisfiedBy(0.0)
22+
).toBeTruthy();
23+
});
24+
25+
});
26+
27+
describe('not satisfied by',()=>{
28+
29+
it('a different value',()=>{
30+
expect(
31+
(newEqualTo(0)).isSatisfiedBy(1)
32+
).toBeFalsy();
33+
});
34+
35+
it('two arrays',()=>{
36+
expect(
37+
(newEqualTo([1,2])).isSatisfiedBy([1,2])
38+
).toBeFalsy();
39+
});
40+
41+
it('two objects',()=>{
42+
expect(
43+
(newEqualTo({"a":10})).isSatisfiedBy({"a":10})
44+
).toBeFalsy();
45+
});
46+
47+
});
48+
49+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp