Skip to content

Commit

Permalink
1.1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
artegoser committed May 7, 2021
1 parent 0a12a67 commit 8ecf55a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ test.prepare().then(()=>{

new JsonChatStorage(name, length, dir)

|Name |Type |Default |Description |
|------|-------------|--------|--------------------------------------|
|name |string | |name of JSON storage |
|length|integer/false|false | number of stored messages |
|dir |string |./chats | folder for storing all json storages |
|Name |Type |Default |Description |
|------|-------------|--------|------------------------------------|
|name |string | |name of JSON storage |
|length|integer/false|false |number of stored messages |
|dir |string |./chats |folder for storing all json storages|
|meta |Object/none | |metadata for JSON storage |

**deletelastmessage(user)** - Deletes a last message of user

Expand Down
8 changes: 7 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
# 1.1.8
## sqlite
### the delete(where) function has been added
### added documentation
### added documentation
# 1.1.10
## sqlite
### the sqlite_all function has been added
# 1.1.11
# Json
## added Json meta
45 changes: 41 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,35 @@ class JsonChatStorage {
* @param {string} name name of JSON storage
* @param {integer|false} length number of stored messages
* @param {string} dir folder for storing all json storages
* @param {Object|none} meta metadata for JSON storage
*/
constructor(name, length=false, dir="./chats"){
constructor(name, length=false, dir="chats", meta){
this.meslength = length;
this._name = name;
this._dir = dir;
if (!fs.existsSync(this._dir)) fs.mkdirSync(this._dir);

try{
this.messages = this._req(`./${this._dir}/${this._name}.json`);
} catch{
this.messages = [];
this.backup();
}

try{
this._meta = this._req(`./${this._dir}/${this._name}_meta.json`);
if(meta){
this._meta = Object.assign(this._meta, meta);
this.backup_meta();
}

} catch{
if(meta) {
this._meta = meta;
this.backup_meta();
}

}

if (!fs.existsSync(this._dir)) fs.mkdirSync(this._dir);
}

_req(path){
Expand Down Expand Up @@ -78,15 +95,35 @@ class JsonChatStorage {
/**
* Get current time in format YYYY-MM-DD HH:MM:SS
*/
get time(){
get time(){
return strftime("%Y-%m-%d %H:%M:%S");
}
/**
* adds metadata to meta
*/
set add_meta(v){
this._meta = Object.assign(this._meta,v);
this.backup_meta();
}
set meta(v){
this._meta = v;
this.backup_meta();
}
get meta(){
return this._meta
}
/**
* backups a JSON storage
*/
backup(){
fs.writeFileSync(`./${this._dir}/${this._name}.json`, JSON.stringify(this.messages, null, 4));
}
/**
* backups a metadata
*/
backup_meta(){
fs.writeFileSync(`./${this._dir}/${this._name}_meta.json`, JSON.stringify(this._meta, null, 4));
}

}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-chat-storage",
"version": "1.1.10",
"version": "1.1.11",
"description": "Simple chat storage for chat's",
"homepage": "https://github.com/artegoser/simple-chat-storage",
"main": "index.js",
Expand All @@ -9,7 +9,7 @@
"coverage": "nyc npm test",
"coveralls": "npm run coverage && nyc report --reporter=text-lcov | coveralls",
"docs": "jsdoc index.js -t docdash -d D:\\github\\artegoser.github.io\\simple-chat-storage\\documentation -r ./README.md",
"buildall": "npm run coveralls && npm run docs"
"buildall": "npm run coveralls && npm run docs && npm publish"
},
"nyc": {
"exclude": "**/*.spec.js"
Expand Down
9 changes: 8 additions & 1 deletion tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ function randch(length) {
let randchi = 0;
describe("JSON",()=>{
let chat = require("./index").JSON;
let test = new chat("test", 15);
let test = new chat("test", 15, 'chats', {author:"Artegoser"});
test = new chat("test", 15, 'chats', {decsription:"Test chat!"});
describe(`JSON meta`, ()=>{
test.meta = {description:"test chat =/"};
test.add_meta = {Author:"Doctor Who"};
test.meta = test.meta;
//this is working trust me
});
describe(`JSON add message`, ()=>{
it(`should return false when !message`, ()=>{
if(test.addmessage("artegoser")) throw new Error("not returning false")
Expand Down

0 comments on commit 8ecf55a

Please sign in to comment.