lodash-es#uniqWith TypeScript Examples

The following examples show how to use lodash-es#uniqWith. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: capacitor-filesystem-table.ts    From capture-lite with GNU General Public License v3.0 6 votes vote down vote up
async insert(
    tuples: T[],
    onConflict = OnConflictStrategy.ABORT,
    comparator = isEqual
  ) {
    return this.mutex.runExclusive(async () => {
      assertNoDuplicatedTuples(tuples, comparator);
      await this.initialize();
      if (onConflict === OnConflictStrategy.ABORT) {
        this.assertNoConflictWithExistedTuples(tuples, comparator);
        this.tuples$.next([...(this.tuples$.value ?? []), ...tuples]);
      } else if (onConflict === OnConflictStrategy.IGNORE) {
        this.tuples$.next(
          uniqWith([...(this.tuples$.value ?? []), ...tuples], comparator)
        );
      } else {
        this.tuples$.next(
          uniqWith([...tuples, ...(this.tuples$.value ?? [])], comparator)
        );
      }
      await this.dumpJson();
      return tuples;
    });
  }
Example #2
Source File: capacitor-filesystem-table.ts    From capture-lite with GNU General Public License v3.0 6 votes vote down vote up
function assertNoDuplicatedTuples<T>(
  tuples: T[],
  comparator: (x: T, y: T) => boolean
) {
  const unique = uniqWith(tuples, comparator);
  if (tuples.length !== unique.length) {
    const conflicted = differenceWith(tuples, unique, comparator);
    throw new Error(`Tuples duplicated: ${JSON.stringify(conflicted)}`);
  }
}