Skip to content

Commit 411c0d3

Browse files
committed
change ArrayResult constructor signature
1 parent eb9503a commit 411c0d3

File tree

11 files changed

+46
-62
lines changed

11 files changed

+46
-62
lines changed

src/array_result.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,18 @@ export class TodoError extends ArrayResultError {
1919
}
2020
}
2121
export class ArrayResult<T> {
22-
readonly array: ReadonlyArray<T>;
23-
readonly errors: ReadonlyArray<ArrayResultError>;
24-
constructor(array?: ReadonlyArray<T> | ArrayResultError);
22+
constructor(array?: ReadonlyArray<T>);
2523
constructor(array: undefined, errors: ReadonlyArray<ArrayResultError>);
2624
constructor(
27-
array: ReadonlyArray<T> | ArrayResultError = [],
28-
errors: ReadonlyArray<ArrayResultError> = [],
29-
) {
30-
if (Array.isArray(array)) {
31-
this.array = array;
32-
} else {
33-
this.array = [];
34-
}
35-
if (this.array.length === 0) {
36-
if (array instanceof ArrayResultError) {
37-
this.errors = [array];
38-
} else {
39-
this.errors = errors;
40-
}
41-
} else {
42-
this.errors = [];
43-
}
44-
}
25+
public readonly array: ReadonlyArray<T> = [],
26+
public readonly errors: ReadonlyArray<ArrayResultError> = [],
27+
) {}
4528
static errors(errors: ReadonlyArray<ArrayResultError>): ArrayResult<never> {
4629
return new ArrayResult(undefined, errors);
4730
}
31+
static empty(): ArrayResult<never> {
32+
return ArrayResult.empty();
33+
}
4834
isError(): boolean {
4935
return this.array.length === 0;
5036
}
@@ -57,7 +43,7 @@ export class ArrayResult<T> {
5743
}
5844
filter(mapper: (value: T) => boolean): ArrayResult<T> {
5945
return this.flatMap((value) =>
60-
mapper(value) ? new ArrayResult([value]) : new ArrayResult()
46+
mapper(value) ? new ArrayResult([value]) : ArrayResult.empty()
6147
);
6248
}
6349
map<U>(mapper: (value: T) => U): ArrayResult<U> {
@@ -70,7 +56,7 @@ export class ArrayResult<T> {
7056
return this.array.reduce(
7157
(rest, value) =>
7258
ArrayResult.concat(rest, ArrayResult.from(() => mapper(value))),
73-
new ArrayResult<U>(),
59+
ArrayResult.empty() as ArrayResult<U>,
7460
);
7561
}
7662
}
@@ -91,7 +77,7 @@ export class ArrayResult<T> {
9177
}
9278
addErrorWhenNone(error: () => ArrayResultError): ArrayResult<T> {
9379
if (this.isError() && this.errors.length === 0) {
94-
return new ArrayResult(error());
80+
return ArrayResult.errors([error()]);
9581
} else {
9682
return this;
9783
}
@@ -104,7 +90,7 @@ export class ArrayResult<T> {
10490
left.isError() && right.isError()
10591
? ArrayResult.errors([...left.errors, ...right.errors])
10692
: new ArrayResult([...left.array, ...right.array]),
107-
new ArrayResult<T>(),
93+
ArrayResult.empty(),
10894
);
10995
}
11096
static combine<T extends ReadonlyArray<unknown>>(

src/parser/parser_lib.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ export class UnrecognizedError extends PositionedError {
117117
}
118118
}
119119
export function error(error: ArrayResultError): Parser<never> {
120-
return new Parser(() => new ArrayResult(error));
120+
return new Parser(() => ArrayResult.errors([error]));
121121
}
122-
export const empty = new Parser<never>(() => new ArrayResult());
122+
export const empty = new Parser<never>(() => ArrayResult.empty());
123123
export const nothing = new Parser(() =>
124124
new ArrayResult([{ value: null, length: 0 }])
125125
);
@@ -239,9 +239,9 @@ function generateError(position: number, expected: string) {
239239
length = token.length;
240240
}
241241
}
242-
return new ArrayResult<never>(
242+
return ArrayResult.errors([
243243
new UnexpectedError(unexpected, expected, { position, length }),
244-
);
244+
]);
245245
}
246246
export function matchCapture(
247247
regex: RegExp,
@@ -292,13 +292,13 @@ export const end = new Parser((position) =>
292292
export const notEnd = new Parser((position) =>
293293
position < currentSource.length
294294
? new ArrayResult([{ value: null, length: 0 }])
295-
: new ArrayResult(
295+
: ArrayResult.errors([
296296
new UnexpectedError(
297297
"end of text",
298298
"not end of text",
299299
{ position, length: currentSource.length - position },
300300
),
301-
)
301+
])
302302
);
303303
export function withSource<T>(
304304
parser: Parser<T>,

src/translator/adjective.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export function compoundAdjective(
6969
emphasis: false,
7070
}));
7171
} else {
72-
return new ArrayResult(
72+
return ArrayResult.errors([
7373
new UntranslatableError("reduplication", "compound adjective"),
74-
);
74+
]);
7575
}
7676
}
7777
export function rankAdjective(kind: Dictionary.AdjectiveType): number {

src/translator/clause.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ export function contextClause(
224224
emphasis: false,
225225
}]);
226226
case "anu":
227-
return new ArrayResult(
227+
return ArrayResult.errors([
228228
new TranslationTodoError(`${contextClause.type} context clause`),
229-
);
229+
]);
230230
default:
231231
return ArrayResult.concat<ReadonlyArray<English.Clause>>(
232232
new ArrayResult(nullableAsArray(unwrapSingleWord(contextClause)))

src/translator/modifier.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ export function defaultModifier(
6565
};
6666
});
6767
case "x ala x":
68-
return new ArrayResult(new TranslationTodoError("x ala x"));
68+
return ArrayResult.errors([new TranslationTodoError("x ala x")]);
6969
case "default":
7070
case "reduplication": {
7171
const reduplicationCount = getReduplicationCount(wordUnit);
7272
return new ArrayResult(dictionary.get(wordUnit.word)!.definitions)
73-
.flatMap((definition) => {
73+
.flatMap<ModifierTranslation>((definition) => {
7474
switch (definition.type) {
7575
case "noun":
7676
return noun({ definition, reduplicationCount, emphasis })
@@ -137,7 +137,7 @@ export function defaultModifier(
137137
}),
138138
}]);
139139
default:
140-
return new ArrayResult();
140+
return ArrayResult.empty();
141141
}
142142
});
143143
}
@@ -224,7 +224,7 @@ export function multipleModifiers(
224224
inPositionPhrase: inPositionPhrase[0] ?? null,
225225
}]);
226226
} else {
227-
adjectival = new ArrayResult();
227+
adjectival = ArrayResult.empty();
228228
}
229229
let adverbial: ArrayResult<MultipleModifierTranslation>;
230230
if (
@@ -255,7 +255,7 @@ export function multipleModifiers(
255255
inWayPhrase,
256256
}]);
257257
} else {
258-
adverbial = new ArrayResult();
258+
adverbial = ArrayResult.empty();
259259
}
260260
return ArrayResult.concat(adjectival, adverbial);
261261
})

