Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isEmpty method on Script #1370

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading