topics/find-actual-activation-date/index.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 57
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 31
mnd 5
bc 5
fnc 2
dl 0
loc 57
rs 10
bpm 2.5
cpm 3.5
noi 0
c 0
b 0
f 0
1
const expiredPeriod = 30 * 24 * 60 * 60 * 1000; // 30 days
2
/**
3
 * 
4
 * @param {Array} data
5
 * @return {object}
6
 */
7
const findActualActivationDate = (data) => {
8
    // put your code here to address problems
9
    if (!data || data.length === 0) {
10
        return {};
11
    }
12
13
    let i = data.length - 1;
14
    let result = {
15
        PHONE_NUMBER: data[i].phone,
16
        REAL_ACTIVATIONDATE: data[i].activation
17
    };
18
    while (i > 0) {
19
        if (new Date(data[i].activation) - new Date(data[i - 1].deactivation) >= expiredPeriod) {
20
            let result = {
21
                PHONE_NUMBER: data[i].phone,
22
                REAL_ACTIVATIONDATE: data[i].activation
23
            };
24
            result.REAL_ACTIVATIONDATE = data[i].activation;
25
            return result;
26
        }
27
        i--;
28
    }
29
    // actualActivation date is the first activation date
30
    result.REAL_ACTIVATIONDATE = data[i].activation;
31
    return result;
32
}
33
34
/**
35
 * 
36
 * @param {Array} data
37
 * @return {boolean}
38
 */
39
const validateData = (data) => {
40
    if (!data || data.length === 0) {
41
        return false;
42
    }
43
    let numberRegex = /^\d+$/;
44
    let dateRegex = /^\d{4}-\d{2}-\d{2}$/;
45
    let activation = data[1] || ''; // not later than deactivation
46
    let deactivation = data[2] || ''; // can be empty
47
    if (deactivation.length) {
48
        return numberRegex.test(data[0] || '') && dateRegex.test(activation) && dateRegex.test(deactivation) && (deactivation >= activation);
49
    }
50
    return numberRegex.test(data[0] || '') && dateRegex.test(activation);
51
52
}
53
54
module.exports = {
55
    findActualActivationDate,
56
    validateData
57
};