Skip to content

Commit f24a80a

Browse files
committed
Added more entities to esily check parameters for validity
1 parent ea6780e commit f24a80a

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

src/entities/HexQuantity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class HexQuantity extends EntityBase implements Entity {
1414
}
1515

1616
valid(): boolean {
17-
return Number.isInteger(parseInt(this.hexString, 16))
17+
var a = parseInt(this.hexString,16);
18+
return (a.toString(16) === this.hexString)
1819
}
1920
}

src/entities/PositiveNumber.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 PositiveNumber extends EntityBase implements Entity {
8+
public errorMessage:string = 'invalid'
9+
private positiveNumber:any
10+
11+
constructor(positiveNumber: any) {
12+
super()
13+
this.positiveNumber = positiveNumber
14+
}
15+
16+
valid(): boolean {
17+
18+
let numerical = parseInt(this.positiveNumber)
19+
20+
if (numerical < 0) {
21+
return false
22+
}
23+
return true
24+
}
25+
}

src/parameters/operand.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export enum operand {and = 'and', or = 'or'}

test/entities/HexQuantityTest.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@ import test from 'ava'
33
import { HexQuantity } from '../../src/entities/HexQuantity'
44

55
test('Instantiated', (t)=> {
6-
const hex = 'TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W'
6+
const hex = '0x10FB78'
77
const a = new HexQuantity(hex)
88
t.truthy(a)
99
})
1010

11+
test.skip('valid', (t)=> {
12+
const hex = '10FB78'
13+
const a = new HexQuantity(hex)
14+
t.truthy(a.valid())
15+
})
16+
17+
test('invalid', (t)=> {
18+
const hex = 'bar'
19+
const a = new HexQuantity(hex)
20+
t.falsy(a.valid())
21+
})
22+
23+
24+

test/entities/PositiveNumberTest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import test from 'ava'
2+
3+
import { PositiveNumber } from '../../src/entities/PositiveNumber'
4+
5+
6+
test('1 is valid', t => {
7+
const numerical = 1;
8+
const n = new PositiveNumber(numerical)
9+
t.truthy(n.valid())
10+
})
11+
12+
test('0 is valid', t => {
13+
const numerical = 0;
14+
const n = new PositiveNumber(numerical)
15+
t.truthy(n.valid())
16+
})
17+
18+
test('-1 is invalid', t => {
19+
const numerical = -1;
20+
const n = new PositiveNumber(numerical)
21+
t.falsy(n.valid())
22+
})

0 commit comments

Comments
 (0)