Skip to content

Commit b046503

Browse files
author
tohosaku
committed
Replace "each" to forEach, find, filter, map...
1 parent aa6255a commit b046503

File tree

16 files changed

+104
-154
lines changed

16 files changed

+104
-154
lines changed

src/core.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { editors } from './editors/index.js'
55
import { templates } from './templates/index.js'
66
import { iconlibs } from './iconlibs/index.js'
77
import { themes } from './themes/index.js'
8-
import { extend, each, getShadowParent } from './utilities.js'
8+
import { extend, getShadowParent } from './utilities.js'
99

1010
export class JSONEditor {
1111
constructor (element, options = {}) {
@@ -191,24 +191,18 @@ export class JSONEditor {
191191
}
192192

193193
getEditorsRules () {
194-
const rules = {}
195-
196-
each(JSONEditor.defaults.editors, (i, editorClass) => editorClass.rules && extend(rules, editorClass.rules))
197-
198-
return rules
194+
const extendRule = (rules, editorClass) => editorClass.rules ? extend(rules, editorClass.rules) : rules
195+
return Object.values(JSONEditor.defaults.editors).reduce(extendRule, {})
199196
}
200197

201198
getEditorClass (schema) {
202199
let classname
203200

204201
schema = this.expandSchema(schema)
205202

206-
each(JSONEditor.defaults.resolvers, (i, resolver) => {
207-
const tmp = resolver(schema)
208-
if (tmp && JSONEditor.defaults.editors[tmp]) {
209-
classname = tmp
210-
return false
211-
}
203+
JSONEditor.defaults.resolvers.find(resolver => {
204+
classname = resolver(schema)
205+
return classname && JSONEditor.defaults.editors[classname]
212206
})
213207

214208
if (!classname) throw new Error(`Unknown editor for schema ${JSON.stringify(schema)}`)

src/defaults.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { each } from './utilities.js'
21
import { resolvers } from './resolvers.js'
32

43
/* default theme */
@@ -252,7 +251,7 @@ languages.en = {
252251
}
253252

254253
/* Default per-editor options */
255-
each(editors, (i, editor) => { editors[i].options = editor.options || {} })
254+
Object.entries(editors).forEach(([i, editor]) => { editors[i].options = editor.options || {} })
256255

257256
/* Default upload handler */
258257
function upload (type, file, cbs) {

src/editor.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extend, each } from './utilities.js'
1+
import { extend } from './utilities.js'
22

33
/**
44
* All editors should extend from this class
@@ -506,9 +506,9 @@ export class AbstractEditor {
506506
destroy () {
507507
const self = this
508508
this.unregister(this)
509-
each(this.watched, (name, adjustedPath) => {
510-
self.jsoneditor.unwatch(adjustedPath, self.watch_listener)
511-
})
509+
if (this.watched) {
510+
Object.values(this.watched).forEach(adjustedPath => self.jsoneditor.unwatch(adjustedPath, self.watch_listener))
511+
}
512512

513513
this.watched = null
514514
this.watched_values = null
@@ -580,7 +580,7 @@ export class AbstractEditor {
580580

581581
/* Determine how many times each attribute name is used. */
582582
/* This helps us pick the most distinct display text for the schemas. */
583-
each(arr, (i, el) => {
583+
arr.forEach(el => {
584584
if (el.title) {
585585
used[el.title] = used[el.title] || 0
586586
used[el.title]++
@@ -600,7 +600,7 @@ export class AbstractEditor {
600600
})
601601

602602
/* Determine display text for each element of the array */
603-
each(arr, (i, el) => {
603+
arr.forEach(el => {
604604
let name
605605

606606
/* If it's a simple string */
@@ -622,7 +622,7 @@ export class AbstractEditor {
622622

623623
/* Replace identical display text with "text 1", "text 2", etc. */
624624
const inc = {}
625-
each(disp, (i, name) => {
625+
disp.forEach((name, i) => {
626626
inc[name] = inc[name] || 0
627627
inc[name]++
628628

src/editors/array.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AbstractEditor } from '../editor.js'
2-
import { extend, each, trigger } from '../utilities.js'
2+
import { extend, trigger } from '../utilities.js'
33

44
export class ArrayEditor extends AbstractEditor {
55
askConfirmation () {
@@ -292,7 +292,7 @@ export class ArrayEditor extends AbstractEditor {
292292
empty (hard) {
293293
if (!this.rows) return
294294
const self = this
295-
each(this.rows, (i, row) => {
295+
this.rows.forEach((row, i) => {
296296
if (hard) {
297297
if (row.tab && row.tab.parentNode) row.tab.parentNode.removeChild(row.tab)
298298
self.destroyRow(row, true)
@@ -326,7 +326,7 @@ export class ArrayEditor extends AbstractEditor {
326326

327327
refreshTabs (refreshHeaders) {
328328
const self = this
329-
each(this.rows, (i, row) => {
329+
this.rows.forEach(row => {
330330
if (!row.tab) return
331331

332332
if (refreshHeaders) {
@@ -356,7 +356,7 @@ export class ArrayEditor extends AbstractEditor {
356356
}
357357

358358
const self = this
359-
each(value, (i, val) => {
359+
value.forEach((val, i) => {
360360
if (self.rows[i]) {
361361
/* TODO: don't set the row's value if it hasn't changed */
362362
self.rows[i].setValue(val, initial)
@@ -380,13 +380,8 @@ export class ArrayEditor extends AbstractEditor {
380380
self.rows = self.rows.slice(0, value.length)
381381

382382
/* Set the active tab */
383-
let newActiveTab = null
384-
each(self.rows, (i, row) => {
385-
if (row.tab === self.active_tab) {
386-
newActiveTab = row.tab
387-
return false
388-
}
389-
})
383+
const row = self.rows.find(row => row.tab === self.active_tab)
384+
let newActiveTab = typeof row !== 'undefined' ? row.tab : null
390385
if (!newActiveTab && self.rows.length) newActiveTab = self.rows[0].tab
391386

392387
self.active_tab = newActiveTab
@@ -403,18 +398,14 @@ export class ArrayEditor extends AbstractEditor {
403398
refreshValue (force) {
404399
const self = this
405400
const oldi = this.value ? this.value.length : 0
406-
this.value = []
407-
408-
each(this.rows, (i, editor) => {
409-
/* Get the value for this editor */
410-
self.value[i] = editor.getValue()
411-
})
401+
/* Get the value for this editor */
402+
this.value = this.rows.map(editor => editor.getValue())
412403

413404
if (oldi !== this.value.length || force) {
414405
/* If we currently have minItems items in the array */
415406
const minItems = this.schema.minItems && this.schema.minItems >= this.rows.length
416407

417-
each(this.rows, (i, editor) => {
408+
this.rows.forEach((editor, i) => {
418409
/* Hide the move down button for the last row */
419410
if (editor.movedown_button) {
420411
if (i === self.rows.length - 1) {
@@ -525,16 +516,9 @@ export class ArrayEditor extends AbstractEditor {
525516
}
526517

527518
const i = this.getAttribute('data-i') * 1
528-
const value = self.getValue()
529-
const newval = []
519+
const newval = self.getValue().filter((row, j) => j !== i)
530520
let newActiveTab = null
531521

532-
each(value, (j, row) => {
533-
if (j !== i) {
534-
newval.push(row)
535-
}
536-
})
537-
538522
const editor = self.rows[i]
539523

540524
self.setValue(newval)
@@ -570,7 +554,7 @@ export class ArrayEditor extends AbstractEditor {
570554
e.stopPropagation()
571555
const i = this.getAttribute('data-i') * 1
572556

573-
each(value, (j, row) => {
557+
value.forEach((row, j) => {
574558
if (j === i) {
575559
value.push(row)
576560
}
@@ -784,7 +768,7 @@ export class ArrayEditor extends AbstractEditor {
784768
/* Get all the errors that pertain to this editor */
785769
const myErrors = []
786770
const otherErrors = []
787-
each(errors, (i, error) => {
771+
errors.forEach(error => {
788772
if (error.path === self.path) {
789773
myErrors.push(error)
790774
} else {
@@ -797,7 +781,7 @@ export class ArrayEditor extends AbstractEditor {
797781
if (myErrors.length) {
798782
this.error_holder.innerHTML = ''
799783
this.error_holder.style.display = ''
800-
each(myErrors, (i, error) => {
784+
myErrors.forEach(error => {
801785
self.error_holder.appendChild(self.theme.getErrorMessage(error.message))
802786
})
803787
/* Hide error area */
@@ -807,8 +791,8 @@ export class ArrayEditor extends AbstractEditor {
807791
}
808792

809793
/* Show errors for child editors */
810-
each(this.rows, (i, row) => {
794+
this.rows.forEach(row =>
811795
row.showValidationErrors(otherErrors)
812-
})
796+
)
813797
}
814798
}

src/editors/checkbox.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { AbstractEditor } from '../editor.js'
2-
import { each } from '../utilities.js'
32

43
export class CheckboxEditor extends AbstractEditor {
54
setValue (value, initial) {
@@ -80,13 +79,13 @@ export class CheckboxEditor extends AbstractEditor {
8079

8180
this.previous_error_setting = this.jsoneditor.options.show_errors
8281

83-
const messages = []
84-
each(errors, (i, error) => {
82+
const addMessage = (messages, error) => {
8583
if (error.path === self.path) {
8684
messages.push(error.message)
8785
}
88-
})
89-
86+
return messages
87+
}
88+
const messages = errors.reduce(addMessage, [])
9089
this.input.controlgroup = this.control
9190

9291
if (messages.length) {

src/editors/describedby.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* hyper-link describeBy Editor */
22
import { AbstractEditor } from '../editor.js'
3-
import { extend, each } from '../utilities.js'
3+
import { extend } from '../utilities.js'
44

55
export class DescribedByEditor extends AbstractEditor {
66
register () {
@@ -72,7 +72,7 @@ export class DescribedByEditor extends AbstractEditor {
7272

7373
this.register()
7474

75-
each(this.editors, (ref, editor) => {
75+
this.editors.forEach((editor, ref) => {
7676
if (!editor) return
7777
if (self.currentEditor === ref) {
7878
editor.container.style.display = ''
@@ -169,7 +169,7 @@ export class DescribedByEditor extends AbstractEditor {
169169
}
170170

171171
destroy () {
172-
each(this.editors, (i, editor) => {
172+
this.editors.forEach(editor => {
173173
if (editor) editor.destroy()
174174
})
175175

@@ -181,7 +181,7 @@ export class DescribedByEditor extends AbstractEditor {
181181
}
182182

183183
showValidationErrors (errors) {
184-
each(this.editors, (i, editor) => {
184+
this.editors.forEach(editor => {
185185
if (!editor) return
186186
editor.showValidationErrors(errors)
187187
})

src/editors/enum.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* Enum Editor (used for objects and arrays with enumerated values) */
22
import { AbstractEditor } from '../editor.js'
3-
import { each } from '../utilities.js'
43

54
export class EnumEditor extends AbstractEditor {
65
getNumColumns () {
@@ -50,7 +49,7 @@ export class EnumEditor extends AbstractEditor {
5049
const self = this
5150
self.selected = -1
5251
const stringified = JSON.stringify(this.value)
53-
each(this.enum, (i, el) => {
52+
this.enum.forEach((el, i) => {
5453
if (stringified === JSON.stringify(el)) {
5554
self.selected = i
5655
return false
@@ -81,26 +80,31 @@ export class EnumEditor extends AbstractEditor {
8180

8281
getHTML (el) {
8382
const self = this
83+
const each = (obj, callback) => {
84+
if (Array.isArray(obj) || (typeof obj.length === 'number' && obj.length > 0 && (obj.length - 1) in obj)) {
85+
Array.from(obj).forEach((e, i) => callback(i, e))
86+
} else {
87+
Object.entries(obj).forEach(([key, value]) => callback(key, value))
88+
}
89+
}
8490

8591
if (el === null) {
8692
return '<em>null</em>'
8793
/* Array or Object */
8894
} else if (typeof el === 'object') {
8995
/* TODO: use theme */
9096
let ret = ''
91-
92-
each(el, (i, child) => {
97+
const callback = (i, child) => {
9398
let html = self.getHTML(child)
94-
9599
/* Add the keys to object children */
96100
if (!(Array.isArray(el))) {
97101
/* TODO: use theme */
98102
html = `<div><em>${i}</em>: ${html}</div>`
99103
}
100-
101104
/* TODO: use theme */
102105
ret += `<li>${html}</li>`
103-
})
106+
}
107+
each(el, callback)
104108

105109
if (Array.isArray(el)) ret = `<ol>${ret}</ol>`
106110
else ret = `<ul style='margin-top:0;margin-bottom:0;padding-top:0;padding-bottom:0;'>${ret}</ul>`

0 commit comments

Comments
 (0)