All files / src/function index.js

100% Statements 351/351
100% Branches 41/41
100% Functions 13/13
100% Lines 351/351

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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 3961x   1x 1x                 1x   1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 391x 391x 391x 391x 391x   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 60x 60x 60x 6x 60x 60x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 60x 6x 60x 60x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 97x 91x 91x   91x 91x 91x 91x 91x 91x 6x 6x   1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 188x 188x 188x 34x 188x 188x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 10x 10x 60x 60x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 10x 10x 60x 60x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 126x 126x 114x 114x 126x 126x   1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 62x 62x 62x 62x 6x   62x 62x 62x 62x   1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 60x 60x 60x 6x 6x   6x 60x 60x   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 73x 73x 59x 59x 59x 9x 59x 39x 39x 59x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x   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 1x 1x 1x 1x 60x 60x   1x  
// @ts-check
 
import { hasOwnPrototype, hasOwnWritablePrototype } from './utility';
import {
  getFunctionSource,
  getDefinedConstructor,
  getDefinedConstructorName,
  getTypeSignature,
  getTaggedType,
  resolveType
} from '../utility';
 
import { isFunction } from '../base';
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').ClassConstructor<typeof Function>} ClassConstructor */
 
/**
 * Detects whether the passed `value` is a
 * constructor function implemented as `class`.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is ClassConstructor}
 *  A boolean value which indicates whether the
 *  tested value is a class-constructor function.
 * @category Function Type Detection
 */
export function isClass(value) {
  return (
    isFunction(value) && getFunctionSource(value).startsWith('class')
    // (/^class(\s+[^{]+)?\s*{/).test(getFunctionSource(value))
  );
}
 
// /**
//  * @template {new (...args: any[]) => any} T
//  * @typedef {import('./typedef.js').SubclassedConstructor<T>} SubclassedConstructor
//  */
//
// /**
//  * Detects whether the passed `value` is a
//  * constructor function implemented as `class`
//  * that in addition extends another class.
//  * @param {any} [value]
//  *  An optionally passed value of any type.
//  * @returns {value is SubclassedConstructor}
//  *  A boolean value which indicates whether the
//  *  tested value is a subclassed constructor function.
//  * @category Function Type Detection
//  */
// export function isSubclass(value) {
//   return (
//     isClass(value) &&
//     // see ... [https://regex101.com/r/MoEhSd/1]
//     /^class\s+([_$\p{L}][_$\p{L}\p{N}]+\s+)?extends\b/u.test(getFunctionSource(value))
//   );
// }
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').GeneratorFunction} GeneratorFunction */
 
/**
 * Detects whether the passed `value` is exclusively a `GeneratorFunction` type.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is GeneratorFunction}
 *  A boolean value which indicates whether the tested value is exclusively a
 *  `GeneratorFunction` type.
 * @category Function Type Detection
 */
export function isGeneratorFunction(value) {
  return (
    isFunction(value) &&
    getTypeSignature(value) === '[object GeneratorFunction]' &&
    resolveType(value) === 'GeneratorFunction'
  );
}
 
/** @typedef {import('./typedef.js').AsyncGeneratorFunction} AsyncGeneratorFunction */
 
/**
 * Detects whether the passed `value` is exclusively an `AsyncGeneratorFunction` type.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AsyncGeneratorFunction}
 *  A boolean value which indicates whether the tested value is exclusively an
 *  `AsyncGeneratorFunction` type.
 * @category Function Type Detection
 */
export function isAsyncGeneratorFunction(value) {
  return (
    isFunction(value) &&
    getTypeSignature(value) === '[object AsyncGeneratorFunction]' &&
    resolveType(value) === 'AsyncGeneratorFunction'
  );
}
 
/** @typedef {import('./typedef.js').AnyGeneratorFunction} AnyGeneratorFunction */
 
/**
 * Detects whether the passed `value` is either kind of generator function,
 * async or non-async.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AnyGeneratorFunction}
 *  A boolean value which indicates whether the tested value is either
 *  an async or a non-async generator function.
 * @category Function Type Detection
 */
