0.1.2 • Published 2 years ago

transform-once v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

transform-once

build status Coverage Status CSP strict

A basic helper to simplify relations among values. Optionally compatible with WeakValue too.

import transformOnce from 'transform-once';

const $ = transformOnce(value => {
  return {object: Math.random()};
});

// references are transformed once
$(globalThis) === $(globalThis):

// transformed values are returned too
$(globalThis) === $($(globalThis)):

$(globalThis);
// {object: 0.123456789}

Provide WeakRef Ability

If the input needs to be referenced, it is possible to use WeakRef based utility, such as WeakValue to automatically cleanup once the related reference is gone.

import transformOnce from 'transform-once';
import WeakValue from 'weak-value';

// pass as context all needed classes (which are these three)
const transform = transformOnce.bind({WeakMap, WeakSet, WeakValue});

// now create a transformer that works with primitives too
const $ = transform(value => ({[typeof value]: Math.random()}));

$(1) === $(1);
$(1) === $($(1));
$($) === $($($));
$(1);
// {number: 0.123456789}

Alternative, it is also possible to pass a Map instead, but it's up to the program to clear its content, whenever it's necessary.

import transformOnce from 'transform-once';
const transform = transformOnce.bind({WeakMap, WeakSet, WeakValue: Map});

// same as before but primitives are not weakly referenced
const $ = transform(value => ({[typeof value]: Math.random()}));

By default this module does not provide any weakly reference mechanism so each time a primitive is passed along, it will be transformed.