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

@konfirm/decoy

v2.0.1

Published

Proxy objects, keeping track of mutations to commit/rollback

Downloads

5

Readme

Decoy

Release Tests

Create Proxy objects and keep track of mutations, reflecting them on access and providing the ability to either commit them or roll them back.

A word of caution

Decoy can only keep track of direct property changes, this means that any decoy target that works solely through getters/setters will directly influence the underlying target. A good example is the built-in Date object, which is modified using its setter methods (e.g. setFullYear(2018)).

Installation

Decoy is a scoped package, which means both the installation and require (or import) need the scope along with the package name:

$ npm install --save @konfirm/decoy

Usage

//  require Decoy
import * as Decoy from '@konfirm/decoy';
//  create the original object
const original = { foo: 'bar', baz: { hello: 'world' }};
//  create the decoy dummy for original
const dummy = Decoy.create(original);

console.log(dummy.foo);     //  'bar'
console.log(original.foo);  //  'bar';

dummy.foo = 'I pity the foo!';

console.log(dummy.foo);     //  'I pity the foo!';
console.log(original.foo);  //  'bar';

Decoy.commit(dummy)
	.then((result) => {
		console.log(result === original);  //  true
		console.log(dummy.foo);            //  'I pity the foo!';
		console.log(original.foo);         //  'I pity the foo!';
	});

API

Decoy is a collection of individual functions that work with any object that can to be proxied. All direct property changes are recorded to be truly applied (commit) or reverted (rollback) at a later stage. The created proxy decoys will reflect the changes made.

exports

