hadron-document
Advanced tools
Comparing version 0.8.3 to 0.9.0
@@ -10,2 +10,4 @@ 'use strict'; | ||
const removeValues = require('lodash.remove'); | ||
const includes = require('lodash.includes'); | ||
const indexOf = require('lodash.indexof'); | ||
const ObjectGenerator = require('./object-generator'); | ||
@@ -26,2 +28,23 @@ const TypeChecker = require('hadron-type-checker'); | ||
/** | ||
* Id field constant. | ||
*/ | ||
const ID = '_id'; | ||
/** | ||
* Types that are not editable. | ||
*/ | ||
const UNEDITABLE_TYPES = [ | ||
'ObjectID', | ||
'Binary', | ||
'Code', | ||
'MinKey', | ||
'MaxKey', | ||
'Timestamp' | ||
]; | ||
const CURLY = '{'; | ||
const BRACKET = '['; | ||
/** | ||
* Represents an element in a document. | ||
@@ -99,2 +122,16 @@ */ | ||
/** | ||
* Go to the next edit. | ||
* | ||
* Will check if the value is either { or [ and take appropriate action. | ||
*/ | ||
next() { | ||
if (this.currentValue === CURLY) { | ||
return this._convertToEmptyObject(); | ||
} else if (this.currentValue === BRACKET) { | ||
return this._convertToEmptyArray(); | ||
} | ||
return this._addToParent(); | ||
} | ||
/** | ||
* Rename the element. | ||
@@ -145,6 +182,35 @@ * | ||
isEdited() { | ||
return (this.key !== this.currentKey || this.value !== this.currentValue) && !this.isAdded(); | ||
return (this.key !== this.currentKey || this.value !== this.currentValue) && | ||
!this.isAdded(); | ||
} | ||
/** | ||
* Is the element the last in the elements. | ||
* | ||
* @returns {Boolean} If the element is last. | ||
*/ | ||
isLast() { | ||
return indexOf(this.parentElement.elements, this) === | ||
(this.parentElement.elements.length - 1); | ||
} | ||
/** | ||
* Determine if the value is editable. | ||
* | ||
* @returns {Boolean} If the value is editable. | ||
*/ | ||
isValueEditable() { | ||
return this.isKeyEditable() && !includes(UNEDITABLE_TYPES, this.currentType); | ||
} | ||
/** | ||
* Determine if the key is editable. | ||
* | ||
* @returns {Boolean} If the key is editable. | ||
*/ | ||
isKeyEditable() { | ||
return this.currentKey !== ID; | ||
} | ||
/** | ||
* Determine if the element is modified at all. | ||
@@ -242,2 +308,7 @@ * | ||
/** | ||
* Fire and bubble up the event. | ||
* | ||
* @param {Event} evt - The event. | ||
*/ | ||
_bubbleUp(evt) { | ||
@@ -254,2 +325,32 @@ this.emit(evt); | ||
} | ||
/** | ||
* Convert this element to an empty object. | ||
*/ | ||
_convertToEmptyObject() { | ||
this.edit({}); | ||
this.add('', ''); | ||
} | ||
/** | ||
* Convert to an empty array. | ||
*/ | ||
_convertToEmptyArray() { | ||
this.edit([]); | ||
this.add('0', ''); | ||
} | ||
/** | ||
* Add a new element to the parent. | ||
*/ | ||
_addToParent() { | ||
if (this.isLast()) { | ||
if (this.parentElement.type === 'Array') { | ||
var length = this.parentElement.elements.length; | ||
this.parentElement.add(String(length), ''); | ||
} else { | ||
this.parentElement.add('', ''); | ||
} | ||
} | ||
} | ||
} | ||
@@ -256,0 +357,0 @@ |
@@ -7,3 +7,3 @@ { | ||
"homepage": "https://github.com/mongodb-js/hadron-document", | ||
"version": "0.8.3", | ||
"version": "0.9.0", | ||
"repository": { | ||
@@ -28,4 +28,6 @@ "type": "git", | ||
"debug": "^2.2.0", | ||
"hadron-type-checker": "0.1.0", | ||
"hadron-type-checker": "^0.1.0", | ||
"lodash.foreach": "^4.3.0", | ||
"lodash.includes": "^4.1.3", | ||
"lodash.indexof": "^4.0.3", | ||
"lodash.isarray": "^4.0.0", | ||
@@ -40,2 +42,3 @@ "lodash.isplainobject": "^4.0.4", | ||
"devDependencies": { | ||
"bson": "^0.4.23", | ||
"chai": "^3.4.1", | ||
@@ -42,0 +45,0 @@ "eslint-config-mongodb-js": "^1.0.6", |
23248
438
12
7
+ Addedlodash.includes@^4.1.3
+ Addedlodash.indexof@^4.0.3
+ Addedlodash.includes@4.3.0(transitive)
+ Addedlodash.indexof@4.0.5(transitive)
Updatedhadron-type-checker@^0.1.0