Utilities

Import members from @edx/frontend-platform

Source:

Methods

(inner) camelCaseObject(object) → {Array|Object}

Performs a deep conversion to camelCase on all keys in the provided object and its tree of children. Uses lodash.camelcase on each key. This is commonly used to convert snake_case keys in models from a backend server into camelCase keys for use in the JavaScript client.

Can accept arrays as well as objects, and will perform its conversion on any objects it finds in the array.

Parameters:
Name Type Description
object Array | Object
Source:
Returns:
Type
Array | Object

(inner) convertKeyNames(object, nameMap) → {Array|Object}

Given a map of key-value pairs, performs a deep conversion key names in the specified object from the key to the value. This is useful for updating names in an API request to the names used throughout a client application if they happen to differ. It can also be used in the reverse - formatting names from the client application to names expected by an API.

import { convertKeyNames } from '@edx/frontend-base';

// This object can be of any shape or depth with subobjects/arrays.
const myObject = {
  myKey: 'my value',
}

const result = convertKeyNames(myObject, { myKey: 'their_key' });

console.log(result) // { their_key: 'my value' }

Can accept arrays as well as objects, and will perform its conversion on any objects it finds in the array.

Parameters:
Name Type Description
object Array | Object
nameMap Object
Source:
Returns:
Type
Array | Object

(inner) ensureDefinedConfig(object, requester)

This function helps catch a certain class of misconfiguration in which configuration variables are not properly defined and/or supplied to a consumer that requires them. Any key that exists is still set to "undefined" indicates a misconfiguration further up in the application, and should be flagged as an error, and is logged to 'warn'.

Keys that are intended to be falsy should be defined using null, 0, false, etc.

Parameters:
Name Type Description
object Object
requester string

A human-readable identifier for the code which called this function. Used when throwing errors to aid in debugging.

Source:

(inner) getPath(url) → {string}

Given a string URL return the path of the URL

Parameters:
Name Type Description
url string
Source:
Returns:
Type
string

(inner) getQueryParameters(searchopt) → {Object}

Deprecated: A method which converts the supplied query string into an object of key-value pairs and returns it. Defaults to the current query string - should perform like window.searchParams

Parameters:
Name Type Attributes Default Description
search string <optional>
global.location.search
Deprecated:
  • Yes
Source:
Returns:
Type
Object

(inner) modifyObjectKeys(object, modify) → {Object}

This is the underlying function used by camelCaseObject, snakeCaseObject, and convertKeyNames above.

Given an object (or array) and a modification function, will perform the function on each key it encounters on the object and its tree of children.

The modification function must take a string as an argument and returns a string.

Example:

(key) => {
  if (key === 'edX') {
    return 'Open edX';
  }
  return key;
}

This function will turn any key that matches 'edX' into 'Open edX'. All other keys will be passed through unmodified.

Can accept arrays as well as objects, and will perform its conversion on any objects it finds in the array.

Parameters:
Name Type Description
object Object
modify function
Source:
Returns:
Type
Object

(inner) parseURL(url) → {Object}

Given a string URL return an element that has been parsed via href. This element has the possibility to return different part of the URL. parser.protocol; // => "http:" parser.hostname; // => "example.com" parser.port; // => "3000" parser.pathname; // => "/pathname/" parser.search; // => "?search=test" parser.hash; // => "#hash" parser.host; // => "example.com:3000" https://gist.github.com/jlong/2428561

Parameters:
Name Type Description
url string
Source:
Returns:
Type
Object

(inner) snakeCaseObject(object) → {Array|Object}

Performs a deep conversion to snake_case on all keys in the provided object and its tree of children. Uses lodash.snakecase on each key. This is commonly used to convert camelCase keys from the JavaScript app into snake_case keys expected by backend servers.

Can accept arrays as well as objects, and will perform its conversion on any objects it finds in the array.

Parameters:
Name Type Description
object Array | Object
Source:
Returns:
Type
Array | Object