Utils

Utilities for creating MindDrop extensions.

Helper functions for modifying resource field values.

import { FieldValue } from '@minddrop/utils';

Deletes a field.

FieldValue.delete(): FieldValueDelete

Example

const update = {
someField: FieldValue.delete(),
};

Merges items into an array or creates the array if it does not exist. Does not merge in items which are already present in the existing array.

FieldValue.arrayUnion<T>(values: T[]): FieldValueArrayUnion

Example

const update = {
someArray: FieldValue.arrayUnion([2, 3]),
};

Removes items from an array. Supports removing primitives (strings, numbers, etc.) and shallow objects (objects without nested objects).

FieldValue.arrayRemove<T>(values: T[]): FieldValueArrayRemove

Example

const update = {
someArray: FieldValue.arrayRemove([1, 2]),
};

Filters an array.

FieldValue.arrayFilter<T>(callback: (item: T) => boolean): FieldValueArrayFilter

Example

const update = {
someArray: FieldValue.arrayFilter((item) => item.id !== 'some-id'),
};

Merges values into an object or creates the object if it does not exist. Fields within the merged object can also make use of FieldValue mutations.

FieldValue.objectUnion(value: object): FieldValueObjectUnion

Example

const update = {
someObject: FieldValue.objectUnion({
foo: 'bar',
bar: FieldValue.delete(),
}),
};

Updates field values by applying FieldValue mutations and merging in regular fields.

import { applyFieldValues } from '@minddrop/utils';
applyFieldValues<R, C>(resource: R, changes: C): R;
ArgumentTypeDescription
resource*ResourceThe resource to update.
changes*ResourceChanges Changes to apply to the resource. Supports FieldValue mutations.

Example

const resource = {
title: 'My archived resource',
drops: ['drop-id'],
archived: true,
};
const changes = {
title: 'My resource',
drops: FieldValue.arrayUnion('drop-id-2'),
archived: FieldValue.delete(),
};
// Result: { title: 'My resource', drops: ['drop-id', 'drop-id-2'] }
const updated = applyFieldValues(resource, changes);

Creates an update object from a resource object and a changes object.

import { createUpdate } from '@minddrop/utils';
createUpdate<R, C>(resource: O, changes: C, options?: CreateUpdateOptions): ResourceUpdate<R, C>;
interface ResourceUpdate<R, C> {
// The resource data before it was updated.
before: R;
// The resource data after it was updated.
after: R;
// The changes applied to the resource.
changes: C;
}
ArgumentTypeDescription
resource*ResourceThe resource to update.
changes*ResourceChanges Changes to apply to the resource. Supports FieldValue mutations.
optionsCreateUpdateOptionsSee below for details.

CreateUpdateOptions

PropTypeDefault
setUpdatedAtbooleanfalse
deleteEmptyFieldsstring[][]

Example

const resource = {
title: 'My archived resource',
drops: ['drop-id'],
archived: true,
};
const changes = {
title: 'My resource',
drops: FieldValue.arrayUnion('drop-id-2'),
archived: FieldValue.delete(),
};
const updated = createUpdate(resource, changes);

Filters the given objects out of the provided array by shalowly comapring all of their properties. Does not support objects with objects as properties.

removeObjectsFromArray<TObject extends Object>(objects: TObject[], objectsToRemove[]): TObject[]
ArgumentTypeDescription
objects*Object[]The array of objects from which to remove objects.
objectsToRemove*Object[]The objects to remove from the array.

Example

// The objects
let parents = [
{ type: 'topic', id: 'topic-id-1' },
{ type: 'topic', id: 'topic-id-2' },
{ type: 'topic', id: 'topic-id-3' },
{ type: 'topic', id: 'topic-id-4' },
];
// Remove 'topic-id-1' and 'topic-id-3' objects
parents = removeObjectsFromArray(parents, [
{ type: 'topic', id: 'topic-id-1' },
{ type: 'topic', id: 'topic-id-3' },
]);
// Result: [{ type: 'topic', id: 'topic-id-2' }, { type: 'topic', id: 'topic-id-4' }]

Checks whether an array contains the given object. For an object to be considered a part of the array, an object in the array must contain the same keys with the same values. Only performs a shallow comparison of objects.

arrayContainsObject(array: unknown[], object: Object): boolean

Example

// The objects
const parents = [
{ type: 'topic', id: 'topic-id-1' },
{ type: 'topic', id: 'topic-id-2' },
{ type: 'topic', id: 'topic-id-3' },
{ type: 'topic', id: 'topic-id-4' },
];
// true
const contains = removeObjectsFromArray(parents, {
type: 'topic',
id: 'topic-id-3',
});
// false
const contains = removeObjectsFromArray(parents, {
type: 'topic',
id: 'topic-id-5',
});
// false
const contains = removeObjectsFromArray(parents, {
id: 'topic-id-3',
});