Skip to content

Commit

Permalink
Merge pull request #1370 from postmanlabs/feature/add-is-event-helper
Browse files Browse the repository at this point in the history
Added `isEmpty` method on Script
  • Loading branch information
codenirvana authored Aug 1, 2024
2 parents 012946b + f9e415f commit df283d0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/collection/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
});

Expand Down
30 changes: 30 additions & 0 deletions test/unit/script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 - -
Expand Down

0 comments on commit df283d0

Please sign in to comment.