Closed zongqichen closed 1 month ago
I think that the code:
function _getPackageID(namespace, packageIds, apiOrEvent) {
if (packageIds instanceof Set) {
const packageArray = Array.from(packageIds);
if (apiOrEvent === "api") {
const apiPackage = packageArray.find((pkg) => pkg.includes("-api"));
if (apiPackage) return apiPackage;
} else if (apiOrEvent === "event") {
const eventPackage = packageArray.find((pkg) => pkg.includes("-event"));
if (eventPackage) return eventPackage;
}
return packageArray.find(pkg => pkg.includes(namespace));
}
}
can be written this way:
function _getPackageID(namespace, packageIds, apiOrEvent) {
return packageIds.find((pkg) => pkg.includes("-" + apiOrEvent)) || packageIds.find((pkg) => pkg.includes(namespace));
}
where I think that instead of apiOrEvent
we should name this input parameter like resourceType
I think that the code:
function _getPackageID(namespace, packageIds, apiOrEvent) { if (packageIds instanceof Set) { const packageArray = Array.from(packageIds); if (apiOrEvent === "api") { const apiPackage = packageArray.find((pkg) => pkg.includes("-api")); if (apiPackage) return apiPackage; } else if (apiOrEvent === "event") { const eventPackage = packageArray.find((pkg) => pkg.includes("-event")); if (eventPackage) return eventPackage; } return packageArray.find(pkg => pkg.includes(namespace)); } }
can be written this way:
function _getPackageID(namespace, packageIds, apiOrEvent) { return packageIds.find((pkg) => pkg.includes("-" + apiOrEvent)) || packageIds.find((pkg) => pkg.includes(namespace)); }
where I think that instead of
apiOrEvent
we should name this input parameter likeresourceType
I create an issue regarding to this refactor: https://github.com/cap-js/ord/issues/73
Putting two cents:
enums vs objects in javascript
You can find on many places that javascript developers prefer using Object.freeze instead of enum (or
as const
casting).Enum can be defined as:
Please pay attention on Object.freeze. It serves to disallow changing of the object which at the end causes that RESOURCE_TYPE acts as an enum.