Skip to content

Commit e3df16d

Browse files
committed
Better minification with # private properties
1 parent baf62c5 commit e3df16d

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

src/morphlex.ts

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ function moveBefore(parent: ParentNode, node: ChildNode, insertionPoint: ChildNo
101101
}
102102

103103
class Morph {
104-
private readonly idMap: IdMap = new WeakMap()
105-
private readonly options: Options
104+
readonly #idMap: IdMap = new WeakMap()
105+
readonly #options: Options
106106

107107
constructor(options: Options = {}) {
108-
this.options = options
108+
this.#options = options
109109
}
110110

111111
// Find longest increasing subsequence to minimize moves during reordering
112112
// Returns the indices in the sequence that form the LIS
113-
private longestIncreasingSubsequence(sequence: Array<number>): Array<number> {
113+
#longestIncreasingSubsequence(sequence: Array<number>): Array<number> {
114114
const n = sequence.length
115115
if (n === 0) return []
116116

@@ -167,89 +167,89 @@ class Morph {
167167

168168
morph(from: ChildNode, to: ChildNode | NodeListOf<ChildNode>): void {
169169
if (isParentNode(from)) {
170-
this.mapIdSets(from)
170+
this.#mapIdSets(from)
171171
}
172172

173173
if (to instanceof NodeList) {
174-
this.mapIdSetsForEach(to)
175-
this.morphOneToMany(from, to)
174+
this.#mapIdSetsForEach(to)
175+
this.#morphOneToMany(from, to)
176176
} else if (isParentNode(to)) {
177-
this.mapIdSets(to)
178-
this.morphOneToOne(from, to)
177+
this.#mapIdSets(to)
178+
this.#morphOneToOne(from, to)
179179
}
180180
}
181181

182-
private morphOneToMany(from: ChildNode, to: NodeListOf<ChildNode>): void {
182+
#morphOneToMany(from: ChildNode, to: NodeListOf<ChildNode>): void {
183183
const length = to.length
184184

185185
if (length === 0) {
186-
this.removeNode(from)
186+
this.#removeNode(from)
187187
} else if (length === 1) {
188-
this.morphOneToOne(from, to[0]!)
188+
this.#morphOneToOne(from, to[0]!)
189189
} else if (length > 1) {
190190
const newNodes = Array.from(to)
191-
this.morphOneToOne(from, newNodes.shift()!)
191+
this.#morphOneToOne(from, newNodes.shift()!)
192192
const insertionPoint = from.nextSibling
193193
const parent = from.parentNode || document
194194

195195
for (const newNode of newNodes) {
196-
if (this.options.beforeNodeAdded?.(parent, newNode, insertionPoint) ?? true) {
196+
if (this.#options.beforeNodeAdded?.(parent, newNode, insertionPoint) ?? true) {
197197
moveBefore(parent, newNode, insertionPoint)
198-
this.options.afterNodeAdded?.(newNode)
198+
this.#options.afterNodeAdded?.(newNode)
199199
}
200200
}
201201
}
202202
}
203203

204-
private morphOneToOne(from: ChildNode, to: ChildNode): void {
204+
#morphOneToOne(from: ChildNode, to: ChildNode): void {
205205
// Fast path: if nodes are exactly the same object, skip morphing
206206
if (from === to) return
207207
if (from.isEqualNode?.(to)) return
208208

209-
if (!(this.options.beforeNodeVisited?.(from, to) ?? true)) return
209+
if (!(this.#options.beforeNodeVisited?.(from, to) ?? true)) return
210210

211211
const pair: PairOfNodes<ChildNode> = [from, to]
212212

213213
if (isElementPair(pair)) {
214214
if (isMatchingElementPair(pair)) {
215-
this.morphMatchingElements(pair)
215+
this.#morphMatchingElements(pair)
216216
} else {
217-
this.morphNonMatchingElements(pair)
217+
this.#morphNonMatchingElements(pair)
218218
}
219219
} else {
220-
this.morphOtherNode(pair)
220+
this.#morphOtherNode(pair)
221221
}
222222

223-
this.options.afterNodeVisited?.(from, to)
223+
this.#options.afterNodeVisited?.(from, to)
224224
}
225225

226-
private morphMatchingElements(pair: PairOfMatchingElements<Element>): void {
226+
#morphMatchingElements(pair: PairOfMatchingElements<Element>): void {
227227
const [from, to] = pair
228228

229229
if (from.hasAttributes() || to.hasAttributes()) {
230-
this.visitAttributes(pair)
230+
this.#visitAttributes(pair)
231231
}
232232

233233
if (isTextAreaElement(from) && isTextAreaElement(to)) {
234-
this.visitTextArea(pair as PairOfMatchingElements<HTMLTextAreaElement>)
234+
this.#visitTextArea(pair as PairOfMatchingElements<HTMLTextAreaElement>)
235235
} else if (from.hasChildNodes() || to.hasChildNodes()) {
236236
this.visitChildNodes(pair)
237237
}
238238
}
239239

240-
private morphNonMatchingElements([from, to]: PairOfNodes<Element>): void {
241-
this.replaceNode(from, to)
240+
#morphNonMatchingElements([from, to]: PairOfNodes<Element>): void {
241+
this.#replaceNode(from, to)
242242
}
243243

244-
private morphOtherNode([from, to]: PairOfNodes<ChildNode>): void {
244+
#morphOtherNode([from, to]: PairOfNodes<ChildNode>): void {
245245
if (from.nodeType === to.nodeType && from.nodeValue !== null && to.nodeValue !== null) {
246246
from.nodeValue = to.nodeValue
247247
} else {
248-
this.replaceNode(from, to)
248+
this.#replaceNode(from, to)
249249
}
250250
}
251251

252-
private visitAttributes([from, to]: PairOfMatchingElements<Element>): void {
252+
#visitAttributes([from, to]: PairOfMatchingElements<Element>): void {
253253
if (from.hasAttribute("morphlex-dirty")) {
254254
from.removeAttribute("morphlex-dirty")
255255
}
@@ -258,33 +258,33 @@ class Morph {
258258
for (const { name, value } of to.attributes) {
259259
if (name === "value") {
260260
if (isInputElement(from) && from.value !== value) {
261-
if (!this.options.preserveModified || from.value === from.defaultValue) {
261+
if (!this.#options.preserveModified || from.value === from.defaultValue) {
262262
from.value = value
263263
}
264264
}
265265
}
266266

267267
if (name === "selected") {
268268
if (isOptionElement(from) && !from.selected) {
269-
if (!this.options.preserveModified || from.selected === from.defaultSelected) {
269+
if (!this.#options.preserveModified || from.selected === from.defaultSelected) {
270270
from.selected = true
271271
}
272272
}
273273
}
274274

275275
if (name === "checked") {
276276
if (isInputElement(from) && !from.checked) {
277-
if (!this.options.preserveModified || from.checked === from.defaultChecked) {
277+
if (!this.#options.preserveModified || from.checked === from.defaultChecked) {
278278
from.checked = true
279279
}
280280
}
281281
}
282282

283283
const oldValue = from.getAttribute(name)
284284

285-
if (oldValue !== value && (this.options.beforeAttributeUpdated?.(from, name, value) ?? true)) {
285+
if (oldValue !== value && (this.#options.beforeAttributeUpdated?.(from, name, value) ?? true)) {
286286
from.setAttribute(name, value)
287-
this.options.afterAttributeUpdated?.(from, name, oldValue)
287+
this.#options.afterAttributeUpdated?.(from, name, oldValue)
288288
}
289289
}
290290

@@ -297,29 +297,29 @@ class Morph {
297297
if (!to.hasAttribute(name)) {
298298
if (name === "selected") {
299299
if (isOptionElement(from) && from.selected) {
300-
if (!this.options.preserveModified || from.selected === from.defaultSelected) {
300+
if (!this.#options.preserveModified || from.selected === from.defaultSelected) {
301301
from.selected = false
302302
}
303303
}
304304
}
305305

306306
if (name === "checked") {
307307
if (isInputElement(from) && from.checked) {
308-
if (!this.options.preserveModified || from.checked === from.defaultChecked) {
308+
if (!this.#options.preserveModified || from.checked === from.defaultChecked) {
309309
from.checked = false
310310
}
311311
}
312312
}
313313

314-
if (this.options.beforeAttributeUpdated?.(from, name, null) ?? true) {
314+
if (this.#options.beforeAttributeUpdated?.(from, name, null) ?? true) {
315315
from.removeAttribute(name)
316-
this.options.afterAttributeUpdated?.(from, name, value)
316+
this.#options.afterAttributeUpdated?.(from, name, value)
317317
}
318318
}
319319
}
320320
}
321321

322-
private visitTextArea([from, to]: PairOfMatchingElements<HTMLTextAreaElement>): void {
322+
#visitTextArea([from, to]: PairOfMatchingElements<HTMLTextAreaElement>): void {
323323
const newTextContent = to.textContent || ""
324324
const isModified = from.value !== from.defaultValue
325325

@@ -328,13 +328,13 @@ class Morph {
328328
from.textContent = newTextContent
329329
}
330330

331-
if (this.options.preserveModified && isModified) return
331+
if (this.#options.preserveModified && isModified) return
332332

333333
from.value = from.defaultValue
334334
}
335335

336336
visitChildNodes([from, to]: PairOfMatchingElements<Element>): void {
337-
if (!(this.options.beforeChildrenVisited?.(from) ?? true)) return
337+
if (!(this.#options.beforeChildrenVisited?.(from) ?? true)) return
338338
const parent = from
339339

340340
const fromChildNodes = from.childNodes
@@ -388,12 +388,12 @@ class Morph {
388388
const element = toChildNodes[i]!
389389
if (!isElement(element)) continue
390390

391-
const idSet = this.idMap.get(element)
391+
const idSet = this.#idMap.get(element)
392392
if (!idSet) continue
393393

394394
candidateLoop: for (const candidate of candidateElements) {
395395
if (isElement(candidate)) {
396-
const candidateIdSet = this.idMap.get(candidate)
396+
const candidateIdSet = this.#idMap.get(candidate)
397397
if (candidateIdSet) {
398398
for (const id of idSet) {
399399
if (candidateIdSet.has(id)) {
@@ -500,7 +500,7 @@ class Morph {
500500
}
501501

502502
// Find LIS - these nodes don't need to move
503-
const lisIndices = this.longestIncreasingSubsequence(sequence)
503+
const lisIndices = this.#longestIncreasingSubsequence(sequence)
504504
const shouldNotMove = new Set<number>()
505505
for (const idx of lisIndices) {
506506
shouldNotMove.add(sequence[idx]!)
@@ -517,7 +517,7 @@ class Morph {
517517
if (!shouldNotMove.has(matchIndex)) {
518518
moveBefore(parent, match, insertionPoint)
519519
}
520-
this.morphOneToOne(match, node)
520+
this.#morphOneToOne(match, node)
521521
insertionPoint = match.nextSibling
522522
// Skip over any nodes that will be removed to avoid unnecessary moves
523523
while (
@@ -527,9 +527,9 @@ class Morph {
527527
insertionPoint = insertionPoint.nextSibling
528528
}
529529
} else {
530-
if (this.options.beforeNodeAdded?.(parent, node, insertionPoint) ?? true) {
530+
if (this.#options.beforeNodeAdded?.(parent, node, insertionPoint) ?? true) {
531531
moveBefore(parent, node, insertionPoint)
532-
this.options.afterNodeAdded?.(node)
532+
this.#options.afterNodeAdded?.(node)
533533
insertionPoint = node.nextSibling
534534
// Skip over any nodes that will be removed to avoid unnecessary moves
535535
while (
@@ -544,43 +544,43 @@ class Morph {
544544

545545
// Remove any remaining unmatched candidates
546546
for (const candidate of candidateNodes) {
547-
this.removeNode(candidate)
547+
this.#removeNode(candidate)
548548
}
549549

550550
for (const candidate of candidateElements) {
551-
this.removeNode(candidate)
551+
this.#removeNode(candidate)
552552
}
553553

554-
this.options.afterChildrenVisited?.(from)
554+
this.#options.afterChildrenVisited?.(from)
555555
}
556556

557-
private replaceNode(node: ChildNode, newNode: ChildNode): void {
557+
#replaceNode(node: ChildNode, newNode: ChildNode): void {
558558
const parent = node.parentNode || document
559559
const insertionPoint = node
560-
if (this.options.beforeNodeAdded?.(parent, newNode, insertionPoint) ?? true) {
560+
if (this.#options.beforeNodeAdded?.(parent, newNode, insertionPoint) ?? true) {
561561
moveBefore(parent, newNode, insertionPoint)
562-
this.options.afterNodeAdded?.(newNode)
563-
this.removeNode(node)
562+
this.#options.afterNodeAdded?.(newNode)
563+
this.#removeNode(node)
564564
}
565565
}
566566

567-
private removeNode(node: ChildNode): void {
568-
if (this.options.beforeNodeRemoved?.(node) ?? true) {
567+
#removeNode(node: ChildNode): void {
568+
if (this.#options.beforeNodeRemoved?.(node) ?? true) {
569569
node.remove()
570-
this.options.afterNodeRemoved?.(node)
570+
this.#options.afterNodeRemoved?.(node)
571571
}
572572
}
573573

574-
private mapIdSetsForEach(nodeList: NodeList): void {
574+
#mapIdSetsForEach(nodeList: NodeList): void {
575575
for (const childNode of nodeList) {
576576
if (isParentNode(childNode)) {
577-
this.mapIdSets(childNode)
577+
this.#mapIdSets(childNode)
578578
}
579579
}
580580
}
581581

582582
// For each node with an ID, push that ID into the IdSet on the IdMap, for each of its parent elements.
583-
private mapIdSets(node: ParentNode): void {
583+
#mapIdSets(node: ParentNode): void {
584584
for (const elementWithId of node.querySelectorAll("[id]")) {
585585
const id = elementWithId.id
586586

@@ -589,9 +589,9 @@ class Morph {
589589
let currentElement: Element | null = elementWithId
590590

591591
while (currentElement) {
592-
const idSet: IdSet | undefined = this.idMap.get(currentElement)
592+
const idSet: IdSet | undefined = this.#idMap.get(currentElement)
593593
if (idSet) idSet.add(id)
594-
else this.idMap.set(currentElement, new Set([id]))
594+
else this.#idMap.set(currentElement, new Set([id]))
595595
if (currentElement === node) break
596596
currentElement = currentElement.parentElement
597597
}

0 commit comments

Comments
 (0)