Compare commits

...

1 Commits

Author SHA1 Message Date
Julien Fontanet
4c7c9f9156 WiP: feat(fs/glob): basic glob implementation 2019-10-06 23:15:04 +02:00
3 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import escapeRegExp from 'lodash/escapeRegExp'
const compileFragment = pattern =>
new RegExp(
`^${pattern
.split('*')
.map(escapeRegExp)
.join('[^]*')}$`
)
export function parseGlob(pattern) {
const parts = []
while (pattern.length !== 0) {
const i = pattern.indexOf('*')
if (i === -1) {
parts.push(pattern)
break
}
let fragmentStart = pattern.lastIndexOf('/', i)
if (fragmentStart === -1) {
fragmentStart = 0
} else {
parts.push(pattern.slice(0, fragmentStart))
++fragmentStart
}
let fragmentEnd = pattern.indexOf('/', i)
if (fragmentEnd === -1) {
fragmentEnd = pattern.length
}
parts.push(compileFragment(pattern.slice(fragmentStart, fragmentEnd)))
pattern = pattern.slice(fragmentEnd + 1)
}
return parts
}

View File

@@ -0,0 +1,12 @@
/* eslint-env jest */
import { parseGlob } from './_parseGlob'
describe('parseGlob', () => {
it.each([['foo/*/bar*baz/qux', ['foo', /^[^]*$/, /^bar[^]*baz$/, 'qux']]])(
'parse %j correctly',
(pattern, result) => {
expect(parseGlob(pattern)).toEqual(result)
}
)
})

View File

@@ -14,6 +14,7 @@ import { type Readable, type Writable } from 'stream'
import normalizePath from './_normalizePath'
import { createChecksumStream, validChecksumOfReadStream } from './checksum'
import { parseGlob } from './_parseGlob'
const { dirname } = path.posix
@@ -258,6 +259,12 @@ export default class RemoteHandlerAbstract {
)
}
// basic glob support, only `*` is supported
async glob(pattern) {
const parts = parseGlob(pattern)
// TODO
}
async list(
dir: string,
{