• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

terminal-nerds / snippets / 5140704919

pending completion
5140704919

Pull #109

github

web-flow
Merge aa37c7518 into e38a9b40e
Pull Request #109: chore(Renovate): ⬆️ Update pnpm to `v8.6.0`

264 of 325 branches covered (81.23%)

Branch coverage included in aggregate %.

3147 of 3285 relevant lines covered (95.8%)

30.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.33
/packages/function/source/schema/schema.ts
1
import {
1✔
2
        isAsyncFunction as nodeIsAsyncFunction,
1✔
3
        isGeneratorFunction as nodeIsGeneratorFunction,
1✔
4
} from "node:util/types";
1✔
5

1✔
6
import { IN_BROWSER } from "@terminal-nerds/snippets-runtime/environment";
1✔
7
import { AsyncFunction, AsyncGeneratorFunction, GeneratorFunction } from "@terminal-nerds/snippets-type/built-in";
1✔
8
import { isNonPrimitive, validateNonPrimitive } from "@terminal-nerds/snippets-type/non-primitive";
1✔
9

1✔
10
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
1✔
11
export type AnyFunction = (..._args: any) => any;
1✔
12

1✔
13
export function validateFunction(value: unknown): asserts value is AnyFunction {
1✔
14
        validateNonPrimitive(value, "function");
618✔
15
}
618✔
16

1✔
17
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function} Function */
1✔
18
export function isFunction(value: unknown): value is AnyFunction {
1✔
19
        return isNonPrimitive(value, "function");
98✔
20
}
98✔
21

1✔
22
/**
1✔
23
 * Credits: https://github.com/inspect-js/is-arrow-function/blob/main/index.js
1✔
24
 *
1✔
25
 * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions} Arrow function
1✔
26
 */
1✔
27
export function isArrowFunction(_function: AnyFunction): boolean {
1✔
28
        validateFunction(_function);
70✔
29

70✔
30
        const isSimpleFunctionRegex = /^\s*function/;
70✔
31
        const isWithParensRegex = /^\([^)]*\) *=>/;
70✔
32
        const isWithoutParensRegex = /^[^=]*=>/;
70✔
33
        const stringifiedFunction = _function.toString();
70✔
34

70✔
35
        return (
70✔
36
                stringifiedFunction.length > 0 &&
70✔
37
                !isSimpleFunctionRegex.test(stringifiedFunction) &&
70✔
38
                (isWithParensRegex.test(stringifiedFunction) || isWithoutParensRegex.test(stringifiedFunction))
30✔
39
        );
70✔
40
}
70✔
41

1✔
42
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions} Simple function */
1✔
43
export function isSimpleFunction(_function: AnyFunction): boolean {
1✔
44
        return !isArrowFunction(_function);
35✔
45
}
35✔
46

1✔
47
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function} Async function */
1✔
48
export function isAsyncFunction(_function: AnyFunction): _function is typeof AsyncFunction {
1✔
49
        validateFunction(_function);
16✔
50

16✔
51
        return IN_BROWSER ? _function instanceof AsyncFunction : nodeIsAsyncFunction(_function);
16!
52
}
16✔
53

1✔
54
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function} Async generator function */
1✔
55
export function isAsyncGeneratorFunction(_function: AnyFunction): _function is typeof AsyncGeneratorFunction {
1✔
56
        validateFunction(_function);
10✔
57

10✔
58
        return IN_BROWSER
10!
59
                ? _function instanceof AsyncGeneratorFunction
×
60
                : isAsyncFunction(_function) && isGeneratorFunction(_function);
10✔
61
}
10✔
62

1✔
63
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function} Generator function */
1✔
64
export function isGeneratorFunction(_function: AnyFunction): _function is GeneratorFunction {
1✔
65
        validateFunction(_function);
10✔
66

10✔
67
        return IN_BROWSER ? _function instanceof GeneratorFunction : nodeIsGeneratorFunction(_function);
10!
68
}
10✔
69

1✔
70
export type FunctionType =
1✔
71
        /** Arrow function `async () => {}` */
1✔
72
        | "arrow"
1✔
73
        /** Arrow and asynchronous function `async () => {}` */
1✔
74
        | "arrow-async"
1✔
75
        /** A classic, simple function `function name() {}` */
1✔
76
        | "simple"
1✔
77
        /** A classic, simple and asynchronous function `async function name() {}` */
1✔
78
        | "simple-async"
1✔
79
        /** A classic, simple, asynchronous and generator function `async function* name() {}` */
1✔
80
        | "simple-async-generator"
1✔
81
        /** A classic, simple and generator function `function* name() {}` */
1✔
82
        | "simple-generator";
1✔
83

1✔
84
function determineArrowFunctionType(_function: AnyFunction): Extract<FunctionType, "arrow" | "arrow-async"> {
×
85
        return isAsyncFunction(_function) ? "arrow-async" : "arrow";
×
86
}
×
87

1✔
88
function determineSimpleAsyncFunction(
×
89
        _function: AnyFunction,
×
90
): Extract<FunctionType, "simple-async-generator" | "simple-async"> {
×
91
        return isGeneratorFunction(_function) ? "simple-async-generator" : "simple-async";
×
92
}
×
93

1✔
94
function determineSimpleSyncFunction(_function: AnyFunction): Extract<FunctionType, "simple" | "simple-generator"> {
×
95
        return isGeneratorFunction(_function) ? "simple-generator" : "simple";
×
96
}
×
97

1✔
98
function determineSimpleFunctionType(
×
99
        _function: AnyFunction,
×
100
): Extract<FunctionType, "simple" | "simple-async" | "simple-async-generator" | "simple-generator"> {
×
101
        return isAsyncFunction(_function)
×
102
                ? determineSimpleAsyncFunction(_function)
×
103
                : determineSimpleSyncFunction(_function);
×
104
}
×
105

1✔
106
/** @see {@link FunctionType} */
1✔
107
export function getFunctionType(_function: AnyFunction): FunctionType {
1✔
108
        return isArrowFunction(_function) ? determineArrowFunctionType(_function) : determineSimpleFunctionType(_function);
×
109
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc