Issues (13)

src/utils.js (4 issues)

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;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
76
            if (check.type === 'format') isValid = check.format(value);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
77
78
            if (!isValid) {
79
                if (!check.optional) return { check, value };
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
80
                continue;
81
            }
82
83
            lastValidated = toValidateIndex;
84
        }
85
86
        if (lastValidated !== input.length - 1) {
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;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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