Skip to content

Commit

Permalink
Fixing string shims (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
rorticus committed Aug 30, 2018
1 parent dcbb8e0 commit ff79ad9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/shim/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,14 @@ if (has('es6-string') && has('es6-string-raw')) {
};

endsWith = function endsWith(text: string, search: string, endPosition?: number): boolean {
if (endPosition == null) {
if (search === '') {
return true;
}

if (typeof endPosition === 'undefined') {
endPosition = text.length;
} else if (endPosition === null || isNaN(endPosition)) {
return false;
}

[text, search, endPosition] = normalizeSubstringArgs('endsWith', text, search, endPosition, true);
Expand Down
2 changes: 1 addition & 1 deletion src/shim/support/has.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ add(
let callSite = getCallSite`a\n${b}`;

(callSite as any).raw = ['a\\n'];
const supportsTrunc = global.String.raw(callSite, 42) === 'a:\\n';
const supportsTrunc = global.String.raw(callSite, 42) === 'a\\n';

return supportsTrunc;
}
Expand Down
41 changes: 31 additions & 10 deletions tests/shim/unit/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,37 @@ registerSuite('shim - string functions', {
},

'position is Infinity, not included, or NaN'() {
const counts = [Infinity, undefined as any, null, NaN];
for (let count of counts) {
assert.isTrue(stringUtil.endsWith('abc', '', count));
assert.isFalse(stringUtil.endsWith('abc', '\0', count));
assert.isTrue(stringUtil.endsWith('abc', 'c', count));
assert.isFalse(stringUtil.endsWith('abc', 'b', count));
assert.isTrue(stringUtil.endsWith('abc', 'bc', count));
assert.isTrue(stringUtil.endsWith('abc', 'abc', count));
assert.isFalse(stringUtil.endsWith('abc', 'abcd', count));
}
assert.isTrue(stringUtil.endsWith('abc', '', Infinity));
assert.isFalse(stringUtil.endsWith('abc', '\0', Infinity));
assert.isTrue(stringUtil.endsWith('abc', 'c', Infinity));
assert.isFalse(stringUtil.endsWith('abc', 'b', Infinity));
assert.isTrue(stringUtil.endsWith('abc', 'bc', Infinity));
assert.isTrue(stringUtil.endsWith('abc', 'abc', Infinity));
assert.isFalse(stringUtil.endsWith('abc', 'abcd', Infinity));

assert.isTrue(stringUtil.endsWith('abc', '', undefined));
assert.isFalse(stringUtil.endsWith('abc', '\0', undefined));
assert.isTrue(stringUtil.endsWith('abc', 'c', undefined));
assert.isFalse(stringUtil.endsWith('abc', 'b', undefined));
assert.isTrue(stringUtil.endsWith('abc', 'bc', undefined));
assert.isTrue(stringUtil.endsWith('abc', 'abc', undefined));
assert.isFalse(stringUtil.endsWith('abc', 'abcd', undefined));

assert.isTrue(stringUtil.endsWith('abc', '', null as any));
assert.isFalse(stringUtil.endsWith('abc', '\0', null as any));
assert.isFalse(stringUtil.endsWith('abc', 'c', null as any));
assert.isFalse(stringUtil.endsWith('abc', 'b', null as any));
assert.isFalse(stringUtil.endsWith('abc', 'bc', null as any));
assert.isFalse(stringUtil.endsWith('abc', 'abc', null as any));
assert.isFalse(stringUtil.endsWith('abc', 'abcd', null as any));

assert.isTrue(stringUtil.endsWith('abc', '', NaN));
assert.isFalse(stringUtil.endsWith('abc', '\0', NaN));
assert.isFalse(stringUtil.endsWith('abc', 'c', NaN));
assert.isFalse(stringUtil.endsWith('abc', 'b', NaN));
assert.isFalse(stringUtil.endsWith('abc', 'bc', NaN));
assert.isFalse(stringUtil.endsWith('abc', 'abc', NaN));
assert.isFalse(stringUtil.endsWith('abc', 'abcd', NaN));
},

'position is 0 or negative'() {
Expand Down

0 comments on commit ff79ad9

Please sign in to comment.