Options
All
  • Public
  • Public/Protected
  • All
Menu

Utilities for Salesforce CLI development

What is this?

A collection of commonly needed utilities used by the Salesforce CLI and the libraries it is built on. It includes high level support for parsing and working with JSON data, interacting with environment variables, a common error base type, a minimal lodash replacement, and support for commonly needed design patterns, among other things. It is intended specifically for use in Node.js (version 8 or newer) projects -- YMMV in the browser.

See the API documentation for more details on each of the utilities that kit provides.

References

This library depends upon another Salesforce TypeScript library, @salesforce/ts-types. The API documentation for this library refers to several types that you will find in ts-types. Some lodash replacement functions are also found in ts-types.

Index

Type aliases

InterruptablePromise

InterruptablePromise: Promise<T> & Interruptable

A promise of result type T that can be interrupted prematurely, resulting in an early resolution.

Variables

Const env

env: Env = new Env()

The default Env instance, which wraps process.env.

Functions

cloneJson

  • cloneJson<T>(obj: T): T
  • Perform a deep clone of an object or array compatible with JSON stringification. Object fields that are not compatible with stringification will be omitted. Array entries that are not compatible with stringification will be censored as null.

    throws

    JsonStringifyError If the object contains circular references or causes other JSON stringification errors.

    Type parameters

    • T: any

    Parameters

    • obj: T

      A JSON-compatible object or array to clone.

    Returns T

defaults

  • defaults<T, S>(obj: T, source: S): S & T
  • defaults<T, S1, S2>(obj: T, source1: S1, source2: S2): S2 & S1 & T
  • defaults<T, S1, S2, S3>(obj: T, source1: S1, source2: S2, source3: S3): S3 & S2 & S1 & T
  • defaults<T, S1, S2, S3, S4>(obj: T, source1: S1, source2: S2, source3: S3, source4: S4): S4 & S3 & S2 & S1 & T
  • defaults<T>(obj: T): T
  • Assigns own enumerable properties of source object(s) to the destination object for all destination properties that resolve to undefined. Once a property is set, additional values of the same property are ignored.

    Note: This method mutates obj.

    Type parameters

    • T

    • S

    Parameters

    • obj: T

      The destination object.

    • source: S

    Returns S & T

  • see

    defaults

    Type parameters

    • T

    • S1

    • S2

    Parameters

    • obj: T
    • source1: S1
    • source2: S2

    Returns S2 & S1 & T

  • see

    defaults

    Type parameters

    • T

    • S1

    • S2

    • S3

    Parameters

    • obj: T
    • source1: S1
    • source2: S2
    • source3: S3

    Returns S3 & S2 & S1 & T

  • see

    defaults

    Type parameters

    • T

    • S1

    • S2

    • S3

    • S4

    Parameters

    • obj: T
    • source1: S1
    • source2: S2
    • source3: S3
    • source4: S4

    Returns S4 & S3 & S2 & S1 & T

  • see

    defaults

    Type parameters

    • T

    Parameters

    • obj: T

    Returns T

findKey

  • findKey<T>(obj: Nullable<T>, predicate?: ObjectIteratee<T>): Optional<string>
  • This method is like find except that it returns the key of the first element predicate returns truthy for instead of the element itself.

    Type parameters

    • T

    Parameters

    • obj: Nullable<T>

      The object to search.

    • Optional predicate: ObjectIteratee<T>

      The function invoked per iteration.

    Returns Optional<string>

getJsonValuesByName

  • getJsonValuesByName<T>(json: JsonMap, name: string): T[]
  • Finds all elements of type T with a given name in a JsonMap. Not suitable for use with object graphs containing circular references. The specification of an appropriate type T that will satisfy all matching element values is the responsibility of the caller.

    Type parameters

    • T: AnyJson

    Parameters

    • json: JsonMap

      The JsonMap tree to search for elements of the given name.

    • name: string

      The name of elements to search for.

    Returns T[]

