File tree Expand file tree Collapse file tree 8 files changed +132
-7
lines changed Expand file tree Collapse file tree 8 files changed +132
-7
lines changed Original file line number Diff line number Diff line change
1
+
2
+ import { Entity } from '../interfaces/Entity'
3
+ import { EntityBase } from './EntityBase'
4
+
5
+ export class Address extends EntityBase implements Entity {
6
+ public errorMessage :string = 'invalid'
7
+ private address
8
+ private regex = / 0 x [ a - f A - F 0 - 9 ] { 40 } / ;
9
+
10
+ constructor ( address : string ) {
11
+ super ( )
12
+ this . address = address
13
+ }
14
+
15
+ valid ( ) : boolean {
16
+ return this . address . match ( this . regex ) !== null
17
+ }
18
+ }
Original file line number Diff line number Diff line change 1
1
2
2
import { Entity } from '../interfaces/Entity'
3
+ import { EntityBase } from './EntityBase'
3
4
4
- export class ApiKey implements Entity {
5
+ export class ApiKey extends EntityBase implements Entity {
6
+ public errorMessage :string = 'invalid'
5
7
private apiKey
6
8
private keyLength : number = 33
7
9
constructor ( apiKey : string ) {
10
+ super ( )
8
11
this . apiKey = apiKey
9
12
}
10
13
11
- validate ( ) : void {
12
- if ( ! this . valid ( ) ) {
13
- throw new Error ( 'invalid' )
14
- }
15
- }
16
-
17
14
valid ( ) : boolean {
18
15
return this . apiKey . length !== this . keyLength
19
16
}
Original file line number Diff line number Diff line change
1
+ export abstract class EntityBase {
2
+ public errorMessage : string
3
+ validate ( ) : void {
4
+ if ( ! this . valid ( ) ) {
5
+ throw new Error ( this . errorMessage )
6
+ }
7
+ }
8
+
9
+ valid ( ) :boolean {
10
+ return true
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+
2
+ import { Entity } from '../interfaces/Entity'
3
+ import { EntityBase } from './EntityBase'
4
+ /*
5
+ * @see https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hexadecimal-in-javascript
6
+ */
7
+ export class HexQuantity extends EntityBase implements Entity {
8
+ public errorMessage :string = 'invalid'
9
+ private hexString :string
10
+
11
+ constructor ( hexString : string ) {
12
+ super ( )
13
+ this . hexString = hexString
14
+ }
15
+
16
+ valid ( ) : boolean {
17
+ return Number . isInteger ( parseInt ( this . hexString , 16 ) )
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+
2
+ import { Entity } from '../interfaces/Entity'
3
+ import { EntityBase } from './EntityBase'
4
+ import { sort } from '../parameters/sort'
5
+
6
+ export class Sort extends EntityBase implements Entity {
7
+ public errorMessage :string = 'invalid'
8
+ private sortParam :string
9
+
10
+ constructor ( sortParam : string ) {
11
+ super ( )
12
+ this . sortParam = sortParam
13
+ }
14
+
15
+ valid ( ) : boolean {
16
+ return sort . asc === this . sortParam || sort . desc === this . sortParam
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ import test from 'ava'
2
+
3
+ import { Address } from '../../src/entities/Address'
4
+
5
+ test ( 'Instantiated' , ( t ) => {
6
+ const address = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
7
+ const a = new Address ( address )
8
+ t . truthy ( a )
9
+ } )
10
+
11
+
12
+ test ( 'valid address is valid' , ( t ) => {
13
+ const address = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
14
+ const a = new Address ( address )
15
+ t . truthy ( a . valid ( ) )
16
+ } )
17
+
18
+
19
+ test ( 'invalid address is invalid' , ( t ) => {
20
+ const address = 'dbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
21
+ const a = new Address ( address )
22
+ t . false ( a . valid ( ) )
23
+ } )
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
Original file line number Diff line number Diff line change
1
+ import test from 'ava'
2
+
3
+ import { HexQuantity } from '../../src/entities/HexQuantity'
4
+
5
+ test ( 'Instantiated' , ( t ) => {
6
+ const hex = 'TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W'
7
+ const a = new HexQuantity ( hex )
8
+ t . truthy ( a )
9
+ } )
10
+
Original file line number Diff line number Diff line change
1
+ import test from 'ava'
2
+ import { Sort } from '../../src/entities/Sort'
3
+
4
+ test ( 'asc is ok' , ( t ) => {
5
+ const apiKey = 'asc'
6
+ const a = new Sort ( apiKey )
7
+ t . truthy ( a . valid ( ) )
8
+ } )
9
+
10
+ test ( 'desc is ok' , ( t ) => {
11
+ const apiKey = 'desc'
12
+ const a = new Sort ( apiKey )
13
+ t . truthy ( a . valid ( ) )
14
+ } )
15
+
16
+ test ( 'foo false' , ( t ) => {
17
+ const apiKey = 'foo'
18
+ const a = new Sort ( apiKey )
19
+ t . falsy ( a . valid ( ) )
20
+ } )
You can’t perform that action at this time.
0 commit comments