Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #606 from insane-developer/master
Browse files Browse the repository at this point in the history
Preserve file order on multi-suffix call of getBySuffix method (#605)
  • Loading branch information
qfox committed Apr 3, 2018
2 parents df15d78 + 473e468 commit 87b27d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
13 changes: 6 additions & 7 deletions lib/file-list.js
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions test/lib/file-list.js
Expand Up @@ -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 }

];
});

Expand Down Expand Up @@ -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));
});

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 87b27d7

Please sign in to comment.