Util.id Module

Import

import { id } from 'util';

Methods

isIdFormat(id) couldBeId(id) equalIds(...ids) timestampToId(timestamp) idToTimestamp(id) getIdOrNull(id, fromObject) indexOfId(ids, id) inIdArray(ids, id) getIdArray(ids, convertToStrings, fnEach) uniqueIdArray(ids) lookupId(obj, id) findIdPos(array, path, id) findIdInArray(array, path, id, useGetter) diffIdArrays(array, ...rest) intersectIdArrays(...arrays) isId(id)

isIdFormat(id)

Returns true if the passed in argument is a string and is in the form of an ObjectID (/^[0-9a-fA-F]{24}$/)

Arguments

  • id (String) the string to test

Returns

Boolean

couldBeId(id)

Returns true if the passed in argument is an instance of ObjectID or a string in the form of an ObjectID (/^[0-9a-fA-F]{24}$/).

Arguments

  • id (String|ObjectID)

Returns

Boolean

equalIds(...ids)

Returns true if all passed in identifiers are equal and valid as ObjectIDs

Arguments

  • ...ids (String[]|ObjectID[])

Returns

Boolean

timestampToId(timestamp)

Converts a timestamp (in ms), parsable date string or date value to an ObjectID. Useful for comparing ObjectIDs (eg. {_id: {$gt: ids.timestampToId(Date.now()-86400000)}})

Arguments

  • timestamp (String|Number|Date)

Returns

ObjectID

idToTimestamp(id)

Converts an ObjectID to a timestamp (in seconds)

Arguments

  • id (ObjectID)

Returns

Number

getIdOrNull(id, fromObject)

Guarantees return of an ObjectID if the id argument is an ObjectID, a valid string representation, or if the fromObject argument is true and the id argument is a javascript object with a valid _id property. Returns null otherwise.

Arguments

  • id (ObjectID|String|Object)

  • fromObject (Boolean) default false. if true, checks for a valid _id property of the id argument.

Returns

ObjectID

indexOfId(ids, id)

Returns the index of the id argument in the ids array, or -1 if ids is not an array or id is not present.

Arguments

  • ids (ObjectID[])

  • id (ObjectID|String)

Returns

Number

inIdArray(ids, id)

Returns true if the id argument value is contained in the ids array.

Arguments

  • ids (ObjectID[])

  • id (ObjectID|String)

Returns

Boolean

getIdArray(ids, convertToStrings, fnEach)

Converts the ids argument into an array of ObjectIDs or String representations thereof. Any values that do not qualify are removed from the array. Note the ids argument may be modified inside the function.

Arguments

  • ids (ObjectID[])

  • convertToStrings (Boolean) If true, converts each valid ObjectID to string.

  • fnEach (Function) If present, calls the function for each raw value in the input array ()(id) => { ...; return id; } ), which should return a value that is then passed into getIdOrNull(id)

Returns

ObjectID[]

uniqueIdArray(ids)

Returns an array of ObjectIDs with all duplicates removed.

Arguments

  • ids (ObjectID[])

Returns

ObjectID[]

findIdPos(objects, path, id)

Given an array of objects, uses path to inspect object properties for the first matching id and returns its index in the array, or -1.

Arguments

  • objects (Object[])

  • path (String) A dot syntax path to inspect (Uses util.paths.to internally)

  • id (ObjectID|String) The identifier to match.

Returns

Number

findIdInArray(objects, path, id)

Given an array of objects, uses path to inspect object properties for the first matching id and returns the matching object.

Arguments

  • objects (Object[])

  • path (String) A dot syntax path to inspect (Uses util.paths.to internally)

  • id (ObjectID|String) The identifier to match.

Returns

Object

diffIdArrays(array, ...rest)

Returns the values from array that are not present in the other arrays.

Arguments

  • objects (ObjectID[])

  • ...rest (ObjectID[])

Returns

ObjectID[]

intersectIdArrays(...arrays)

Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

Arguments

  • ...arrays (ObjectID[])

Returns

ObjectID[]

isId(id)

Returns true if id is an instance of ObjectID

Arguments

  • id (ObjectID)

Returns

Boolean

Last updated