src/translator/number.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ function combineNumbers(numbers: ReadonlyArray<number>) {
7272
ArrayResult.from(() => new ArrayResult([regularNumber(numbers)])),
7373
);
7474
} else {
75-
return new ArrayResult<never>(
75+
return ArrayResult.errors([
7676
new FilteredError('"ala" along with other numeral'),
77-
);
77+
]);
7878
}
7979
}
8080
export function number(number: ReadonlyArray<string>): ArrayResult<number> {

src/translator/phrase.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function nounPhrase(
100100
}));
101101
} else {
102102
// will be filled by ExhaustedError on `defaultPhrase`
103-
return new ArrayResult<never>();
103+
return ArrayResult.empty();
104104
}
105105
});
106106
}
@@ -202,7 +202,7 @@ function defaultPhrase(
202202
},
203203
}]);
204204
} else {
205-
return new ArrayResult();
205+
return ArrayResult.empty();
206206
}
207207
})
208208
.addErrorWhenNone(() => new ExhaustedError(Composer.phrase(phrase)));
@@ -249,12 +249,12 @@ export function phrase(
249249
verb: { ...verb, type: "simple" },
250250
}));
251251
} else {
252-
return new ArrayResult(
252+
return ArrayResult.errors([
253253
new UntranslatableError("preposition", "noun or adjective"),
254-
);
254+
]);
255255
}
256256
case "preverb":
257-
return new ArrayResult(new TranslationTodoError(phrase.type));
257+
return ArrayResult.errors([new TranslationTodoError(phrase.type)]);
258258
}
259259
}
260260
function compoundNoun(

src/translator/predicate.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ function associatedPredicate(
121121
} else if (object.type === "noun") {
122122
verbObject = predicateVerb(predicate, object.noun);
123123
} else {
124-
return new ArrayResult<never>(
125-
new UntranslatableError(object.type, "object"),
126-
);
124+
return ArrayResult.errors([new UntranslatableError(object.type, "object")]);
127125
}
128126
return verbObject.map((verbObject) => ({
129127
...verbObject,

src/translator/preposition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ function prepositionAsWord(
5252
): ArrayResult<English.Word> {
5353
switch (preposition.type) {
5454
case "x ala x":
55-
return new ArrayResult(
55+
return ArrayResult.errors([
5656
new TranslationTodoError("preposition ala preposition"),
57-
);
57+
]);
5858
case "default":
5959
case "reduplication":
6060
return new ArrayResult(

src/translator/sentence.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ function emphasisAsPunctuation(
6666
}
6767
function sentence(sentence: TokiPona.Sentence, isFinal: boolean) {
6868
if (sentence.interrogative === "x ala x") {
69-
return new ArrayResult<never>(new TranslationTodoError("x ala x"));
69+
return ArrayResult.errors([new TranslationTodoError("x ala x")]);
7070
}
7171
const punctuation = !isFinal && sentence.punctuation === ""
7272
? ","
7373
: sentence.punctuation;
7474
switch (sentence.type) {
7575
case "default": {
7676
if (sentence.startingParticle != null) {
77-
return new ArrayResult<never>(
77+
return ArrayResult.errors([
7878
new TranslationTodoError(
7979
`"${sentence.startingParticle.word}" starting particle`,
8080
),
81-
);
81+
]);
8282
}
8383
const useAnuSeme = nullableAsArray(sentence.anuSeme)
8484
.map((seme) =>
@@ -107,7 +107,7 @@ function sentence(sentence: TokiPona.Sentence, isFinal: boolean) {
107107
)
108108
)
109109
.map((interjection) => ({ type: "interjection", interjection }))
110-
: new ArrayResult();
110+
: ArrayResult.empty();
111111
const clauses = ArrayResult.combine(
112112
ArrayResult.combine(...sentence.contextClauses.map(contextClause))
113113
.map((clause) => clause.flat()),

0 commit comments

Comments
 (0)