| name | syntax | description | | ------------ | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | | Decoy | Decoy<object> | TypeScript Type describing a Decoy (which is just an alias for the object provided) | | create | create<T>(target:T, boolean: onlyLastKeyMutations): Decoy<T> | create a new decoy for the target | | checksum | checksum(decoy: Decoy<T>):string | calculate the checksum of the decoy | | isDecoy | isDecoy<T>(target: Decoy<T>):boolean | is the target a known Decoy | | purge | purge<T = object>(decoy: Decoy<T>): T | clean the Decoy (including its nested ones) | | commit | commit<T = object>(decoy: Decoy [, key 1, ..., keyN]) | commit the changes to the decoyed object, optionally limited to only the keys specified | | rollback |rollback<T = object>(decoy: Decoy [, key 1, ..., keyN]) | roll back the changes to the decoyed object, optionally limited to only the keys specified | | hasMutations |hasMutations<T = object>(decoy: Decoy [, key 1, ..., keyN])` | determine whether the decoyed object has any changes, optionally limited to only the keys specified |

create

Creates a decoy proxy instance, any modification made to the decoy is recorded and can be effected using commit or dropped using rollback.

Syntax: create(<object> [, <bool>]): Decoy<object>

Examples:

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original);

dummy.hello = 'universe';

console.log(dummy.hello);     //  'universe';
console.log(original.hello);  //  'world';
import { create } from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = create(original);

dummy.hello = 'universe';

console.log(dummy.hello);     //  'universe';
console.log(original.hello);  //  'world';

Tracking only the latest change (new in v1.3.0)

An additional argument has been added to the create method, a boolean value indicating whether to keep track of every change (the default) or to preserve only the latest state. Any change that effectively resets the original value is removed entirely, as Decoy no longer needs to update the value in a commit.

Example:

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original, true);

console.log(Decoy.hasMutations(dummy));  //  false
console.log(dummy.hello);                //  'world';

dummy.hello = 'universe';

console.log(Decoy.hasMutations(dummy));  //  true
console.log(dummy.hello);                //  'universe';

dummy.hello = 'world';

console.log(Decoy.hasMutations(dummy));  //  false
console.log(dummy.hello);                //  'world';

isDecoy

Test whether the given target is a known proxy decoy created by Decoy.create

Syntax: <boolean> Decoy.isDecoy(<any>)

Example:

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original);

console.log(Decoy.isDecoy(dummy));     //  true;
console.log(Decoy.isDecoy(original));  //  false;

hasMutations

Determine whether the provided Decoy has any (nested) mutations pending. If the provided value is not a known Decoy (boolean) false is returned.

Syntax: <Promise> Decoy.hasMutations(<proxy decoy> [, <keyof proxy 1>, <keyof proxy N>, ...])

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original);

console.log(Decoy.hasMutations(dummy));  //  false;

dummy.hello = 'universe';

console.log(Decoy.hasMutations(dummy));  //  true;

Decoy.rollback(dummy)
	.then(() => {
		console.log(Decoy.hasMutations(dummy));  //  false
	})

NOTE: If the decoy was created with the flag to track only the last change, hasMutations will return false if the last change brought the previously changed value(s) back to their original values.

Check for specific key mutations (new in v2.0.0)

In order to determine whether one or more specific keys have been changed, the hasMutations function allows for one or more property keys to be provided, the result will then be true if any of those keys were changed on the decoy provided.

import { create, hasMutations } from '@konfirm/decoy';

const subject = { ones: 1, twos: 2, nested: { ones: 1, twos: 2} };
const dummy = create(subject);

dummy.ones = 11;
dummy.nested.twos = 22;

console.log(hasMutations(dummy)); // true
console.log(hasMutations(dummy, 'ones')); // true
console.log(hasMutations(dummy, 'twos')); // false
console.log(hasMutations(dummy.nested)); // true
console.log(hasMutations(dummy.nested, 'ones')); // false
console.log(hasMutations(dummy.nested, 'twos')); // true

commit

Commit the proxy decoy, applying all recorded changes to the original object. Once committed, the recorded changes are truncated and the recording of changes starts over. The return value is a Promise, which rejects if the given value is not a (known) proxy decoy.

Syntax: <Promise> Decoy.commit(<proxy decoy> [, <keyof proxy 1>, <keyof proxy N>, ...])

Example:

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original);

dummy.hello = 'universe';

Decoy.commit(dummy)
	.then((result) => {
		console.log(result === original);  //  true
		console.log(dummy.hello);          //  'universe';
		console.log(original.hello);       //  'universe';
	});

//  try to commit the original, resulting in an error
Decoy.commit(original)
	.then((result) => console.log('never reached'))
	.catch((error) => console.error(error));

commit specific key mutations (new in v2.0.0)

In order to commit one or more specific changes, it is possible to specify the keys which should be committed.

import { create, commit } from '@konfirm/decoy';

const original = { hello: 'world', nested: { hello: 'world' } };
const dummy = create(original);

dummy.hello = 'universe';
dummy.nested.hello = 'universe';

commit(dummy, 'nested')
	.then((result) => {
		console.log(result === original); // true
		console.log(dummy.nested.hello); // universe
		console.log(original.nested.hello); // universe
		console.log(dummy.hello); // universe
		console.log(original.hello); // world
	});

rollback

Roll back all recorded changes to the proxy decoy, dropping all recorded changes. This truncates all recorded changes. The return value is a Promise, which rejects if the given value is not a (known) proxy decoy.

Syntax: <Promise> Decoy.rollback(<proxy decoy>)

Example:

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original);

dummy.hello = 'universe';

Decoy.rollback(dummy)
	.then((result) => {
		console.log(result === original);  //  true
		console.log(dummy.hello);          //  'world';
		console.log(original.hello);       //  'world';
	});

//  try to roll back the original, resulting in an error
Decoy.rollback(original)
	.then((result) => console.log('never reached'))
	.catch((error) => console.error(error));

rollback specific key mutations (new in v2.0.0)

In order to roll back one or more specific changes, it is possible to specify the keys that should be rolled back.

import { create, rollback } from '@konfirm/decoy';

const original = { hello: 'world', nested: { hello: 'world' } };
const dummy = create(original);

dummy.hello = 'universe';
dummy.nested.hello = 'universe';

rollback(dummy, 'nested')
	.then((result) => {
		console.log(result === original); // true
		console.log(dummy.nested.hello); // world
		console.log(original.nested.hello); // world
		console.log(dummy.hello); // universe
		console.log(original.hello); // world
	});

purge

Removes a proxy decoy from the internal list of created decoys. Cleaning up all the record changes. The return value is a Promise, which rejects if the given value is not a (known) proxy decoy.

Syntax: <Promise> Decoy.purge(<proxy decoy>)

Example:

import * as Decoy from '@konfirm/decoy';
const original = { hello: 'world' };
const dummy = Decoy.create(original);

dummy.hello = 'universe';

Decoy.purge(dummy)
	.then((result) => {
		console.log(result === original);  //  true
		console.log(original.hello);       //  'world';
	});

//  try to purge the original, resulting in an error
Decoy.purge(original)
	.then((result) => console.log('never reached'))
	.catch((error) => console.error(error));

//  try to purge the dummy again, resulting in an error
Decoy.purge(dummy)
	.then((result) => console.log('never reached'))
	.catch((error) => console.error(error));

License

MIT License Copyright (c) 2017-2023 Rogier Spieker (Konfirm)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.