Skip to content

Commit

Permalink
[Refactor] prefer typeof over Type(), except for Object, where po…
Browse files Browse the repository at this point in the history
…ssible

This reflects the ES2023+ convention of using eg "x is a String" rather than "Type(x) is String", and ends up being more performant, produces smaller bundles, improves TypeScript narrowing, and allows engines to optimize more typeof comparisons.
  • Loading branch information
ljharb committed Feb 3, 2024
1 parent c710d7c commit bd7551c
Show file tree
Hide file tree
Showing 462 changed files with 616 additions and 1,241 deletions.
5 changes: 2 additions & 3 deletions 2015/AbstractRelationalComparison.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions 2015/AdvanceStringIndex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var Type = require('./Type');

var isInteger = require('../helpers/isInteger');
var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
Expand All @@ -14,13 +12,13 @@ var $charCodeAt = require('call-bind/callBound')('String.prototype.charCodeAt');
// https://262.ecma-international.org/6.0/#sec-advancestringindex

module.exports = function AdvanceStringIndex(S, index, unicode) {
if (Type(S) !== 'String') {
if (typeof S !== 'string') {
throw new $TypeError('Assertion failed: `S` must be a String');
}
if (!isInteger(index) || index < 0 || index > MAX_SAFE_INTEGER) {
throw new $TypeError('Assertion failed: `length` must be an integer >= 0 and <= 2**53');
}
if (Type(unicode) !== 'Boolean') {
if (typeof unicode !== 'boolean') {
throw new $TypeError('Assertion failed: `unicode` must be a Boolean');
}
if (!unicode) {
Expand Down
3 changes: 1 addition & 2 deletions 2015/CanonicalNumericIndexString.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ var $TypeError = require('es-errors/type');
var SameValue = require('./SameValue');
var ToNumber = require('./ToNumber');
var ToString = require('./ToString');
var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-canonicalnumericindexstring

module.exports = function CanonicalNumericIndexString(argument) {
if (Type(argument) !== 'String') {
if (typeof argument !== 'string') {
throw new $TypeError('Assertion failed: `argument` must be a String');
}
if (argument === '-0') { return -0; }
Expand Down
6 changes: 2 additions & 4 deletions 2015/Canonicalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ var hasOwn = require('hasown');
var $charCodeAt = callBound('String.prototype.charCodeAt');
var $toUpperCase = callBound('String.prototype.toUpperCase');

var Type = require('./Type');

var caseFolding = require('../helpers/caseFolding.json');

// https://262.ecma-international.org/6.0/#sec-runtime-semantics-canonicalize-ch

module.exports = function Canonicalize(ch, IgnoreCase, Unicode) {
if (Type(ch) !== 'String') {
if (typeof ch !== 'string') {
throw new $TypeError('Assertion failed: `ch` must be a character');
}

if (Type(IgnoreCase) !== 'Boolean' || Type(Unicode) !== 'Boolean') {
if (typeof IgnoreCase !== 'boolean' || typeof Unicode !== 'boolean') {
throw new $TypeError('Assertion failed: `IgnoreCase` and `Unicode` must be Booleans');
}

Expand Down
3 changes: 1 addition & 2 deletions 2015/CreateHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ var $replace = callBound('String.prototype.replace');

var RequireObjectCoercible = require('./RequireObjectCoercible');
var ToString = require('./ToString');
var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-createhtml

module.exports = function CreateHTML(string, tag, attribute, value) {
if (Type(tag) !== 'String' || Type(attribute) !== 'String') {
if (typeof tag !== 'string' || typeof attribute !== 'string') {
throw new $TypeError('Assertion failed: `tag` and `attribute` must be strings');
}
var str = RequireObjectCoercible(string);
Expand Down
4 changes: 1 addition & 3 deletions 2015/CreateIterResultObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

var $TypeError = require('es-errors/type');

var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-createiterresultobject

module.exports = function CreateIterResultObject(value, done) {
if (Type(done) !== 'Boolean') {
if (typeof done !== 'boolean') {
throw new $TypeError('Assertion failed: Type(done) is not Boolean');
}
return {
Expand Down
11 changes: 5 additions & 6 deletions 2015/GetSubstitution.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var $charAt = callBound('String.prototype.charAt');
var $strSlice = callBound('String.prototype.slice');

var IsArray = require('./IsArray');
var Type = require('./Type');

var isInteger = require('../helpers/isInteger');
var isStringOrUndefined = require('../helpers/isStringOrUndefined');
Expand All @@ -27,12 +26,12 @@ var isStringOrUndefined = require('../helpers/isStringOrUndefined');

// eslint-disable-next-line max-statements, max-lines-per-function
module.exports = function GetSubstitution(matched, str, position, captures, replacement) {
if (Type(matched) !== 'String') {
if (typeof matched !== 'string') {
throw new $TypeError('Assertion failed: `matched` must be a String');
}
var matchLength = matched.length;

if (Type(str) !== 'String') {
if (typeof str !== 'string') {
throw new $TypeError('Assertion failed: `str` must be a String');
}
var stringLength = str.length;
Expand All @@ -45,7 +44,7 @@ module.exports = function GetSubstitution(matched, str, position, captures, repl
throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures));
}

if (Type(replacement) !== 'String') {
if (typeof replacement !== 'string') {
throw new $TypeError('Assertion failed: `replacement` must be a String');
}

Expand Down Expand Up @@ -78,14 +77,14 @@ module.exports = function GetSubstitution(matched, str, position, captures, repl
// $1 through $9, and not followed by a digit
var n = $parseInt(next, 10);
// if (n > m, impl-defined)
result += n <= m && Type(captures[n - 1]) === 'Undefined' ? '' : captures[n - 1];
result += n <= m && typeof captures[n - 1] === 'undefined' ? '' : captures[n - 1];
i += 1;
} else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
// $00 through $99
var nn = next + nextNext;
var nnI = $parseInt(nn, 10) - 1;
// if nn === '00' or nn > m, impl-defined
result += nn <= m && Type(captures[nnI]) === 'Undefined' ? '' : captures[nnI];
result += nn <= m && typeof captures[nnI] === 'undefined' ? '' : captures[nnI];
i += 2;
} else {
result += '$';
Expand Down
4 changes: 1 addition & 3 deletions 2015/QuoteJSONString.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var $toLowerCase = callBound('String.prototype.toLowerCase');
var $strSlice = callBound('String.prototype.slice');
var $strSplit = callBound('String.prototype.split');

var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-quotejsonstring

var escapes = {
Expand All @@ -24,7 +22,7 @@ var escapes = {
};

module.exports = function QuoteJSONString(value) {
if (Type(value) !== 'String') {
if (typeof value !== 'string') {
throw new $TypeError('Assertion failed: `value` must be a String');
}
var product = '"';
Expand Down
4 changes: 2 additions & 2 deletions 2015/RegExpExec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ module.exports = function RegExpExec(R, S) {
if (Type(R) !== 'Object') {
throw new $TypeError('Assertion failed: `R` must be an Object');
}
if (Type(S) !== 'String') {
if (typeof S !== 'string') {
throw new $TypeError('Assertion failed: `S` must be a String');
}
var exec = Get(R, 'exec');
if (IsCallable(exec)) {
var result = Call(exec, R, [S]);
if (result === null || Type(result) === 'Object') {
if (typeof result === 'object') {
return result;
}
throw new $TypeError('"exec" method must return `null` or an Object');
Expand Down
2 changes: 1 addition & 1 deletion 2015/Set.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function Set(O, P, V, Throw) {
if (!IsPropertyKey(P)) {
throw new $TypeError('Assertion failed: `P` must be a Property Key');
}
if (Type(Throw) !== 'Boolean') {
if (typeof Throw !== 'boolean') {
throw new $TypeError('Assertion failed: `Throw` must be a Boolean');
}
if (Throw) {
Expand Down
6 changes: 2 additions & 4 deletions 2015/SetFunctionName.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var getSymbolDescription = require('get-symbol-description');

var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
var IsExtensible = require('./IsExtensible');
var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-setfunctionname

Expand All @@ -19,11 +18,10 @@ module.exports = function SetFunctionName(F, name) {
if (!IsExtensible(F) || hasOwn(F, 'name')) {
throw new $TypeError('Assertion failed: `F` must be extensible, and must not have a `name` own property');
}
var nameType = Type(name);
if (nameType !== 'Symbol' && nameType !== 'String') {
if (typeof name !== 'symbol' && typeof name !== 'string') {
throw new $TypeError('Assertion failed: `name` must be a Symbol or a String');
}
if (nameType === 'Symbol') {
if (typeof name === 'symbol') {
var description = getSymbolDescription(name);
// eslint-disable-next-line no-param-reassign
name = typeof description === 'undefined' ? '' : '[' + description + ']';
Expand Down
6 changes: 2 additions & 4 deletions 2015/SplitMatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ var callBound = require('call-bind/callBound');

var $TypeError = require('es-errors/type');

var Type = require('./Type');

var isInteger = require('../helpers/isInteger');

var $charAt = callBound('String.prototype.charAt');

// https://262.ecma-international.org/6.0/#sec-splitmatch

module.exports = function SplitMatch(S, q, R) {
if (Type(S) !== 'String') {
if (typeof S !== 'string') {
throw new $TypeError('Assertion failed: `S` must be a String');
}
if (!isInteger(q)) {
throw new $TypeError('Assertion failed: `q` must be an integer');
}
if (Type(R) !== 'String') {
if (typeof R !== 'string') {
throw new $TypeError('Assertion failed: `R` must be a String');
}
var r = R.length;
Expand Down
3 changes: 1 addition & 2 deletions 2015/StringCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ var $SyntaxError = require('es-errors/syntax');
var $TypeError = require('es-errors/type');

var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
var Type = require('./Type');

var setProto = require('../helpers/setProto');

// https://262.ecma-international.org/6.0/#sec-stringcreate

module.exports = function StringCreate(value, prototype) {
if (Type(value) !== 'String') {
if (typeof value !== 'string') {
throw new $TypeError('Assertion failed: `S` must be a String');
}

Expand Down
3 changes: 1 addition & 2 deletions 2015/StringGetIndexProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var unbox = require('unbox-primitive');
var CanonicalNumericIndexString = require('./CanonicalNumericIndexString');
var IsInteger = require('./IsInteger');
var IsPropertyKey = require('./IsPropertyKey');
var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-stringgetindexproperty

Expand All @@ -25,7 +24,7 @@ module.exports = function StringGetIndexProperty(S, P) {
throw new $TypeError('Assertion failed: `P` must be a Property Key');
}

if (Type(P) !== 'String') {
if (typeof P !== 'string') {
return void undefined;
}

Expand Down
4 changes: 1 addition & 3 deletions 2015/SymbolDescriptiveString.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ var callBound = require('call-bind/callBound');

var $SymbolToString = callBound('Symbol.prototype.toString', true);

var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-symboldescriptivestring

module.exports = function SymbolDescriptiveString(sym) {
if (Type(sym) !== 'Symbol') {
if (typeof sym !== 'symbol') {
throw new $TypeError('Assertion failed: `sym` must be a Symbol');
}
return $SymbolToString(sym);
Expand Down
4 changes: 1 addition & 3 deletions 2015/ToDateString.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ var $String = GetIntrinsic('%String%');

var $isNaN = require('../helpers/isNaN');

var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-todatestring

module.exports = function ToDateString(tv) {
if (Type(tv) !== 'Number') {
if (typeof tv !== 'number') {
throw new $TypeError('Assertion failed: `tv` must be a Number');
}
if ($isNaN(tv)) {
Expand Down
4 changes: 1 addition & 3 deletions 2015/thisBooleanValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

var $BooleanValueOf = require('call-bind/callBound')('Boolean.prototype.valueOf');

var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-properties-of-the-boolean-prototype-object

module.exports = function thisBooleanValue(value) {
if (Type(value) === 'Boolean') {
if (typeof value === 'boolean') {
return value;
}

Expand Down
4 changes: 1 addition & 3 deletions 2015/thisNumberValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

var callBound = require('call-bind/callBound');

var Type = require('./Type');

var $NumberValueOf = callBound('Number.prototype.valueOf');

// https://262.ecma-international.org/6.0/#sec-properties-of-the-number-prototype-object

module.exports = function thisNumberValue(value) {
if (Type(value) === 'Number') {
if (typeof value === 'number') {
return value;
}

Expand Down
4 changes: 1 addition & 3 deletions 2015/thisStringValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

var $StringValueOf = require('call-bind/callBound')('String.prototype.valueOf');

var Type = require('./Type');

// https://262.ecma-international.org/6.0/#sec-properties-of-the-string-prototype-object

module.exports = function thisStringValue(value) {
if (Type(value) === 'String') {
if (typeof value === 'string') {
return value;
}

Expand Down
5 changes: 2 additions & 3 deletions 2016/AbstractRelationalComparison.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bd7551c

Please sign in to comment.