Giter Site home page Giter Site logo

jaster-model's Introduction

Jaster Model

How To Install

git clone https://github.com/andersondrosa/jaster-model.git
cd jaster-model
npm install
npm test

How To Use

We will use the characters from the game zelda to show the basic features of the jaster-model class

import model from "jaster-model";

const character = {
  name: "Character",
  type: "villager",
  rupees: 30,
  gender: "male"
};

const link = new Model(character);

link.put({
  name: "Link"
});

// So to see the link name just run the get function
console.log("link name", link.get("name"));
// Link, no longer Character

// But the character type is still Villager
console.log("link type", link.get("type")); // villager

// Then, let's change the type of the link to warrior ๐Ÿ’ช๐Ÿป
link.put({ type: "warrior ๐Ÿง๐Ÿผโ€โ™‚" });

// And also increase his rupees into 100 ๐Ÿ˜๐Ÿ’Ž
link.put(state => {
  return { rupees: state.rupees + 100 };
});

// let's see how our character became?
console.log("data", link.getData());

// Now we can save the changes by making the commits
link.commit();

// Now that it has been saved, if something happens to the link
// just restore the data for the last commit

// Oops, some zombie infected the link and changed its name ๐Ÿ˜ฑ
link.put(({ name }) => ({
  name: `Evil '${name}' ๐ŸงŸโ€โ™‚`
}));

console.log("link name", link.get("name")); // Evil 'Link' ๐ŸงŸโ€โ™‚

// Lets try to restoring removing the latest changes
link.discardChanges();

// Phew.. the link went back to normal ๐Ÿ˜†๐Ÿ™Œ๐Ÿป
console.log("link name", link.get("name")); // Link

// So, I think we better save it somewhere so we don't lose the link again
let { data, commits } = link.exportData();

// And we can take a look to see if everything is alright
console.log("Link initial data", data);

// And see our first commit too
console.log("Link commits", commits);

//The exported data can reconstruct the link to edit later if we need to,
// however, if you don't need to edit it anymore,
// just get your data in a simple way
console.log("full data", link.getData());

Extending data

What do you think about using the link as a model to create the Zelda princess?

// So we need to create a simple model based on the saved data
const zelda = new Model(data, commits);

// We must then set the princess's values and (of course)
// multiply her rupees by 20 times
zelda.put(state => {
  return {
    name: "Zelda",
    type: "princess ๐Ÿง๐Ÿผโ€โ™€",
    gender: "female",
    rupees: state.rupees * 20,
    // And we can use her crush data here ๐Ÿ˜
    wholoves: {
      name: state.name + " โ™ฅ",
      type: state.type + " ๐Ÿ’ช๐Ÿป"
    }
  };
});

// Then we make commit again
zelda.commit();

// Now just start your adventure game 
console.log("data", zelda.getData());

jaster-model's People

Contributors

andersondrosa avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.