Skip to content

Commit

Permalink
add isRegisteredFormat method (#462)
Browse files Browse the repository at this point in the history
Adding a check  if the format is already registered. It useful, if you are using the method  registerFormat() explicitly, to avoid the error "..format already registered", you can check if the format exists, if not you can register the new one.
  • Loading branch information
marioczpn authored Nov 19, 2020
1 parent e2b6fda commit 984fa99
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ Archiver ships with out of the box support for TAR and ZIP archives.

You can register additional formats with `registerFormat`.

You can check if format already exists before to register a new one with `isRegisteredFormat`.

_Formats will be changing in the future to implement a middleware approach._
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ vending.registerFormat = function(format, module) {
formats[format] = module;
};

/**
* Check if the format is already registered.
*
* @param {String} format the name of the format.
* @return boolean
*/
vending.isRegisteredFormat = function (format) {
if (formats[format]) {
return true;
}

return false;
};

vending.registerFormat('zip', require('./lib/plugins/zip'));
vending.registerFormat('tar', require('./lib/plugins/tar'));
vending.registerFormat('json', require('./lib/plugins/json'));
Expand Down
8 changes: 8 additions & 0 deletions test/archiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,13 @@ describe('archiver', function() {
archive.finalize()
});
});

describe('#isRegisteredFormat', function () {
var isRegisteredFormat = archiver.isRegisteredFormat('zip');
it('should return true when the value is present', function () {
assert.equal(true, isRegisteredFormat);
});
});

});
});

0 comments on commit 984fa99

Please sign in to comment.