diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 192ea1383..603b3fb58 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,6 +1,7 @@ unreleased: new features: - GH-1369 Added `isEvent` method in Event class + - GH-1370 Added `isEmpty` method in Script class 4.4.1: date: 2024-07-29 diff --git a/lib/collection/script.js b/lib/collection/script.js index 45d6e1871..0f24af864 100644 --- a/lib/collection/script.js +++ b/lib/collection/script.js @@ -94,6 +94,15 @@ _.assign(Script.prototype, /** @lends Script.prototype */ { this.exec = _.isString(options.exec) ? options.exec.split(SCRIPT_NEWLINE_PATTERN) : _.isArray(options.exec) ? options.exec : undefined; } + }, + + /** + * Checks if the script is empty i.e does not have any code to execute. + * + * @returns {Boolean} + */ + isEmpty: function () { + return _.isEmpty(_.trim(this.toSource())); } }); diff --git a/test/unit/script.test.js b/test/unit/script.test.js index 478bd137d..622176e67 100644 --- a/test/unit/script.test.js +++ b/test/unit/script.test.js @@ -195,6 +195,36 @@ describe('Script', function () { }); }); + describe('.isEmpty', function () { + it('should return true for an empty script', function () { + var script = new Script(); + + expect(script.isEmpty()).to.be.true; + }); + + it('should return true for a script with no exec', function () { + var script = new Script({ id: '123' }); + + expect(script.isEmpty()).to.be.true; + }); + + it('should return true for a script with an empty exec', function () { + var script1 = new Script({ exec: '' }), + script2 = new Script({ exec: ['', ''] }); + + expect(script1.isEmpty()).to.be.true; + expect(script2.isEmpty()).to.be.true; + }); + + it('should return false for a script with exec', function () { + var script1 = new Script({ exec: 'console.log("Hello, World!");' }), + script2 = new Script({ exec: ['console.log("Hello, World!");'] }); + + expect(script1.isEmpty()).to.be.false; + expect(script2.isEmpty()).to.be.false; + }); + }); + describe('json representation', function () { it('must match what the script was initialized with', function () { var jsonified = script.toJSON(); diff --git a/types/index.d.ts b/types/index.d.ts index 470b3592c..e65b47eeb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2201,6 +2201,10 @@ declare module "postman-collection" { packages: Packages; src: Url; exec: string[]; + /** + * Checks if the script is empty i.e does not have any code to execute. + */ + isEmpty(): boolean; /** * Check whether an object is an instance of ItemGroup. * @param obj - -