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 (#249)
  • Loading branch information
wschurman authored Nov 20, 2024
1 parent c2ae21b commit 27a358d
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 108 deletions.
12 changes: 12 additions & 0 deletions packages/entity/src/__tests__/entityUtils-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
successfulResultsFilterMap,
failedResultsFilterMap,
pick,
partitionArray,
} from '../entityUtils';

describe(enforceResultsAsync, () => {
Expand Down Expand Up @@ -97,3 +98,14 @@ describe(pick, () => {
});
});
});

describe(partitionArray, () => {
it('partitions array', () => {
type A = true;
type B = false;
const arr: (A | B)[] = [true, false, true, true, false];
const [as, bs] = partitionArray<A, B>(arr, (val: A | B): val is A => val === true);
expect(as).toMatchObject([true, true, true]);
expect(bs).toMatchObject([false, false]);
});
});
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 27a358d

Please sign in to comment.