@squeep/translated-keyed-collections v1.0.0
@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 withtoKey()
andfromKey()
, respectively. The optionaltoStringPrefix
string affects thetoString()
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 withtoKey()
andfromKey()
, respectively. The optionaltoStringPrefix
string affects thetoString()
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'
2 months ago