npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tnesh-stack/utils

v0.400.0

Published

Common utilities to build Holochain web applications

Downloads

679

Readme

@tnesh-stack/utils

Utilities to build Holochain web applications.

HoloHashMap

Map of HoloHash to any JS object.

We can't really use well normal JS objects to index by holo hashes because we lose the ability to compare hashes together. Namely, in JS console.log(new Uint8Array([1]) == new Uint8Array([1])) prints false.

import { AgentPubKey } from '@holochain/client';
import { HoloHashMap } from '@tnesh-stack/utils';

// Imagine we have out public key
const myAgentPubKey = appInfo.cell_info[0].cell_id[1];

const map = new HoloHashMap<AgentPubKey, number>();

// We can add entries to the dictionary
map.set(myAgentPubKey, 1);

// Get the value for an entry
console.log(map.get(myAgentPubKey));                // Will print `1`

// Check if the key exists
console.log(map.has(myAgentPubKey));                // Will print `true`

// Get iterators for the entries

console.log(Array.from(map.keys()));                // Will print an array with MYAGENTPUBKEY as the only member
console.log(Array.from(map.values()));              // Will print `[1]`
console.log(Array.from(map.entries()));             // Will print an array with `[MYAGENTPUBKEY, 1]` as the only member

map.delete(myAgentPubKey);                          // Will delete this member

Some variants exist for this type:

  • EntryHashMap
  • ActionHashMap
  • AgentPubKeyMap
  • DnaHashMap

MemoHoloHashMap

This is an special kind of map, where there is no set function, only get.

Instead, a callback function is passed as the constructor. Then, whenever get is called, if it's the first time that the given hash is requested, it will call the callback and initialize the value of the hash with its result. If that hash was already initialized, it will just return that value.

import { AgentPubKey, fakeAgentPubKey } from '@holochain/client';
import { MemoHoloHashMap } from '@tnesh-stack/utils';

const pubKey = fakeAgentPubKey();

// Imagine we want to fetch the profile of the agent whenever an agent public key is requested
const memoMap = new MemoHoloHashMap((agent: AgentPubKey) => callZome('get_profile', agent));

console.log(memoMap.get(pubKey)); // Will print a pending promise

// After the request has finished...
await memoMap.get(pubKey);

console.log(memoMap.get(pubKey)); // Will print a completed promise with the value

EntryRecord

Utility to type a single Record and extract useful information from them.

import { Record } from '@holochain/client';
import { EntryRecord } from '@tnesh-stack/utils';

// Imagine a zome function that returns a record,
// but we know its entry type
const record: Record = await callZome(...);             

// Then we can type it
const profileRecord = new EntryRecord<Profile>(record); 

// Access its entry easily
const profile: Profile = profileRecord.entry;           

// Access its action easily, timestamp will be in milliseconds
const action: Action = profileRecord.action;            

// Access its entry hash easily
const entryHash: EntryHash = profileRecord.entryHash;   

// Access its action hash easily
const actionHash: ActionHash = profileRecord.actionHash;

RecordBag

Utility to type a list of Records and extract useful information from them.

import { Record } from '@holochain/client';
import { RecordBag } from '@tnesh-stack/utils';

// Imagine a zome function that returns a list of records,
// but we know their entry type
const records: Record[] = await callZome(...);          

// Then we can type it
const profiles = new RecordBag<Profile>(records);

// Map of entry hash -> entry
const profileEntries: EntryHashMap<Profile> = profiles.entryMap; 

// Map of action hash -> action
// Timestamps are in milliseconds
const profileActions: ActionHashMap<Action> = profiles.actionMap;

// Map of entry hash -> all the actions that have created or updated to that entry
const entryActions: EntryHashMap<ActionHash[]> = profiles.entryActions;

// For each agent, contains all the actions it has authored
const authorMap: AgentPubKeyMap<ActionHash[]> = profiles.authorMap;

// Get the array of all records
const profileEntries: Array<EntryRecord<Profile>> = profiles.entryRecords;