1 | /*!
|
2 | * A MongoDB inspired ES6 Map() query language. - Copyright (c) 2017 Louis T. (https://lou.ist/)
|
3 | * Licensed under the MIT license https://raw.githubusercontent.com/LouisT/MapQL/master/LICENSE
|
4 | */
|
5 | ;
|
6 | const Helpers = require('./Helpers');
|
7 |
|
8 | class Document {
|
9 | constructor (id = Helpers._null, value = {}) {
|
10 | this._id = id;
|
11 | this.value = value;
|
12 | }
|
13 |
|
14 | /*
|
15 | * Convert an array of entries to an array of Document objects.
|
16 | */
|
17 | static convert (entries) {
|
18 | return entries.map((entry) => {
|
19 | return new Document(entry[0], entry[1]);
|
20 | });
|
21 | }
|
22 |
|
23 | /*
|
24 | * Convert a Document to a plain Object.
|
25 | */
|
26 | static toObject (doc) {
|
27 | return Object.assign(Object.create(null), doc);
|
28 | }
|
29 | toObject (doc = this) {
|
30 | return this.constructor.toObject(doc);
|
31 | }
|
32 |
|
33 | /*
|
34 | * Validate a possible Document object.
|
35 | */
|
36 | static isDocument (obj = {}) {
|
37 | return (obj instanceof Document && '_id' in obj && 'value' in obj);
|
38 | }
|
39 | get isDocument () {
|
40 | return this.constructor.isDocument(this);
|
41 | }
|
42 |
|
43 | /*
|
44 | * Mark if this Document was added by the key.
|
45 | * Note: This is just extra metadata; not actively used anywhere.
|
46 | */
|
47 | bykey (value = false) {
|
48 | return (this[Symbol.for('bykey')] = value) === value ? this : false;
|
49 | }
|
50 |
|
51 | /*
|
52 | * Allow the class to have a custom object string tag.
|
53 | */
|
54 | get [Symbol.toStringTag]() {
|
55 | return (this.constructor.name || 'Document');
|
56 | }
|
57 | }
|
58 |
|
59 | /*
|
60 | * Export the Document class for use!
|
61 | */
|
62 | module.exports = Document;
|