Stryker

get-data.js - Stryker report

File / Directory
Mutation score
# Killed
# Survived
# Timeout
# No coverage
# Runtime errors
# Transpile errors
Total detected
Total undetected
Total mutants
get-data.js
91.80 %
91.80 56 5 0 0 0 0 56 5 61
Expand all
import deepmerge from './deepmerge';
import { convertNameToPath } from './convert-data';

/**
 * Create object by path and value
 * @param {Array} path - path array
 * @param {string|FileList} value - input value
 * @returns {object} - data object
 */
export function createObject(path, value) 0{
    const segment = path[0];

    if (1234!segment || 567segment.length === 0) 8{
        return value;
    }

    if (91011segment === 12'[]') 13{
        return 14[createObject(path.slice(1), value)];
    }

    return 15{ [segment]: createObject(path.slice(1), value) };
}

/**
 * Get value from data object by path
 * @param {Array} path - value path
 * @param {object} data - data object
 * @returns {string} - value
 */
export function getValueByPath(path, data) 16{
    return path.reduce((value, segment) => (171819segment && value ? value[segment] : value), 202122data || 23'');
}

/**
 * Get value from data object by name
 * @param {string} name - input name
 * @param {object} data - data object
 */
export function getValueByName(name, data) 24{
    const path = convertNameToPath(name);
    return getValueByPath(path, data);
}

/**
 * Get value from radio group
 * @param {Array} inputs - array of radio inputs
 * @returns {string|Array.<string>} - value of checked input
 */
export function getRadioGroupValue(inputs) 25{
    const values = 26[...inputs].map(radio => getInputValue(radio)).filter(Boolean);

    return 27282930values.length > 1 ? values : values[0];
}

/**
 * Get form input value
 * @param {Element|HTMLInputElement|HTMLSelectElement|Array} input - input
 * @returns {string|Date|FileList|Array} - value of input, or array of values if input is select
 */
export function getInputValue(input) 31{
    if (323334!input) return 35'';

    const { type } = input;

    if (3637Array.isArray(input)) 38{
        return getRadioGroupValue(input);
    }

    switch (type) {
        39case 40'select-one':
            return 414243input.options && input.options[input.selectedIndex] ? input.options[input.selectedIndex].value : 44'';
        45case 46'select-multiple':
            return 47[...input.options].filter(option => option.selected).map(option => option.value);
        case 48'checkbox':
        49case 50'radio':
            return input.checked ? input.value : 51'';
        52case 53'file':
            return input.files;
        54case 55'date':
            return input.value ? new Date(input.value) : 56'';
        57default:
            return input.value;
    }
}

/**
 * Get name from input or array of inputs
 * @param {HTMLInputElement|Array} input - input element or Array of HTMLInputElements
 * @returns {string} - input name
 */
export function getInputName(input) 58{
    return Array.isArray(input) ? input[0].name : input.name;
}

/**
 * Get input value as an object keyed by input name
 * @param {HTMLInputElement|Array} input - input element or Array of HTMLInputElements
 * @returns {object} - data
 */
export function getInputData(input) 59{
    const value = getInputValue(input);
    const name = getInputName(input);
    const path = convertNameToPath(name);

    return createObject(path, value);
}

/**
 * Get data object with values from inputs object
 * @param {object} inputs - inputs object
 * @returns {object} - data object
 */
export function getData(inputs) 60{
    return Object.keys(inputs).reduce((data, name) => deepmerge(data, getInputData(inputs[name])), {});
}
# Mutator State Location Original Replacement
0 Block Killed 9 : 42 { ... }; } {}
1 IfStatement Killed 12 : 8 ! ... ===
2 IfStatement Killed 12 : 8 ! ... ===
3 BinaryExpression Killed 12 : 8 ! ... === ! ... ===
4 PrefixUnaryExpression Killed 12 : 8 !
5 BinaryExpression Killed 12 : 20 . === . !==
6 ConditionalExpression Survived 12 : 20 . ===
7 ConditionalExpression Killed 12 : 20 . ===
8 Block Killed 12 : 42 { ... } {}
9 IfStatement Killed 16 : 8 === '[]'
10 IfStatement Killed 16 : 8 === '[]'
11 BinaryExpression Killed 16 : 8 === '[]' !== '[]'
12 StringLiteral Killed 16 : 20 '[]' ""
13 Block Killed 16 : 26 { ... } {}
14 ArrayLiteral Killed 17 : 15 [ ... )] []
15 ObjectLiteral Killed 20 : 11 { [ ... ) } {}
16 Block Killed 29 : 43 { ...'); } {}
17 BinaryExpression Killed 30 : 44 && ||
18 ConditionalExpression Killed 30 : 44 &&
19 ConditionalExpression Killed 30 : 44 &&
20 BinaryExpression Killed 30 : 88 || '' && ''
21 ConditionalExpression Killed 30 : 88 || ''
22 ConditionalExpression Killed 30 : 88 || ''
23 StringLiteral Killed 30 : 96 '' " ... !"
24 Block Killed 38 : 43 { ... ); } {}
25 Block Killed 48 : 43 { ... ]; } {}
26 ArrayLiteral Killed 49 : 19 [... ] []
27 BinaryExpression Killed 51 : 11 . > . >=
28 BinaryExpression Killed 51 : 11 . > . <=
29 ConditionalExpression Killed 51 : 11 . >
30 ConditionalExpression Killed 51 : 11 . >
31 Block Killed 59 : 37 { ... } } {}
32 IfStatement Killed 60 : 8 !
33 IfStatement Killed 60 : 8 !
34 PrefixUnaryExpression Killed 60 : 8 !
35 StringLiteral Killed 60 : 23 '' " ... !"
36 IfStatement Killed 64 : 8 . ( )
37 IfStatement Killed 64 : 8 . ( )
38 Block Killed 64 : 30 { ... } {}
39 SwitchCase Killed 69 : 8 ' ...: ''; ' - ':
40 StringLiteral Survived 69 : 13 ' - ' ""
41 ConditionalExpression Killed 70 : 19 . ... ]
42 BinaryExpression Killed 70 : 19 . ... ] . ... ]
43 ConditionalExpression Killed 70 : 19 . ... ]
44 StringLiteral Killed 70 : 116 '' " ... !"
45 SwitchCase Killed 71 : 8 ' ... ); ' ... ':
46 StringLiteral Killed 71 : 13 ' ... ' ""
47 ArrayLiteral Killed 72 : 19 [... ... ] []
48 StringLiteral Survived 73 : 13 ' ' ""
49 SwitchCase Killed 74 : 8 ' ...: ''; ' ':
50 StringLiteral Killed 74 : 13 ' ' ""
51 StringLiteral Killed 75 : 49 '' " ... !"
52 SwitchCase Survived 76 : 8 ' ... ; ' ':
53 StringLiteral Survived 76 : 13 ' ' ""
54 SwitchCase Killed 78 : 8 ' ...: ''; ' ':
55 StringLiteral Killed 78 : 13 ' ' ""
56 StringLiteral Killed 79 : 57 '' " ... !"
57 SwitchCase Killed 80 : 8 : ... ; :
58 Block Killed 90 : 36 { ... ; } {}
59 Block Killed 99 : 36 { ... ); } {}
60 Block Killed 112 : 32 { ...}); } {}