prefer !== undefined check instead of hasOwnProperty

This commit is contained in:
Kevin Jahns 2018-05-09 16:27:55 +02:00
parent bc32f7348e
commit b9245f323c
2 changed files with 11 additions and 10 deletions

View File

@ -14,7 +14,6 @@ export default function typeObserver (events) {
if (dom !== undefined && dom !== false) { if (dom !== undefined && dom !== false) {
if (yxml.constructor === YXmlText) { if (yxml.constructor === YXmlText) {
dom.nodeValue = yxml.toString() dom.nodeValue = yxml.toString()
// TODO: use hasOwnProperty instead of === undefined check
} else if (event.attributesChanged !== undefined) { } else if (event.attributesChanged !== undefined) {
// update attributes // update attributes
event.attributesChanged.forEach(attributeName => { event.attributesChanged.forEach(attributeName => {

View File

@ -155,7 +155,7 @@ function insertAttributes (y, parent, left, right, attributes, currentAttributes
*/ */
function insertText (y, text, parent, left, right, currentAttributes, attributes) { function insertText (y, text, parent, left, right, currentAttributes, attributes) {
for (let [key] of currentAttributes) { for (let [key] of currentAttributes) {
if (attributes.hasOwnProperty(key) === false) { if (attributes[key] === undefined) {
attributes[key] = null attributes[key] = null
} }
} }
@ -189,8 +189,9 @@ function formatText (y, length, parent, left, right, currentAttributes, attribut
if (right._deleted === false) { if (right._deleted === false) {
switch (right.constructor) { switch (right.constructor) {
case ItemFormat: case ItemFormat:
if (attributes.hasOwnProperty(right.key)) { const attr = attributes[right.key]
if (attributes[right.key] === right.value) { if (attr !== undefined) {
if (attr === right.value) {
negatedAttributes.delete(right.key) negatedAttributes.delete(right.key)
} else { } else {
negatedAttributes.set(right.key, right.value) negatedAttributes.set(right.key, right.value)
@ -405,8 +406,9 @@ class YTextEvent extends YArrayEvent {
} }
} else if (item._deleted === false) { } else if (item._deleted === false) {
oldAttributes.set(item.key, item.value) oldAttributes.set(item.key, item.value)
if (attributes.hasOwnProperty(item.key)) { const attr = attributes[item.key]
if (attributes[item.key] !== item.value) { if (attr !== undefined) {
if (attr !== item.value) {
if (action === 'retain') { if (action === 'retain') {
addOp() addOp()
} }
@ -433,7 +435,7 @@ class YTextEvent extends YArrayEvent {
addOp() addOp()
while (this._delta.length > 0) { while (this._delta.length > 0) {
let lastOp = this._delta[this._delta.length - 1] let lastOp = this._delta[this._delta.length - 1]
if (lastOp.hasOwnProperty('retain') && !lastOp.hasOwnProperty('attributes')) { if (lastOp.retain !== undefined && lastOp.attributes === undefined) {
// retain delta's if they don't assign attributes // retain delta's if they don't assign attributes
this._delta.pop() this._delta.pop()
} else { } else {
@ -505,11 +507,11 @@ export default class YText extends YArray {
const currentAttributes = new Map() const currentAttributes = new Map()
for (let i = 0; i < delta.length; i++) { for (let i = 0; i < delta.length; i++) {
let op = delta[i] let op = delta[i]
if (op.hasOwnProperty('insert')) { if (op.insert !== undefined) {
;[left, right] = insertText(y, op.insert, this, left, right, currentAttributes, op.attributes || {}) ;[left, right] = insertText(y, op.insert, this, left, right, currentAttributes, op.attributes || {})
} else if (op.hasOwnProperty('retain')) { } else if (op.retain !== undefined) {
;[left, right] = formatText(y, op.retain, this, left, right, currentAttributes, op.attributes || {}) ;[left, right] = formatText(y, op.retain, this, left, right, currentAttributes, op.attributes || {})
} else if (op.hasOwnProperty('delete')) { } else if (op.delete !== undefined) {
;[left, right] = deleteText(y, op.delete, this, left, right, currentAttributes) ;[left, right] = deleteText(y, op.delete, this, left, right, currentAttributes)
} }
} }