2018-09-22 12:34:26 +02:00
|
|
|
import { Type } from "./Type"
|
|
|
|
|
|
|
|
export class ArrayType implements Type {
|
2019-03-26 06:34:40 +01:00
|
|
|
public kind = "array"
|
2018-09-22 12:34:26 +02:00
|
|
|
|
2019-05-31 13:25:51 +02:00
|
|
|
constructor(public elemType: Type, public isDotDotDot = false, public isQuestion = false) {}
|
2018-09-22 12:34:26 +02:00
|
|
|
|
2019-03-26 06:34:40 +01:00
|
|
|
public toConstructor() {
|
2019-05-31 13:25:51 +02:00
|
|
|
return `new ArrayType(${this.elemType.toConstructor()}, ${this.isDotDotDot}, ${this.isQuestion})`
|
2018-09-22 12:34:26 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 06:34:40 +01:00
|
|
|
public toString() {
|
2018-09-22 12:34:26 +02:00
|
|
|
return `${this.elemType.toString()}[]`
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:34:40 +01:00
|
|
|
public convert(argument) {
|
2018-09-22 12:34:26 +02:00
|
|
|
if (!Array.isArray(argument)) {
|
|
|
|
try {
|
|
|
|
argument = JSON.parse(argument)
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Can't convert ${argument} to array:`)
|
|
|
|
}
|
|
|
|
if (!Array.isArray(argument)) {
|
|
|
|
throw new Error(`Can't convert ${argument} to array:`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return argument.map(v => this.elemType.convert(v))
|
|
|
|
}
|
|
|
|
}
|