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

marcomontalbano / figma-export / 8040637088

25 Feb 2024 08:41PM CUT coverage: 96.473%. Remained the same
8040637088

push

github

web-flow
Merge pull request #162 from marcomontalbano/add-overrides-for-axios

Add `overrides` for axios

236 of 258 branches covered (91.47%)

Branch coverage included in aggregate %.

612 of 621 relevant lines covered (98.55%)

17.9 hits per line

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

97.83
/packages/core/src/lib/utils.ts
1
import axios from 'axios';
1✔
2
import * as FigmaExport from '@figma-export/types';
3

4
export const toArray = <T>(any: T | T[]): T[] => (Array.isArray(any) ? any : [any]);
7✔
5

6
export const emptySvg = '<svg></svg>';
1✔
7

8
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9
export const fromEntries = (iterable: any[][]): { [key: string]: any } => {
1✔
10
    return [...iterable].reduce((obj: { [key: string]: unknown }, [key, val]) => {
15✔
11
        // eslint-disable-next-line no-param-reassign
12
        obj[key] = val;
39✔
13
        return obj;
39✔
14
    }, {});
15
};
16

17
// eslint-disable-next-line @typescript-eslint/ban-types
18
export const promiseSequentially = (promiseFactories: Function[], initialValue: unknown): Promise<unknown> => {
1✔
19
    const promise = promiseFactories.reduce((previousPromise, promiseFactory) => {
38✔
20
        return previousPromise.then((value) => promiseFactory(value));
20✔
21
    }, Promise.resolve(initialValue));
22

23
    return promise;
38✔
24
};
25

26
export const chunk = <T>(array: T[], perChunk: number): T[][] => {
1✔
27
    return array.reduce((all: T[][], one, i) => {
20✔
28
        const ch = Math.floor(i / perChunk);
47✔
29

30
        // eslint-disable-next-line no-param-reassign
31
        all[ch] = [...(all[ch] || []), one];
47✔
32
        return all;
47✔
33
    }, []);
34
};
35

36
export const fetchAsSvgXml = (url: string): Promise<string> => {
1✔
37
    if (!/https?:\/\/.*/.test(url)) {
42✔
38
        throw new TypeError('Only absolute URLs are supported');
1✔
39
    }
40

41
    return axios.get(url, {
41✔
42
        headers: {
43
            'Content-Type': 'images/svg+xml',
44
        },
45
    }).then((response) => {
46
        if (response.data.length === 0) return emptySvg;
38!
47

48
        return response.data;
38✔
49
    }).catch((error: Error) => {
50
        throw new Error(`while fetching svg "${url}": ${error.message}`);
3✔
51
    });
52
};
53

54
/**
55
 * Check whether the provided value is `undefined` or `null`. It this case it will return `false`.
56
 *
57
 * Useful when you need to exclude nullish values from a list.
58
 * @example [23, null, null, 'John', undefined].filter(notNullish) //= [23, 'John']
59
 */
60
export const notNullish = <T>(value: T | null | undefined): value is T => {
1✔
61
    return value !== null && value !== undefined;
249✔
62
};
63

64
/**
65
 * Check whether the given string is not empty.
66
 *
67
 * Useful when you need to exclude empty strings from a list.
68
 * @example ['', 'John'].filter(notEmptyString) //= ['John']
69
 */
70
export function notEmptyString(value: string): boolean {
1✔
71
    return value.trim() !== '';
20✔
72
}
73

74
export type PickOption<T extends FigmaExport.ComponentsCommand | FigmaExport.StylesCommand, K extends keyof Parameters<T>[0]> =
75
    Pick<Parameters<T>[0], K>
76

77
/**
78
 * Sanitize an array by converting it to a not nullish and not empty string array.
79
 */
80
export function forceArray(maybeArray: string[] | undefined) {
1✔
81
    return (maybeArray ?? [])
108✔
82
        .filter((v) => notNullish(v) && notEmptyString(v));
14✔
83
}
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

© 2025 Coveralls, Inc