forked from ethereum/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiHelper.js
86 lines (79 loc) · 2.31 KB
/
uiHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict'
module.exports = {
formatMemory: function (mem, width) {
const ret = {}
if (!mem) {
return ret
}
if (!mem.substr) {
mem = mem.join('') // geth returns an array, eth return raw string
}
for (let k = 0; k < mem.length; k += (width * 2)) {
const memory = mem.substr(k, width * 2)
const content = this.tryConvertAsciiFormat(memory)
ret['0x' + (k / 2).toString(16)] = content.raw + '\t' + content.ascii
}
return ret
},
tryConvertAsciiFormat: function (memorySlot) {
const ret = { ascii: '', raw: '' }
for (let k = 0; k < memorySlot.length; k += 2) {
const raw = memorySlot.substr(k, 2)
let ascii = String.fromCharCode(parseInt(raw, 16))
ascii = ascii.replace(/[^\w\s]/, '?')
if (ascii === '') {
ascii = '?'
}
ret.ascii += ascii
ret.raw += raw
}
return ret
},
/**
* format @args css1, css2, css3 to css inline style
*
* @param {Object} css1 - css inline declaration
* @param {Object} css2 - css inline declaration
* @param {Object} css3 - css inline declaration
* @param {Object} ...
* @return {String} css inline style
* if the key start with * the value is direcly appended to the inline style (which should be already inline style formatted)
* used if multiple occurences of the same key is needed
*/
formatCss: function (css1, css2) {
let ret = ''
for (let arg in arguments) {
for (let k in arguments[arg]) {
if (arguments[arg][k] && ret.indexOf(k) === -1) {
if (k.indexOf('*') === 0) {
ret += arguments[arg][k]
} else {
ret += k + ':' + arguments[arg][k] + ';'
}
}
}
}
return ret
},
normalizeHex: function (hex) {
if (hex.indexOf('0x') === 0) {
hex = hex.replace('0x', '')
}
hex = hex.replace(/^0+/, '')
return '0x' + hex
},
normalizeHexAddress: function (hex) {
if (hex.indexOf('0x') === 0) hex = hex.replace('0x', '')
if (hex.length >= 40) {
const reg = /(.{40})$/.exec(hex)
if (reg) {
return '0x' + reg[0]
}
} else {
return '0x' + (new Array(40 - hex.length + 1).join('0')) + hex
}
},
runInBrowser: function () {
return typeof window !== 'undefined'
}
}