Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1337x 1337x 1337x 1337x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 254x 254x 254x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 702x 702x 11x 11x 691x 702x 702x 702x 661x 661x 702x 30x 30x 30x 26x 26x 26x 30x 4x 4x 4x 4x 4x 702x 702x 702x 4x 4x 702x 702x 702x 702x 702x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 579x 579x 579x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 517x 517x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 279x 279x 279x 279x 1x 1x 279x 279x 279x 4x 4x 4x 4x 274x 274x 279x 279x 279x 18x 18x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 1x | // @ts-check
import { isFunction, isString } from './base';
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
/** @typedef {import('./function/typedef.js').ES3Function} ES3Function */
/** @typedef {import('./function/typedef.js').ClassConstructor<typeof Function>} ClassConstructor */
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
/** @internal */
export const getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;
/** @internal */
export const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
/** @internal */
export const getPrototypeOf = Object.getPrototypeOf;
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
/**
* Returns the internal type signature of the first argument, if provided.
*
* This function exposes a value’s internal `[[Class]]` tag (aka type signature),
* such as `'[object Function]'` for a function or `'[object String]'` for a string -
* whether it's a primitive string value or a `String` object type.
*
* Internally, it delegates to the standard prototype `toString` method:
*
* ```js
* Object.prototype.toString.call(value);
* ```
* @param {...any} args
* A variadic argument list. The first argument (`args[0]`) is the optional
* `value` parameter. Its **presence** is detected via `args.length`, allowing
* the function to distinguish between an explicitly passed `undefined` value
* and a completely omitted argument.
* @returns {string | undefined}
* The value’s internal type signature (e.g., `'[object Array]'` for an
* `Array` instance), or the `undefined` value if no argument was passed.
* @category Basic Type Detection Helper
*/
export function getTypeSignature(...args) {
/** @type {any} */
const value = args[0];
return (args.length >= 1 && Object.prototype.toString.call(value)) || value;
}
/**
* Returns the tag name extracted from a value's internal type signature.
*
* This function wraps `getTypeSignature` and extracts the value’s internal
* `[[Class]]` tag name - e.g., `'Array'` for arrays, `'Date'` for dates, or
* even `'FooBar'` for objects _"spoofed"_ via `Symbol.toStringTag` ...
*
* ```js
* const myObj = { foo: 'bar' }
* myObj[Symbol.toStringTag] = 'FooBar';
* ```
*
* If no argument is passed, the function returns `undefined`.
*
* ### Note
* The tag name is the portion inside the brackets of the full type signature:
*
* ```js
* Object.prototype.toString.call([]); // => '[object Array]'
* ```
*
* Custom tag names can be defined via the `Symbol.toStringTag` property.
*
* Full example code for a successful _"spoofing"_ attempt:
*
* ```js
* const myObj = { foo: 'bar' }
* myObj[Symbol.toStringTag] = 'FooBar';
*
* console.log(myObj+''); // '[object FooBar]'
* console.log(String(myObj)); // '[object FooBar]'
* console.log(myObj.toString()); // '[object FooBar]'
* console.log(Object.prototype.toString.call(myObj)); // '[object FooBar]'
* ```
*
* This works for both custom types and overrides of built-in types.
* @param {...any} args
* A variadic argument list. The first argument (`args[0]`) is optional.
* Its **presence** is detected via the result of the forwarding call to
* `getTypeSignature`.
* @returns {string | undefined}
* The extracted tag name (e.g. `'Array'`, `'Date'`) or `undefined` if no
* value was provided.
* @category Basic Type Detection Helper
*/
export function getTaggedType(...args) {
const result = getTypeSignature(...args);
return (isString(result) && result.slice(8, -1)) || result;
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
/**
* @param {any} [value]
* An optionally passed value of any type.
* @returns {ClassConstructor | ES3Function | NewableFunction | undefined}
* if available, the passed value's constructor-function - either a built-in
* type's constructor-function or an ES6-class constructor-function or an
* ES3-function - otherwise `undefined`.
* @category Basic Type Detection Helper
*/
export function getDefinedConstructor(value = null) {
// guard.
if (value === null) {
return;
}
/** @type {Function | Object} */
const constructor = getOwnPropertyDescriptor(value, 'constructor')?.value ?? value.constructor;
// various guards.
if (isFunction(constructor)) {
// exit early with a valid result.
return constructor;
} else {
/** @type {Function | undefined} */
const creator = constructor?.constructor;
if (isFunction(creator)) {
// exit early with a valid result.
return creator;
}
}
// - in case function execution reaches beyond this comment,
// the `constructor` slot most probably has been manipulated, ...
//
// ... or the passed `value` was created via `Object.create(null)`.
/** @type {Object | null} */
const prototype = getPrototypeOf(value) ?? null;
// guard.
if (prototype === null) {
return;
}
// - in case function execution reaches beyond this comment,
// the `constructor` slot definitely has been maliciously
// manipulated.
/** @type {Function | Object} */
const protoConstructor =
getOwnPropertyDescriptor(prototype, 'constructor')?.value ?? prototype.constructor;
// various guards.
if (isFunction(protoConstructor)) {
// exit with a probably still valid result.
return protoConstructor;
} else {
/** @type {Function | undefined} */
const protoCreator = protoConstructor?.constructor;
if (isFunction(protoCreator)) {
// exit with a probably still valid result.
return protoCreator;
}
}
// implicitly return the `undefined` value.
}
/**
* Implements a getter for the passed value's constructor-function name.
* In case of being able to retrieve a constructor, the remaining constraint
* is due to any function's `name` related property descriptor, which by default,
* hence without any intentional further change, is ...
*
* ```
* { ... writable: false, enumerable: false, configurable: true }
* ```
*
* ...
* - neither writable
* - nor enumerable
* - but configurable.
*
* Thus, something like ...
*
* ```
* Object.defineProperty(fct, 'name', { value: 'FOO' })
* ```
*
* ... will change any passed function's `name` value to "FOO". As long
* as the latter can be safely excluded, the detection approach is safe.
* One even can or better yet should take advantage of it, branding a
* function permanently, in order to e.g. let constructor functions
* harden each their name as a countermeasure to code-minification tasks.
* @param {any} [value]
* An optionally passed value of any type.
* @returns {string | undefined}
* if available, the passed value's constructor-function name - retrieved
* exclusively from linked property-descriptors - otherwise `undefined`.
* Any unnamed function refers to the empty string value/`''` as its name.
* @category Basic Type Detection Helper
*/
export function getDefinedConstructorName(value) {
const constructor = getDefinedConstructor(value);
return constructor && constructor.name;
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
/**
* Reaches for a function's stringified version by making
* use of ...
*
* ```
* Function.prototype.toString.call(value);
* ```
*
* ... which helps in passing by some possibly manipulated
* `toString` functionality.
* @param {Function} value
* Assumes a function type but does not check for it.
* @returns {string}
* Returns a function's stringified implementation.
* @category Basic Type Detection Helper
*/
export function getFunctionSource(value) {
return Function.prototype.toString.call(value).trim();
}
/**
* Resolves the passed value's type-name through a combined, balanced approach of
* retrieving either the value's constructor-function name or its `toString` tag.
*
* This works for every built-in type.
*
* In order to assure stable type-identity of custom type systems, based
* on both class- and ES3-constructor functions, that remain unaffected
* by code minification processes, one has to apply a utility function
* which does permanently brand such types by writing and freezing both
* of a constructor-function's property-descriptors - the function's `name`
* property and its `Symbol.toStringTag` slot.
* @param {...any} args
* A variadic argument list. The first argument (`args[0]`) is the optional
* `value` parameter. Its **presence** is detected via `args.length`, allowing
* the function to distinguish between an explicitly passed `undefined` value
* and a completely omitted argument.
* @returns {string | undefined}
* A `'string'` value which either corresponds with the passed value's
* constructor-function's name or its tagged type; or the `undefined`
* value if no argument was passed.
* @category Basic Type Detection Helper
*/
export function resolveType(...args) {
/** @type {any} */
const value = args[0];
// guard.
if (args.length === 0) {
return;
}
const resolvedType = getDefinedConstructorName(value) ?? null;
// guard.
if (resolvedType === null) {
// - covers the `undefined` and the `null` value as well as
// objects that were created via `Object.create(null)`.
return getTaggedType(value);
}
// - The following block provides a more generic solution ...
/** @type {Function | Object} */
const constructor = getOwnPropertyDescriptor(value, 'constructor')?.value ?? value.constructor;
// guard.
if (!isFunction(constructor)) {
return getTaggedType(value);
}
return resolvedType;
// ... to special cases like the one of ...
//
// // - `Generator` and `AsyncGenerator` instances (objects) as well as
// // `GeneratorFunction` and `AsyncGeneratorFunction` types (functions)
// // need to be handled separately, in order to distinguish them.
// ((
//
// (resolvedType === 'GeneratorFunction' || resolvedType === 'AsyncGeneratorFunction') &&
// getTaggedType(value)
//
// ) || resolvedType);
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
|