includes

  • includes<T>(collection: Nullable<ArrayLike<T> | Dictionary<T> | NumericDictionary<T>>, target: T, fromIndex?: undefined | number): boolean
  • Checks if target is in collection using SameValueZero for equality comparisons. If fromIndex is negative, it’s used as the offset from the end of collection.

    Type parameters

    • T

    Parameters

    • collection: Nullable<ArrayLike<T> | Dictionary<T> | NumericDictionary<T>>

      The collection to search.

    • target: T

      The value to search for.

    • Optional fromIndex: undefined | number

      The index to search from.

    Returns boolean

isEmpty

  • isEmpty(value: unknown): boolean
  • Checks if value is empty. A value is considered empty unless it’s an arguments object, array, string, or jQuery-like collection with a length greater than 0 or an object with own enumerable properties.

    Parameters

    • value: unknown

      The value to inspect.

    Returns boolean

jsonIncludes

  • jsonIncludes(json: Optional<AnyJson>, value: Optional<AnyJson>): boolean
  • Tests whether an AnyJson value contains another AnyJson value. This is a shallow check only and does not recurse deeply into collections.

    Parameters

    • json: Optional<AnyJson>

      The container to search.

    • value: Optional<AnyJson>

      The value search for.

    Returns boolean

keyBy

  • keyBy<T>(collection: Nullable<ArrayLike<T>>, iteratee?: ValueIterateeCustom<T, PropertyKey>): Dictionary<T>
  • keyBy<T>(collection: Nullable<T>, iteratee?: ValueIterateeCustom<T[keyof T], PropertyKey>): Dictionary<T[keyof T]>
  • Creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is the last element responsible for generating the key. The iteratee function is invoked with one argument: (value).

    Type parameters

    • T

    Parameters

    • collection: Nullable<ArrayLike<T>>

      The collection to iterate over.

    • Optional iteratee: ValueIterateeCustom<T, PropertyKey>

      The function invoked per iteration.

    Returns Dictionary<T>

  • see

    keyBy

    Type parameters

    • T: any

    Parameters

    • collection: Nullable<T>
    • Optional iteratee: ValueIterateeCustom<T[keyof T], PropertyKey>

    Returns Dictionary<T[keyof T]>

lowerFirst

  • lowerFirst(value: string): string
  • lowerFirst(value?: undefined | string): Optional<string>
  • Converts the first character of string to lower case.

    Parameters

    • value: string

    Returns string

  • see

    lowerFirst

    Parameters

    • Optional value: undefined | string

    Returns Optional<string>

mapKeys

  • mapKeys<T>(object: Nullable<ArrayLike<T>>, iteratee?: ListIteratee<T>): Dictionary<T>
  • mapKeys<T>(object: Nullable<T>, iteratee?: ObjectIteratee<T>): Dictionary<T[keyof T]>
  • This method creates an object with the same values as object and keys generated by running each own enumerable property of object through iteratee.

    Type parameters

    • T

    Parameters

    • object: Nullable<ArrayLike<T>>
    • Optional iteratee: ListIteratee<T>

      The function invoked per iteration.

    Returns Dictionary<T>

  • see

    mapKeys

    Type parameters

    • T: any

    Parameters

    • object: Nullable<T>
    • Optional iteratee: ObjectIteratee<T>

    Returns Dictionary<T[keyof T]>

maxBy

  • maxBy<T>(collection: Nullable<ArrayLike<T>>, iteratee?: ValueIteratee<T>): Optional<T>
  • This method is like _.max except that it accepts iteratee which is invoked for each element in array to generate the criterion by which the value is ranked. The iteratee is invoked with one argument: (value).

    Type parameters

    • T

    Parameters

    • collection: Nullable<ArrayLike<T>>
    • Optional iteratee: ValueIteratee<T>

      The iteratee invoked per element.

    Returns Optional<T>

