JSFiddle - React, Tailwind, and code Playground
CSS
html, body {
background-color: #fff;
}
JavaScript
function print (string) {
document.write(string+'<br>')
}
function test(){
var files = [
'2017-08-31 18_57_42-shouldwork.png',
'2017-08-31 18_57_43-shouldwork.png',
'2017-08-31 18_57_42-shouldwork - Kopie.png',
'2017-08-31 18_57_42-shouldwork2.png',
'2019-03-09 11_11_09 - shouldwork - 000003.png',
'2019-03-09 11_11_10 - shouldwork - 000003.png',
'should fail.png'
];
const RG = {
date: '(?<date>\\d{4}-\\d{2}-\\d{2})',
time: '(?<time>\\d{2}_\\d{2}_\\d{2})',
window: '(?<window>.*?)',
index: '(?<index>\\d{6})',
extension: '(?<extension>\\.(png|jpg|jpeg))',
dash: '(-| - )'
}
const EXTENSION_MATCH = new RegExp(`${RG.extension}$`);
const ALLOWED_PATTERNS = [
new RegExp(`${RG.date} ${RG.time}${RG.dash}${RG.window}${RG.dash}${RG.index}${RG.extension}`, 'g'),
new RegExp(`${RG.date} ${RG.time}${RG.dash}${RG.window}${RG.extension}`, 'g'),
];
function extractExtension (fileName) {
return EXTENSION_MATCH.exec(fileName).groups.extension || NULL;
}
var titleCounter = {};
var allFiles = [];
function processFile (fileName) {
print('------------------------------------------------');
print(' file: '+ fileName);
var extension = extractExtension(fileName);
var matchedGroups = false;
var allowed = ALLOWED_PATTERNS.some(ap=>{
matchedGroups = ap.exec(fileName) || false;
print('match: ' + (!!matchedGroups ? 'TRUE ' : 'FALSE ') + ap);
(matchedGroups) && (matchedGroups = matchedGroups.groups);
return matchedGroups;
});
if (allowed) {
titleCounter[matchedGroups.window] = titleCounter[matchedGroups.window] || 0;
titleCounter[matchedGroups.window]++;
allFiles.push({
file: fileName,
group: matchedGroups
});
}
}
files.forEach(fileName=>processFile(fileName));
}
document.write('<code>');
test();
document.write('</code><style>body{background-color: #fff}</style>');