Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-deps   js

Repository URL to install this package:

Version: 6.0.4 

/ src / dot / has.ts

import { AnyArrayOrObj } from '../_typings'
import { isObj } from '../is'
import getPathSegments from './segments'
import isDottable from './dottable'

/**
 * @name dot.has
 * @memberOf dot
 * @func
 * @since 3.0.0
 * @extends dot/getPathSegments
 *
 * @param obj the object to retrieve the nested property from.
 * @param path dot-prop-path to use
 * @return has at path
 *
 * {@link https://github.com/jashkenas/underscore/blob/master/underscore.js#L1369 underscore-has}
 * @see {@link underscore-has}
 *
 * @example
 *
 *    dot.has({a: {b: 2}}, 'a.b'); //=> true
 *    dot.has({a: {b: 2}}, ['a', 'b']); //=> true
 *    dot.has({c: {b: 2}}, ['a', 'b']); //=> undefined
 *
 */
export default function dotHas(obj: AnyArrayOrObj, path: string | string[]): boolean {
  if (!isDottable(obj, path)) {
    return false
  }

  const pathArr = getPathSegments(path)

  for (let i = 0; i < pathArr.length; i++) {
    if (isObj(obj)) {
      if (!(pathArr[i] in obj)) {
        return false
      }

      obj = obj[pathArr[i]]
    }
    else {
      return false
    }
  }

  return true
}