-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clamp numeric values to reguired validation constraints or allowed range
- Loading branch information
Showing
9 changed files
with
90 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NumericRange, TypeDefinition } from '../types'; | ||
|
||
import { Value } from '../core/Value'; | ||
|
||
import { clamp } from '../utils'; | ||
|
||
export abstract class NumericValue extends Value<number> { | ||
public constructor(public readonly definition: Readonly<Pick<TypeDefinition, 'validation'>>) { | ||
super(); | ||
} | ||
|
||
public abstract get range(): NumericRange; | ||
|
||
public get validation(): NumericRange | undefined { | ||
const { validation } = this.definition; | ||
|
||
if (validation) { | ||
const match = validation.match(/min:(.*)\|max:(.*)/); | ||
if (match) { | ||
return { | ||
min: Number(match[1]), | ||
max: Number(match[2]), | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public clamp(value: number) { | ||
const { min, max } = this.validation || this.range; | ||
|
||
return clamp(value, min, max); | ||
} | ||
|
||
public setValue(value: number) { | ||
this._value = this.clamp(value); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
import { getNumericInputRange } from '../utils'; | ||
|
||
import { IntegerValue } from './IntegerValue'; | ||
|
||
export class UintValue extends IntegerValue {} | ||
export class UintValue extends IntegerValue { | ||
public get range() { | ||
return getNumericInputRange(this.size, true); | ||
} | ||
} |