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

[Improvement-16534][Master] switch task doesn't support includes method #16594

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ public class SwitchTaskUtils {

private static final NashornSandbox sandbox;
private static final String rgex = "['\"]*\\$\\{(.*?)\\}['\"]*";
public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES =
"if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }";

static {
sandbox = NashornSandboxes.create();
try {
sandbox.eval(NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES);
} catch (ScriptException e) {
log.error("failed to load Nashorn polyfill", e);
}
}

public static boolean evaluate(String expression) throws ScriptException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ public void testIllegalCondition() {
});

}

@Test
public void testIncludes() throws ScriptException {
String content = "['abc','def'].includes('abc')";
boolean result = SwitchTaskUtils.evaluate(content);
Assertions.assertTrue(result);
}

}
Loading