mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-01 21:13:09 +00:00
Feature/shared refactor (#1176)
* refactor: move most of the shared entities to @start8labs/shared library
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
export interface ConfigSpec {
|
||||
[key: string]: ValueSpec
|
||||
}
|
||||
export type ConfigSpec = Record<string, ValueSpec>
|
||||
|
||||
export type ValueType =
|
||||
| 'string'
|
||||
|
||||
@@ -6,7 +6,7 @@ export class Range {
|
||||
minInclusive: boolean
|
||||
maxInclusive: boolean
|
||||
|
||||
static from (s: string): Range {
|
||||
static from(s: string): Range {
|
||||
const r = new Range()
|
||||
r.minInclusive = s.startsWith('[')
|
||||
r.maxInclusive = s.endsWith(']')
|
||||
@@ -16,44 +16,38 @@ export class Range {
|
||||
return r
|
||||
}
|
||||
|
||||
checkIncludes (n: number) {
|
||||
checkIncludes(n: number) {
|
||||
if (
|
||||
this.hasMin() !== undefined &&
|
||||
(
|
||||
(this.min > n) ||
|
||||
(!this.minInclusive && this.min == n)
|
||||
)
|
||||
(this.min > n || (!this.minInclusive && this.min == n))
|
||||
) {
|
||||
throw new Error(this.minMessage())
|
||||
}
|
||||
if (
|
||||
this.hasMax() &&
|
||||
(
|
||||
(this.max < n) ||
|
||||
(!this.maxInclusive && this.max == n)
|
||||
)
|
||||
(this.max < n || (!this.maxInclusive && this.max == n))
|
||||
) {
|
||||
throw new Error(this.maxMessage())
|
||||
}
|
||||
}
|
||||
|
||||
hasMin (): boolean {
|
||||
hasMin(): boolean {
|
||||
return this.min !== undefined
|
||||
}
|
||||
|
||||
hasMax (): boolean {
|
||||
hasMax(): boolean {
|
||||
return this.max !== undefined
|
||||
}
|
||||
|
||||
minMessage (): string {
|
||||
minMessage(): string {
|
||||
return `greater than${this.minInclusive ? ' or equal to' : ''} ${this.min}`
|
||||
}
|
||||
|
||||
maxMessage (): string {
|
||||
maxMessage(): string {
|
||||
return `less than${this.maxInclusive ? ' or equal to' : ''} ${this.max}`
|
||||
}
|
||||
|
||||
description (): string {
|
||||
description(): string {
|
||||
let message = 'Value can be any number.'
|
||||
|
||||
if (this.hasMin() || this.hasMax()) {
|
||||
@@ -71,7 +65,7 @@ export class Range {
|
||||
return message
|
||||
}
|
||||
|
||||
integralMin (): number | undefined {
|
||||
integralMin(): number | undefined {
|
||||
if (this.min) {
|
||||
const ceil = Math.ceil(this.min)
|
||||
if (this.minInclusive) {
|
||||
@@ -86,7 +80,7 @@ export class Range {
|
||||
}
|
||||
}
|
||||
|
||||
integralMax (): number | undefined {
|
||||
integralMax(): number | undefined {
|
||||
if (this.max) {
|
||||
const floor = Math.floor(this.max)
|
||||
if (this.maxInclusive) {
|
||||
@@ -102,7 +96,7 @@ export class Range {
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultDescription (spec: ValueSpec): string {
|
||||
export function getDefaultDescription(spec: ValueSpec): string {
|
||||
let toReturn: string | undefined
|
||||
switch (spec.type) {
|
||||
case 'string':
|
||||
@@ -128,7 +122,7 @@ export function getDefaultDescription (spec: ValueSpec): string {
|
||||
return toReturn || ''
|
||||
}
|
||||
|
||||
export function getDefaultString (defaultSpec: DefaultString): string {
|
||||
export function getDefaultString(defaultSpec: DefaultString): string {
|
||||
if (typeof defaultSpec === 'string') {
|
||||
return defaultSpec
|
||||
} else {
|
||||
@@ -142,7 +136,7 @@ export function getDefaultString (defaultSpec: DefaultString): string {
|
||||
}
|
||||
|
||||
// a,g,h,A-Z,,,,-
|
||||
export function getRandomCharInSet (charset: string): string {
|
||||
export function getRandomCharInSet(charset: string): string {
|
||||
const set = stringToCharSet(charset)
|
||||
let charIdx = Math.floor(Math.random() * set.len)
|
||||
for (let range of set.ranges) {
|
||||
@@ -154,7 +148,7 @@ export function getRandomCharInSet (charset: string): string {
|
||||
throw new Error('unreachable')
|
||||
}
|
||||
|
||||
function stringToCharSet (charset: string): CharSet {
|
||||
function stringToCharSet(charset: string): CharSet {
|
||||
let set: CharSet = { ranges: [], len: 0 }
|
||||
let start: string | null = null
|
||||
let end: string | null = null
|
||||
@@ -238,4 +232,4 @@ interface CharSet {
|
||||
len: number
|
||||
}[]
|
||||
len: number
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user