Issues (13)

src/utils.js (1 issue)

1
import { ASCII_RANGES } from './constants';
2
3
export function isASCII(type, code) {
4
    const { min, max } = ASCII_RANGES[type];
5
6
    return code <= max && code >= min;
7
}
8
9
export class Mask {
10
    constructor(pattern, formatters) {
11
        this.pattern = pattern;
12
        this.FormatChars = formatters;
13
        this.parse();
14
    }
15
16
    parse() {
17
        this._mask = [];
18
19
        let isNextEscaped = false;
20
21
        let  isNextOpional = false;
22
23
        for (const symbol of this.pattern) {
24
            if (isNextEscaped) {
25
                this._mask.push({
26
                    type     : 'constant',
27
                    optional : isNextOpional,
28
                    symbol
29
                });
30
                isNextEscaped = isNextOpional = false;
31
                continue;
32
            }
33
34
            if (symbol === '\\') {
35
                isNextEscaped = true;
36
                continue;
37
            }
38
39
            if (symbol === '?') {
40
                isNextOpional = true;
41
                continue;
42
            }
43
44
            const format = this.FormatChars[symbol];
45
46
            if (format) {
47
                this._mask.push({
48
                    type     : 'format',
49
                    format,
50
                    optional : isNextOpional
51
                });
52
                isNextOpional = false;
53
54
                continue;
55
            }
56
57
            isNextOpional = false;
58
59
            this._mask.push({
60
                type     : 'constant',
61
                symbol,
62
                optional : isNextOpional
63
            });
64
        }
65
    }
66
    validate(input) {
67
        let lastValidated = -1;
68
69
        for (const check of this._mask) {
70
            const toValidateIndex = lastValidated + 1;
71
            const value = input[toValidateIndex];
72
73
            let isValid = false;
74
75
            if (check.type === 'constant') isValid = check.symbol === value;
76
            if (check.type === 'format') isValid = check.format(value);
77
78
            if (!isValid) {
79
                if (!check.optional) return { check, value };
80
                continue;
81
            }
82
83
            lastValidated = toValidateIndex;
84
        }
85
86
        if (lastValidated !== input.length - 1) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if lastValidated !== input.length - 1 is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
87
            return {
88
                check : null,
89
                value : input[lastValidated + 1]
90
            };
91
        }
92
    }
93
}
94
95
/* eslint-disable no-param-reassign */
96
// TODO: move to myrmidon
97
export function setProp(obj, prop, value, { delimiter = '.' } = {}) {
98
    let current = obj;
99
100
    prop.split(delimiter).forEach((token, index, tokens) => {
101
        const isLast = index === tokens.length - 1;
102
103
        if (!(token in current)) {
104
            if (isLast) return current[token] = value;
105
            const nextToken = tokens[index + 1];
106
            const isIndex = Number.isInteger(+nextToken);
107
108
            current[token] = isIndex ? [] : {};
109
        }
110
111
        current = current[token];
112
    });
113
}
114
/* eslint-enable no-param-reassign */
115