All files / src/types array.js

100% Statements 52/52
100% Branches 31/31
100% Functions 5/5
100% Lines 52/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 531x 1x 1x 1x 1x 3x 3x 1x 1x 42x 1x 1x 1x 42x 40x 42x 1x 1x 10x 10x 10x 10x 10x 20x 20x 10x 10x 1x 1x 44x 13x 13x 7x 13x 6x 6x 6x 6x 6x 6x 13x 13x 44x 28x 44x 1x 1x 8x 8x 8x 8x 1x  
import { InternalStaty, rawValue } from './internal.js'
import { actions, action } from '../action.js'
 
export class ArrayStaty extends InternalStaty {
  forEach (callback) {
    this.target.forEach(callback)
  }
 
  onGetSnapshot (target, prop, value) {
    if (prop === 'splice' || prop === 'unshift' || prop === 'push' || prop === 'pop' || prop === 'shift' || prop === 'reverse' || prop === 'sort') {
      this.onReadOnly(target, prop, value)
      return () => {}
    }
    if (typeof value === 'function') return value.bind(target)
    return value
  }
 
  clone () {
    let k
    const x = this.target
    k = x.length
    const tmp = Array(k)
    for (; k--;) {
      tmp[k] = rawValue(x[k])
    }
    return tmp
  }
 
  handler (value, prop) {
    if (prop === 'splice' || prop === 'unshift' || prop === 'push' || prop === 'pop' || prop === 'shift' || prop === 'reverse' || prop === 'sort') {
      return (...args) => {
        if (actions.current) {
          return value.call(this.proxy, ...args)
        } else {
          let res
          action(() => {
            res = value.call(this.proxy, ...args)
          })
          return res
        }
      }
    }
    if (typeof value === 'function') return value.bind(this.target)
    return value
  }
 
  reflectDeleteProperty (target, prop) {
    const newArr = target.filter((_, i) => `${i}` !== prop)
    target.splice(0, target.length, ...newArr)
    return true
  }
}