merge

  • merge<T, S>(object: T, source: S): T & S
  • merge<T, S1, S2>(object: T, source1: S1, source2: S2): T & S1 & S2
  • merge<T, S1, S2, S3>(object: T, source1: S1, source2: S2, source3: S3): T & S1 & S2 & S3
  • merge<T, S1, S2, S3, S4>(object: T, source1: S1, source2: S2, source3: S3, source4: S4): T & S1 & S2 & S3 & S4
  • Recursively merges own and inherited enumerable properties of source objects into the destination object, skipping source properties that resolve to undefined. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

    Note: This method mutates object.

    Type parameters

    • T

    • S

    Parameters

    • object: T

      The destination object.

    • source: S

    Returns T & S

  • see

    merge

    Type parameters

    • T

    • S1

    • S2

    Parameters

    • object: T
    • source1: S1
    • source2: S2

    Returns T & S1 & S2

  • see

    merge

    Type parameters

    • T

    • S1

    • S2

    • S3

    Parameters

    • object: T
    • source1: S1
    • source2: S2
    • source3: S3

    Returns T & S1 & S2 & S3

  • see

    merge

    Type parameters

    • T

    • S1

    • S2

    • S3

    • S4

    Parameters

    • object: T
    • source1: S1
    • source2: S2
    • source3: S3
    • source4: S4

    Returns T & S1 & S2 & S3 & S4

minBy

  • minBy<T>(collection: Nullable<ArrayLike<T>>, iteratee?: ValueIteratee<T>): Optional<T>
  • This method is like _.min except that it accepts iteratee which is invoked for each element in array to generate the criterion by which the value is ranked. The iteratee is invoked with one argument: (value).

    Type parameters

    • T

    Parameters

    • collection: Nullable<ArrayLike<T>>
    • Optional iteratee: ValueIteratee<T>

      The iteratee invoked per element.

    Returns Optional<T>

omit

  • omit<T>(obj: Nullable<T>, ...paths: Array<Many<PropertyKey>>): T
  • omit<T, K>(obj: Nullable<T>, ...paths: Array<Many<K>>): Omit<T, K>
  • omit<T>(obj: Nullable<T>, ...paths: Array<Many<PropertyKey>>): Partial<T>
  • The opposite of _.pick; this method creates an object composed of the own and inherited enumerable properties of object that are not omitted.

    Type parameters

    • T: Dictionary<unknown>

    Parameters

    • obj: Nullable<T>

      The source object.

    • Rest ...paths: Array<Many<PropertyKey>>

      The property names to omit, specified individually or in arrays..

    Returns T

  • see

    omit

    Type parameters

    • T: any

    • K: keyof T

    Parameters

    • obj: Nullable<T>
    • Rest ...paths: Array<Many<K>>

    Returns Omit<T, K>

  • see

    omit

    Type parameters

    • T: any

    Parameters

    • obj: Nullable<T>
    • Rest ...paths: Array<Many<PropertyKey>>

    Returns Partial<T>

once

  • once<T>(func: T): T
  • Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first call. The func is invoked with the this binding and arguments of the created function.

    Type parameters

    • T: AnyFunction

    Parameters

    • func: T

      The function to restrict.

    Returns T

parseJson

  • parseJson(data: string, jsonPath?: undefined | string, throwOnEmpty?: boolean): AnyJson
  • Parse JSON string data.

    throws

    JsonParseError If the data contents are empty or the data is invalid.

    Parameters

    • data: string

      Data to parse.

    • Optional jsonPath: undefined | string

      The file path from which the JSON was loaded.

    • Default value throwOnEmpty: boolean = true

      If the data contents are empty.

    Returns AnyJson

