'Camel case matching in package.json

I need to write a task in package.json. It looks like this:

"nyc": {
  "all": true,
  "include": [
    "src/main/**/*.tsx" // here i need to match all camelCase file names
  ]
},

Here"src/main/**/*.tsx" i need to match all camelCase titles of files.
ex: correct "src/main/**/catBlack.tsx", "src/main/**/carBlack.tsx"
not correct "src/main/**/cat.tsx", "src/main/**/test-file.tsx"
Who can help?



Solution 1:[1]

You can create a function to check if a string is camelCase using regex

function isCamelCased(str){
  const rgx = new RegExp('[a-z]{1,}([A-Z][a-z]{1,}){1,}$');
  return rgx.test(str);
}

you may want to create another function to remove the path and file extension:

function getFileName(str){
  let file;
   // remove the path
  file = str.split('/');
  file = file[file.length -1 ];
  // remove the extention
  return file.split('.')[0];
}

Test:

const arr =  [
    '/main/camelCase.txt',
    '/main/CamelCase.txt',
    '/camel.txt',
    '/main/camel-case.txt',
    '/main/camel2Case.txt', 
    '/main/camelCase2.txt', 
    ''
]
let file;
arr.forEach((str) => {
  file = getFileName(str);
  console.log(`${file} => ${isCamelCased(file)}`);
});

output:

"camelCase => true"
"CamelCase => true"
"camel => false"
"camel-case => false"
"camel2Case => false"
"camelCase2 => false"
" '' => false"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdelaziz Alsabagh