<commit_before>import _ from 'lodash/fp'
import { dotJoinWith } from './array'
import { overNone } from './logic'
import {
  reduceIndexed,
  pickIn,
  getIn,
  hasIn,
  mapIndexed,
  mapValuesIndexed,
} from './conversion'
import { findApply } from './collection'

// (k, v) -> {k: v}
export const singleObject = _.curry((key, value) => ({ [key]: value }))
export const singleObjectR = _.flip(singleObject)

// Formerly objToObjArr
// ({a, b}) -> [{a}, {b}]
export const chunkObject = value =>
  _.isArray(value) ? value : _.map(_.spread(singleObject), _.toPairs(value))

// Remove properties with falsey values: ({ a: 1, b: null, c: false}) -> {a:1}
export const compactObject = _.pickBy(_.identity)

export const isEmptyObject = _.isEqual({})

export const isNotEmptyObject = _.negate(isEmptyObject)

// { a:1, b:{}, c:2 } -> { a:1, c:2 }
export const stripEmptyObjects = _.pickBy(isNotEmptyObject)

// const crazyBS = (f, g) => (a, b) => f(a)(g(b))

// { a: { b: 1, c: 2 } }, [ 'b' ] -> { a: { b: 1 } }
export const pickInto = (map, source) => _.mapValues(pickIn(source), map)

// map rename implementation (not used here yet):
// http://jsfiddle.net/daedalus28/8uQUD/
export const renameProperty = _.curry((from, to, target) => {
  target[to] = target[from]
  delete target[from]
  return target
})

// { x:['a','b'], y:1 } -> [{ x:'a', y:1 }, { x:'b', y:1 }] just like mongo's `$unwind`
export const unwind = _.curry((prop, x) =>
  _.map(y => _.set(prop, y, x), _.get(prop, x))
)

export const isFlatObject = overNone([_.isPlainObject, _.isArray])

// { a: { b: { c: 1 } } } => { 'a.b.c' : 1 }
export const flattenObject = (input, paths) =>
  reduceIndexed(
    (output, value, key) =>
      _.merge(
        output,
        (isFlatObject(value) ? singleObjectR : flattenObject)(
          value,
          dotJoinWith(x => x === 0 || x)([paths, key])
        )
      ),
    {},
    input
  )

// { 'a.b.c' : 1 } => { a: { b: { c: 1 } } }
export const unflattenObject = x => _.zipObjectDeep(_.keys(x), _.values(x))

// Returns true if object keys are only elements from signature list (but does not require all signature keys to be present)
export const matchesSignature = _.curry(
  (signature, value) =>
    _.isObject(value) && !_.difference(_.keys(value), signature).length
)

// Checks if a property deep in a given item equals to a given value
export const compareDeep = _.curry(
  (path, item, value) => _.get(path, item) === value
)

// Applies a map function at a specific path
// e.g.: mapProp(double, 'a', {a:2, b:1}) -> {a:4, b:1}
export const mapProp = _.curry((fn, key, obj) =>
  _.set(key, fn(_.get(key, obj)), obj)
)

// `_.get` that returns the target object if lookup fails
export let getOrReturn = _.curry((prop, x) => _.getOr(x, prop, x))
// `_.get` that returns the prop if lookup fails
export let alias = _.curry((prop, x) => _.getOr(prop, prop, x))
// flipped alias<commit_msg>Automatically fixed by duti! (Just kidding, but one day....)<commit_after>import _ from 'lodash/fp'
import { dotJoinWith } from './array'
import { overNone } from './logic'
import { isNotNil } from './lang'
import {
  reduceIndexed,
  pickIn,
  getIn,
  hasIn,
  mapIndexed,
  mapValuesIndexed,
} from './conversion'
import { findApply } from './collection'

// (k, v) -> {k: v}
export const singleObject = _.curry((key, value) => ({ [key]: value }))
export const singleObjectR = _.flip(singleObject)

// Formerly objToObjArr
// ({a, b}) -> [{a}, {b}]
export const chunkObject = value =>
  _.isArray(value) ? value : _.map(_.spread(singleObject), _.toPairs(value))

// Remove properties with falsey values: ({ a: 1, b: null, c: false}) -> {a:1}
export const compactObject = _.pickBy(_.identity)

export const isEmptyObject = _.isEqual({})

export const isNotEmptyObject = _.negate(isEmptyObject)

// { a:1, b:{}, c:2 } -> { a:1, c:2 }
export const stripEmptyObjects = _.pickBy(isNotEmptyObject)

// const crazyBS = (f, g) => (a, b) => f(a)(g(b))

// { a: { b: 1, c: 2 } }, [ 'b' ] -> { a: { b: 1 } }
export const pickInto = (map, source) => _.mapValues(pickIn(source), map)

// map rename implementation (not used here yet):
// http://jsfiddle.net/daedalus28/8uQUD/
export const renameProperty = _.curry((from, to, target) => {
  target[to] = target[from]
  delete target[from]
  return target
})

// { x:['a','b'], y:1 } -> [{ x:'a', y:1 }, { x:'b', y:1 }] just like mongo's `$unwind`
export const unwind = _.curry((prop, x) =>
  _.map(y => _.set(prop, y, x), _.get(prop, x))
)

export const isFlatObject = overNone([_.isPlainObject, _.isArray])

// { a: { b: { c: 1 } } } => { 'a.b.c' : 1 }
export const flattenObject = (input, paths) =>
  reduceIndexed(
    (output, value, key) =>
      _.merge(
        output,
        (isFlatObject(value) ? singleObjectR : flattenObject)(
          value,
          dotJoinWith(isNotNil)([paths, key])
        )
      ),
    {},
    input
  )

// { 'a.b.c' : 1 } => { a: { b: { c: 1 } } }
export const unflattenObject = x => _.zipObjectDeep(_.keys(x), _.values(x))

// Returns true if object keys are only elements from signature list (but does not require all signature keys to be present)
export const matchesSignature = _.curry(
  (signature, value) =>
    _.isObject(value) && !_.difference(_.keys(value), signature).length
)

// Checks if a property deep in a given item equals to a given value
export const compareDeep = _.curry(
  (path, item, value) => _.get(path, item) === value
)

// Applies a map function at a specific path
// e.g.: mapProp(double, 'a', {a:2, b:1}) -> {a:4, b:1}
export const mapProp = _.curry((fn, key, obj) =>
  _.set(key, fn(_.get(key, obj)), obj)
)

// `_.get` that returns the target object if lookup fails
export let getOrReturn = _.curry((prop, x) => _.getOr(x, prop, x))
// `_.get` that returns the prop if lookup fails
export let alias = _.curry((prop, x) => _.getOr(prop, prop, x))
// flipped alias
{'fraction_non_alphanumeric': 0.1854180829367777, 'fraction_numerical': 0.007987763426240652, 'mean_word_length': 3.3689680772086117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '2074371', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2073, 'n_words': 624}