Skip to content

Commit 3a6449d

Browse files
committed
use Object.keys
1 parent d7dc7fd commit 3a6449d

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

src/dom.ts

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import { Attributes, DOM, IFiber } from "./type"
2-
import { isStr, LANE } from "./reconcile"
1+
import { Attributes, DOM, IFiber } from './type'
2+
import { isStr, LANE } from './reconcile'
33

4-
const hasOwnProperty = Object.prototype.hasOwnProperty;
5-
6-
const defaultObj = {} as const;
4+
const defaultObj = {} as const
75

86
const jointIter = <P extends Attributes>(
97
aProps = defaultObj as P,
108
bProps = defaultObj as P,
119
callback: (name: string, a: any, b: any) => void
1210
) => {
13-
for (const name in aProps) {
14-
if (hasOwnProperty.call(aProps, name)) {
15-
callback(name, aProps[name], bProps[name]);
16-
}
17-
}
18-
for (const name in bProps) {
19-
if (hasOwnProperty.call(bProps, name) && !hasOwnProperty.call(aProps, name)) {
20-
callback(name, undefined, bProps[name]);
21-
}
22-
}
11+
if (!aProps || !bProps) return
12+
Object.keys(aProps).forEach(k => callback(k, aProps[k], bProps[k]))
13+
Object.keys(bProps).forEach(k => callback(k, undefined, bProps[k]))
2314
}
2415

2516
export const updateElement = <P extends Attributes>(
@@ -28,19 +19,19 @@ export const updateElement = <P extends Attributes>(
2819
bProps: P
2920
) => {
3021
jointIter(aProps, bProps, (name, a, b) => {
31-
if (a === b || name === "children") {
32-
} else if (name === "style" && !isStr(b)) {
22+
if (a === b || name === 'children') {
23+
} else if (name === 'style' && !isStr(b)) {
3324
jointIter(a, b, (styleKey, aStyle, bStyle) => {
3425
if (aStyle !== bStyle) {
35-
; (dom as any)[name][styleKey] = bStyle || ""
26+
;(dom as any)[name][styleKey] = bStyle || ''
3627
}
3728
})
38-
} else if (name[0] === "o" && name[1] === "n") {
29+
} else if (name[0] === 'o' && name[1] === 'n') {
3930
name = name.slice(2).toLowerCase() as Extract<keyof P, string>
4031
if (a) dom.removeEventListener(name, a)
4132
dom.addEventListener(name, b)
4233
} else if (name in dom && !(dom instanceof SVGElement)) {
43-
; (dom as any)[name] = b || ''
34+
;(dom as any)[name] = b || ''
4435
} else if (b == null || b === false) {
4536
dom.removeAttribute(name)
4637
} else {
@@ -51,14 +42,14 @@ export const updateElement = <P extends Attributes>(
5142

5243
export const createElement = <P = Attributes>(fiber: IFiber) => {
5344
const dom =
54-
fiber.type === ""
55-
? document.createTextNode("")
45+
fiber.type === ''
46+
? document.createTextNode('')
5647
: fiber.lane & LANE.SVG
57-
? document.createElementNS(
58-
"http://www.w3.org/2000/svg",
48+
? document.createElementNS(
49+
'http://www.w3.org/2000/svg',
5950
fiber.type as string
6051
)
61-
: document.createElement(fiber.type as string)
52+
: document.createElement(fiber.type as string)
6253
updateElement(dom as DOM, {} as P, fiber.props as P)
6354
return dom
6455
}

0 commit comments

Comments
 (0)