0

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...

David
  • 2,741
  • 16
  • 26
  • Your tests work fine. For example, just run `/\.css$/gi.test('src/public/app/cookieMsgs/cookieMsgs.js')` directly. The problem is `isProcessFile = !isProcessFile;`. You're toggling that value back and forth between `true` and `false`. – Mike Cluck Oct 06 '16 at 16:30
  • @MikeC, don't agree, there is a `break;` after toggling the `isProcessFile`. – David Oct 06 '16 at 16:32
  • Moreover, I've tried to change `console.log(opt, file, isProcessFile);` to `console.log(opt, file, opt.test(file), isProcessFile);` and then no file passes the test. Seems like passing twice the regex to same string it makes failure... – David Oct 06 '16 at 16:33
  • You're right. I missed that. Regardless, your checks are working as expected. You need to dig through and figure out why you're reporting the wrong result. You aren't going to get non-deterministic results from testing identical strings against regular expressions. – Mike Cluck Oct 06 '16 at 16:33
  • 1
    Do not use `g` modifier with a pattern used with `RegExp#test()`. – Wiktor Stribiżew Oct 06 '16 at 16:36
  • Thanks! changed the title – David Oct 06 '16 at 16:42
  • Do you want to say that removing `g` from regex in `RegExp#test()` does not help? – Wiktor Stribiżew Oct 06 '16 at 16:48
  • 1
    @WiktorStribiżew it worked removing `g` from regex – David Oct 06 '16 at 19:32

0 Answers0