From 984fa99669e13e5db2e25c27b380679d0483635b Mon Sep 17 00:00:00 2001 From: Mario Ponciano <308156+marioczpn@users.noreply.github.com> Date: Wed, 18 Nov 2020 23:20:21 -0300 Subject: [PATCH] add isRegisteredFormat method (#462) 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. --- README.md | 2 ++ index.js | 14 ++++++++++++++ test/archiver.js | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index d8d99c81..4e56edc9 100644 --- a/README.md +++ b/README.md @@ -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._ diff --git a/index.js b/index.js index 324db8c6..0996daef 100644 --- a/index.js +++ b/index.js @@ -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')); diff --git a/test/archiver.js b/test/archiver.js index 4cadff34..f2780345 100644 --- a/test/archiver.js +++ b/test/archiver.js @@ -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); + }); + }); + }); });