discourse/app/assets/javascripts/discourse-common/addon/lib/case-converter.js
Martin Brennan 964945a11f
DEV: Add case-converter common lib and use with AdminPlugin (#27725)
The AdminPlugin JS model uses a similar pattern to chat models,
where it is a plain JS class manually converting provided
snake_case attributes from the serializer to JS camelCase.

However this doesn't work when it comes to using `add_to_serializer`
in plugins since core does not know about these new attributes.

Instead, we can use a JS function to convert snake_case to camelCase
and use that when initializing AdminPlugin. This commit also moves
similar functions to a new case-converter.js file in
discourse-common/lib.
2024-07-05 13:22:48 +10:00

18 lines
659 B
JavaScript

// Converts snake_case to camelCase, useful for converting
// rails serializer attributes to JS object keys.
export function snakeCaseToCamelCase(str) {
return str.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase());
}
// Converts camelCase to dash-case, useful for converting
// JS object keys to HTML attributes.
export function camelCaseToDash(str) {
return str.replace(/([a-zA-Z])(?=[A-Z])/g, "$1-").toLowerCase();
}
// Converts camelCase to snake_case, useful for converting
// JS object keys to Rails serializer attributes.
export function camelCaseToSnakeCase(str) {
return str.replace(/([a-zA-Z])(?=[A-Z])/g, "$1_").toLowerCase();
}