Skip to content

Commit 8756e80

Browse files
committed
Shim String.includes
1 parent 208ed6e commit 8756e80

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

internal/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ createErrorType('ERR_INVALID_ARG_TYPE',
106106
// For cases like 'first argument'
107107
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
108108
} else {
109-
const type = name.includes('.') ? 'property' : 'argument';
109+
const type = includes(name, '.') ? 'property' : 'argument';
110110
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
111111
}
112112

test/common/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ function repeat(str, count) {
4242
return str;
4343
}
4444

45+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
46+
function includes(str, search, start) {
47+
if (typeof start !== 'number') {
48+
start = 0;
49+
}
50+
51+
if (start + search.length > str.length) {
52+
return false;
53+
} else {
54+
return str.indexOf(search, start) !== -1;
55+
}
56+
}
57+
4558
const isBrowser = typeof window !== 'undefined';
4659

4760
const bigIntSupported = typeof BigInt !== 'undefined';
@@ -273,6 +286,7 @@ process.on('unhandledRejection', crashOnUnhandledRejection);
273286
module.exports = {
274287
getOwnPropertyDescriptors,
275288
repeat,
289+
includes,
276290
isBrowser,
277291
bigIntSupported,
278292
symbolToStringTagSupported,

test/parallel/test-assert-async.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const invalidThenableFunc = () => {
7979
assert.strictEqual(err.message,
8080
'Missing expected rejection (mustNotCall).');
8181
assert.strictEqual(err.operator, 'rejects');
82-
assert.ok(!err.stack.includes('at Function.rejects'));
82+
assert.ok(!common.includes(err.stack, 'at Function.rejects'));
8383
return true;
8484
};
8585

@@ -170,7 +170,7 @@ promises.push(assert.rejects(
170170
'Got unwanted rejection.\nActual message: "Failed"');
171171
assert.strictEqual(err.operator, 'doesNotReject');
172172
assert.ok(err.stack);
173-
assert.ok(!err.stack.includes('at Function.doesNotReject'));
173+
assert.ok(!common.includes(err.stack, 'at Function.doesNotReject'));
174174
return true;
175175
};
176176

test/parallel/test-assert.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ assert.throws(() => thrower(TypeError));
137137
} catch (e) {
138138
threw = true;
139139
assert.ok(e instanceof a.AssertionError);
140-
assert.ok(!e.stack.includes('at Function.doesNotThrow'));
140+
assert.ok(!common.includes(e.stack, 'at Function.doesNotThrow'));
141141
}
142142
assert.ok(threw, 'a.doesNotThrow is not catching type matching errors');
143143
}
@@ -251,7 +251,7 @@ a.throws(() => thrower(TypeError), (err) => {
251251
} catch (e) {
252252
threw = true;
253253
assert.ok(e instanceof a.AssertionError);
254-
assert.ok(!e.stack.includes('at Function.throws'));
254+
assert.ok(!common.includes(e.stack, 'at Function.throws'));
255255
}
256256
assert.ok(threw);
257257
}
@@ -341,13 +341,13 @@ try {
341341
}, TypeError, rangeError);
342342
} catch (e) {
343343
threw = true;
344-
assert.ok(e.message.includes(rangeError.message));
344+
assert.ok(common.includes(e.message, rangeError.message));
345345
assert.ok(e instanceof assert.AssertionError);
346346
// [browserify]
347347
// This fails because `doesNotThrow` appears in the stack trace.
348348
// I'm not quite sure why that's an issue if the error message is set
349349
// and the above tests pass so commenting out for now.
350-
// assert.ok(!e.stack.includes('doesNotThrow'), e.stack);
350+
// assert.ok(!common.includes(e.stack, 'doesNotThrow'), e.stack);
351351
}
352352
assert.ok(threw);
353353
}

test/pseudo-tty/test-assert-position-indicator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// https://github.com/nodejs/node/commit/40a8a7391664e7a5d8a264a1d85d059f9c05063b
33

44
'use strict';
5-
require('../common');
5+
const common = require('../common');
66
const assert = require('../../assert');
77

88
process.env.NODE_DISABLE_COLORS = true;
@@ -15,11 +15,11 @@ process.stderr.columns = 20;
1515
// Confirm that there is no position indicator.
1616
assert.throws(
1717
() => { assert.deepStrictEqual('a'.repeat(30), 'a'.repeat(31)); },
18-
(err) => !err.message.includes('^')
18+
(err) => !common.includes(err.message, '^')
1919
);
2020

2121
// Confirm that there is a position indicator.
2222
assert.throws(
2323
() => { assert.deepStrictEqual('aaa', 'aaaa'); },
24-
(err) => err.message.includes('^')
24+
(err) => common.includes(err.message, '^')
2525
);

0 commit comments

Comments
 (0)