Object Url Mapping In Javascript / Typescript / Angular4
I have a class for SearchFilter class SearchFilter { constructor(bucket: string, pin: number, qty: number, category: string) { }
Solution 1:
Adding a static factory method would work:
classSearchFilter {
constructor(
bucket: string,
pin: number,
qty: number,
category: string) {
}
publicstaticfromParams({ bucket, pin, qty, category }) {
// TODO Add validation to ensure pin & qty are integersreturnnewSearchFilter(
bucket,
parseInt(pin),
parseInt(qty),
category);
}
}
Then we can use it like this:
const demoParams = {
bucket: "MPC123",
category: "JOINT",
qty: "90",
pin: "9087"
};
const filter = SearchFilter.fromParams(demoParams);
Post a Comment for "Object Url Mapping In Javascript / Typescript / Angular4"