Skip to content

Commit

Permalink
Remove need to avoid for..of loops. It's almost 2024.
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Dec 28, 2023
1 parent 7383afd commit 9e07655
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
8 changes: 1 addition & 7 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ module.exports = {

'no-restricted-syntax': [
'error',
{
selector: 'ForOfStatement',
message:
'Avoid for..of loops in libraries, because they generate unneeded Babel iterator runtime support code in the bundle',
},
{
selector: 'ForInStatement',
message:
'for..in loops are never what you want. Loop over Object.keys() instead.',
'for..in loops are never what you want. Use for..of, or use a .forEach() instead.',
},
],
},
Expand All @@ -52,7 +47,6 @@ module.exports = {
files: ['test/**'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'no-restricted-syntax': 'off',
},
},
],
Expand Down
24 changes: 12 additions & 12 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function lazyval<V>(value: (() => V) | V): V {
*/
export function subtract<C extends Scalar>(xs: Set<C>, ys: Set<C>): Set<C> {
const result = new Set<C>();
xs.forEach((x) => {
for (const x of xs) {
if (!ys.has(x)) {
result.add(x);
}
});
}
return result;
}

Expand Down Expand Up @@ -79,21 +79,21 @@ export function summarize(
if (ann.type === 'array') {
const items = ann.items;
let index = 0;
items.forEach((ann) => {
summarize(ann, [...keypath, index++]).forEach((item) =>
for (const ann of items) {
for (const item of summarize(ann, [...keypath, index++])) {
// Collect to results
result.push(item),
);
});
result.push(item);
}
}
} else if (ann.type === 'object') {
const fields = ann.fields;
Object.keys(fields).forEach((key) => {
for (const key of Object.keys(fields)) {
const value = fields[key];
summarize(value, [...keypath, key]).forEach((item) =>
for (const item of summarize(value, [...keypath, key])) {
// Collect to results
result.push(item),
);
});
result.push(item);
}
}
}

const text = ann.text;
Expand Down
4 changes: 2 additions & 2 deletions src/annotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ function annotateObject(
seen.add(obj);

const fields: Record<string, Annotation> = {};
Object.keys(obj).forEach((key) => {
for (const key of Object.keys(obj)) {
const value = obj[key];
fields[key] = annotate(value, undefined, seen);
});
}
return object(fields, text);
}

Expand Down
8 changes: 4 additions & 4 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
}

const result: string[] = [];
items.forEach((item) => {
for (const item of items) {
const [ser, ann] = serializeAnnotation(item, `${prefix}${INDENT}`);
result.push(`${prefix}${INDENT}${ser}${','}`);
if (ann !== undefined) {
result.push(indent(ann, `${prefix}${INDENT}`));
}
});
}
return ['[', ...result, `${prefix}]`].join('\n');
}

Expand All @@ -43,7 +43,7 @@ function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
}

const result: string[] = [];
fieldNames.forEach((key) => {
for (const key of fieldNames) {
const valueAnnotation = fields[key];
const kser = serializeValue(key);

Expand All @@ -54,7 +54,7 @@ function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
if (vann !== undefined) {
result.push(indent(vann, valPrefix));
}
});
}
return ['{', ...result, `${prefix}}`].join('\n');
}

Expand Down
14 changes: 7 additions & 7 deletions src/lib/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function object<DS extends Record<string, Decoder<any>>>(
const record = {};
let errors: Record<string, Annotation> | null = null;

Object.keys(decodersByKey).forEach((key) => {
for (const key of Object.keys(decodersByKey)) {
const decoder = decodersByKey[key];
const rawValue = plainObj[key];
const result: DecodeResult<unknown> = decoder.decode(rawValue);
Expand Down Expand Up @@ -78,7 +78,7 @@ export function object<DS extends Record<string, Decoder<any>>>(
errors[key] = ann;
}
}
});
}

// Deal with errors now. There are two classes of errors we want to
// report. First of all, we want to report any inline errors in this
Expand Down Expand Up @@ -162,12 +162,12 @@ export function inexact<O extends Record<string, Decoder<any>>>(
const safekeys = new Set(Object.keys(decodersByKey));

// To account for hard-coded keys that aren't part of the input
safekeys.forEach((k) => allkeys.add(k));
for (const k of safekeys) allkeys.add(k);

const rv = {} as {
[K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
} & Record<string, unknown>;
allkeys.forEach((k) => {
for (const k of allkeys) {
if (safekeys.has(k)) {
const value = safepart[k];
if (value !== undefined) {
Expand All @@ -178,7 +178,7 @@ export function inexact<O extends Record<string, Decoder<any>>>(
// @ts-expect-error - look into this later
rv[k] = plainObj[k];
}
});
}
return rv;
});
return decoder.decode(plainObj);
Expand All @@ -200,7 +200,7 @@ export function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>> {
let rv: Record<string, T> = {};
let errors: Record<string, Annotation> | null = null;

Object.keys(plainObj).forEach((key: string) => {
for (const key of Object.keys(plainObj)) {
const value = plainObj[key];
const result = decoder.decode(value);
if (result.ok) {
Expand All @@ -214,7 +214,7 @@ export function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>> {
}
errors[key] = result.error;
}
});
}

if (errors !== null) {
return err(merge(annotateObject(plainObj), errors));
Expand Down

0 comments on commit 9e07655

Please sign in to comment.