parseJsonMap

  • parseJsonMap(data: string, jsonPath?: undefined | string, throwOnEmpty?: undefined | false | true): JsonMap
  • Parse JSON string data, expecting the result to be a JsonMap.

    throws

    JsonParseError If the data contents are empty or the data is invalid.

    throws

    JsonDataFormatError If the data contents are not a JsonMap.

    Parameters

    • data: string

      The string data to parse.

    • Optional jsonPath: undefined | string

      The file path from which the JSON was loaded.

    • Optional throwOnEmpty: undefined | false | true

      If the data contents are empty.

    Returns JsonMap

set

  • set<T>(obj: T, path: string, value: unknown): T
  • set<R>(obj: object, path: string, value: unknown): R
  • Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for missing index properties while objects are created for all other missing properties. Use _.setWith to customize path creation.

    Type parameters

    • T: any

    Parameters

    • obj: T

      The object to modify.

    • path: string

      The path of the property to set.

    • value: unknown

      The value to set.

    Returns T

  • see

    set

    Type parameters

    • R

    Parameters

    • obj: object
    • path: string
    • value: unknown

    Returns R

sleep

  • Sleeps for a given Duration. This is essentially a promisified version of setTimeout. May be interrupted by calling interrupt() on the returned InterruptablePromise.

    Parameters

    • duration: Duration

      The duration to wait.

      // sleep 5 seconds
      await sleep(Duration.seconds(5));
      
      // sleep 10 minutes unless interrupted by an event
      const promise = sleep(Duration.minutes(10));
      events.on('someEvent', promise.interrupt);
      await promise;

    Returns InterruptablePromise<void>

  • Sleeps for a given duration, with units defaulting to milliseconds. This is essentially a promisified version of setTimeout. May be interrupted by calling interrupt() on the returned InterruptablePromise.

    Parameters

    • quantity: number

      The quantity of duration to wait.

    • Optional unit: Unit

      The Duration.Unit in which quantity is specified, defaulting to milliseconds.

      // sleep 5 seconds
      await sleep(5000);
      
      // sleep 10 minutes unless interrupted by an event
      const promise = sleep(10, Duration.Unit.MINUTES);
      events.on('someEvent', promise.interrupt);
      await promise;

    Returns InterruptablePromise<void>

snakeCase

  • snakeCase(str: string): string
  • snakeCase(str?: undefined | string): Optional<string>
  • Converts string to snake case.

    Parameters

    • str: string

      The string to convert.

    Returns string

  • Parameters

    • Optional str: undefined | string

    Returns Optional<string>

sortBy

  • sortBy<T>(collection: Nullable<ArrayLike<T>>, ...iteratees: Array<Many<ListIteratee<T>>>): T[]
  • sortBy<T>(collection: Nullable<T>, ...iteratees: Array<Many<ObjectIteratee<T>>>): Array<T[keyof T]>
  • Creates an array of elements, sorted in ascending order by the results of running each element in a collection through each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratees are invoked with one argument: (value).

    Type parameters

    • T

    Parameters

    • collection: Nullable<ArrayLike<T>>

      The collection to iterate over.

    • Rest ...iteratees: Array<Many<ListIteratee<T>>>

      The iteratees to sort by, specified individually or in arrays.

    Returns T[]

  • see

    sortBy

    Type parameters

    • T: any

    Parameters

    • collection: Nullable<T>
    • Rest ...iteratees: Array<Many<ObjectIteratee<T>>>

    Returns Array<T[keyof T]>

toNumber

  • toNumber(value: unknown): number
  • Converts value to a number.

    Parameters

    • value: unknown

      The value to process.

      _.toNumber(3);
      // => 3
      
      _.toNumber(Number.MIN_VALUE);
      // => 5e-324
      
      _.toNumber(Infinity);
      // => Infinity
      
      _.toNumber('3');
      // => 3

    Returns number

upperFirst

  • upperFirst(value: string): string
  • upperFirst(value?: undefined | string): Optional<string>
  • Converts the first character of string to upper case.

    Parameters

    • value: string

    Returns string

  • see

    upperFirst

    Parameters

    • Optional value: undefined | string

    Returns Optional<string>