Utilities for creating MindDrop extensions.
Helper functions for modifying resource field values.
import { FieldValue } from '@minddrop/utils';
Deletes a field.
FieldValue.delete(): FieldValueDelete
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
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
const update = {
someArray: FieldValue.arrayRemove([1, 2]),
};
Filters an array.
FieldValue.arrayFilter<T>(callback: (item: T) => boolean): FieldValueArrayFilter
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
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;
Argument | Type | Description |
---|---|---|
resource* | Resource | The resource to update. |
changes* | ResourceChanges | Changes to apply to the resource. Supports FieldValue mutations. |
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;
}
Argument | Type | Description |
---|---|---|
resource* | Resource | The resource to update. |
changes* | ResourceChanges | Changes to apply to the resource. Supports FieldValue mutations. |
options | CreateUpdateOptions | See below for details. |
Prop | Type | Default |
---|---|---|
setUpdatedAt | boolean | false |
deleteEmptyFields | string[] | [] |
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[]
Argument | Type | Description |
---|---|---|
objects* | Object[] | The array of objects from which to remove objects. |
objectsToRemove* | Object[] | The objects to remove from the array. |
// 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
// 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',
});