app/util/uuid.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 28
Function Count 2

Duplication

Duplicated Lines 14
Ratio 50 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 2
eloc 16
c 2
b 0
f 0
nc 1
mnd 0
bc 2
fnc 2
dl 14
loc 28
rs 10
bpm 1
cpm 1
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A module.exports.genOrderNo 0 4 1
A module.exports.uniqueIdOfToday 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
const MULTIPLY_BASE = 1000
2
var General = require('../helpers/general')
3
4
module.exports = {
5
6 View Code Duplication
  uniqueIdOfToday: async function (serverId = 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
    let timebase = new Date(new Date().setHours(0, 0, 0, 0)).getTime()
8
    let time = Date.now() + Math.random()  // 没有锁,增加随机因子
9
10
    let elapse = (time - timebase) * MULTIPLY_BASE            // 阿里云执行保证不重复
11
    let elapseBin = (parseInt(Math.pow(2, 37) - 1) + parseInt(elapse)).toString(2) // 38bit
12
    let serverIdBin = (Math.pow(2, 4) - 1 + serverId).toString(2)     // 5bit // 1 to 2^4-1 ,即最大支持15台服务器
13
    let random = parseInt(Math.random() * (Math.pow(2, 6) - 1 - 1 + 1) + 1, 10)               //
14
    let randomBin = (Math.pow(2, 6) - 1 + random).toString(2)     // 7bit
15
    let resultBin = elapseBin + serverIdBin + randomBin   // 50bit
16
    // let result = await General.bindec(resultBin)
17
    let result = parseInt(resultBin, 2)
18
    return result
19
  },
20
21
  /* 在部署集群时需要修改不同机器的这个编号。范围1-15 */
22
  // 生成订单号
23
  genOrderNo: async function () {
24
25
    return await General.today() + await this.uniqueIdOfToday(1)//AppConfig:: getInstance() -> get('server_id')
26
  },
27
28
}