Skip to content

Commit

Permalink
refactor: avoid non-null assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
BohdanOne committed Dec 3, 2024
1 parent 2891d49 commit 3f8b66b
Showing 1 changed file with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ type TrackPushedGroupsArgs = {
changedSlices: ReadonlyArray<ChangedSlice>;
changedCustomTypes: ReadonlyArray<ChangedCustomType>;
};

type FieldsCount = {
[key in FieldType]?: number;
};
type FieldStats = {
isInStaticZone: boolean;
isInSlice: boolean;
} & {
[key in FieldType]?: number;
};
} & FieldsCount;

function trackPushedGroups(args: TrackPushedGroupsArgs) {
const { changedCustomTypes, changedSlices } = args;
Expand All @@ -122,18 +122,16 @@ function trackPushedGroups(args: TrackPushedGroupsArgs) {
customType.customType.tabs.forEach((tab) => {
tab.value.forEach((field) => {
if (field.value.type === "Group" && field.value.config?.fields) {
const fieldStats: FieldStats = {
const fieldsCount: FieldsCount = {};
field.value.config.fields.forEach(({ value: fieldValue }) => {
const value = fieldsCount[fieldValue.type] ?? 0;
fieldsCount[fieldValue.type] = value + 1;
});
acc.push({
isInStaticZone: true,
isInSlice: false,
};
field.value.config.fields.forEach((field) => {
if (typeof fieldStats[field.value.type] !== "number") {
fieldStats[field.value.type] = 1;
} else {
fieldStats[field.value.type]! += 1;
}
...fieldsCount,
});
acc.push(fieldStats);
}
});
});
Expand All @@ -149,18 +147,16 @@ function trackPushedGroups(args: TrackPushedGroupsArgs) {
slice.slice.model.variations.forEach((variation) => {
variation.primary?.forEach((field) => {
if (field.value.type === "Group" && field.value.config?.fields) {
const fieldStats: FieldStats = {
const fieldsCount: FieldsCount = {};
field.value.config.fields.forEach(({ value: fieldValue }) => {
const value = fieldsCount[fieldValue.type] ?? 0;
fieldsCount[fieldValue.type] = value + 1;
});
acc.push({
isInStaticZone: false,
isInSlice: true,
};
field.value.config.fields.forEach((field) => {
if (typeof fieldStats[field.value.type] !== "number") {
fieldStats[field.value.type] = 1;
} else {
fieldStats[field.value.type]! += 1;
}
...fieldsCount,
});
acc.push(fieldStats);
}
});
});
Expand Down

0 comments on commit 3f8b66b

Please sign in to comment.