summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/models/File.js
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-core/src/models/File.js')
-rw-r--r--packages/gitbook-core/src/models/File.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/models/File.js b/packages/gitbook-core/src/models/File.js
new file mode 100644
index 0000000..efc4f11
--- /dev/null
+++ b/packages/gitbook-core/src/models/File.js
@@ -0,0 +1,54 @@
+const path = require('path');
+const { Record } = require('immutable');
+
+const DEFAULTS = {
+ type: '',
+ mtime: new Date(),
+ path: '',
+ url: ''
+};
+
+class File extends Record(DEFAULTS) {
+ constructor(file = {}) {
+ if (typeof file === 'string') {
+ file = { path: file, url: file };
+ }
+
+ super({
+ ...file,
+ mtime: new Date(file.mtime)
+ });
+ }
+
+ /**
+ * @param {String} to Absolute path
+ * @return {String} The same path, but relative to this file
+ */
+ relative(to) {
+ return path.relative(
+ path.dirname(this.path),
+ to
+ ) || './';
+ }
+
+ /**
+ * Return true if file is an instance of File
+ * @param {Mixed} file
+ * @return {Boolean} isFile
+ */
+ static is(file) {
+ return (file instanceof File);
+ }
+
+ /**
+ * Create a file instance
+ * @param {Mixed|File} file
+ * @return {File} file
+ */
+ static create(file) {
+ return File.is(file) ?
+ file : new File(file);
+ }
+}
+
+module.exports = File;