diff.js 918 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  isPlainObject
} from 'uni-shared'

fxy060608's avatar
init v3  
fxy060608 已提交
5 6 7 8
function setResult (data, k, v) {
  data[k] = v
}

fxy060608's avatar
fxy060608 已提交
9 10
function diffObject (newObj, oldObj) {
  let result, key, cur, old
fxy060608's avatar
init v3  
fxy060608 已提交
11 12 13 14
  for (key in newObj) {
    cur = newObj[key]
    old = oldObj[key]
    if (old !== cur) {
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20
      if (key === 's' && isPlainObject(cur) && isPlainObject(old)) {
        const style = diffObject(cur, old)
        style && setResult(result || (result = Object.create(null)), 's', style)
      } else {
        setResult(result || (result = Object.create(null)), key, cur)
      }
fxy060608's avatar
init v3  
fxy060608 已提交
21 22
    }
  }
fxy060608's avatar
fxy060608 已提交
23
  return result
fxy060608's avatar
init v3  
fxy060608 已提交
24 25
}

fxy060608's avatar
fxy060608 已提交
26
export function diff (newData, oldData, result) {
fxy060608's avatar
init v3  
fxy060608 已提交
27 28 29 30 31 32 33 34
  let id, cur, old
  for (id in newData) {
    cur = newData[id]
    old = oldData[id]
    if (!old) {
      setResult(result, id, cur)
      continue
    }
fxy060608's avatar
fxy060608 已提交
35 36
    const idObj = diffObject(cur, old)
    idObj && setResult(result, id, idObj)
fxy060608's avatar
init v3  
fxy060608 已提交
37 38 39
  }
  return result
}