Skip to content

Commit

Permalink
refactor: cleanup ObjectUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilous committed Apr 29, 2024
1 parent ed052d5 commit 4f0ea45
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 128 deletions.
47 changes: 2 additions & 45 deletions apps/server/testSrc/classes/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import { FIELD_TYPE } from "@/variables";

export class Requester<IOType extends IO> {
private error?: NativeError;
private options: RequesterOptions = {
shouldFilterRequestData: true,
};
private event: SocketEvent<IOType>;
private options: RequesterOptions = {};
private requestData: IOType["input"];
private response: SocketResponse<IOType["output"]>;
private event: SocketEvent<IOType>;
private socket: Client;

constructor(socket: Client, event: SocketEvent<IOType>) {
Expand Down Expand Up @@ -76,42 +74,6 @@ export class Requester<IOType extends IO> {
return this;
}

// private handleFilterRequestData(options = this.getOptions()) {
// const inputFields = this.convertInputField(this.getInputFields());
// const requestData = this.getEmitData();
// this.checkRequestDataFields(options, inputFields);
// return this.filterEmitData(requestData, inputFields);
// }

// private convertInputField(inputFields: ValidationModel) {
// return Object.entries(inputFields).reduce((prevValue, currentValue) => {
// const [requiredFieldKey, requiredFieldProperties] = currentValue;
// prevValue[requiredFieldKey] = requiredFieldProperties.value;
// return prevValue;
// }, {} as StringMap);
// }
// private checkRequestDataFields(
// options = this.getOptions(),
// inputFields: StringMap
// ) {
// if (!this.getEmitData() && Object.keys(inputFields).length) {
// const error = {
// ...errorStore.find("INPUT_FIELDS_MISSING"),
// options,
// requestData: this.getEmitData(),
// };
// logger.dir("error", error, { depth: 10 });
// loggerHelper.logEndTestRequest();
// throw error;
// }
// }
// private filterEmitData(requestData: IOType["input"], inputFields: StringMap) {
// return objectUtils.excludePropsPeerToPeer(
// requestData,
// inputFields
// ) as StringMap;
// }

async emit() {
const response = (await new Promise((resolve, _reject) => {
// this.socket.connect();
Expand All @@ -135,11 +97,6 @@ export class Requester<IOType extends IO> {

if (data) this.setEmitData(data);

if (options.shouldFilterRequestData) {
// const filteredRequestData = this.handleFilterRequestData(finalOptions);
// this.setEmitData(finalOptions);
}

loggerHelper.logRequestDetails(
finalOptions,
this.getEmitData(),
Expand Down
4 changes: 1 addition & 3 deletions apps/server/testSrc/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export type AssertionInitializer<
options: Partial<AssertionInitializerOptions>
) => void;

export interface RequesterOptions {
shouldFilterRequestData: boolean;
}
export interface RequesterOptions {}

export type MiddlewareName = keyof typeof middlewares;
export type ServiceName = keyof typeof mergedServices;
Expand Down
80 changes: 0 additions & 80 deletions packages/classes/src/classes/ObjectUtils.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import { StringMap } from "@repo/type-store";

interface FilterFields {
[key: string]: any | FilterFields | FilterFields[];
}

class ObjectUtils {
constructor() {
this.excludeProps = this.excludeProps.bind(this);
this.excludePropsPeerToPeer = this.excludePropsPeerToPeer.bind(this);
}

excludePropsPeerToPeer<T>(data: StringMap, filterFields: FilterFields): T {
return Object.entries(filterFields).reduce(
(prevValue: StringMap, [kye, filterFieldValue]) => {
const objectFieldValue = data[kye];
if (typeof filterFieldValue === "object" && filterFieldValue !== null) {
prevValue[kye] = this.excludePropsPeerToPeer(
objectFieldValue,
filterFieldValue
);
return prevValue;
}

if (Array.isArray(filterFieldValue)) {
prevValue[kye] = objectFieldValue.map((v: StringMap) =>
this.excludePropsPeerToPeer(v, filterFieldValue[0] as any)
);
return prevValue;
}

prevValue[kye] = objectFieldValue;
return prevValue;
},
{}
) as T;
}

excludeProps(data: StringMap, props: string[]) {
Expand All @@ -59,36 +28,6 @@ class ObjectUtils {
}, {});
}

clarify(dirtyObject: StringMap) {
return Object.entries(dirtyObject).reduce(
(prevValue: StringMap, [key, value]) => {
if (typeof value !== "undefined") {
if (
typeof dirtyObject[key] === "object" &&
dirtyObject[key] !== null
) {
prevValue[key] = this.clarify(dirtyObject[key]);

return prevValue;
}

if (Array.isArray(dirtyObject[key])) {
prevValue[key] = dirtyObject[key].map((item: StringMap) =>
this.clarify(item)
);

return prevValue;
}

prevValue[key] = value;
}

return prevValue;
},
{}
);
}

rename<T extends StringMap>(object: T, oldKey: string, newKey: string) {
const { [oldKey as keyof T]: oldKeyValue, ...rest } = object;

Expand All @@ -113,22 +52,3 @@ class ObjectUtils {
const objectUtils = new ObjectUtils();

export { objectUtils, ObjectUtils };

// const assigner = (imports, path, value) => {
// const newPath = pathSplitter(path);

// const lastKeyIndex = newPath.length - 1;

// for (var i = 0; i < lastKeyIndex; ++i) {
// const key = newPath[i];
// if (!(key in imports)) {
// imports[key] = {};
// }
// imports = imports[key];
// }

// imports[newPath[lastKeyIndex]] = {
// ...imports[newPath[lastKeyIndex]],
// ...value,
// };
// };

0 comments on commit 4f0ea45

Please sign in to comment.