Object.hasPath()

Test if an Object has a property.

Signature

Object.hasPath(object: { *: Any }, path: Array<String>) => Boolean

Description

Object.hasPath() tests if an Object contains a property based on a provided path.

The path is defined as an Array of Strings, where each String represents a property key in the Object’s hierarchy. The method resolves the Object structure following the path to search for the property.

Parameters

Parameter Type Required Description

object

Object containing fields of Any type.

true

Object to test for a property.

path

Array of Strings

true

Path to an Object property. Each element in the Array represents a level in the Object’s hierarchy.

Return value

Type Description

Boolean

If true, the Object contains the property.

Examples

Basic example

// Test if the Object contains the top-level `foo` property.
Object.hasPath({ foo : 'bar' }, ['foo'])
true

Property that doesn’t exist

// Test if the Object contains the top-level `baz` property.
Object.hasPath({ foo : 'bar' }, ['baz'])
false

Nested property

// Defines an Object with customer data.
let customer = {
  "name": "Ruby Von Rails",
  "email": "ruby@example.com",
  "address": {
    "street": "87856 Mendota Court",
    "city": "Washington",
    "state": "DC",
    "postalCode": "20220",
    "country": "US"
  }
}

// Test if the customer Object contains the
// nested `address.state` property.
Object.hasPath(customer, ['address', 'state'])
true

Nested property that doesn’t exist

// Defines an Object with customer data.
let customer = {
  "name": "Ruby Von Rails",
  "email": "ruby@example.com",
  "address": {
    "street": "87856 Mendota Court",
    "city": "Washington",
    "state": "DC",
    "postalCode": "20220",
    "country": "US"
  }
}

// Test if the customer Object contains the
// nested `address.zipCode` property.
Object.hasPath(customer, ['address', 'zipCode'])
false

Format results for dynamic projection

The following getFormatter() user-defined function (UDF) uses Object.hasPath() to determine whether a predefined formatterMap object contains a property with a matching collection name. If not, it returns an abort error.

The getList() UDF calls getFormatter() to return the projection for the provided collection.

The UDFs are used for dynamic projection.

// Defines the `getFormatter()` UDF.
// Accepts a collection name and returns a
// format for the collection.
function getFormatter(collName) {

  // Defines an object with a format
  // for each accepted collection name.
  let formatterMap = {
    Product: product => product {
      name,
      description,
      price },
    Category: category => category {
      name,
      description
    }
  }

  // Use abort() to return an error if the
  // collection name doesn't have a format.
  if (!Object.hasPath(formatterMap, [collName])) {
    abort("No formatter named '#{collName}'")
  }

  formatterMap[collName]
}

// Returns the collection's data using the
// predefined colleciton.
function getList(collName) {
  let collection = Collection(collName)
  // Calls the previous `getFormatter()` UDF.
  let formatFn = getFormatter(collName)
  collection.all().map(formatFn)
}
\