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

vue-ditto

v0.1.4

Published

vue-ditto

Downloads

7

Readme

vue-ditto

Ditto Pokemon

Vue-ditto will create a Ditto ref for the original ref, you can add any metadata to it, even if the original ref changes, these metadata will be retained.

npm version CI Coverage Status gzip

Features

  • Support primitive, plain object, and array types
  • Support the complete operation method of array, namely push, pop, unshift, shift, reverse, sort, lookup set, delete, length and sparse array are all supported.

Getting Started

It works for Vue 2 & 3 by the power of Vue Demi!

Installation

NPM

$ npm i vue-ditto # yarn add vue-ditto
import { dittoRef } from "vue-ditto";

CDN

<script src="https://unpkg.com/vue-ditto"></script>
const dittoRef = VueDitto.dittoRef;

Usage

To create a Ditto, you just need to pass ref to dittoRef:

const original = ref("foo");
console.log(dittoRef({ original }).value);
// {}

It also works with nesting plain objects and arrays:

const original = ref({ foo: 42 });
console.log(dittoRef({ original }).value);
// { foo: {} }

For arrays, each item must be a plain object, and the object must contain a unique id property to hint Vue-ditto to diff the new item against the old item.

const original = ref([{ id: 0, foo: 42 }]);
console.log(dittoRef({ original }).value);
// [ { id: {}, foo: {} } ]

There are several callbacks that can be used to track creation and updates:

const original = ref({ foo: 42 });
const ditto = dittoRef({
  original,
  onCreated: ({ ditto, path, original }) => {
    console.log(`${["$"].concat(path as string[]).join(".")} is created`);
    // $ is created
    // $.foo is created
  },
});

You can add any metadata for each Ditto in these callbacks, and you must also provide metaKeys to preserve these metadata during the operation.

const original = ref({ foo: 42 });
const ditto = dittoRef({
  original,
  metaKeys: ["$original"],
  onCreated: ({ ditto, path, original }) => {
    ditto.$original = original.value;
  },
});

console.log(ditto.value);
// { '$original': { foo: 42 }, foo: { '$original': 42 } }

This also applies to the array itself:

const original = ref([
  { id: 1, value: "foo" },
  { id: 2, value: "bar" },
]);
const ditto = dittoRef({
  original,
  metaKeys: ["$original"],
  onCreated: ({ ditto, path, original }) => {
    ditto.$original = original.value;
  },
});

console.log(ditto.value);
/*
  [
    {
      '$original': { id: 1, value: 'foo' },
      id: { '$original': 1 },
      value: { '$original': 'foo' }
    },
    {
      '$original': { id: 2, value: 'bar' },
      id: { '$original': 2 },
      value: { '$original': 'bar' }
    },
    '$original': [ { id: 1, value: 'foo' }, { id: 2, value: 'bar' } ]
  ]
*/

When the original ref changes, the corresponding callback will be called, which gives you the opportunity to update the metadata:

const original = ref<{ foo: number }>({ foo: 42 });
const ditto = dittoRef({
  original,
  metaKeys: ["$updateCount"],
  flush: "sync",
  onCreated: ({ ditto, path, original }) => {
    ditto.$updateCount = 0;
  },
  onUpdated: ({ ditto, path, original }) => {
    ditto.$updateCount++;
  },
});

console.log(ditto.value);
// { '$updateCount': 0, foo: { '$updateCount': 0 } }

original.value.foo++;
console.log(ditto.value);
// { '$updateCount': 0, foo: { '$updateCount': 1 } }

For TypeScript, you must extend the type for the Ditto interface:

declare module "vue-ditto" {
  interface DittoMeta<T> {
    $original: T;
    $updateCount: number;
  }
}

API Reference

function dittoRef<T>({
  original,
  metaKeys,
  flush,
  onCreated,
  onChildrenCreated,
  onUpdated,
  onChildrenUpdated,
}: {
  original: Ref<T>;
  metaKeys?: Key[];
  flush?: WatchOptions["flush"];
  onCreated?: DittoCallback;
  onChildrenCreated?: DittoCallback;
  onUpdated?: DittoCallback;
  onChildrenUpdated?: DittoCallback;
}): Ref<Ditto<T>>;

type Ditto<T> = T extends any[]
  ? Ditto<T[number]>[] & DittoMeta<T>
  : T extends object
  ? {
      [P in keyof T]: Ditto<T[P]>;
    } & DittoMeta<T>
  : DittoMeta<T>;

interface DittoMeta<T> {}

type DittoCallback = <T>({
  ditto,
  path,
  original,
}: {
  ditto: Ditto<T>;
  path: Path;
  original: Ref<any>;
}) => void;

type Path = Key[];

type Key = string | number | symbol;

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

This project use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details