From 8d7905e5a40187846a09de3a24015144f2ffef29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr> Date: Wed, 13 Jul 2022 08:35:14 +0200 Subject: [PATCH] [dome] new data/array component --- ivette/src/dome/renderer/data/arrays.ts | 45 +++++++++++++++++++++++++ ivette/src/dome/renderer/dnd.tsx | 23 +------------ 2 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 ivette/src/dome/renderer/data/arrays.ts diff --git a/ivette/src/dome/renderer/data/arrays.ts b/ivette/src/dome/renderer/data/arrays.ts new file mode 100644 index 00000000000..74a3109da28 --- /dev/null +++ b/ivette/src/dome/renderer/data/arrays.ts @@ -0,0 +1,45 @@ +/* ************************************************************************ */ +/* */ +/* This file is part of Frama-C. */ +/* */ +/* Copyright (C) 2007-2022 */ +/* CEA (Commissariat à l'énergie atomique et aux énergies */ +/* alternatives) */ +/* */ +/* you can redistribute it and/or modify it under the terms of the GNU */ +/* Lesser General Public License as published by the Free Software */ +/* Foundation, version 2.1. */ +/* */ +/* It is distributed in the hope that it will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU Lesser General Public License for more details. */ +/* */ +/* See the GNU Lesser General Public License version 2.1 */ +/* for more details (enclosed in the file licenses/LGPLv2.1). */ +/* */ +/* ************************************************************************ */ + +/** + Safe ARRAY utilities. + @packageDocumentation + @module dome/data/arrays +*/ + +/** Swaps items at index i and j if they are both in range. */ +export function swap<A>(ls: A[], a: number, b: number): A[] { + const n = ls.length; + if (a === b || 0 > a || a >= n || 0 > b || b >= n) return ls; + const [i, j] = a < b ? [a, b] : [b, a]; + return ls.slice(0, i).concat(ls.slice(i + 1, j + 1), ls[i], ls.slice(j + 1)); +} + +/** Remove item at index i when in range. */ +export function removeAt<A>(ls: A[], k: number): A[] { + return 0 <= k && k < ls.length ? ls.slice(0, k).concat(ls.slice(k + 1)) : ls; +} + +/** Insert an item at index i when in range or off-by-one. */ +export function insertAt<A>(ls: A[], id: A, k: number): A[] { + return 0 <= k && k <= ls.length ? ls.slice(0, k).concat(id, ls.slice(k)) : ls; +} diff --git a/ivette/src/dome/renderer/dnd.tsx b/ivette/src/dome/renderer/dnd.tsx index 7a552285445..7081cff7364 100644 --- a/ivette/src/dome/renderer/dnd.tsx +++ b/ivette/src/dome/renderer/dnd.tsx @@ -30,6 +30,7 @@ import React from 'react'; import { classes, styles } from 'dome/misc/utils'; +import { swap } from 'dome/data/arrays'; import { DraggableCore, DraggableEvent, @@ -412,28 +413,6 @@ export function DragSource(props: DragSourceProps): JSX.Element { ); } -/* -------------------------------------------------------------------------- */ -/* --- Ordering --- */ -/* -------------------------------------------------------------------------- */ - -/** Swaps items at index i and j if they are both in range. */ -export function swap<A>(ls: A[], a: number, b: number): A[] { - const n = ls.length; - if (a === b || 0 > a || a >= n || 0 > b || b >= n) return ls; - const [i, j] = a < b ? [a, b] : [b, a]; - return ls.slice(0, i).concat(ls.slice(i + 1, j + 1), ls[i], ls.slice(j + 1)); -} - -/** Remove item at index i when in range. */ -export function removeAt<A>(ls: A[], k: number): A[] { - return 0 <= k && k < ls.length ? ls.slice(0, k).concat(ls.slice(k + 1)) : ls; -} - -/** Insert an item at index i when in range or off-by-one. */ -export function insertAt<A>(ls: A[], id: A, k: number): A[] { - return 0 <= k && k <= ls.length ? ls.slice(0, k).concat(id, ls.slice(k)) : ls; -} - /* -------------------------------------------------------------------------- */ /* --- List Container --- */ /* -------------------------------------------------------------------------- */ -- GitLab