1.0.0 • Published 2 months ago

@squeep/translated-keyed-collections v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 months ago

@squeep/translated-keyed-collections

Provide augmented Set and Map classes which translate keys before they are stored or referenced, allowing for such things as a Set of Buffer contents rather than a Set of Buffer object references.

The included BufferKeyTranslator provides an implementation which serializes Buffers (or hex-strings) to hex-strings for the internal representation, and returns the de-hexed Buffers.

API

  • TranslatedSetFactory({ toKey, fromKey, toStringPrefix })
    Returns a class which extends Set to wrap all incoming and outgoing values with toKey() and fromKey(), respectively. The optional toStringPrefix string affects the toString() rendering of the class.

    const { BufferKeyTranslator, TranslatedSetFactory } = require('@squeep/translated-keyed-collections');
    const BufferSet = TranslatedSetFactory(BufferKeyTranslator);
    
    const bs = new BufferSet();
    
    const b1 = Buffer.from('squeep');
    const b2 = Buffer.from('squeep');
    
    bs.add(b1);
    bs.add(b2);
    
    console.log(bs.size); // 1
  • TranslatedMapFactory({ toKey, fromKey, toStringPrefix }) Returns a class which extends Map to wrap all incoming and outgoing keys with toKey() and fromKey(), respectively. The optional toStringPrefix string affects the toString() rendering of the class.

    const { BufferKeyTranslator, TranslatedMapFactory } = require('@squeep/translated-keyed-collections');
    const BufferMap = TranslatedMapFactory(BufferKeyTranslator);
    
    const bm = new BufferMap();
    
    const b1 = Buffer.from('squeep');
    const b2 = Buffer.from('squeep');
    
    bm.set(b1, 'first');
    bm.set(b2, 'second');
    
    console.log(bm.get(b1)); // 'second'
1.0.0

2 months ago