Skip to content

Commit

Permalink
Merge pull request #98 from near/fix-utils
Browse files Browse the repository at this point in the history
Fix inappropriate naming of u8Array<>bytes
  • Loading branch information
volovyks authored Jun 7, 2022
2 parents c3f64df + 0dbf6dc commit dccb7e2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/collections/unordered-map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as near from '../api'
import {u8ArrayToString, stringToU8Array} from '../utils'
import {u8ArrayToBytes, bytesToU8Array} from '../utils'
import { Vector, VectorIterator } from './vector'

const ERR_INCONSISTENT_STATE = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"
Expand Down Expand Up @@ -35,11 +35,11 @@ export class UnorderedMap {
serializeIndex(index) {
let data = new Uint32Array([index])
let array = new Uint8Array(data.buffer)
return u8ArrayToString(array)
return u8ArrayToBytes(array)
}

deserializeIndex(rawIndex) {
let array = stringToU8Array(rawIndex)
let array = bytesToU8Array(rawIndex)
let data = new Uint32Array(array.buffer)
return data[0]
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/unordered-set.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as near from '../api'
import {u8ArrayToString, stringToU8Array} from '../utils'
import {u8ArrayToBytes, bytesToU8Array} from '../utils'
import { Vector } from './vector'

const ERR_INCONSISTENT_STATE = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"
Expand All @@ -23,11 +23,11 @@ export class UnorderedSet {
serializeIndex(index) {
let data = new Uint32Array([index])
let array = new Uint8Array(data.buffer)
return u8ArrayToString(array)
return u8ArrayToBytes(array)
}

deserializeIndex(rawIndex) {
let array = stringToU8Array(rawIndex)
let array = bytesToU8Array(rawIndex)
let data = new Uint32Array(array.buffer)
return data[0]
}
Expand Down
4 changes: 2 additions & 2 deletions src/collections/vector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as near from '../api'
import {u8ArrayToString} from '../utils'
import {u8ArrayToBytes} from '../utils'

const ERR_INDEX_OUT_OF_BOUNDS = "Index out of bounds"
const ERR_INCONSISTENT_STATE = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"
Expand All @@ -23,7 +23,7 @@ export class Vector {
indexToKey(index) {
let data = new Uint32Array([index])
let array = new Uint8Array(data.buffer)
let key = u8ArrayToString(array)
let key = u8ArrayToBytes(array)
return this.prefix + key
}

Expand Down
10 changes: 5 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export function u8ArrayToString(array) {
export function u8ArrayToBytes(array) {
let ret = ''
for (let e of array) {
ret += String.fromCharCode(e)
}
return ret
}

export function stringToU8Array(string) {
let ret = new Uint8Array(string.length)
for (let i in string) {
ret[i] = string.charCodeAt(i)
export function bytesToU8Array(bytes) {
let ret = new Uint8Array(bytes.length)
for (let i in bytes) {
ret[i] = bytes.charCodeAt(i)
}
return ret
}

0 comments on commit dccb7e2

Please sign in to comment.