export default function () {
  if (!Array.prototype.flat) {
    // eslint-disable-next-line no-extend-native
    Array.prototype.flat = function (depth = 1) {
      depth = isNaN(Number(depth)) ? 1 : Number(depth)
      function flatten (arr) {
        let result = []
        if (depth) {
          for (let i = 0; i < arr.length; i++) {
            if (Array.isArray(arr[i])) {
              result = result.concat(flatten(arr[i], --depth))
            } else {
              result.push(arr[i])
            }
          }
        } else {
          result = result.concat(arr)
        }
        return result
      }
      return flatten(this)
    }
  }
}
