Skip to content
Permalink
Browse files

Merge pull request #606 from insane-developer/master

Preserve file order on multi-suffix call of getBySuffix method (#605)
  • Loading branch information...
zxqfox committed Apr 3, 2018
2 parents df15d78 + 473e468 commit 87b27d7b9bfab518c159ab5c9f47bba21e291051
Showing with 17 additions and 9 deletions.
  1. +6 −7 lib/file-list.js
  2. +11 −2 test/lib/file-list.js
@@ -55,15 +55,14 @@ module.exports = inherit({
}
if (Array.isArray(suffix)) {
const res = [];
const suffixes = new Set(suffix);
this.slices.forEach(slice => {
suffix.forEach(s => {
for (let i = 0, l = slice.length; i < l; i++) {
const file = slice[i];
if (file.suffix === s) {
res.push(file);
}
for (let i = 0, l = slice.length; i < l; i++) {
const file = slice[i];
if (suffixes.has(file.suffix)) {
res.push(file);
}
});
}
});
return res;
} else {
@@ -23,7 +23,10 @@ describe('lib', () => {
];
files2 = [
{ fullname: '/foo/bar/file3.txt', name: 'file3', suffix: 'txt', mtime: 1437573848385 },
{ fullname: '/foo/bar/file4.html', name: 'file4', suffix: 'html', mtime: 1437573848385 }
{ fullname: '/foo/bar/file4.html', name: 'file4', suffix: 'html', mtime: 1437573848385 },
{ fullname: '/foo/bar/file5.css', name: 'file5', suffix: 'css', mtime: 1437573848385 },
{ fullname: '/foo/baz/file5.styl', name: 'file5', suffix: 'styl', mtime: 1437573848385 }

];
});

@@ -59,7 +62,7 @@ describe('lib', () => {
});

it('should add each file from files to items array', () => {
fileList.items.should.have.length(4);
fileList.items.should.have.length(6);
fileList.items.should.be.deep.equal(files1.concat(files2));
});

@@ -109,6 +112,12 @@ describe('lib', () => {
fileList.getBySuffix(['bemhtml.js']).should.be.instanceOf(Array).and.have.length(1);
fileList.getBySuffix(['bemhtml.js'])[0].should.be.deep.equal(advancedFiles[0]);
});

it('should preserve file order', () => {
fileList.addFiles(files2);
fileList.getBySuffix(['styl', 'css']).should.be.deep.equal(files2.slice(2));
fileList.getBySuffix(['css', 'styl']).should.be.deep.equal(files2.slice(2));
});
});

describe('suffix argument is string', () => {

0 comments on commit 87b27d7

Please sign in to comment.
You can’t perform that action at this time.