Skip to content

Commit

Permalink
feat: add function to get authorization results for canViewerUpdateAs…
Browse files Browse the repository at this point in the history
…ync/canViewerDeleteAsync
  • Loading branch information
wschurman committed Nov 20, 2024
1 parent c2ae21b commit c4dd3d5
Show file tree
Hide file tree
Showing 3 changed files with 321 additions and 108 deletions.
33 changes: 24 additions & 9 deletions packages/entity/src/entityUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,37 @@ export const failedResultsFilterMap = <K, T>(
return ret;
};

export type PartitionArrayPredicate<T, U> = (val: T | U) => val is T;

/**
* Partition array of values and errors into an array of values and an array of errors.
* @param valuesAndErrors - array of values and errors
* Partition an array of values into two arrays based on evaluation of a binary predicate.
* @param values - array of values to partition
* @param predicate - binary predicate to evaluate partition group of each value
*/
export const partitionErrors = <T>(valuesAndErrors: (T | Error)[]): [T[], Error[]] => {
const values: T[] = [];
const errors: Error[] = [];
export const partitionArray = <T, U>(
values: (T | U)[],
predicate: PartitionArrayPredicate<T, U>,
): [T[], U[]] => {
const ts: T[] = [];
const us: U[] = [];

for (const valueOrError of valuesAndErrors) {
if (isError(valueOrError)) {
errors.push(valueOrError);
for (const value of values) {
if (predicate(value)) {
ts.push(value);
} else {
values.push(valueOrError);
us.push(value);
}
}

return [ts, us];
};

/**
* Partition array of values and errors into an array of values and an array of errors.
* @param valuesAndErrors - array of values and errors
*/
export const partitionErrors = <T>(valuesAndErrors: (T | Error)[]): [T[], Error[]] => {
const [errors, values] = partitionArray<Error, T>(valuesAndErrors, isError);
return [values, errors];
};

Expand Down
Loading

0 comments on commit c4dd3d5

Please sign in to comment.