diff --git a/packages/ipfs-unixfs-importer/src/utils/to-path-components.js b/packages/ipfs-unixfs-importer/src/utils/to-path-components.js index 085a53b6..26835e7a 100644 --- a/packages/ipfs-unixfs-importer/src/utils/to-path-components.js +++ b/packages/ipfs-unixfs-importer/src/utils/to-path-components.js @@ -2,7 +2,7 @@ const toPathComponents = (path = '') => { // split on / unless escaped with \ return (path .trim() - .match(/([^\\^/]|\\\/)+/g) || []) + .match(/([^\\/]|\\\/)+/g) || []) .filter(Boolean) } diff --git a/packages/ipfs-unixfs-importer/test/utils.spec.js b/packages/ipfs-unixfs-importer/test/utils.spec.js new file mode 100644 index 00000000..3943bae6 --- /dev/null +++ b/packages/ipfs-unixfs-importer/test/utils.spec.js @@ -0,0 +1,25 @@ +/* eslint-env mocha */ + +import { expect } from 'aegir/utils/chai.js' +import toPathComponents from '../src/utils/to-path-components.js' + +describe('toPathComponents', () => { + it('splits on unescaped "/" characters', () => { + const path = 'foo/bar/baz' + const components = toPathComponents(path) + expect(components.length).to.eq(3) + }) + + it('does not split on escaped "/" characters', () => { + const path = 'foo\\/bar/baz' + const components = toPathComponents(path) + expect(components.length).to.eq(2) + }) + + // see https://github.com/ipfs/js-ipfs-unixfs/issues/177 for context + it('does not split on "^" characters', () => { + const path = 'foo/bar^baz^^qux' + const components = toPathComponents(path) + expect(components.length).to.eq(2) + }) +})