Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Fix mutability issue in addHexPrefix and removeHexPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
s1na committed Sep 18, 2019
1 parent e028024 commit 63b49a3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/util/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
* @returns {Array} - returns buffer of encoded data
**/
export function addHexPrefix(key: number[], terminator: boolean): number[] {
const res = key.slice()
// odd
if (key.length % 2) {
key.unshift(1)
if (res.length % 2) {
res.unshift(1)
} else {
// even
key.unshift(0)
key.unshift(0)
res.unshift(0)
res.unshift(0)
}

if (terminator) {
key[0] += 2
res[0] += 2
}

return key
return res
}

/**
Expand All @@ -28,13 +29,15 @@ export function addHexPrefix(key: number[], terminator: boolean): number[] {
* @private
*/
export function removeHexPrefix(val: number[]): number[] {
if (val[0] % 2) {
val = val.slice(1)
let res = val.slice()

if (res[0] % 2) {
res = val.slice(1)
} else {
val = val.slice(2)
res = val.slice(2)
}

return val
return res
}

/**
Expand Down

0 comments on commit 63b49a3

Please sign in to comment.