Passed
Push — master ( 47dd3c...36b76c )
by Sungyub
03:04 queued 18s
created

src/utils/index.ts   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 65
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 47
mnd 4
bc 4
fnc 0
dl 0
loc 65
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { DotenvConfigOutput } from 'dotenv'
2
import Wallet from '../models/wallet'
3
4
const toJson = (str: string): Wallet | { result: string } => {
5
  try {
6
    return JSON.parse(str)
7
  } catch (e) {
8
    return { result: str }
9
  }
10
}
11
12
const hasWallet = (target: object | { value: string }): boolean => {
13
  return (
14
    !Object.prototype.hasOwnProperty.call(target, 'value') &&
15
    Object.prototype.hasOwnProperty.call(target, 'member_wallet')
16
  )
17
}
18
19
const addNullToBuffer = (
20
  channelName: string,
21
  chaincodeId: string,
22
  walletId: string,
23
): Buffer => {
24
  return Buffer.from(
25
    `${ channelName }\u0000${ chaincodeId }\u0000${ walletId }`,
26
    'utf8',
27
  )
28
}
29
30
const removeUselessToString = (str: string): string => {
31
  const pattern = '\u0000\n\u0003\u0001\u0004\u0000\u0012�\u0004'
32
  const indexOfBrace = str.indexOf('{')
33
34
  if (str.startsWith(pattern)) {
35
    return str.replace(pattern, '')
36
  }
37
  if (indexOfBrace > 0) {
38
    return str.slice(indexOfBrace, str.length)
39
  }
40
41
  return str
42
}
43
44
const isEnvFile = (env: DotenvConfigOutput): boolean => {
45
  if (env.error) {
46
    console.error(`
47
      ".env" file is required.
48
      No env file in 
49
      ${ process.cwd() }
50
      
51
      Exiting...
52
      `)
53
    return false
54
  }
55
  return true
56
}
57
58
export {
59
  toJson,
60
  hasWallet,
61
  addNullToBuffer,
62
  removeUselessToString,
63
  isEnvFile,
64
}
65