Skip to content

Commit

Permalink
feat: add isEmpty method on Script
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Aug 1, 2024
1 parent f6f29b0 commit f5a88a1
Show file tree
Hide file tree
Showing 4 changed files with 46 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-1369 Added `isEmpty` method in Script class

4.4.1:
date: 2024-07-29
Expand Down
11 changes: 11 additions & 0 deletions lib/collection/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ _.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 () {
var source = this.toSource();

return _.isEmpty(_.trim(source));
}
});

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 an 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 f5a88a1

Please sign in to comment.