hadron-document 7.6.0 → 7.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ const TypeChecker = require('hadron-type-checker');
2
+ const Events = require('../element-events');
3
+ const { fieldStringLen } = require('../utils');
4
+ const StandardEditor = require('./standard');
5
+
6
+ /**
7
+ * CRUD editor for date values.
8
+ */
9
+ class DateEditor extends StandardEditor {
10
+ /**
11
+ * Create the editor with the element.
12
+ *
13
+ * @param {Element} element - The hadron document element.
14
+ */
15
+ constructor(element) {
16
+ super(element);
17
+ }
18
+
19
+ /**
20
+ * Complete the date edit by converting the valid string to a date
21
+ * object or leaving as invalid.
22
+ */
23
+ complete() {
24
+ if (this.element.isCurrentTypeValid()) {
25
+ this.element.edit(TypeChecker.cast(this._formattedValue(), 'Date'));
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Edit the element with the provided value.
31
+ *
32
+ * @param {Object} value - The new value.
33
+ */
34
+ edit(value) {
35
+ try {
36
+ const date = TypeChecker.cast(value, 'Date');
37
+ if (date.toString() === 'Invalid Date') {
38
+ this.element.setInvalid(value, 'Date', `${value} is not in a valid date format`);
39
+ } else {
40
+ this.element.currentValue = value;
41
+ this.element.setValid();
42
+ this.element._bubbleUp(Events.Edited, this.element);
43
+ }
44
+ } catch (e) {
45
+ this.element.setInvalid(value, this.element.currentType, e.message);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Get the number of characters the value should display.
51
+ *
52
+ * @param {Boolean} editMode - If the element is being edited.
53
+ *
54
+ * @returns {Number} The number of characters.
55
+ */
56
+ size(editMode) {
57
+ const value = this.element.currentValue;
58
+ if (editMode) {
59
+ return fieldStringLen(value);
60
+ }
61
+ return this.element.isCurrentTypeValid() ? fieldStringLen(this._formattedValue()) : fieldStringLen(value);
62
+ }
63
+
64
+ /**
65
+ * Start the date edit.
66
+ *
67
+ * @param {Object} value - The value in the field.
68
+ */
69
+ start() {
70
+ if (this.element.isCurrentTypeValid()) {
71
+ this.edit(this._formattedValue());
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Get the value being edited.
77
+ *
78
+ * @param {Boolean} editMode - If the UI is in edit mode.
79
+ *
80
+ * @returns {String} The value.
81
+ */
82
+ value(editMode) {
83
+ const value = this.element.currentValue;
84
+ if (!editMode && this.element.isCurrentTypeValid()) {
85
+ return this._formattedValue();
86
+ }
87
+ return value;
88
+ }
89
+
90
+ _formattedValue() {
91
+ return new Date(this.element.currentValue).toISOString().replace('Z', '+00:00');
92
+ }
93
+ }
94
+
95
+ module.exports = DateEditor;
@@ -0,0 +1,46 @@
1
+ const TypeChecker = require('hadron-type-checker');
2
+ const Events = require('../element-events');
3
+ const StandardEditor = require('./standard');
4
+
5
+ /**
6
+ * CRUD editor for decimal128 values.
7
+ */
8
+ class Decimal128Editor extends StandardEditor {
9
+ /**
10
+ * Create the editor with the element.
11
+ *
12
+ * @param {Element} element - The hadron document element.
13
+ */
14
+ constructor(element) {
15
+ super(element);
16
+ }
17
+
18
+ /**
19
+ * Complete the decimal128 edit by converting the valid string to a decimal128
20
+ * value or leaving as invalid.
21
+ */
22
+ complete() {
23
+ if (this.element.isCurrentTypeValid()) {
24
+ this.element.edit(TypeChecker.cast(this.element.currentValue, 'Decimal128'));
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Edit Decimal128 element. Check if the value is a Decimal128 before setting typed
30
+ * up value.
31
+ *
32
+ * @param {Object} value - The new value.
33
+ */
34
+ edit(value) {
35
+ try {
36
+ TypeChecker.cast(value, 'Decimal128');
37
+ this.element.currentValue = value;
38
+ this.element.setValid();
39
+ this.element._bubbleUp(Events.Edited, this.element);
40
+ } catch (error) {
41
+ this.element.setInvalid(value, this.element.currentType, error.message);
42
+ }
43
+ }
44
+ }
45
+
46
+ module.exports = Decimal128Editor;
@@ -0,0 +1,70 @@
1
+ const TypeChecker = require('hadron-type-checker');
2
+ const Events = require('../element-events');
3
+ const { fieldStringLen } = require('../utils');
4
+ const StandardEditor = require('./standard');
5
+
6
+ /**
7
+ * CRUD editor for double values.
8
+ */
9
+ class DoubleEditor extends StandardEditor {
10
+ /**
11
+ * Create the editor with the element.
12
+ *
13
+ * @param {Element} element - The hadron document element.
14
+ */
15
+ constructor(element) {
16
+ super(element);
17
+ }
18
+
19
+ /**
20
+ * Complete the double edit by converting the valid string to a double
21
+ * value or leaving as invalid.
22
+ */
23
+ complete() {
24
+ if (this.element.isCurrentTypeValid()) {
25
+ this.element.edit(TypeChecker.cast(this.element.currentValue, 'Double'));
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Edit Double element. Check if the value is a Double before setting tped
31
+ * up value.
32
+ *
33
+ * @param {Object} value - The new value.
34
+ */
35
+ edit(value) {
36
+ try {
37
+ const double = TypeChecker.cast(value, 'Double');
38
+ if (isNaN(double.value)) {
39
+ this.element.setInvalid(value, 'Double', `${value} is not a valid double format`);
40
+ } else {
41
+ this.element.currentValue = value;
42
+ this.element.setValid();
43
+ this.element._bubbleUp(Events.Edited, this.element);
44
+ }
45
+ } catch (error) {
46
+ this.element.setInvalid(value, this.element.currentType, error.message);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Get the number of characters the value should display.
52
+ *
53
+ * @param {Boolean} editMode - If the element is being edited.
54
+ *
55
+ * @returns {Number} The number of characters.
56
+ */
57
+ size() {
58
+ const currentValue = this.element.currentValue;
59
+ return fieldStringLen(
60
+ // Not all values that will be checked here are bson types with a `value`
61
+ // property, using valueOf is a more resilient way of getting the "native"
62
+ // value from `currentValue`
63
+ typeof currentValue.valueOf === 'function'
64
+ ? currentValue.valueOf()
65
+ : currentValue
66
+ );
67
+ }
68
+ }
69
+
70
+ module.exports = DoubleEditor;
@@ -0,0 +1,40 @@
1
+ const StandardEditor = require('./standard');
2
+ const StringEditor = require('./string');
3
+ const Decimal128Editor = require('./decimal128');
4
+ const Int32Editor = require('./int32');
5
+ const Int64Editor = require('./int64');
6
+ const DoubleEditor = require('./double');
7
+ const DateEditor = require('./date');
8
+ const NullEditor = require('./null');
9
+ const UndefinedEditor = require('./undefined');
10
+ const ObjectIdEditor = require('./objectid');
11
+
12
+ const init = (element) => {
13
+ return {
14
+ Standard: new StandardEditor(element),
15
+ String: new StringEditor(element),
16
+ Decimal128: new Decimal128Editor(element),
17
+ Date: new DateEditor(element),
18
+ Double: new DoubleEditor(element),
19
+ Int32: new Int32Editor(element),
20
+ Int64: new Int64Editor(element),
21
+ Null: new NullEditor(element),
22
+ Undefined: new UndefinedEditor(element),
23
+ ObjectId: new ObjectIdEditor(element)
24
+ };
25
+ };
26
+
27
+ Object.assign(init, {
28
+ DateEditor,
29
+ StandardEditor,
30
+ StringEditor,
31
+ Decimal128Editor,
32
+ DoubleEditor,
33
+ Int32Editor,
34
+ Int64Editor,
35
+ NullEditor,
36
+ UndefinedEditor,
37
+ ObjectIdEditor
38
+ });
39
+
40
+ module.exports = init;
@@ -0,0 +1,29 @@
1
+ const { fieldStringLen } = require('../utils');
2
+ const StandardEditor = require('./standard');
3
+
4
+ /**
5
+ * CRUD editor for int32 values.
6
+ */
7
+ class Int32Editor extends StandardEditor {
8
+ /**
9
+ * Create the editor with the element.
10
+ *
11
+ * @param {Element} element - The hadron document element.
12
+ */
13
+ constructor(element) {
14
+ super(element);
15
+ }
16
+
17
+ /**
18
+ * Get the number of characters the value should display.
19
+ *
20
+ * @param {Boolean} editMode - If the element is being edited.
21
+ *
22
+ * @returns {Number} The number of characters.
23
+ */
24
+ size() {
25
+ return fieldStringLen(this.element.currentValue.valueOf());
26
+ }
27
+ }
28
+
29
+ module.exports = Int32Editor;
@@ -0,0 +1,46 @@
1
+ const TypeChecker = require('hadron-type-checker');
2
+ const Events = require('../element-events');
3
+ const StandardEditor = require('./standard');
4
+
5
+ /**
6
+ * CRUD editor for int32 values.
7
+ */
8
+ class Int64Editor extends StandardEditor {
9
+ /**
10
+ * Create the editor with the element.
11
+ *
12
+ * @param {Element} element - The hadron document element.
13
+ */
14
+ constructor(element) {
15
+ super(element);
16
+ }
17
+
18
+ /**
19
+ * Complete the int64 edit by converting the valid string to a int64
20
+ * value or leaving as invalid.
21
+ */
22
+ complete() {
23
+ if (this.element.isCurrentTypeValid()) {
24
+ this.element.edit(TypeChecker.cast(this.element.currentValue, 'Int64'));
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Edit Int64 element. Check if the value is a Int64 before setting typed
30
+ * up value.
31
+ *
32
+ * @param {Object} value - The new value.
33
+ */
34
+ edit(value) {
35
+ try {
36
+ TypeChecker.cast(value, 'Int64');
37
+ this.element.currentValue = value;
38
+ this.element.setValid();
39
+ this.element._bubbleUp(Events.Edited, this.element);
40
+ } catch (error) {
41
+ this.element.setInvalid(value, this.element.currentType, error.message);
42
+ }
43
+ }
44
+ }
45
+
46
+ module.exports = Int64Editor;
@@ -0,0 +1,42 @@
1
+ const StandardEditor = require('./standard');
2
+
3
+ /**
4
+ * Null is always 'null'
5
+ */
6
+ const VALUE = 'null';
7
+
8
+ /**
9
+ * CRUD editor for null values.
10
+ */
11
+ class NullEditor extends StandardEditor {
12
+ /**
13
+ * Create the editor with the element.
14
+ *
15
+ * @param {Element} element - The hadron document element.
16
+ */
17
+ constructor(element) {
18
+ super(element);
19
+ }
20
+
21
+ /**
22
+ * Get the number of characters the value should display.
23
+ *
24
+ * @param {Boolean} editMode - If the element is being edited.
25
+ *
26
+ * @returns {Number} The number of characters.
27
+ */
28
+ size() {
29
+ return 4;
30
+ }
31
+
32
+ /**
33
+ * Get the value being edited.
34
+ *
35
+ * @returns {Object} The value.
36
+ */
37
+ value() {
38
+ return VALUE;
39
+ }
40
+ }
41
+
42
+ module.exports = NullEditor;
@@ -0,0 +1,54 @@
1
+ const TypeChecker = require('hadron-type-checker');
2
+ const Events = require('../element-events');
3
+ const StandardEditor = require('./standard');
4
+
5
+ /**
6
+ * CRUD editor for object id values.
7
+ */
8
+ class ObjectIdEditor extends StandardEditor {
9
+ /**
10
+ * Create the editor with the element.
11
+ *
12
+ * @param {Element} element - The hadron document element.
13
+ */
14
+ constructor(element) {
15
+ super(element);
16
+ }
17
+
18
+ /**
19
+ * Complete the object id edit by converting the valid string to an object id
20
+ * object or leaving as invalid.
21
+ */
22
+ complete() {
23
+ if (this.element.isCurrentTypeValid()) {
24
+ this.element.edit(TypeChecker.cast(this.element.currentValue, 'ObjectId'));
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Edit the element with the provided value.
30
+ *
31
+ * @param {Object} value - The new value.
32
+ */
33
+ edit(value) {
34
+ try {
35
+ TypeChecker.cast(value, 'ObjectId');
36
+ this.element.currentValue = value;
37
+ this.element.setValid();
38
+ this.element._bubbleUp(Events.Edited, this.element);
39
+ } catch (e) {
40
+ this.element.setInvalid(value, this.element.currentType, e.message);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Start the object id edit.
46
+ */
47
+ start() {
48
+ if (this.element.isCurrentTypeValid()) {
49
+ this.edit(String(this.element.currentValue));
50
+ }
51
+ }
52
+ }
53
+
54
+ module.exports = ObjectIdEditor;
@@ -0,0 +1,78 @@
1
+ const TypeChecker = require('hadron-type-checker');
2
+ const Events = require('../element-events');
3
+ const { fieldStringLen } = require('../utils');
4
+
5
+ /**
6
+ * Regex to match an array or object string.
7
+ */
8
+ const ARRAY_OR_OBJECT = /^(\[|\{)(.+)(\]|\})$/;
9
+
10
+ /**
11
+ * CRUD editor for standard values.
12
+ */
13
+ class StandardEditor {
14
+ /**
15
+ * Create the editor with the element.
16
+ *
17
+ * @param {Element} element - The hadron document element.
18
+ */
19
+ constructor(element) {
20
+ this.element = element;
21
+ }
22
+
23
+ /**
24
+ * Edit the element with the provided value.
25
+ *
26
+ * @param {Object} value - The new value.
27
+ */
28
+ edit(value) {
29
+ const currentType = this.element.currentType;
30
+ try {
31
+ const newValue = TypeChecker.cast(value, currentType);
32
+ this.element.edit(newValue);
33
+ } catch (e) {
34
+ this.element.setInvalid(value, currentType, e.message);
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Edit the element via a paste.
40
+ *
41
+ * @param {String} value - The balue.
42
+ */
43
+ paste(value) {
44
+ if (value.match(ARRAY_OR_OBJECT)) {
45
+ this.edit(JSON.parse(value));
46
+ this.element._bubbleUp(Events.Converted, this.element);
47
+ } else {
48
+ this.edit(value);
49
+ }
50
+ }
51
+
52
+
53
+ /**
54
+ * Get the number of characters the value should display.
55
+ *
56
+ * @param {Boolean} editMode - If the element is being edited.
57
+ *
58
+ * @returns {Number} The number of characters.
59
+ */
60
+ size() {
61
+ return fieldStringLen(this.element.currentValue);
62
+ }
63
+
64
+ /**
65
+ * Get the value being edited.
66
+ *
67
+ * @returns {Object} The value.
68
+ */
69
+ value() {
70
+ return this.element.currentValue;
71
+ }
72
+
73
+ // Standard editing requires no special start/complete behaviour.
74
+ start() {}
75
+ complete() {}
76
+ }
77
+
78
+ module.exports = StandardEditor;
@@ -0,0 +1,20 @@
1
+ const StandardEditor = require('./standard');
2
+
3
+ const STRING_TYPE = 'String';
4
+
5
+ /**
6
+ * CRUD editor for string values.
7
+ */
8
+ class StringEditor extends StandardEditor {
9
+ /**
10
+ * Create the editor with the element.
11
+ *
12
+ * @param {Element} element - The hadron document element.
13
+ */
14
+ constructor(element) {
15
+ super(element);
16
+ }
17
+ }
18
+
19
+ module.exports = StringEditor;
20
+ module.exports.STRING_TYPE = STRING_TYPE;
@@ -0,0 +1,42 @@
1
+ const StandardEditor = require('./standard');
2
+
3
+ /**
4
+ * Undefined is always 'undefined'
5
+ */
6
+ const VALUE = 'undefined';
7
+
8
+ /**
9
+ * CRUD editor for undefined values.
10
+ */
11
+ class UndefinedEditor extends StandardEditor {
12
+ /**
13
+ * Create the editor with the element.
14
+ *
15
+ * @param {Element} element - The hadron document element.
16
+ */
17
+ constructor(element) {
18
+ super(element);
19
+ }
20
+
21
+ /**
22
+ * Get the number of characters the value should display.
23
+ *
24
+ * @param {Boolean} editMode - If the element is being edited.
25
+ *
26
+ * @returns {Number} The number of characters.
27
+ */
28
+ size() {
29
+ return VALUE.length;
30
+ }
31
+
32
+ /**
33
+ * Get the value being edited.
34
+ *
35
+ * @returns {Object} The value.
36
+ */
37
+ value() {
38
+ return VALUE;
39
+ }
40
+ }
41
+
42
+ module.exports = UndefinedEditor;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The event constant.
3
+ */
4
+ const Events = {
5
+ Added: 'Element::Added',
6
+ Edited: 'Element::Edited',
7
+ Removed: 'Element::Removed',
8
+ Reverted: 'Element::Reverted',
9
+ Converted: 'Element::Converted',
10
+ Invalid: 'Element::Invalid',
11
+ Valid: 'Element::Valid'
12
+ };
13
+
14
+ module.exports = Events;