Skip to content

Commit

Permalink
Add number range
Browse files Browse the repository at this point in the history
  • Loading branch information
buresmi7 committed May 22, 2019
1 parent 9eab610 commit 25f822d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valid-objects-ts",
"version": "1.0.8",
"version": "1.0.9",
"main": ".dist/index.js",
"types": ".dist/index.d.ts",
"repository": "[email protected]:Travelport-Czech/valid-objects-ts.git",
Expand Down
3 changes: 3 additions & 0 deletions src/errors/ValidObjectLogicError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ExtendableError } from 'extendable-error'

export class ValidObjectLogicError extends ExtendableError {}
23 changes: 23 additions & 0 deletions src/validObjects/ValidNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ValidNumber } from '@/validObjects/ValidNumber'
import { expect } from 'chai'

describe('ValidNumber', () => {
it('Construct default', () => {
expect(new ValidNumber(0).value).to.equal(0)
expect(new ValidNumber(1).value).to.equal(1)
expect(new ValidNumber(-1).value).to.equal(-1)
expect(new ValidNumber(-1000000000).value).to.equal(-1000000000)
expect(new ValidNumber(1000000000).value).to.equal(1000000000)
})

it('Construct with range', () => {
expect(new ValidNumber(-2, -2, 2).value).to.equal(-2)
expect(new ValidNumber(0, -2, 2).value).to.equal(0)
expect(new ValidNumber(1, -2, 2).value).to.equal(1)
expect(new ValidNumber(2, -2, 2).value).to.equal(2)

expect(() => new ValidNumber(-3, -2, 2)).to.throw("Invalid number 'Number (-3) can not be smaller than -2'.")
expect(() => new ValidNumber(0, 1, 2)).to.throw("Invalid number 'Number (0) can not be smaller than 1'.")
expect(() => new ValidNumber(3, 1, 2)).to.throw("Invalid number 'Number (3) can not be bigger than 2'.")
})
})
12 changes: 11 additions & 1 deletion src/validObjects/ValidNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InvalidNumberError } from '@/errors/InvalidNumberError'
import { ValidObjectLogicError } from '@/errors/ValidObjectLogicError'

// tslint:disable-next-line:no-any
const validate = (val: any): number => {
Expand All @@ -16,8 +17,17 @@ export class ValidNumber {
private readonly val: number

// tslint:disable-next-line:no-any
constructor(val: any) {
constructor(val: any, rangeFromInclusive?: number, rangeToInclusive?: number) {
if (rangeFromInclusive !== undefined && rangeToInclusive !== undefined && rangeFromInclusive > rangeToInclusive) {
throw new ValidObjectLogicError(`Parameter rangeFromInclusive (${rangeFromInclusive}) can not be bigger then rangeToInclusive (${rangeToInclusive})`)
}
this.val = validate(val)
if (rangeFromInclusive && this.value < rangeFromInclusive) {
throw new InvalidNumberError(`Number (${this.value}) can not be smaller than ${rangeFromInclusive}`)
}
if (rangeToInclusive && this.value > rangeToInclusive) {
throw new InvalidNumberError(`Number (${this.value}) can not be bigger than ${rangeToInclusive}`)
}
}

get value(): number {
Expand Down

0 comments on commit 25f822d

Please sign in to comment.