Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert extend util to TypeScript #2928

Merged
merged 18 commits into from
Nov 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow using file extension in core compat imports
Necessary for extend imports to have proper typings as we also have an unrelated extend/index.js file
  • Loading branch information
dsevillamartin committed Oct 30, 2021
commit e59c2172c430684253b15c5c9e3f6e00de97f7b9
11 changes: 9 additions & 2 deletions js/src/common/utils/proxifyCompat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
export default (compat: { [key: string]: any }, namespace: string) => {
// regex to replace common/ and NAMESPACE/ for core & core extensions
// and remove .js, .ts and .tsx extensions
// e.g. admin/utils/extract --> utils/extract
// e.g. tags/common/utils/sortTags --> tags/utils/sortTags
const regex = new RegExp(`(\\w+\\/)?(${namespace}|common)\\/`);
const regex = new RegExp(`^(?:\w+\/)?(?:${namespace}|common)\/(.+?)(?:\.(?:js|tsx?))?$`);

return new Proxy(compat, {
get: (obj, prop: string) => obj[prop] || obj[prop.replace(regex, '$1')],
get: (obj, prop: string) => {
if (obj[prop]) return obj[prop];

const out = regex.exec(prop);

return out && obj[out[1]];
},
});
};