-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e10fe02
commit b4df105
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@holochain-open-dev/utils", | ||
"version": "0.15.1", | ||
"version": "0.15.2", | ||
"description": "Common utilities to build Holochain web applications", | ||
"author": "[email protected]", | ||
"main": "dist/index.js", | ||
|
@@ -12,7 +12,8 @@ | |
"url": "git+https://github.com/holochain-open-dev/common.git" | ||
}, | ||
"exports": { | ||
".": "./dist/index.js" | ||
".": "./dist/index.js", | ||
"./dist/*": "./dist/*" | ||
}, | ||
"scripts": { | ||
"build": "npm run lint && tsc --incremental", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Deeply convert nulls into undefineds, and buffers into Uint8Array | ||
* | ||
* When msgpack is used to decode() in nodejs, it returns hashes as buffers | ||
* and nulls instead of undefineds. | ||
* This is mostly fine, except in tryorama tests when using deepEqual. This function | ||
* is useful in that case, to allow objects constructed in the tests to deeply equal | ||
* the return of calls to @holochain/client | ||
*/ | ||
export function cleanNodeDecoding(object: any): any { | ||
return deepMap(object, (value) => { | ||
if (Buffer.isBuffer(value)) return new Uint8Array(value); | ||
if (value === null) return undefined; | ||
return value; | ||
}); | ||
} | ||
|
||
function mapObject(obj, fn) { | ||
return Object.keys(obj).reduce((res, key) => { | ||
res[key] = fn(obj[key]); | ||
return res; | ||
}, {}); | ||
} | ||
|
||
function deepMap(obj, fn) { | ||
const deepMapper = (val) => | ||
typeof val === "object" && !Buffer.isBuffer(val) | ||
? deepMap(val, fn) | ||
: fn(val); | ||
if (Array.isArray(obj)) { | ||
return obj.map(deepMapper); | ||
} | ||
if (obj === null) { | ||
return fn(obj); | ||
} else if (typeof obj === "object") { | ||
return mapObject(obj, deepMapper); | ||
} | ||
return obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { encodeHashToBase64 } from "@holochain/client"; | ||
import { test, assert } from "vitest"; | ||
import { cleanNodeDecoding } from "../src/clean-node-decoding.js"; | ||
|
||
test("test clean", async () => { | ||
const hi = { | ||
hi: null, | ||
bu: Buffer.from(new Uint8Array([1, 1, 1])), | ||
}; | ||
assert.deepEqual(cleanNodeDecoding(hi), { | ||
hi: undefined, | ||
bu: new Uint8Array([1, 1, 1]), | ||
}); | ||
}); |