<commit_before>import check from 'check-arg-types'

import pathGet from '@wasmuth/path'
import pathSet from '@wasmuth/path-set'
import merge from '@wasmuth/merge'
import without from '@wasmuth/without'
import last from '@wasmuth/last'

const toType = check.prototype.toType

function pathUpdate (path, payload, state) {
  let {value} = payload
  const currentValue = pathGet(path, state)
  const newType = toType(value)

  if (newType === toType(currentValue)) {
    if (newType === 'array') {
      return pathSet(path, currentValue.concat(value), state)
    } else if (newType === 'object') {
      return pathSet(path, merge(currentValue, value), state)
    }
  }
  return pathSet(path, value, state)
}

function pathRemove (path, payload, state) {
  const parent = pathGet(path.slice(0, -1), state)
  const parentType = toType(parent)
  if (parentType === 'object') {
    return path.length > 1
      ? pathSet(
        path.slice(0, -1),
        without(path.slice(-1), parent),
        state
      )
      : without(path, state)
  } else if (parentType === 'array') {
    const idx = last(path)
    if (toType(idx) === 'number') {
      parent.splice(idx, 1)
      return pathSet(path.slice(0, -1), parent, state)
    }
  }
  return pathSet(path, null, state)
}

function batch (payload, state) {
  const {actions} = payload
  if (!actions || !actions.length) {
    return state
  }
  const len = actions.length
  let newState = state
  for (let x = 0; x < len; x++) {
    newState = pathReducer(newState, actions[x])
  }
  return newState
}

export const actions = {
  set (path, value) {
    return {type: 'PATH_SET', payload: {path, value}}
  },

  update (path, value) {
    return {type: 'PATH_UPDATE', payload: {path, value}}
  },

  remove (path) {
    return {type: 'PATH_REMOVE', payload: {path}}
  },

  batch () {
    return {type: 'PATH_BATCH', payload: {actions: [].slice.call(arguments)}}
  }
}

export default function pathReducer (state, action) {
  const {type, payload} = action
  const path = pathGet('path', payload || {})
  switch (type) {
    case 'PATH_SET':
      return pathSet(path, payload.value, state)

    case 'PATH_UPDATE':
      return pathUpdate(path, payload, state)

    case 'PATH_REMOVE':
      return pathRemove(path, payload, state)
<commit_msg>Swap arg order for path-reducer.<commit_after>import check from 'check-arg-types'

import pathGet from '@wasmuth/path'
import pathSet from '@wasmuth/path-set'
import merge from '@wasmuth/merge'
import without from '@wasmuth/without'
import last from '@wasmuth/last'

const toType = check.prototype.toType

function pathUpdate (path, payload, state) {
  let {value} = payload
  const currentValue = pathGet(path, state)
  const newType = toType(value)

  if (newType === toType(currentValue)) {
    if (newType === 'array') {
      return pathSet(path, currentValue.concat(value), state)
    } else if (newType === 'object') {
      return pathSet(path, merge(currentValue, value), state)
    }
  }
  return pathSet(path, value, state)
}

function pathRemove (path, payload, state) {
  const parent = pathGet(path.slice(0, -1), state)
  const parentType = toType(parent)
  if (parentType === 'object') {
    return path.length > 1
      ? pathSet(
        path.slice(0, -1),
        without(path.slice(-1), parent),
        state
      )
      : without(path, state)
  } else if (parentType === 'array') {
    const idx = last(path)
    if (toType(idx) === 'number') {
      parent.splice(idx, 1)
      return pathSet(path.slice(0, -1), parent, state)
    }
  }
  return pathSet(path, null, state)
}

function batch (payload, state) {
  const {actions} = payload
  if (!actions || !actions.length) {
    return state
  }
  const len = actions.length
  let newState = state
  for (let x = 0; x < len; x++) {
    newState = pathReducer(newState, actions[x])
  }
  return newState
}

export const actions = {
  set (path, value) {
    return {type: 'PATH_SET', payload: {path, value}}
  },

  update (path, value) {
    return {type: 'PATH_UPDATE', payload: {path, value}}
  },

  remove (path) {
    return {type: 'PATH_REMOVE', payload: {path}}
  },

  batch () {
    return {type: 'PATH_BATCH', payload: {actions: [].slice.call(arguments)}}
  }
}

export default function pathReducer (action, state) {
  const {type, payload} = action
  const path = pathGet('path', payload || {})
  switch (type) {
    case 'PATH_SET':
      return pathSet(path, payload.value, state)

    case 'PATH_UPDATE':
      return pathUpdate(path, payload, state)

    case 'PATH_REMOVE':
      return pathRemove(path, payload, state)

{'fraction_non_alphanumeric': 0.1438817648337318, 'fraction_numerical': 0.0043468811128015645, 'mean_word_length': 3.3538315988647116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4439393', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1510, 'n_words': 428}