Skip to content

Commit fd533bd

Browse files
committed
Implement CheckObjectCoercible
1 parent 345dbd3 commit fd533bd

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

codepointat.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/*! http://mths.be/codepointat v0.1.0 by @mathias */
22
if (!String.prototype.codePointAt) {
33
String.prototype.codePointAt = function(position) {
4+
'use strict';
5+
if (this == null) {
6+
throw TypeError();
7+
}
48
var string = String(this);
59
var size = string.length;
610
// `ToInteger`

tests/tests.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var assert = require('assert');
22
var assertEquals = assert.equal;
3+
var assertThrows = assert['throws'];
34

45
require('../codepointat.js');
56

@@ -61,3 +62,19 @@ assertEquals('\uDF06abc'.codePointAt(false), 0xDF06);
6162
assertEquals('\uDF06abc'.codePointAt(NaN), 0xDF06);
6263
assertEquals('\uDF06abc'.codePointAt(null), 0xDF06);
6364
assertEquals('\uDF06abc'.codePointAt(undefined), 0xDF06);
65+
66+
assertThrows(function() { String.prototype.codePointAt.call(undefined); }, TypeError);
67+
assertThrows(function() { String.prototype.codePointAt.call(undefined, 4); }, TypeError);
68+
assertThrows(function() { String.prototype.codePointAt.call(null); }, TypeError);
69+
assertThrows(function() { String.prototype.codePointAt.call(null, 4); }, TypeError);
70+
assertEquals(String.prototype.codePointAt.call(42, 0), 0x34);
71+
assertEquals(String.prototype.codePointAt.call(42, 1), 0x32);
72+
assertEquals(String.prototype.codePointAt.call({ 'toString': function() { return 'abc'; } }, 2), 0x63);
73+
74+
assertThrows(function() { String.prototype.codePointAt.apply(undefined); }, TypeError);
75+
assertThrows(function() { String.prototype.codePointAt.apply(undefined, [4]); }, TypeError);
76+
assertThrows(function() { String.prototype.codePointAt.apply(null); }, TypeError);
77+
assertThrows(function() { String.prototype.codePointAt.apply(null, [4]); }, TypeError);
78+
assertEquals(String.prototype.codePointAt.apply(42, [0]), 0x34);
79+
assertEquals(String.prototype.codePointAt.apply(42, [1]), 0x32);
80+
assertEquals(String.prototype.codePointAt.apply({ 'toString': function() { return 'abc'; } }, [2]), 0x63);

0 commit comments

Comments
 (0)