mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 02:03:39 +00:00
[PM-7289] implement generator libraries (#9549)
This is a copy of the files. The source in `@bitwarden/common` will be deleted once all of the applications have been ported to the library.
This commit is contained in:
@@ -1,45 +1,10 @@
|
||||
import { distinctUntilChanged, map, OperatorFunction, pipe } from "rxjs";
|
||||
import { map, pipe } from "rxjs";
|
||||
|
||||
import { reduceCollection, distinctIfShallowMatch } from "@bitwarden/common/tools/rx";
|
||||
|
||||
import { DefaultPolicyEvaluator } from "./default-policy-evaluator";
|
||||
import { PolicyConfiguration } from "./policies";
|
||||
|
||||
/**
|
||||
* An observable operator that reduces an emitted collection to a single object,
|
||||
* returning a default if all items are ignored.
|
||||
* @param reduce The reduce function to apply to the filtered collection. The
|
||||
* first argument is the accumulator, and the second is the current item. The
|
||||
* return value is the new accumulator.
|
||||
* @param defaultValue The default value to return if the collection is empty. The
|
||||
* default value is also the initial value of the accumulator.
|
||||
*/
|
||||
export function reduceCollection<Item, Accumulator>(
|
||||
reduce: (acc: Accumulator, value: Item) => Accumulator,
|
||||
defaultValue: Accumulator,
|
||||
): OperatorFunction<Item[], Accumulator> {
|
||||
return map((values: Item[]) => {
|
||||
const reduced = (values ?? []).reduce(reduce, structuredClone(defaultValue));
|
||||
return reduced;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* An observable operator that emits distinct values by checking that all
|
||||
* values in the previous entry match the next entry. This method emits
|
||||
* when a key is added and does not when a key is removed.
|
||||
* @remarks This method checks objects. It does not check items in arrays.
|
||||
*/
|
||||
export function distinctIfShallowMatch<Item>(): OperatorFunction<Item, Item> {
|
||||
return distinctUntilChanged((previous, current) => {
|
||||
let isDistinct = true;
|
||||
|
||||
for (const key in current) {
|
||||
isDistinct &&= previous[key] === current[key];
|
||||
}
|
||||
|
||||
return isDistinct;
|
||||
});
|
||||
}
|
||||
|
||||
/** Maps an administrative console policy to a policy evaluator using the provided configuration.
|
||||
* @param configuration the configuration that constructs the evaluator.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user