app/helpers/general.js   A
last analyzed

Complexity

Total Complexity 14
Complexity/F 2.33

Size

Lines of Code 66
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 14
eloc 39
c 1
b 0
f 0
nc 20
mnd 3
bc 14
fnc 6
dl 0
loc 66
rs 10
bpm 2.3333
cpm 2.3333
noi 5

5 Functions

Rating   Name   Duplication   Size   Complexity  
A module.exports.checkObject 0 16 2
A module.exports.today 0 4 3
A module.exports.versionCompare 0 17 4
A module.exports.isEmpty 0 4 1
A module.exports.bindec 0 11 2
1
module.exports = {
2
3
    isEmpty: async function(value) {
4
        return (Array.isArray(value) && value.length === 0) ||
5
            (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0)
6
    },
7
8
    /**
9
     * 二进制转十进制
10
     */
11
    bindec: async function(binNum) {
12
        let result = 0
13
        let total = binNum.length
14
        for (let index = 1; index <= total; index++) {
15
            let tmp = binNum.slice(total - index, total - index + 1)
16
            let tmpAdd = parseInt(tmp) * parseInt(Math.pow(2, index - 1))
17
            result += tmpAdd
18
        }
19
20
        return result
21
    },
22
23
    /**
24
     * 
25
     */
26
    today: async function(symbol = '') {
27
        let date = new Date()
28
        return date.getFullYear() + symbol + (date.getMonth() < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + symbol + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
29
    },
30
31
    versionCompare: function(v1, v2) {
32
        let version1pre = parseFloat(v1)
33
        let version2pre = parseFloat(v2)
34
        let version1next = v1.replace(version1pre + ".", "")
35
        let version2next = v2.replace(version2pre + ".", "")
36
        if (version1pre > version2pre) {
37
            return true;
38
        } else if (version1pre < version2pre) {
39
            return false;
40
        } else {
41
            if (version1next >= version2next) {
42
                return true;
43
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
44
                return false;
45
            }
46
        }
47
    },
48
49
    checkObject: function(object, fields, tips) {
50
        let result = {}
51
        if (!UnderScore.isObject(object)) {
0 ignored issues
show
Bug introduced by
The variable UnderScore seems to be never declared. If this is a global, consider adding a /** global: UnderScore */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
            console.log('not object')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
53
            object = JSON.parse(object)
54
        }
55
56
        fields.forEach(element => {
57
            if (UnderScore.has(object, element) !== true) {
0 ignored issues
show
Bug introduced by
The variable UnderScore seems to be never declared. If this is a global, consider adding a /** global: UnderScore */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
58
                throw new ApiError('validate.error', tips + ' 对象缺 ' + element + ' 属性')
0 ignored issues
show
Bug introduced by
The variable ApiError seems to be never declared. If this is a global, consider adding a /** global: ApiError */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
            }
60
            result[element] = object[element]
61
        })
62
63
        return result
64
    },
65
66
}