Skip to content

Commit df283d0

Browse files
authored
Merge pull request #1370 from postmanlabs/feature/add-is-event-helper
Added `isEmpty` method on Script
2 parents 012946b + f9e415f commit df283d0

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

CHANGELOG.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased:
22
new features:
33
- GH-1369 Added `isEvent` method in Event class
4+
- GH-1370 Added `isEmpty` method in Script class
45

56
4.4.1:
67
date: 2024-07-29

lib/collection/script.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
9494
this.exec = _.isString(options.exec) ? options.exec.split(SCRIPT_NEWLINE_PATTERN) :
9595
_.isArray(options.exec) ? options.exec : undefined;
9696
}
97+
},
98+
99+
/**
100+
* Checks if the script is empty i.e does not have any code to execute.
101+
*
102+
* @returns {Boolean}
103+
*/
104+
isEmpty: function () {
105+
return _.isEmpty(_.trim(this.toSource()));
97106
}
98107
});
99108

test/unit/script.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,36 @@ describe('Script', function () {
195195
});
196196
});
197197

198+
describe('.isEmpty', function () {
199+
it('should return true for an empty script', function () {
200+
var script = new Script();
201+
202+
expect(script.isEmpty()).to.be.true;
203+
});
204+
205+
it('should return true for a script with no exec', function () {
206+
var script = new Script({ id: '123' });
207+
208+
expect(script.isEmpty()).to.be.true;
209+
});
210+
211+
it('should return true for a script with an empty exec', function () {
212+
var script1 = new Script({ exec: '' }),
213+
script2 = new Script({ exec: ['', ''] });
214+
215+
expect(script1.isEmpty()).to.be.true;
216+
expect(script2.isEmpty()).to.be.true;
217+
});
218+
219+
it('should return false for a script with exec', function () {
220+
var script1 = new Script({ exec: 'console.log("Hello, World!");' }),
221+
script2 = new Script({ exec: ['console.log("Hello, World!");'] });
222+
223+
expect(script1.isEmpty()).to.be.false;
224+
expect(script2.isEmpty()).to.be.false;
225+
});
226+
});
227+
198228
describe('json representation', function () {
199229
it('must match what the script was initialized with', function () {
200230
var jsonified = script.toJSON();

types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,10 @@ declare module "postman-collection" {
22012201
packages: Packages;
22022202
src: Url;
22032203
exec: string[];
2204+
/**
2205+
* Checks if the script is empty i.e does not have any code to execute.
2206+
*/
2207+
isEmpty(): boolean;
22042208
/**
22052209
* Check whether an object is an instance of ItemGroup.
22062210
* @param obj - -

0 commit comments

Comments
 (0)