forked from MapComplete/MapComplete
Refactoring: change order of parameters and remove parameter for Conversion.ts
This commit is contained in:
parent
f9ba3126a4
commit
a0b7a32223
20 changed files with 147 additions and 213 deletions
|
|
@ -22,13 +22,11 @@ export interface ConversionMessage {
|
|||
}
|
||||
|
||||
export abstract class Conversion<TIn, TOut> {
|
||||
public readonly modifiedAttributes: string[]
|
||||
public readonly name: string
|
||||
protected readonly doc: string
|
||||
|
||||
constructor(doc: string, modifiedAttributes: string[] = [], name: string) {
|
||||
this.modifiedAttributes = modifiedAttributes
|
||||
this.doc = doc + "\n\nModified attributes are\n" + modifiedAttributes.join(", ")
|
||||
constructor(name: string, doc: string) {
|
||||
this.doc = doc
|
||||
this.name = name
|
||||
}
|
||||
|
||||
|
|
@ -76,7 +74,7 @@ export class Pipe<TIn, TInter, TOut> extends Conversion<TIn, TOut> {
|
|||
private readonly _failfast: boolean
|
||||
|
||||
constructor(step0: Conversion<TIn, TInter>, step1: Conversion<TInter, TOut>, failfast = false) {
|
||||
super("Merges two steps with different types", [], `Pipe(${step0.name}, ${step1.name})`)
|
||||
super(`Pipe(${step0.name}, ${step1.name})`, "Merges two steps with different types")
|
||||
this._step0 = step0
|
||||
this._step1 = step1
|
||||
this._failfast = failfast
|
||||
|
|
@ -95,11 +93,11 @@ export class Pure<TIn, TOut> extends Conversion<TIn, TOut> {
|
|||
private readonly _f: (t: TIn) => TOut
|
||||
|
||||
constructor(f: (t: TIn) => TOut) {
|
||||
super("Wrapper around a pure function", [], "Pure")
|
||||
super("Pure", "Wrapper around a pure function")
|
||||
this._f = f
|
||||
}
|
||||
|
||||
convert(json: TIn, context: ConversionContext): TOut {
|
||||
convert(json: TIn): TOut {
|
||||
return this._f(json)
|
||||
}
|
||||
}
|
||||
|
|
@ -109,7 +107,7 @@ export class Bypass<T> extends DesugaringStep<T> {
|
|||
private readonly _step: DesugaringStep<T>
|
||||
|
||||
constructor(applyIf: (t: T) => boolean, step: DesugaringStep<T>) {
|
||||
super("Applies the step on the object, if the object satisfies the predicate", [], "Bypass")
|
||||
super("Bypass", "Applies the step on the object, if the object satisfies the predicate")
|
||||
this._applyIf = applyIf
|
||||
this._step = step
|
||||
}
|
||||
|
|
@ -127,11 +125,7 @@ export class Each<X, Y> extends Conversion<X[], Y[]> {
|
|||
private readonly _msg: string
|
||||
|
||||
constructor(step: Conversion<X, Y>, options?: { msg?: string }) {
|
||||
super(
|
||||
"Applies the given step on every element of the list",
|
||||
[],
|
||||
"OnEach(" + step.name + ")"
|
||||
)
|
||||
super("OnEach(" + step.name + ")", "Applies the given step on every element of the list")
|
||||
this._step = step
|
||||
this._msg = options?.msg
|
||||
}
|
||||
|
|
@ -168,14 +162,13 @@ export class On<P, T> extends DesugaringStep<T> {
|
|||
|
||||
constructor(key: string, step: Conversion<P, P> | ((t: T) => Conversion<P, P>)) {
|
||||
super(
|
||||
`On(${key}, ${step.name})`,
|
||||
"Applies " + step.name + " onto property `" + key + "`",
|
||||
[key],
|
||||
`On(${key}, ${step.name})`
|
||||
)
|
||||
if (typeof step === "function") {
|
||||
this.step = step
|
||||
} else {
|
||||
this.step = (_) => step
|
||||
this.step = () => step
|
||||
}
|
||||
this.key = key
|
||||
}
|
||||
|
|
@ -196,10 +189,10 @@ export class On<P, T> extends DesugaringStep<T> {
|
|||
|
||||
export class Pass<T> extends Conversion<T, T> {
|
||||
constructor(message?: string) {
|
||||
super(message ?? "Does nothing, often to swap out steps in testing", [], "Pass")
|
||||
super("Pass", message ?? "Does nothing, often to swap out steps in testing")
|
||||
}
|
||||
|
||||
convert(json: T, _: ConversionContext): T {
|
||||
convert(json: T): T {
|
||||
return json
|
||||
}
|
||||
}
|
||||
|
|
@ -208,11 +201,7 @@ export class Concat<X, T> extends Conversion<X[], T[]> {
|
|||
private readonly _step: Conversion<X, T[]>
|
||||
|
||||
constructor(step: Conversion<X, T[]>) {
|
||||
super(
|
||||
"Executes the given step, flattens the resulting list",
|
||||
[],
|
||||
"Concat(" + step.name + ")"
|
||||
)
|
||||
super("Concat(" + step.name + ")", "Executes the given step, flattens the resulting list")
|
||||
this._step = step
|
||||
}
|
||||
|
||||
|
|
@ -230,11 +219,7 @@ export class FirstOf<T, X> extends Conversion<T, X> {
|
|||
private readonly _conversion: Conversion<T, X[]>
|
||||
|
||||
constructor(conversion: Conversion<T, X[]>) {
|
||||
super(
|
||||
"Picks the first result of the conversion step",
|
||||
[],
|
||||
"FirstOf(" + conversion.name + ")"
|
||||
)
|
||||
super("FirstOf(" + conversion.name + ")", "Picks the first result of the conversion step")
|
||||
this._conversion = conversion
|
||||
}
|
||||
|
||||
|
|
@ -252,7 +237,7 @@ export class Cached<TIn, TOut> extends Conversion<TIn, TOut> {
|
|||
private readonly key: string
|
||||
|
||||
constructor(step: Conversion<TIn, TOut>) {
|
||||
super("Secretly caches the output for the given input", [], "cached")
|
||||
super("cached", "Secretly caches the output for the given input")
|
||||
this._step = step
|
||||
this.key = "__super_secret_caching_key_" + step.name
|
||||
}
|
||||
|
|
@ -276,11 +261,10 @@ export class Fuse<T> extends DesugaringStep<T> {
|
|||
|
||||
constructor(doc: string, ...steps: DesugaringStep<T>[]) {
|
||||
super(
|
||||
"Fuse",
|
||||
(doc ?? "") +
|
||||
"This fused pipeline of the following steps: " +
|
||||
steps.map((s) => s.name).join(", "),
|
||||
Utils.Dedup([].concat(...steps.map((step) => step.modifiedAttributes))),
|
||||
"Fuse"
|
||||
)
|
||||
this.steps = Utils.NoNull(steps)
|
||||
}
|
||||
|
|
@ -318,19 +302,19 @@ export class Fuse<T> extends DesugaringStep<T> {
|
|||
}
|
||||
}
|
||||
|
||||
export class SetDefault<T> extends DesugaringStep<T> {
|
||||
private readonly value: any
|
||||
export class SetDefault<X, T extends object> extends DesugaringStep<T> {
|
||||
private readonly value: X
|
||||
private readonly key: string
|
||||
private readonly _overrideEmptyString: boolean
|
||||
|
||||
constructor(key: string, value: any, overrideEmptyString = false) {
|
||||
super("Sets " + key + " to a default value if undefined", [], "SetDefault of " + key)
|
||||
constructor(key: string, value: X, overrideEmptyString = false) {
|
||||
super("SetDefault of " + key, "Sets " + key + " to a default value if undefined")
|
||||
this.key = key
|
||||
this.value = value
|
||||
this._overrideEmptyString = overrideEmptyString
|
||||
}
|
||||
|
||||
convert(json: T, _: ConversionContext): T {
|
||||
convert(json: T): T {
|
||||
if (json === undefined) {
|
||||
return undefined
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue