MCPcopy Index your code
hub / github.com/nodejs/node / determineSpecificType

Function determineSpecificType

lib/internal/errors.js:1008–1056  ·  view source on GitHub ↗

* Determine the specific type of a value for type-mismatch errors. * @param {*} value * @returns {string}

(value)

Source from the content-addressed store, hash-verified

1006 * @returns {string}
1007 */
1008function determineSpecificType(value) {
1009 if (value === null) {
1010 return 'null';
1011 } else if (value === undefined) {
1012 return 'undefined';
1013 }
1014
1015 const type = typeof value;
1016
1017 switch (type) {
1018 case 'bigint':
1019 return `type bigint (${value}n)`;
1020 case 'number':
1021 if (value === 0) {
1022 return 1 / value === -Infinity ? 'type number (-0)' : 'type number (0)';
1023 } else if (value !== value) { // eslint-disable-line no-self-compare
1024 return 'type number (NaN)';
1025 } else if (value === Infinity) {
1026 return 'type number (Infinity)';
1027 } else if (value === -Infinity) {
1028 return 'type number (-Infinity)';
1029 }
1030 return `type number (${value})`;
1031 case 'boolean':
1032 return value ? 'type boolean (true)' : 'type boolean (false)';
1033 case 'symbol':
1034 return `type symbol (${String(value)})`;
1035 case 'function':
1036 return `function ${value.name}`;
1037 case 'object':
1038 if (value.constructor && 'name' in value.constructor) {
1039 return `an instance of ${value.constructor.name}`;
1040 }
1041 return `${lazyInternalUtilInspect().inspect(value, { depth: -1 })}`;
1042 case 'string':
1043 value.length > 28 && (value = `${StringPrototypeSlice(value, 0, 25)}...`);
1044 if (StringPrototypeIndexOf(value, "'") === -1) {
1045 return `type string ('${value}')`;
1046 }
1047 return `type string (${JSONStringify(value)})`;
1048 default:
1049 value = lazyInternalUtilInspect().inspect(value, { colors: false });
1050 if (value.length > 28) {
1051 value = `${StringPrototypeSlice(value, 0, 25)}...`;
1052 }
1053
1054 return `type ${type} (${value})`;
1055 }
1056}
1057
1058/**
1059 * Create a list string in the form like 'A and B' or 'A, B, ..., and Z'.

Callers 3

errors.jsFile · 0.85
mainFunction · 0.85

Calls 3

StringClass · 0.85
lazyInternalUtilInspectFunction · 0.85
inspectMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…