From bf428df4e25760b23bf12d356439966ca59de685 Mon Sep 17 00:00:00 2001 From: Mitchell Donaldson Date: Tue, 3 Nov 2020 08:28:43 +1100 Subject: [PATCH] Add hasPrevious method to Path (#3963) * Add hasPrevious method to Path API. Co-authored-by: Mitchell Donaldson --- packages/slate/src/interfaces/path.ts | 8 ++++++++ packages/slate/test/interfaces/Path/hasPrevious/root.tsx | 7 +++++++ .../slate/test/interfaces/Path/hasPrevious/success.tsx | 7 +++++++ 3 files changed, 22 insertions(+) create mode 100644 packages/slate/test/interfaces/Path/hasPrevious/root.tsx create mode 100644 packages/slate/test/interfaces/Path/hasPrevious/success.tsx diff --git a/packages/slate/src/interfaces/path.ts b/packages/slate/src/interfaces/path.ts index fb256082b..94120b216 100755 --- a/packages/slate/src/interfaces/path.ts +++ b/packages/slate/src/interfaces/path.ts @@ -118,6 +118,14 @@ export const Path = { ) }, + /** + * Check if the path of previous sibling node exists + */ + + hasPrevious(path: Path): boolean { + return path[path.length - 1] > 0 + }, + /** * Check if a path is after another. */ diff --git a/packages/slate/test/interfaces/Path/hasPrevious/root.tsx b/packages/slate/test/interfaces/Path/hasPrevious/root.tsx new file mode 100644 index 000000000..827d102e8 --- /dev/null +++ b/packages/slate/test/interfaces/Path/hasPrevious/root.tsx @@ -0,0 +1,7 @@ +import { Path } from 'slate' + +export const input = [0, 0] +export const test = path => { + return Path.hasPrevious(path) +} +export const output = false diff --git a/packages/slate/test/interfaces/Path/hasPrevious/success.tsx b/packages/slate/test/interfaces/Path/hasPrevious/success.tsx new file mode 100644 index 000000000..76e772bd4 --- /dev/null +++ b/packages/slate/test/interfaces/Path/hasPrevious/success.tsx @@ -0,0 +1,7 @@ +import { Path } from 'slate' + +export const input = [0, 1] +export const test = path => { + return Path.hasPrevious(path) +} +export const output = true