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-changeset

v0.3.0

Published

[![gzip size](https://img.badgesize.io/https:/unpkg.com/vue-changeset/dist/vue-changeset.modern.js?label=gzip&compression=gzip)](https://unpkg.com/vue-changeset/dist/vue-changeset.modern.js) ![npm](https://img.shields.io/npm/v/vue-changeset)

Downloads

148

Readme

📜 vue-changeset - alpha

gzip size npm

A tiny change management library. Useful for validation and making sure that unchecked, unvalidated state does not leak / persist into your main data store.

Inspired by Ecto Changeset and ember-changeset.

As opposed to ember-changeset this has just a core tiny feature set and does not support a lot of features like rollbacks, snapshots and nested data handling.

Features

  • [x] Vue 3 support
  • [ ] Vue 2 + composition API support
  • [x] createChangeset
  • [x] useChangeset (createChangeset + cleanup on unmount)
  • [ ] nested data handling
  • [x] changeset.assign()
  • [x] changeset.assignIfValid()
  • [x] changeset.validate()

Example

vue-changeset tries to not stand in the way and for simple usecases allows you just to pass validation function to rule them all:

import { defineComponent } from 'vue';
import { useChangeset } from 'vue-changeset';

export default defineComponent({
  setup() {
    const model = { firstName: 'John', lastName: 'Doe',  };
    const userChangeset = useChangeset(model, {
      validate: (prop, value) => {
        if (prop === 'firstName') {
          return value.length > 2 || 'First Name must be at least 2 characters long';
        }

        if (prop === 'lastName') {
          return value.length > 2 || 'Last Name must be at least 2 characters long';
        }
      }
    });

    return { useChangeset };
  }
});
 <form @submit.prevent="changeset.assignIfValid">
    <label>First name</label>
    <input :model="changeset.firstName">
    <span v-if="changeset.changes.firstName.error"> {{ changeset.changes.firstName.error }} </span>
    <button type="submit">Save</button>
 </form>

If your validations needs are more advanced, you can use the createValidator function, maybe even together with a separate validation library. I'm using favalid in this example.

import { defineComponent } from 'vue';
import { useChangeset, createValidator } from 'vue-changeset';
// 
import { tester, combine, minLength, regexp } from 'favalid';

export default defineComponent({
  setup() {
    const model = { firstName: 'John', lastName: 'Doe',  };
    const userChangeset = useChangeset(model, {
      validate: createValidator({
        firstName: (value) => value && value.length > 2,
        lastName: combine(
          minLength(3, () => 'too few letters'), 
          regexp(/^[a-zA-Z]+$/, () => 'invalid format', {}),
          tester(() => {
            // some other custom validation here
          }, () => 'Invalid input'))
      })
    });

    return { useChangeset, onSubmit };
  }
});

createChangeset accepts options as a second argument

// These are the defaults
const options = {
  autoValidate: false,
  validate: (prop, value) => true,
  copy: obj => ({ ...obj }), // shallow copy
  checkifDirty: ({ key, oldValue, newValue }) => oldValue !== newValue 
};

const changeset = useChangeset(model, options);

autoValidate will validate a prop on any change validate is a function that is used to validate all data (see examples above)

These functions can be passed to alter the behavior of vue-changeset: copy is used to create a copy of the object, it's a shallow copy by default. You can use deep copy via JSON.stringify() and JSON.parse() or use a library like fast-copy if you need. checkIfDirty is used to handle the isDirty flags and is very naive by default, you can pass your own implementation that suits your needs

Docs

WIP