front-git
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Front-Git

This is a framework for JavaScript (TypeScript) that can manage data like Git.

Since it was made for studying, there is no practicality so far.

Example

import { createGit } from "front-git";
 
const initState = { name: "John", age: 31 };
 
const git = createGit(initState);
 
git.remote.set("origin", {
  push: done => {
    updateUser(get.getState())
      .then(done)
      .catch(console.error);
  },
  pull: async done => {
    const user = await getUser();
 
    if (user && user.name && user.age) {
      done(user);
    }
  }
});
 
git.subscribe("add", () => {
  console.log(`current index value : ${git.getIndex()}`);
  console.log(`cureent state value : ${git.getCurrentState()}`);
});
 
git.subscribe("commit", () => {
  console.log(`current commit logs : ${git.getLogs()}`);
  console.log(`state value : ${git.getState()}`);
});
 
git.pull("origin");
 
git.add({ name: "Hello World!" });
git.commit("first commit");
 
git.push("origin");

Usage

createGit

imoprt { createGit } from "front-git";
 
const initState = {
  hello: "world!"
};
 
const git = createGit(initState);

add

// ...
 
const git = createGit(initState);
 
// OK👌
git.add({ name: "Hoge" });
git.add({ age: 41 });
git.add({ name: "Hogu", age: 100 });
git.add({});
 
// NG👎
git.add();
git.add(null);
git.add(1);
git.add("");

commit

// ...
 
const git = createGit(initState);
 
// OK👌
git.add({ name: "hoge" }).commit();
git.add({ age: 100 }).commit("Hello!");
 
// NG👎
git.commit();
git.add({ name: "hoge" }).commit({ message: "Hello" });

subscribe

// ...
 
const git = createGit(initState);
 
git.subscribe("add", () => {
  console.log("Add was executed !");
});
 
git.subscribe("commit", () => {
  console.log("Commit was executed !");
});
 
git.add({ name: "hoge" }); // Add was executed !
 
git.commit(); // Commit was executed !

getIndex

// ...
 
const git = createGit(initState);
 
let index = git.getIndex(); // null
 
git.add({ name: "hoge" });
 
state = git.getIndex(); // { name: "hoge" }
 
git.commit();
 
state = git.getIndex(); // null

getState

// ...
 
const git = createGit(initState);
 
let state = git.getState(); // { name: "John", age: 31 }
 
git.add({ name: "hoge" });
 
state = git.getState(); // { name: "John", age: 31 }
 
git.commit();
 
state = git.getState(); // { name: "hoge", age: 31 }

getCurrentState

// ...
 
const git = createGit(initState);
 
let state = git.getCurrentState(); // { name: "John", age: 31 }
 
git.add({ name: "hoge" });
 
state = git.getCurrentState(); // { name: "hoge", age: 31 }
 
git.commit();
 
state = git.getCurrentState(); // { name: "hoge", age: 31 }

getLogs

// ...
 
const git = createGit(initState);
 
let logs = git.getLogs(); // []
 
git.add({ name: "hoge" }).commit("First Commit");
 
logs = git.getIndex();
/*
[
  { 
    id: "xxxxxxxxxxxx", 
    comment: "First Commit",
    log: { ex: { name: "John" }, diff: { name: "hoge" }  },
    created_at: new Date()
  }
]
*/

clearIndex

// ...
 
const git = createGit(initState);
 
let index = git.getIndex(); // null
 
git.add({ name: "hoge" });
 
state = git.getIndex(); // { name: "hoge" }
 
git.clearIndex();
 
state = git.getIndex(); // null

revertLog

// ...
 
// init
const git = createGit(initState);
let state = git.getState(); // { name: "John", age: 31 }
let logs = git.getLogs(); // []
 
// commit
git.add({ name: "hoge" }).commit();
state = git.getState(); // { name: "hoge", age: 31 }
logs = git.getLogs(); // [ {...} ]
 
// commit
git.add({ age: 100 }).commit();
state = git.getState(); // { name: "hoge", age: 100 }
logs = git.getLogs(); // [ {...}, {...} ]
 
// revertLog
git.revertLog();
state = git.getState(); // { name: "hoge", age: 31 }
logs = git.getLogs(); // [ {...} ]
 
// revertLog
git.revertLog();
state = git.getState(); // { name: "John", age: 31 }
logs = git.getLogs(); // []

remote.set

// ...
 
const git = createGit(initState);
 
// OK👌
git.remote.set("origin", {
  push: done => {
    updateUser().then(done);
  },
  pull: async done => {
    const user = await getUser();
    done(user);
  }
});
 
// NG👎
git.remote("origin", {});
git.remote("origin", { push: () => updateUser() });

push

// ...
 
const git = createGit(initState);
 
// OK👌
git.remote.set("origin", { push: ... , pull: ... });
git.push("origin");
 
// NG👎
git.push();
git.push("unknown");

pull

// ...
 
const git = createGit(initState);
 
// OK👌
git.remote.set("origin", { push: ... , pull: ... });
git.pull("origin");
 
// NG👎
git.pull();
git.pull("unknown");

Readme

Keywords

Package Sidebar

Install

npm i front-git

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

14 kB

Total Files

5

Last publish

Collaborators

  • uttk