Skip to content

Commit aa6255a

Browse files
author
tohosaku
committed
Remove unneed "hasOwnProperty"
1 parent aa94c87 commit aa6255a

File tree

9 files changed

+111
-169
lines changed

9 files changed

+111
-169
lines changed

src/core.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,28 +374,24 @@ export class JSONEditor {
374374
const sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet
375375
const qualifier = this.element.nodeName.toLowerCase()
376376

377-
for (var selector in rules) {
378-
if (!rules.hasOwnProperty(selector)) continue
379-
var sel = qualifier + '[data-theme="' + themeName + '"] ' + selector
377+
Object.keys(rules).forEach(selector => {
378+
const sel = `${qualifier}[data-theme="${themeName}"] ${selector}`
380379

381380
// all browsers, except IE before version 9
382381
if (sheet.insertRule) sheet.insertRule(sel + ' {' + decodeURIComponent(rules[selector]) + '}', 0)
383382
// Internet Explorer before version 9
384383
else if (sheet.addRule) sheet.addRule(sel, decodeURIComponent(rules[selector]), 0)
385-
}
384+
})
386385
}
387386

388387
addNewStyleRulesToShadowRoot (themeName, rules, shadowRoot) {
389388
const qualifier = this.element.nodeName.toLowerCase()
390389
let cssText = ''
391390

392-
for (var selector in rules) {
393-
if (!rules.hasOwnProperty(selector)) continue
394-
var sel = qualifier + '[data-theme="' + themeName + '"] ' + selector
395-
391+
Object.keys(rules).forEach(selector => {
392+
const sel = `${qualifier}[data-theme="${themeName}"] ${selector}`
396393
cssText += sel + ' {' + decodeURIComponent(rules[selector]) + '}' + '\n'
397-
}
398-
394+
})
399395
const styleSheet = new CSSStyleSheet()
400396
styleSheet.replaceSync(cssText)
401397
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleSheet]

src/editor.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ export class AbstractEditor {
156156

157157
const displayMode = this.dependenciesFulfilled ? 'block' : 'none'
158158
if (wrapper.tagName === 'TD') {
159-
for (const child in wrapper.childNodes) {
160-
if (wrapper.childNodes.hasOwnProperty(child)) wrapper.childNodes[child].style.display = displayMode
161-
}
159+
Object.keys(wrapper.childNodes).forEach(child => (wrapper.childNodes[child].style.display = displayMode))
162160
} else wrapper.style.display = displayMode
163161
}
164162

