I'm having this code:
console.log(/\.js$/gi.test('src/public/app/cookieMsgs/cookieMsgs.js'));
const pathReplaces = {};
function getPathReplace(path) {
if (!pathReplaces[path]) {
pathReplaces[path] = new RegExp(`^${path}\/?`, 'gi');
}
return pathReplaces[path];
}
function copyFiles(files, path, dest, opts = {}) {
const pathReplace = getPathReplace(path);
const isProcessFileCfg = (opts.exclude !== undefined) || (opts.include === undefined) || (!opts.exclude && !opts.include);
const popts = opts.exclude || opts.include || [];
let fileParts;
let fileName;
let isProcessFile;
for (let file of files) {
try {
console.log(file);
isProcessFile = isProcessFileCfg;
for (let opt of popts) {
console.log(opt, file, isProcessFile);
if (opt.test(file)) {
isProcessFile = !isProcessFile;
console.log('Test result:', isProcessFile);
break;
}
}
if (isProcessFile) {
fileParts = file.replace(pathReplace, '').split('/');
fileName = fileParts.pop();
}
} catch (err) {
console.log(err);
}
}
}
function test() {
copyFiles([
'src/public/app/app.js',
'src/public/app/checkout/directCard.js',
'src/public/app/cookieMsgs/cookieMsgs.js',
], 'src/public', 'build', { exclude: [/\.js$/gi, /\.css$/gi, /\.html$/gi]});
}
test();
And the output is really strange, you can see test in here or copy paste the code in babeljs.io:
true
src/public/app/app.js
/\.js$/gi src/public/app/app.js true
Test result: false
src/public/app/checkout/directCard.js
/\.js$/gi src/public/app/checkout/directCard.js true
Test result: false
src/public/app/cookieMsgs/cookieMsgs.js
/\.js$/gi src/public/app/cookieMsgs/cookieMsgs.js true
/\.css$/gi src/public/app/cookieMsgs/cookieMsgs.js true
/\.html$/gi src/public/app/cookieMsgs/cookieMsgs.js true
What is going on? I can't understand why /\.js$/gi.test('src/public/app/checkout/cookieMsgs.js')
passes the test, but when being done inside for (.. of ...)
it does not pass the test...