question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimization of algorithms for complex patterns

See original GitHub issue

Environment

  • OS Version: Manjaro Linux x86_64
  • Node.js Version: v11.8.0

Actual behavior

(node:22393) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied

Steps to reproduce and Code sample

.
├── dir
│   ├── one
│   │   ├── a
│   │   │   └── 1.js
│   │   └── b
│   │       └── 2.js
│   └── two [error opening dir]
├── dir2
│   └── one
│       ├── a
│       │   └── 1.js
│       └── b
│           └── 2.js
├── fast-glob.js
├── package.json
└── package-lock.json
// fast-glob.js
const fg = require('fast-glob');
fg(['+(dir|dir2)/one/**/*']).then((entries) => console.log(entries));
$ node fast-glob.js
(node:22859) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, scandir '/home/keenwon/Test/fast-glob-test/dir/two'
(node:22859) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:22859) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

current user doesn’t have permission to access dir/two:

total 8.0K
drwxr-xr-x 4 keenwon keenwon 4.0K 2月  13 17:37 one
dr-x------ 2 root    root    4.0K 2月  13 17:54 two

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mrmlnccommented, Feb 9, 2020

Will be available with fast-glob@3.2.0 (#251).

1reaction
mrmlnccommented, Dec 22, 2019

Just another try to split pattern into segments. This works for most of the patterns from the micromatch tests (random selection).

'use strict';

const mm = require('../micromatch');
const { Minimatch } = require('minimatch');
const utils = require('../fast-glob/out/utils');

const PATTERN = 'root/+(dir1|dir2)/one/**/*';

const mn = new Minimatch(PATTERN);

console.dir(mn, { colors: true });

const buildSegments = (tokens) => {
  const segments = [];
  let segment = '';

  const state = {
    parens: 0
  };

  let index = 0;

  while (index < tokens.length) {
    const token = tokens[index];

    if (token.type === 'paren') {
      if (token.value === '(') {
        state.parens++;
      }
      if (token.value === ')') {
        state.parens--;
      }
    }

    if ((token.type === 'slash' && state.parens === 0)) {
      segments.push(segment);
      segment = '';
    } else {
      segment += token.value;
    }

    index++;
  }

  segments.push(segment);

  return segments;
};

const makeSegments = (pattern) => {
  const [result] = mm.parse(pattern);

  if (pattern === '') {
    return [];
  }

  // mm.parse returns [bos] for aa*.txt
  if (result.tokens.length === 1 && result.tokens[0].type === 'bos') {
    return [pattern];
  }

  return buildSegments(result.tokens);
};

const convertPatternsToSegments = (patterns) => patterns.map(makeSegments);

const prepare = (pattern) => {
  return mm.braceExpand(PATTERN);
};

const makeRe = (segments) => {
  return segments.map(segment => {
    return segment.map(part => utils.pattern.isDynamicPattern(part) ? mm.makeRe(part) : part);
  });
};

const patterns = prepare(PATTERN);
const segments = convertPatternsToSegments(patterns);
const final = makeRe(segments);

console.dir(final, { colors: true });

const parts = [
  [
    'root',
    /^(?:(?=.)(?:dir1|dir2)+)$/,
    'one',
    /^(?:(?!\.)(?:(?:(?!(?:^|[\\/])\.).)*?)[\\/]?)$/,
    /^(?:(?!\.)(?=.)[^\\/]*?[\\/]?)$/
  ]
];

const files = [
  'root/dir1/a/1.js', // +, +, -
  'root/dir1/b/2.js', // +, +, -
  'root/dir1/two', // +, +, -
  'root/dir2/one/a/1.js', // +, +, +, +, +
  'root/dir2/one/b/2.js',  // +, +, +, +, +
  'root/fast-glob.js', // +, -
  'root/package.json' // +, -
];
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Choose an Optimization Algorithm
One approach to grouping optimization algorithms is based on the amount of information available about the target function that is being ...
Read more >
Optimization of the Complex-RFM Optimization Algorithm
Abstract This paper presents and compares different modifications made to the Complex-. RF optimization algorithm with the aim of improving its performance ...
Read more >
Optimization of Complex Systems: Theory, Models, Algorithms ...
Pattern Recognition with Using Effective Algorithms and Methods ... world engineering optimization problems remain very complex in nature ...
Read more >
Optimizing optimization algorithms | MIT News
Optimizing optimization algorithms. Analysis shows how to get the best results when approximating solutions to complex engineering problems.
Read more >
Optimization Methods for Engineering Design - APMonitor
In summary, computer-based optimization refers to using computer algorithms to search the design space of a computer model. The design variables are adjusted ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found