feat: create symmetric difference

This commit is contained in:
Arman
2022-11-07 12:42:40 +01:00
parent 0e5e59c4b1
commit ad8ba17833
11 changed files with 25 additions and 51 deletions
@@ -19,7 +19,7 @@
TableHeader,
TableRow
} from '$lib/elements/table';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { onDestroy, onMount } from 'svelte';
import { writable, type Unsubscriber } from 'svelte/store';
import Actions from './actions.svelte';
@@ -40,10 +40,7 @@
permissions.forEach(fromPermissionString);
unsubscribe = groups.subscribe(() => {
const current = exportRoles();
if (
difference(current, permissions).length ||
difference(permissions, current).length
) {
if (symmetricDifference(current, permissions).length) {
permissions = current;
}
});
+2 -2
View File
@@ -9,7 +9,7 @@
TableHeader,
TableRow
} from '$lib/elements/table';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { onDestroy, onMount } from 'svelte';
import { writable, type Unsubscriber } from 'svelte/store';
import Actions from './actions.svelte';
@@ -30,7 +30,7 @@
roles.forEach(addRole);
unsubscribe = groups.subscribe((n) => {
const current = Array.from(n.keys());
if (difference(current, roles).length || difference(roles, current).length) {
if (symmetricDifference(current, roles).length) {
roles = current;
}
});
+4
View File
@@ -8,3 +8,7 @@ export function difference(arr1: unknown[], arr2: unknown[]) {
const intersection = new Set(arr1.filter((elem) => !set.has(elem)));
return Array.from(intersection);
}
export function symmetricDifference(arr1: unknown[], arr2: unknown[]) {
return difference(arr1, arr2).concat(difference(arr2, arr1));
}
@@ -8,7 +8,7 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import Document from './document.svelte';
import Delete from './delete.svelte';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { Permissions } from '$lib/components/permissions';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
@@ -41,10 +41,7 @@
}
$: if (permissions) {
if (
difference(permissions, $doc.$permissions).length ||
difference($doc.$permissions, permissions).length
) {
if (symmetricDifference(permissions, $doc.$permissions).length) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}
@@ -9,7 +9,7 @@
import { addNotification } from '$lib/stores/notifications';
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import Delete from './deleteCollection.svelte';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
@@ -37,10 +37,7 @@
if (collectionDocumentSecurity !== $collection.documentSecurity) {
arePermsDisabled = false;
} else if (collectionPermissions) {
if (
difference(collectionPermissions, $collection.$permissions).length ||
difference($collection.$permissions, collectionPermissions).length
) {
if (symmetricDifference(collectionPermissions, $collection.$permissions).length) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}
@@ -23,7 +23,7 @@
import Upload from './uploadVariables.svelte';
import { toLocaleDateTime } from '$lib/helpers/date';
import { Roles } from '$lib/components/permissions';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import TableList from '$lib/elements/table/tableList.svelte';
import { TableCell, TableCellText } from '$lib/elements/table';
import Heading from '$lib/components/heading.svelte';
@@ -244,19 +244,13 @@
}
$: if (permissions) {
if (
difference(permissions, $func.execute).length ||
difference($func.execute, permissions).length
) {
if (symmetricDifference(permissions, $func.execute).length) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}
$: if ($eventSet) {
if (
difference(Array.from($eventSet), $func.events).length ||
difference($func.events, Array.from($eventSet)).length
) {
if (symmetricDifference(Array.from($eventSet), $func.events).length) {
areEventsDisabled = false;
} else areEventsDisabled = true;
}
@@ -3,7 +3,7 @@
import { CardGrid, Heading, Secret } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, Form, FormList, InputText, InputDateTime } from '$lib/elements/forms';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { toLocaleDateTime } from '$lib/helpers/date';
import { Container } from '$lib/layout';
import { addNotification } from '$lib/stores/notifications';
@@ -145,10 +145,7 @@
submit
disabled={scopes &&
$key?.scopes &&
!(
difference(scopes, $key.scopes).length !== 0 ||
difference($key.scopes, scopes).length !== 0
)}>Update</Button>
!symmetricDifference(scopes, $key.scopes).length}>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
@@ -3,7 +3,7 @@
import { Button, FormList, InputChoice } from '$lib/elements/forms';
import { scopes as allScopes } from '$lib/constants';
import { onMount } from 'svelte';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
export let scopes: string[];
@@ -70,10 +70,7 @@
const newScopes = allScopes
.filter((scope) => activeScopes[scope.scope])
.map(({ scope }) => scope);
if (
difference(scopes, newScopes).length !== 0 ||
difference(newScopes, scopes).length !== 0
) {
if (symmetricDifference(scopes, newScopes).length) {
scopes = newScopes;
}
}
@@ -18,7 +18,7 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import Delete from './delete.svelte';
import Regenerate from './regenerate.svelte';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { writable, type Writable } from 'svelte/store';
@@ -144,10 +144,7 @@
}
$: if ($eventSet) {
if (
difference(Array.from($eventSet), $webhook.events).length ||
difference($webhook.events, Array.from($eventSet)).length
) {
if (symmetricDifference(Array.from($eventSet), $webhook.events).length) {
areEventsDisabled = false;
} else areEventsDisabled = true;
}
@@ -9,7 +9,7 @@
import { addNotification } from '$lib/stores/notifications';
import { calculateSize } from '$lib/helpers/sizeConvertion';
import { onMount } from 'svelte';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { Permissions } from '$lib/components/permissions';
import Delete from './deleteFile.svelte';
import { invalidate } from '$app/navigation';
@@ -30,10 +30,7 @@
sdkForProject.storage.getFileView($file.bucketId, fileId).toString() + '&mode=admin';
$: if (filePermissions) {
if (
difference(filePermissions, $file.$permissions).length ||
difference($file.$permissions, filePermissions).length
) {
if (symmetricDifference(filePermissions, $file.$permissions).length) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}
@@ -18,7 +18,7 @@
import { sdkForProject } from '$lib/stores/sdk';
import { addNotification } from '$lib/stores/notifications';
import { onMount } from 'svelte';
import { difference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { Permissions } from '$lib/components/permissions';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
@@ -58,10 +58,7 @@
if (bucketFileSecurity !== $bucket.fileSecurity) {
arePermsDisabled = false;
} else if (bucketPermissions) {
if (
difference(bucketPermissions, $bucket.$permissions).length ||
difference($bucket.$permissions, bucketPermissions).length
) {
if (symmetricDifference(bucketPermissions, $bucket.$permissions).length) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}