Passed
Push — master ( fe9289...f10e30 )
by Dmytro
02:11
created

utils.js ➔ setProp   A

Complexity

Conditions 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
c 0
b 0
f 0
rs 9.3333
cc 5
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) {
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;
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];
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
112
    });
113
}
114
/* eslint-enable no-param-reassign */
115