Skip to content

charperbonaroo/bonaroo-changeset

Repository files navigation

bonaroo-changeset

A simple abstraction for a set of changes.

class User {
  id!: number;
  name!: string;
  email!: string;

  constructor(...assigns: Partial<User>[]) {
    Object.assign(this, ...assigns);
  }
}

function isValidUser(changeset: Changeset<User>) {
  changeset.validateSanitizedWhitespace(["name", "email"]);
  changeset.validateRequired(["name", "email"]);
  changeset.validateFormat("email", /^[^@]+@[^@]+$/);
  return changeset.isValid;
}

const user = new User({ name: "Bob Teh Builder" });
const changeset = new Changeset(user);
changeset.addChange("name", "Bob The Builder");

// { name: [ 'Bob Teh Builder', 'Bob The Builder' ] }
console.log(changeset.getChanges());

if (!isValidUser(changeset)) {
  // { email: [ { type: 'Required' } ] }
  console.log(changeset.errors);

  // throws ValidatorError
  // changeset.maybeThrow();
}

// creates a new instance of the object using the object's constructor and
// passes the properties into the constructor.
const strategy = CopyStrategy.ConstructorSpread;

const changedUser = changeset.applyChanges(strategy);
// User { name: 'Bob The Builder' }
console.log(changedUser);

// User { name: 'Bob Teh Builder' }
console.log(user);

Publish

Don't forget to increment the version

npm whoami # bonaroo
npm publish --dry-run
npm publish

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors