Skip to content

Commit ea6780e

Browse files
committed
Add more entities
1 parent c9276a6 commit ea6780e

File tree

8 files changed

+132
-7
lines changed

8 files changed

+132
-7
lines changed

src/entities/Address.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 = /0x[a-fA-F0-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+
}

src/entities/Apikey.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11

22
import { Entity } from '../interfaces/Entity'
3+
import { EntityBase } from './EntityBase'
34

4-
export class ApiKey implements Entity {
5+
export class ApiKey extends EntityBase implements Entity {
6+
public errorMessage:string = 'invalid'
57
private apiKey
68
private keyLength: number = 33
79
constructor(apiKey: string) {
10+
super()
811
this.apiKey = apiKey
912
}
1013

11-
validate(): void {
12-
if (!this.valid()) {
13-
throw new Error('invalid')
14-
}
15-
}
16-
1714
valid(): boolean {
1815
return this.apiKey.length !== this.keyLength
1916
}

src/entities/EntityBase.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

src/entities/HexQuantity.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

src/entities/Sort.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

test/entities/AddressTest.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+

test/entities/HexQuantityTest.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+

test/entities/SortTest.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
})

0 commit comments

Comments
 (0)