Skip to content

Commit

Permalink
Merge branch 'version-path-object'
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Jun 6, 2015
2 parents e091dfd + 8ca1fe5 commit 9fac306
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@ module.exports.crop = function(image, ratio) {
/**
* Get new path with suffix
*
* @param string path- image path
* @param string suffix - path suffix
* @param string format - output format
* @param string path - image path
* @param string opts - output path transformations
* * format
* * suffix
*
* @return string path
*/
module.exports.path = function(path, suffix, format) {
module.exports.path = function(path, opts) {
var dir = dirname(path);
var ext = extname(path);
var base = basename(path, ext);

if (format) {
ext = '.' + format;
if (opts.format) {
ext = '.' + opts.format;
}

return join(dir, base + suffix + ext);
return join(dir, base + opts.suffix + ext);
};

/**
Expand Down Expand Up @@ -116,7 +117,11 @@ module.exports.cmdVersion = function(image, version, last) {
cmd.push(sprintf('-resize "%dx%d"', version.maxWidth, version.maxHeight));

// -write
version.path = module.exports.path(image.path, version.suffix, version.format);
version.path = module.exports.path(image.path, {
suffix: version.suffix || '',
format: version.format
});

if (last) {
cmd.push(version.path);
} else {
Expand Down
15 changes: 8 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ var resize = require('./index');

describe('resize.path()', function() {
it('returns new relative path with suffix', function() {
assert.equal(resize.path('./foo.jpg', '-bar'), 'foo-bar.jpg');
var path = resize.path('./foo.jpg', {suffix: '-bar'});
assert.equal(path, 'foo-bar.jpg');
});

it('returns new relative path with custom format', function() {
assert.equal(resize.path('./foo.jpg', '-bar', 'png'), 'foo-bar.png');
var path = resize.path('./foo.jpg', {suffix: '-bar', format: 'png'});
assert.equal(path, 'foo-bar.png');
});

it('returns new absolute path with suffix', function() {
assert.equal(resize.path('/foo/bar/baz.jpg', '-bix'), '/foo/bar/baz-bix.jpg');
var path = resize.path('/foo/bar/baz.jpg', {suffix: '-bix'});
assert.equal(path, '/foo/bar/baz-bix.jpg');
});

it('returns new absolute path with custom format', function() {
assert.equal(
resize.path('/foo/bar/baz.jpg', '-bix', 'png'),
'/foo/bar/baz-bix.png'
);
var path = resize.path('/foo/bar/baz.jpg', {suffix: '-bix', format: 'png'});
assert.equal(path, '/foo/bar/baz-bix.png');
});
});

Expand Down

0 comments on commit 9fac306

Please sign in to comment.