JSFiddle - React, Tailwind, and code Playground

by Salmin Skenderovic

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

JavaScript

const a = 1
const b = 1
const d = 1;

const testObj = {
  a,
  b,
  c: {
    a,
    b,
    c: {
      a,
      b,
      c: {
        a,
        b,
        c: {
          a,
          b,
          c: {
            a,
            b,
            c: 'hej',
            d
          },
          d,
        },
        d,
      },
      d
    },
    d,
  },
  d,
}

const path = 'c.c.c.c.c.c';

function objectPath() {
  'use strict'

  var toStr = Object.prototype.toString

  function hasOwnProperty(obj, prop) {
    if (obj == null) {
      return false
    }
    //to handle objects with null prototypes (too edge case?)
    return Object.prototype.hasOwnProperty.call(obj, prop)
  }

  function isEmpty(value) {
    if (!value) {
      return true
    }
    if (isArray(value) && value.length === 0) {
      return true
    } else if (typeof value !== 'string') {
      for (var i in value) {
        if (hasOwnProperty(value, i)) {
          return false
        }
      }
      return true
    }
    return false
  }

  function toString(type) {
    return toStr.call(type)
  }

  function isObject(obj) {
    return typeof obj === 'object' && toString(obj) === '[object Object]'
  }

  var isArray = Array.isArray || function(obj) {
    /*istanbul ignore next:cant test*/
    return toStr.call(obj) === '[object Array]'
  }

  function isBoolean(obj) {
    return typeof obj === 'boolean' || toString(obj) === '[object Boolean]'
  }

  function getKey(key) {
    var intKey = parseInt(key)
    if (intKey.toString() === key) {
      return intKey
    }
    return key
  }

  function factory(options) {
    options = options || {}

    var objectPath = function(obj) {
      return Object.keys(objectPath).reduce(function(proxy, prop) {
        if (prop === 'create') {
          return proxy
        }

        /*istanbul ignore else*/
        if (typeof objectPath[prop] === 'function') {
          proxy[prop] = objectPath[prop].bind(objectPath, obj)
        }

        return proxy
      },...