Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 09b17ea

Browse files
committed
Refactor frame into baseFrame without fs dependency and frame with fs dependency
1 parent 5febe1c commit 09b17ea

File tree

2 files changed

+87
-82
lines changed

2 files changed

+87
-82
lines changed

src/models/baseFrame.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const moment = require('moment-timezone');
2+
const Hashids = require('hashids');
3+
const hashids = new Hashids();
4+
5+
class baseFrame {
6+
/**
7+
* constructor.
8+
* @param config
9+
* @param id
10+
* @param type
11+
*/
12+
constructor(config, id, type) {
13+
this.config = config;
14+
this.project = config.get('project');
15+
this.resource = {id, type};
16+
17+
if(typeof id === 'string' || id instanceof String)
18+
this.resource.new = true;
19+
20+
this.id = baseFrame.generateId();
21+
this._start = false;
22+
this._stop = false;
23+
this.timezone = config.get('timezone');
24+
this.notes = [];
25+
}
26+
27+
static fromJson(config, json) {
28+
let frame = new this(config, json.resource.id, json.resource.type);
29+
frame.project = json.project;
30+
frame.id = json.id;
31+
frame._start = json.start;
32+
frame._stop = json.stop;
33+
frame.notes = json.notes;
34+
frame.timezone = json.timezone;
35+
frame.validate();
36+
37+
return frame;
38+
}
39+
40+
validate() {
41+
moment.suppressDeprecationWarnings = true;
42+
43+
if(!moment(this._start).isValid())
44+
throw `Error: Start date is not in a valid ISO date format!`;
45+
46+
if(this._stop && !moment(this._stop).isValid())
47+
throw `Error: Stop date is not in a valid ISO date format!`;
48+
49+
moment.suppressDeprecationWarnings = false;
50+
}
51+
52+
_getCurrentDate() {
53+
if(this.timezone)
54+
return moment().tz(this.timezone).format();
55+
56+
return moment();
57+
}
58+
59+
get duration() {
60+
return moment(this.stop).diff(this.start) / 1000;
61+
}
62+
63+
get date() {
64+
return this.start;
65+
}
66+
67+
get start() {
68+
return this.timezone ? moment(this._start).tz(this.timezone) : moment(this._start);
69+
}
70+
71+
get stop() {
72+
return this.timezone ? moment(this._stop).tz(this.timezone) : (this._stop ? moment(this._stop) : false );
73+
}
74+
75+
/**
76+
* generate a unique id
77+
* @returns {number}
78+
*/
79+
static generateId() {
80+
return hashids.encode(new Date().getTime());
81+
}
82+
}
83+
84+
module.exports = baseFrame;

src/models/frame.js

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,8 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const moment = require('moment-timezone');
4-
const Hashids = require('hashids');
5-
const hashids = new Hashids();
6-
7-
class frame {
8-
/**
9-
* constructor.
10-
* @param config
11-
* @param id
12-
* @param type
13-
*/
14-
constructor(config, id, type) {
15-
this.config = config;
16-
this.project = config.get('project');
17-
this.resource = {id, type};
18-
19-
if(typeof id === 'string' || id instanceof String)
20-
this.resource.new = true;
21-
22-
this.id = frame.generateId();
23-
this._start = false;
24-
this._stop = false;
25-
this.timezone = config.get('timezone');
26-
this.notes = [];
27-
}
28-
29-
static fromJson(config, json) {
30-
let frame = new this(config, json.resource.id, json.resource.type);
31-
frame.project = json.project;
32-
frame.id = json.id;
33-
frame._start = json.start;
34-
frame._stop = json.stop;
35-
frame.notes = json.notes;
36-
frame.timezone = json.timezone;
37-
frame.validate();
38-
39-
return frame;
40-
}
3+
const BaseFrame = require('./baseFrame.js');
414

5+
class frame extends BaseFrame {
426
static fromFile(config, file) {
437
return frame.fromJson(config, JSON.parse(fs.readFileSync(file)));
448
}
@@ -57,25 +21,6 @@ class frame {
5721
return this;
5822
}
5923

60-
validate() {
61-
moment.suppressDeprecationWarnings = true;
62-
63-
if(!moment(this._start).isValid())
64-
throw `Error: Start date is not in a valid ISO date format!`;
65-
66-
if(this._stop && !moment(this._stop).isValid())
67-
throw `Error: Stop date is not in a valid ISO date format!`;
68-
69-
moment.suppressDeprecationWarnings = false;
70-
}
71-
72-
_getCurrentDate() {
73-
if(this.timezone)
74-
return moment().tz(this.timezone).format();
75-
76-
return moment();
77-
}
78-
7924
/**
8025
* assert file exists
8126
*/
@@ -102,30 +47,6 @@ class frame {
10247
get file() {
10348
return path.join(this.config.frameDir, this.id + '.json');
10449
}
105-
106-
get duration() {
107-
return moment(this.stop).diff(this.start) / 1000;
108-
}
109-
110-
get date() {
111-
return this.start;
112-
}
113-
114-
get start() {
115-
return this.timezone ? moment(this._start).tz(this.timezone) : moment(this._start);
116-
}
117-
118-
get stop() {
119-
return this.timezone ? moment(this._stop).tz(this.timezone) : (this._stop ? moment(this._stop) : false );
120-
}
121-
122-
/**
123-
* generate a unique id
124-
* @returns {number}
125-
*/
126-
static generateId() {
127-
return hashids.encode(new Date().getTime());
128-
}
12950
}
13051

131-
module.exports = frame;
52+
module.exports = frame;

0 commit comments

Comments
 (0)