-
Notifications
You must be signed in to change notification settings - Fork 193
TypeScript: Convert wp-utils package
#12247
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
Conversation
barklund
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great start, adding some pointer to getting the last bits over the line.
|
I have one last error, than I think we are good. |
swissspidy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should resolve the remaining type errors in snakeToCamelCase.ts:
/**
* Takes a string that is in snake case and returns the same
* term in camel case.
*
* @param string The key in snake case
* @return The key in camel case
*/
export function snakeToCamelCase(string = ''): string {
if (!string.includes('_') && !string.includes('-')) {
return string;
}
return string
.toLowerCase()
.replace(
/([a-z])([_|-][a-z])/g,
(_match, group1: string, group2: string) =>
group1 + group2.toUpperCase().replace('_', '').replace('-', '')
);
}
type ObjectOrPrimitive = SnakeOrCamelCaseObject | unknown;
interface SnakeOrCamelCaseObject {
[key: string]: ObjectOrPrimitive;
}
const isObject = (obj: unknown) =>
obj && 'object' === typeof obj && !Array.isArray(obj);
/**
* Transform a given object keys from snake case to camel case recursively.
*
* @param obj Object to be transformed.
* @param ignore Array of keys to be ignored for camelcase.
* @return Transformed object.
*/
export function snakeToCamelCaseObjectKeys(
obj: ObjectOrPrimitive,
ignore: string[] = []
): ObjectOrPrimitive {
if (!isObject(obj)) {
return obj;
}
return Object.fromEntries(
Object.entries(obj as SnakeOrCamelCaseObject).map(
([key, value]: [string, ObjectOrPrimitive]) => {
return [
snakeToCamelCase(key),
isObject(value) && !ignore.includes(key)
? snakeToCamelCaseObjectKeys(value)
: value,
] as [string, SnakeOrCamelCaseObject];
}
)
);
}|
Plugin builds for 20ed6d7 are ready 🛎️!
|
|
Size Change: -20 B (0%) Total Size: 2.68 MB
ℹ️ View Unchanged
|
|
There are some non-TypeScript lint issues to fix. |
Fixed. |
| "main": "./src/index.js", | ||
| "main": "./src/index.ts", | ||
| "types": "dist-types/index.d.ts", | ||
| "source": "src/index.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this shouldn't be needed for this package
Context
Summary
Convert wp-utils to typescript.
Relevant Technical Choices
To-do
User-facing changes
Testing Instructions
This PR can be tested by following these steps:
Reviews
Does this PR have a security-related impact?
Does this PR change what data or activity we track or use?
Does this PR have a legal-related impact?
Checklist
Type: XYZlabel to the PRFixes #12235