Просмотр исходного кода

chore: adds suffix striping for model keys

e22m4u 2 месяцев назад
Родитель
Сommit
4c72ec27fb

+ 2 - 0
dist/cjs/index.cjs

@@ -403,6 +403,8 @@ function modelNameToModelKey(modelName) {
       "The model name should be a non-empty String without spaces, but %v was given.",
       modelName
     );
+  if (modelName.toLowerCase() !== "model")
+    modelName = modelName.replace(/[-_]?Model$/, "").replace(/[-_](MODEL|model)$/, "");
   return modelName.toLowerCase().replace(/[-_]/g, "");
 }
 var init_model_name_to_model_key = __esm({

+ 4 - 0
src/utils/model-name-to-model-key.js

@@ -13,5 +13,9 @@ export function modelNameToModelKey(modelName) {
         'without spaces, but %v was given.',
       modelName,
     );
+  if (modelName.toLowerCase() !== 'model')
+    modelName = modelName
+      .replace(/[-_]?Model$/, '')
+      .replace(/[-_](MODEL|model)$/, '');
   return modelName.toLowerCase().replace(/[-_]/g, '');
 }

+ 25 - 0
src/utils/model-name-to-model-key.spec.js

@@ -12,6 +12,8 @@ describe('modelNameToModelKey', function () {
       'UserProfileDetails',
       'user-profile-details',
       'user_profile_details',
+      'User-Profile-Details',
+      'User_Profile_Details',
       'USER-PROFILE-DETAILS',
       'USER_PROFILE_DETAILS',
       'USERPROFILEDETAILS',
@@ -34,6 +36,29 @@ describe('modelNameToModelKey', function () {
     expect(modelNameToModelKey(modelName)).to.be.eq(expected);
   });
 
+  it('should remove the "model" word from a model name', function () {
+    const modelNames = [
+      'userProfileDetailsModel',
+      'UserProfileDetailsModel',
+      'user-profile-details-model',
+      'user_profile_details_model',
+      'User-Profile-Details-Model',
+      'User_Profile_Details_Model',
+      'USER-PROFILE-DETAILS-MODEL',
+      'USER_PROFILE_DETAILS_MODEL',
+    ];
+    modelNames.forEach(v =>
+      expect(modelNameToModelKey(v)).to.be.eq('userprofiledetails'),
+    );
+  });
+
+  it('should not remove the "model" suffix as a part of last word in a model name', function () {
+    const exceptions = ['SUPERMODEL', 'supermodel'];
+    exceptions.forEach(v =>
+      expect(modelNameToModelKey(v)).to.be.eq('supermodel'),
+    );
+  });
+
   it('should throw an error for an empty string', function () {
     const throwable = () => modelNameToModelKey('');
     expect(throwable).to.throw(