export function isAnyGeneratorFunction(value) {
  if (isFunction(value)) {
    const typeName = resolveType(value);
    const taggedType = getTaggedType(value);
 
    return (
      !!typeName &&
      typeName === taggedType &&
      (typeName === 'GeneratorFunction' || typeName === 'AsyncGeneratorFunction')
    );
  }
  return false;
}
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').AsyncFunction} AsyncFunction */
 
/**
 * Detects whether the passed `value` is an async function type, either an
 * async arrow function expression or an async non-arrow function expression
 * or an async function statement.
 * It does not detect an async generator function since the latter is not
 * an async function itself but the factory function of an async generator.
 * Async functions do return promises but do not return async generators.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AsyncFunction}
 *  A boolean value which indicates whether
 *  the tested value is an async function.
 * @category Function Type Detection
 */
export function isAsyncFunction(value) {
  return (
    isFunction(value) &&
    getTypeSignature(value) === '[object AsyncFunction]' &&
    getDefinedConstructorName(value) === 'AsyncFunction'
  );
}
 
/**
 * Detects whether the passed `value` is exclusively one of following two
 * async function variants, either an async non-arrow function expression
 * or an async function statement.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AsyncFunction}
 *  A boolean value which indicates whether the tested value does match
 *  exclusively a non-arrow async function.
 * @category Function Type Detection
 */
export function isAsyncNonArrow(value) {
  return (
    isAsyncFunction(value) &&
    // see ... [https://regex101.com/r/9SamJe/2]
    !/^async\s+(?:\(.*?\)|[^(),=]+)\s*=>/.test(getFunctionSource(value))
  );
}
 
/**
 * Detects whether the passed `value` is exclusively an async arrow function.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AsyncFunction}
 *  A boolean value which indicates whether the tested value is exclusively
 *  an async arrow function.
 * @category Function Type Detection
 */
export function isAsyncArrow(value) {
  return (
    isAsyncFunction(value) &&
    // see ... [https://regex101.com/r/9SamJe/2]
    /^async\s+(?:\(.*?\)|[^(),=]+)\s*=>/.test(getFunctionSource(value))
  );
}
 
/** @typedef {import('./typedef.js').NonAsyncArrow} NonAsyncArrow */
 
/**
 * Detects whether the passed `value` is exclusively a non-async arrow function.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is NonAsyncArrow}
 *  A boolean value which indicates whether the tested value is exclusively
 *  a non-async arrow function.
 * @category Function Type Detection
 */
export function isNonAsyncArrow(value) {
  return isArrow(value) && !isAsyncFunction(value);
}
 
/** @typedef {import('./typedef.js').AnyArrow} AnyArrow */
 
/**
 * Detects whether the passed `value` is either kind of an arrow
 * function expression - async or non-async.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AnyArrow}
 *  A boolean value which indicates whether the tested value is
 *  either an async or a non-async arrow function expression.
 * @category Function Type Detection
 */
export function isArrow(value) {
  return (
    isFunction(value) &&
    // see ... [https://regex101.com/r/9SamJe/1]
    /^(?:async\s+)?(?:\(.*?\)|[^(),=]+)\s*=>/.test(getFunctionSource(value))
  );
}
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').ES3Function} ES3Function */
 
/**
 * Detects whether the passed `value` is exclusively the only available
 * function type of ES3 (in addition to all the built-in constructors).
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is ES3Function}
 *  A boolean value which indicates whether the tested value is exclusively
 *  the only available/known function type back at ES3 (in addition to all
 *  the built-in constructor functions).
 * @category Function Type Detection
 */
export function isES3Function(value) {
  return (
    isFunction(value) &&
    hasOwnWritablePrototype(value) &&
    !isAnyGeneratorFunction(value) &&
    !getFunctionSource(getDefinedConstructor(value)).startsWith('class')
 
    // (/^class(\s+[^{]+)?\s*{/).test(getFunctionSource(getDefinedConstructor(value)))
    // !isExtendedFunction(value)
  );
}
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').ConciseGenericMethod} ConciseGenericMethod */
 
