Skip to content

Commit

Permalink
Adds option subPath
Browse files Browse the repository at this point in the history
  • Loading branch information
fkammer committed Jun 16, 2016
1 parent 2bf02a4 commit a5c26a7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 32 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -143,6 +143,27 @@ will create this structure:
└── bootstrap.min.css
```

#### options.subPath

Type: Number or Array of two Numbers [begin, end]

This options applies `Array.slice` to the array of path elements and allows you
to receive a subsequences of the path.

```js
gulp.src(['bower_components/**/*.css'])
.pipe(flatten({ subPath: [1, 1]} ))
.pipe(gulp.dest('build/'));
```
This as an example would flatten `top1/top2/bottom2/bottom1/file.txt` to `top2/file.txt`.

`[1, -1]` would flatten `top1/top2/bottom2/bottom1/file.txt` to `top2/bottom2/file.txt`.

Please refer to the [Array.slice docs](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) for a detailed description.

**!** If you're using both `options.includeParents` combined with `options.subPath`
please note that `options.includeParents` is applied first.

## License

MIT
78 changes: 47 additions & 31 deletions lib/flatten-path.js
Expand Up @@ -8,46 +8,62 @@ var path = require('path');
* @return {String}
*/
function flattenPath(file, opts) {
var topLevels;
var bottomLevels = 0;
var dirs;
var topPath = [];
var bottomPath = [];
var newPath = [];
var fileName = path.basename(file.path);
var dirs;

if (!opts.includeParents) {
return fileName;
}
var includeParents = function (dirs, opts) {
var topLevels;
var bottomLevels = 0;
var topPath = [];
var bottomPath = [];
var newPath = [];

opts = opts.includeParents;
if (Array.isArray(opts)) {
topLevels = Math.abs(opts[0]);
bottomLevels = Math.abs(opts[1]);
} else if (opts >= 0) {
topLevels = opts;
} else {
bottomLevels = Math.abs(opts);
}
if (Array.isArray(opts)) {
topLevels = Math.abs(opts[0]);
bottomLevels = Math.abs(opts[1]);
} else if (opts >= 0) {
topLevels = opts;
} else {
bottomLevels = Math.abs(opts);
}

dirs = path.dirname(file.relative).split(path.sep);
if (topLevels + bottomLevels > dirs.length) {
return file.relative;
if (topLevels + bottomLevels > dirs.length) {
return dirs;
}

while (topLevels > 0) {
topPath.push(dirs.shift());
topLevels--;
}
while (bottomLevels > 0) {
bottomPath.unshift(dirs.pop());
bottomLevels--;
}
return topPath.concat(bottomPath);
};

var subPath = function (dirs, opts) {
if (Array.isArray(opts)) {
return dirs.slice(opts[0], opts[1]);
} else {
return dirs.slice(opts);
}
};

if (!opts.includeParents && !opts.subPath) {
return fileName;
}

while (topLevels > 0) {
topPath.push(dirs.shift());
topLevels--;
dirs = path.dirname(file.relative).split(path.sep);
if (opts.includeParents) {
dirs = includeParents(dirs, opts.includeParents);
}
while (bottomLevels > 0) {
bottomPath.unshift(dirs.pop());
bottomLevels--;
if (opts.subPath) {
dirs = subPath(dirs, opts.subPath);
}
newPath = topPath.concat(bottomPath);
newPath.push(fileName);

return path.join.apply(path, newPath);
dirs.push(fileName);
return path.join.apply(path, dirs);
}


module.exports = flattenPath
25 changes: 24 additions & 1 deletion test/flatten-path.js
Expand Up @@ -13,7 +13,7 @@ describe('gulp-flatten', function () {
};
});

describe('flatten-path()', function () {
describe('includeParents', function () {
it('should keep top parent dirs from indludeParents option', function (done) {
var topOnly = flattenPath(fileInstance, {includeParents: 1});
topOnly.should.equal('top1/app.css');
Expand Down Expand Up @@ -49,4 +49,27 @@ describe('gulp-flatten', function () {
done();
});
});

describe('subPath', function () {
it('should keep top parent dirs from subPath option', function (done) {
var topOnly = flattenPath(fileInstance, {subPath: [0, 2]});
topOnly.should.equal('top1/top2/app.css');

done();
});

it('should keep bottom parent dirs from subPath option', function (done) {
var bottomOnly = flattenPath(fileInstance, {subPath: -2});
bottomOnly.should.equal('bottom2/bottom1/app.css');

done();
});

it('should keep top2 and bottom2 from subPath option', function (done) {
var middleOnly = flattenPath(fileInstance, {subPath: [1, -1]});
middleOnly.should.equal('top2/bottom2/app.css');

done();
});
});
});

0 comments on commit a5c26a7

Please sign in to comment.