Stryker

convert-data.js - Stryker report

File / Directory
Mutation score
# Killed
# Survived
# Timeout
# No coverage
# Runtime errors
# Transpile errors
Total detected
Total undetected
Total mutants
convert-data.js
93.65 %
93.65 55 4 4 0 0 0 59 4 63
Expand all
/**
 * Create query portion of url for serialize method
 * @param {string} name
 * @param {object|Array|string} data
 * @returns {string} - part of url
 */
export function getQueryPart(name, data) 0{
    if (12Array.isArray(data)) 3{
        return data.reduce((part, value) => 4part + getQueryPart(5`${name}[]`, value), 6'');
    }

    if (789typeof data === 10'object') 11{
        return Object.keys(data).reduce((part, index) => 12part + getQueryPart(13`${name}[${index}]`, data[index]), 14'');
    }

    return 15`${name}=${encodeURIComponent(data)}&`;
}

/**
 * Name regexp for conversion to path
 * @type {RegExp}
 */
const NAME = /(\[(\w*)\]|\w*)/gi;

/**
 * Convert input name to path array
 * @param {string} name - input name
 * @returns {Array} - path to value in data object
 */
export function convertNameToPath(name) 16{
    const path = [];

    let matches = NAME.exec(name);
    while (1718matches !== null) 19{
        if (202122matches.index === NAME.lastIndex) 23{
            NAME.lastIndex += 1;
        }

        path.push(242526matches[2] || matches[1]);

        matches = NAME.exec(name);
    }

    return path;
}

/**
 * Convert data object to a value for sending
 * @param {object} data - data object
 * @param {string} type - type of conversion
 * @param {function} FileListType
 * @param {function} FormDataType
 * @returns {string|FormData} - output value
 */
export function convertData(data, type, FileListType = FileList, FormDataType = FormData) 27{
    let convertedData;

    switch (type) {
        28case 29'serialize':
            convertedData = Object.keys(data).reduce((query, name) => 30`${query}${getQueryPart(name, data[name])}`, 31'');
            return convertedData.length ? convertedData.slice(0, 32-1) : 33'';
        34case 35'formData':
            return Object.keys(data).reduce((formData, name) => 36{
                if (3738data[name] instanceof FileListType) 39{
                    if (40414243data[name].length > 1) 44{
                        for (let i = 0; 454647i < data[name].length; i += 1) 48{
                            formData.append(49`${name}[${i}]`, data[name][i]);
                        }
                    } else if (505152data[name].length === 1) 53{
                        formData.append(name, data[name][0]);
                    }
                } else if (545556typeof data[name] === 57'object') 58{
                    Object.keys(data[name]).forEach(key => formData.append(59`${name}[${key}]`, data[name][key]));
                } else 60{
                    formData.append(name, data[name]);
                }

                return formData;
            }, new FormDataType());
        case 61'json':
        62default:
            return JSON.stringify(data);
    }
}
# Mutator State Location Original Replacement
0 Block Killed 6 : 41 { ...&`; } {}
1 IfStatement Killed 7 : 8 . ( )
2 IfStatement Killed 7 : 8 . ( )
3 Block Killed 7 : 29 { ... } {}
4 BinaryExpression Killed 8 : 44 + ... ) - ... )
5 StringLiteral Killed 8 : 64 `${ }[]` ""
6 StringLiteral Killed 8 : 85 '' " ... !"
7 IfStatement Killed 11 : 8 === ' '
8 IfStatement Killed 11 : 8 === ' '
9 BinaryExpression Killed 11 : 8 === ' ' !== ' '
10 StringLiteral Killed 11 : 24 ' ' ""
11 Block Killed 11 : 34 { ... } {}
12 BinaryExpression Killed 12 : 57 + ... ]) - ... ])
13 StringLiteral Killed 12 : 77 `${ ... }]` ""
14 StringLiteral Killed 12 : 112 '' " ... !"
15 StringLiteral Killed 15 : 11 `${ ... )}&` ""
16 Block Killed 29 : 40 { ... ; } {}
17 WhileStatement Killed 33 : 11 !==
18 BinaryExpression Killed 33 : 11 !== ===
19 Block TimedOut 33 : 29 { ... } {}
20 IfStatement TimedOut 34 : 12 . ... .
21 IfStatement Survived 34 : 12 . ... .
22 BinaryExpression TimedOut 34 : 12 . ... . . ... .
23 Block TimedOut 34 : 46 { ... } {}
24 BinaryExpression Killed 38 : 18 [ ] |... [ ] [ ] &... [ ]
25 ConditionalExpression Killed 38 : 18 [ ] |... [ ]
26 ConditionalExpression Killed 38 : 18 [ ] |... [ ]
27 Block Killed 54 : 90 { ... } } {}
28 SwitchCase Killed 58 : 8 ' ...: ''; ' ':
29 StringLiteral Killed 58 : 13 ' ' ""
30 StringLiteral Killed 59 : 70 `${ ... ])}` ""
31 StringLiteral Killed 59 : 115 '' " ... !"
32 PrefixUnaryExpression Killed 60 : 65 - +
33 StringLiteral Killed 60 : 71 '' " ... !"
34 SwitchCase Killed 61 : 8 ' ... ()); ' ':
35 StringLiteral Killed 61 : 13 ' ' ""
36 Block Killed 62 : 64 { ... } {}
37 IfStatement Killed 63 : 20 [ ]
38 IfStatement Killed 63 : 20 [ ]
39 Block Killed 63 : 56 { ... } {}
40 IfStatement Killed 64 : 24 [ ]. >
41 IfStatement Killed 64 : 24 [ ]. >
42 BinaryExpression Killed 64 : 24 [ ]. > [ ... >=
43 BinaryExpression Killed 64 : 24 [ ]. > [ ... <=
44 Block Killed 64 : 47 { ... } {}
45 ForStatement Killed 65 : 40 < [ ].
46 BinaryExpression Survived 65 : 40 < [ ]. <= [ ].
47 BinaryExpression Killed 65 : 40 < [ ]. >= [ ].
48 Block Killed 65 : 71 { ... } {}
49 StringLiteral Killed 66 : 44 `${ }[${ }]` ""
50 IfStatement Killed 68 : 31 [ ... ===
51 IfStatement Survived 68 : 31 [ ... ===
52 BinaryExpression Killed 68 : 31 [ ... === [ ... !==
53 Block Killed 68 : 56 { ... } {}
54 IfStatement Killed 71 : 27 [ ... '
55 IfStatement Killed 71 : 27 [ ... '
56 BinaryExpression Killed 71 : 27 [ ... ' [ ... '
57 StringLiteral Killed 71 : 49 ' ' ""
58 Block Killed 71 : 59 { ... } {}
59 StringLiteral Killed 72 : 75 `${ ... }]` ""
60 Block Killed 73 : 23 { ... } {}
61 StringLiteral Survived 79 : 13 ' ' ""
62 SwitchCase Killed 80 : 8 : ... ); :