/**
 * Detects whether the passed `value` is a concise generic method, a function
 * which has been created exclusively by a non-async and non-generator shorthand
 * method definition.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is ConciseGenericMethod}
 *  A boolean value which indicates whether the tested value is exclusively a
 *  concise generic method, a function created by a non-async and non-generator
 *  shorthand method definition.
 * @category Function Type Detection
 */
export function isConciseGenericMethod(value) {
  return (
    isFunction(value) &&
    !hasOwnPrototype(value) &&
    getTypeSignature(value) === '[object Function]' &&
    getDefinedConstructorName(value) === 'Function' &&
    // - until here all above steps do apply
    //   for non-async arrow functions too.
 
    !isArrow(value)
  );
}
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/**
 * Detects whether the passed `value` is a generic (unspecific/non-specific)
 * function ...
 *
 * - either a good old ES3 function,
 * - or a non-async arrow function expression.
 * - or either a non-async and non-generator concise method (shorthand function definition).
 *
 * Thus, the following specific (non-generic) function types are excluded ...
 *
 * - class constructor functions,
 * - any generator function,
 * - any async function variant,
 * - extended `Function` types,
 * - built-in constructor functions,
 * - Web Api constructor functions.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is (ES3Function | NonAsyncArrow)}
 *  A boolean value which indicates whether the tested value is either
 *  a good old ES3 function or a non-async arrow function expression.
 * @category Function Type Detection
 */
export function isGenericFunction(value) {
  return (
    isFunction(value) &&
    // non-async arrow-functions and shorthand methods (aka concise methods)
    (!hasOwnPrototype(value)
      ? getTypeSignature(value) === '[object Function]' &&
        getDefinedConstructorName(value) === 'Function'
      : // ES3 function
        hasOwnWritablePrototype(value) &&
        !isAnyGeneratorFunction(value) &&
        !getFunctionSource(getDefinedConstructor(value)).startsWith('class'))
  );
  // // ... was ...
  //
  // return isNonAsyncArrow(value) || isES3Function(value);
  //
  // // ... but is too strict due to methods that are written like ...
  // //
  // // const idealThenable = {
  // //   // Concise Method Syntax aka "Shorthand Method Definitions"
  // //   then(onFulfilled, onRejected) {
  // //     try {
  // //       const result = 'fulfilled from thenable';
  // //       if (typeof onFulfilled === 'function') {
  // //         onFulfilled(result);
  // //       }
  // //     } catch (err) {
  // //       if (typeof onRejected === 'function') {
  // //         onRejected(err);
  // //       }
  // //     }
  // //   }
  // // };
  // // ... where `then` is a function which does not have an own `prototype` descriptor slot.
}
 
// // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
//
// /** @typedef {import('./typedef.js').FunctionSubtype<Function>} FunctionSubtype */
//
// /**
//  * Detects whether the passed `value` is exclusively an instance
//  * of a `Function` subclass, hence a `Function` subtype of e.g.
//  * following form ...
//  *
//  * ```
//  * class Applicator extends Function {
//  *   constructor(...args) {
//  *     super(...args);
//  *   }
//  * }
//  * // - constructable and callable instance of the
//  * //   custom `Applicator` function subtype/class.
//  * const Area = new Applicator('length = 1, width = 1', 'this.area = length * width');
//  *
//  * console.log(new Area); // { area: 1 }
//  * ```
//  * @param {any} [value]
//  *  An optionally passed value of any type.
//  * @returns {value is FunctionSubtype}
//  *  A boolean value which indicates whether the tested type is a
//  *  `Function` subtype (an instance of a class which extends `Function`).
//  * @category Function Type Detection
//  */
// export function isFunctionSubtype(value) {
//   return (
//     isFunction(value) &&
//     getFunctionSource(
//       getOwnPropertyDescriptor(value, 'constructor') ?? value.constructor ?? ((_) => _)
//     ).startsWith('class')
//   );
// }
//
// // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').UnnamedFunction} UnnamedFunction */
 
/**
 * Detects whether the passed `value` is any kind of function type
 * but without a given name.
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is UnnamedFunction}
 *  A boolean value which indicates whether
 *  the tested type is an unnamed function.
 * @category Function Type Detection
 */
export function isUnnamedFunction(value) {
  return isFunction(value) && value.name === '';
}
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----