@@ -229,12 +227,10 @@ export class AbstractEditor {
229227
let path; let pathParts; let first; let root; let adjustedPath
230228
const myPath = self.container.getAttribute('data-schemapath')
231229

232-
for (const name in this.schema.watch) {
233-
if (!this.schema.watch.hasOwnProperty(name)) continue
230+
Object.keys(this.schema.watch).forEach(name => {
234231
path = this.schema.watch[name]
235-
236232
if (Array.isArray(path)) {
237-
if (path.length < 2) continue
233+
if (path.length < 2) return
238234
pathParts = [path[0]].concat(path[1].split('.'))
239235
} else {
240236
pathParts = path.split('.')
@@ -255,7 +251,7 @@ export class AbstractEditor {
255251
self.jsoneditor.watch(adjustedPath, self.watch_listener)
256252

257253
self.watched[name] = adjustedPath
258-
}
254+
})
259255
}
260256

261257
/* Dynamic header */
@@ -409,15 +405,12 @@ export class AbstractEditor {
409405
const self = this
410406

411407
if (this.watched) {
412-
let val
413-
let editor
414-
for (const name in this.watched) {
415-
if (!this.watched.hasOwnProperty(name)) continue
416-
editor = self.jsoneditor.getEditor(this.watched[name])
417-
val = editor ? editor.getValue() : null
408+
Object.keys(this.watched).forEach(name => {
409+
const editor = self.jsoneditor.getEditor(this.watched[name])
410+
const val = editor ? editor.getValue() : null
418411
if (self.watched_values[name] !== val) changed = true
419412
watched[name] = val
420-
}
413+
})
421414
}
422415

423416
watched.self = this.getValue()
@@ -649,22 +642,25 @@ export class AbstractEditor {
649642
if (this.schema.options && this.schema.options.inputAttributes) {
650643
const inputAttributes = this.schema.options.inputAttributes
651644
const protectedAttributes = ['name', 'type'].concat(inputAttribute)
652-
for (const key in inputAttributes) {
653-
if (inputAttributes.hasOwnProperty(key) && !protectedAttributes.includes(key.toLowerCase())) {
645+
Object.keys(inputAttributes).forEach(key => {
646+
if (!protectedAttributes.includes(key.toLowerCase())) {
654647
this.input.setAttribute(key, inputAttributes[key])
655648
}
656-
}
649+
})
657650
}
658651
}
659652

660653
expandCallbacks (scope, options) {
661-
for (const i in options) {
662-
if (options.hasOwnProperty(i) && options[i] === Object(options[i])) {
663-
options[i] = this.expandCallbacks(scope, options[i])
664-
} else if (options.hasOwnProperty(i) && typeof options[i] === 'string' && typeof this.defaults.callbacks[scope] === 'object' && typeof this.defaults.callbacks[scope][options[i]] === 'function') {
665-
options[i] = this.defaults.callbacks[scope][options[i]].bind(null, this)/* .bind(this); */
654+
const callback = this.defaults.callbacks[scope]
655+
Object.entries(options).forEach(([key, value]) => {
656+
if (value === Object(value)) {
657+
options[key] = this.expandCallbacks(scope, value)
658+
} else if (typeof value === 'string' &&
659+
typeof callback === 'object' &&
660+
typeof callback[value] === 'function') {
661+
options[key] = callback[value].bind(null, this)
666662
}
667-
}
663+
})
668664
return options
669665
}
670666

src/editors/multiselect.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ export class MultiSelectEditor extends AbstractEditor {
2121

2222
getNumColumns () {
2323
let longestText = this.getTitle().length
24-
for (const i in this.select_values) {
25-
if (!this.select_values.hasOwnProperty(i)) continue
26-
longestText = Math.max(longestText, (`${this.select_values[i]}`).length + 4)
27-
}
24+
Object.keys(this.select_values).forEach(i => (longestText = Math.max(longestText, (`${this.select_values[i]}`).length + 4)))
2825

2926
return Math.min(12, Math.max(longestText / 7, 2))
3027
}
@@ -119,18 +116,16 @@ export class MultiSelectEditor extends AbstractEditor {
119116
}
120117

121118
setValue (value, initial) {
122-
let i
123119
value = value || []
124120
if (!(Array.isArray(value))) value = [value]
125121

126122
/* Make sure we are dealing with an array of strings so we can check for strict equality */
127123
value = value.map(e => `${e}`)
128124

129125
/* Update selected status of options */
130-
for (i in this.select_options) {
131-
if (!this.select_options.hasOwnProperty(i)) continue
126+
Object.keys(this.select_options).forEach(i => {
132127
this.select_options[i][this.input_type === 'select' ? 'selected' : 'checked'] = (value.includes(i))
133-
}
128+
})
134129

135130
this.updateValue(value)
136131
this.onChange(true)
@@ -176,10 +171,7 @@ export class MultiSelectEditor extends AbstractEditor {
176171
if (this.input) {
177172
this.input.disabled = false
178173
} else if (this.inputs) {
179-
for (const i in this.inputs) {
180-
if (!this.inputs.hasOwnProperty(i)) continue
181-
this.inputs[i].disabled = false
182-
}
174+
Object.keys(this.inputs).forEach(i => (this.inputs[i].disabled = false))
183175
}
184176
super.enable()
185177
}
@@ -190,10 +182,7 @@ export class MultiSelectEditor extends AbstractEditor {
190182
if (this.input) {
191183
this.input.disabled = true
192184
} else if (this.inputs) {
193-
for (const i in this.inputs) {
194-
if (!this.inputs.hasOwnProperty(i)) continue
195-
this.inputs[i].disabled = true
196-
}
185+
Object.keys(this.inputs).forEach(i => (this.inputs[i].disabled = true))
197186
}
198187
super.disable()
199188
}

src/editors/object.js

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@ export class ObjectEditor extends AbstractEditor {
1313
register () {
1414
super.register()
1515
if (this.editors) {
16-
for (const i in this.editors) {
17-
if (!this.editors.hasOwnProperty(i)) continue
18-
this.editors[i].register()
19-
}
16+
Object.values(this.editors).forEach(e => e.register())
2017
}
2118
}
2219

2320
unregister () {
2421
super.unregister()
2522
if (this.editors) {
26-
for (const i in this.editors) {
27-
if (!this.editors.hasOwnProperty(i)) continue
28-
this.editors[i].unregister()
29-
}
23+
Object.values(this.editors).forEach(e => e.unregister())
3024
}
3125
}
3226

@@ -41,13 +35,12 @@ export class ObjectEditor extends AbstractEditor {
4135

4236
super.enable()
4337
if (this.editors) {
44-
for (const i in this.editors) {
45-
if (!this.editors.hasOwnProperty(i)) continue
46-
if (this.editors[i].isActive()) {
47-
this.editors[i].enable()
38+
Object.values(this.editors).forEach(e => {
39+
if (e.isActive()) {
40+
e.enable()
4841
}
49-
this.editors[i].optInCheckbox.disabled = false
50-
}
42+
e.optInCheckbox.disabled = false
43+
})
5144
}
5245
}
5346
}
@@ -60,13 +53,12 @@ export class ObjectEditor extends AbstractEditor {
6053

6154
super.disable()
6255
if (this.editors) {
63-
for (const i in this.editors) {
64-
if (!this.editors.hasOwnProperty(i)) continue
65-
if (this.editors[i].isActive()) {
66-
this.editors[i].disable(alwaysDisabled)
56+
Object.values(this.editors).forEach(e => {
57+
if (e.isActive()) {
58+
e.disable(alwaysDisabled)
6759
}
68-
this.editors[i].optInCheckbox.disabled = true
69-
}
60+
e.optInCheckbox.disabled = true
61+
})
7062
}
7163
}
7264

@@ -351,15 +343,14 @@ export class ObjectEditor extends AbstractEditor {
351343

352344
/* Any matching patternProperties should be merged in */
353345
if (this.schema.patternProperties) {
354-
for (const i in this.schema.patternProperties) {
355-
if (!this.schema.patternProperties.hasOwnProperty(i)) continue
346+
Object.keys(this.schema.patternProperties).forEach(i => {
356347
const regex = new RegExp(i)
357348
if (regex.test(key)) {
358349
schema.allOf = schema.allOf || []
359350
schema.allOf.push(this.schema.patternProperties[i])
360351
matched = true
361352
}
362-
}
353+
})
363354
}
364355

365356
/* Hasn't matched other rules, use additionalProperties schema */
@@ -1077,21 +1068,18 @@ export class ObjectEditor extends AbstractEditor {
10771068
return undefined
10781069
}
10791070
const result = super.getValue()
1071+
const isEmpty = i => typeof result[i] === 'undefined' || result[i] === '' ||
1072+
(
1073+
result[i] === Object(result[i]) &&
1074+
Object.keys(result[i]).length === 0 &&
1075+
result[i].constructor === Object
1076+
)
10801077
if (this.jsoneditor.options.remove_empty_properties || this.options.remove_empty_properties) {
1081-
for (const i in result) {
1082-
if (result.hasOwnProperty(i)) {
1083-
if (
1084-
typeof result[i] === 'undefined' ||
1085-
result[i] === '' ||
1086-
(
1087-
result[i] === Object(result[i]) &&
1088-
Object.keys(result[i]).length === 0 &&
1089-
result[i].constructor === Object
1090-
)) {
1091-
delete result[i]
1092-
}
1078+
Object.keys(result).forEach(i => {
1079+
if (isEmpty(i)) {
1080+
delete result[i]
10931081
}
1094-
}
1082+
})
10951083
}
10961084

10971085
return result
@@ -1100,12 +1088,11 @@ export class ObjectEditor extends AbstractEditor {
11001088
refreshValue () {
11011089
this.value = {}
11021090

1103-
for (const i in this.editors) {
1104-
if (!this.editors.hasOwnProperty(i)) continue
1091+
Object.keys(this.editors).forEach(i => {
11051092
if (this.editors[i].isActive()) {
11061093
this.value[i] = this.editors[i].getValue()
11071094
}
1108-
}
1095+
})
11091096

11101097
if (this.adding_property) this.refreshAddProperties()
11111098
}
@@ -1116,13 +1103,10 @@ export class ObjectEditor extends AbstractEditor {
11161103
return
11171104
}
11181105

1119-
let canAdd = false; let numProps = 0; let i; let showModal = false
1106+
let canAdd = false; let numProps = 0; let showModal = false
11201107

11211108
/* Get number of editors */
1122-
for (i in this.editors) {
1123-
if (!this.editors.hasOwnProperty(i)) continue
1124-
numProps++
1125-
}
1109+
Object.keys(this.editors).forEach(i => numProps++)
11261110

11271111
/* Determine if we can add back removed properties */
11281112
canAdd = this.canHaveAdditionalProperties() && !(typeof this.schema.maxProperties !== 'undefined' && numProps >= this.schema.maxProperties)
@@ -1133,9 +1117,7 @@ export class ObjectEditor extends AbstractEditor {
11331117
this.addproperty_checkboxes = {}
11341118

11351119
/* Check for which editors can't be removed or added back */
1136-
for (i in this.cached_editors) {
1137-
if (!this.cached_editors.hasOwnProperty(i)) continue
1138-
1120+
Object.keys(this.cached_editors).forEach(i => {
11391121
this.addPropertyCheckbox(i)
11401122

11411123
if (this.isRequiredObject(this.cached_editors[i]) && i in this.editors) {
@@ -1155,19 +1137,18 @@ export class ObjectEditor extends AbstractEditor {
11551137
} else {
11561138
showModal = true
11571139
}
1158-
}
1140+
})
11591141

11601142
if (this.canHaveAdditionalProperties()) {
11611143
showModal = true
11621144
}
11631145

11641146
/* Additional addproperty checkboxes not tied to a current editor */
1165-
for (i in this.schema.properties) {
1166-
if (!this.schema.properties.hasOwnProperty(i)) continue
1167-
if (this.cached_editors[i]) continue
1147+
Object.keys(this.schema.properties).forEach(i => {
1148+
if (this.cached_editors[i]) return
11681149
showModal = true
11691150
this.addPropertyCheckbox(i)
1170-
}
1151+
})
11711152

11721153
/* If no editors can be added or removed, hide the modal button */
11731154
if (!showModal) {

src/editors/string.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,19 @@ export class StringEditor extends AbstractEditor {
240240
ajustIMaskOptions (obj) {
241241
/* iMask config format is not JSON friendly, so function and regex based mask */
242242
/* properties have to be adjusted from string to the correct format */
243-
for (const prop in obj) {
244-
if (obj.hasOwnProperty(prop)) {
245-
if (obj[prop] === Object(obj[prop])) obj[prop] = this.ajustIMaskOptions(obj[prop])
246-
else if (prop === 'mask') {
247-
if (obj[prop].substr(0, 6) === 'regex:') {
248-
const regExMatch = obj[prop].match(/^regex:\/(.*)\/([gimsuy]*)$/)
249-
if (regExMatch !== null) {
250-
try {
251-
obj[prop] = new RegExp(regExMatch[1], regExMatch[2])
252-
} catch (e) { }
253-
}
254-
} else obj[prop] = this.getGlobalPropertyFromString(obj[prop])
255-
}
243+
Object.keys(obj).forEach(prop => {
244+
if (obj[prop] === Object(obj[prop])) obj[prop] = this.ajustIMaskOptions(obj[prop])
245+
else if (prop === 'mask') {
246+
if (obj[prop].substr(0, 6) === 'regex:') {
247+
const regExMatch = obj[prop].match(/^regex:\/(.*)\/([gimsuy]*)$/)
248+
if (regExMatch !== null) {
249+
try {
250+
obj[prop] = new RegExp(regExMatch[1], regExMatch[2])
251+
} catch (e) { }
252+
}
253+
} else obj[prop] = this.getGlobalPropertyFromString(obj[prop])
256254
}
257-
}
255+
})
258256
return obj
259257
}
260258

0 commit comments

Comments
 (0)