-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5aed657
commit 5c0ff6d
Showing
8 changed files
with
7,241 additions
and
5,077 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,46 @@ | ||
export function isEmptyRichText<T>(obj: T): obj is T { | ||
if (typeof obj === "object" && obj !== null && "raw" in obj) { | ||
if (typeof obj.raw === "object" && obj.raw !== null && "children" in obj.raw) { | ||
if (Array.isArray(obj.raw.children)) { | ||
if (obj.raw.children.length === 0) { | ||
return true; | ||
} | ||
if (obj.raw.children.length === 1) { | ||
const child = obj.raw.children[0]; | ||
if ( | ||
typeof child === "object" && | ||
child !== null && | ||
"children" in child && | ||
Array.isArray(child.children) | ||
) { | ||
if (child.children.length === 0) { | ||
return true; | ||
} | ||
if ( | ||
child.children.length === 1 && | ||
typeof child.children[0] === "object" && | ||
child.children[0] !== null && | ||
"text" in child.children[0] | ||
) { | ||
if (child.children[0].text === "") { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export function is<T>(obj: T): obj is NonNullable<T> { | ||
if (isEmptyRichText(obj)) { | ||
return false; | ||
} | ||
if (typeof obj === "object" && obj !== null) { | ||
return Object.keys(obj).length >= 1; | ||
} | ||
return typeof obj !== "undefined" && obj !== null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { isEmptyRichText, is } from "./type-guards"; | ||
|
||
describe("isEmptyRichText", () => { | ||
it("should be true", () => { | ||
const obj1 = { | ||
raw: { | ||
children: [ | ||
{ | ||
type: "paragraph", | ||
children: [ | ||
{ | ||
text: "", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
expect(isEmptyRichText(obj1)).toBe(true); | ||
|
||
const obj2 = { | ||
raw: { | ||
children: [], | ||
}, | ||
}; | ||
expect(isEmptyRichText(obj2)).toBe(true); | ||
|
||
const obj3 = { | ||
raw: { | ||
children: [ | ||
{ | ||
type: "paragraph", | ||
children: [], | ||
}, | ||
], | ||
}, | ||
}; | ||
expect(isEmptyRichText(obj3)).toBe(true); | ||
}); | ||
|
||
it("should be false", () => { | ||
const obj = { | ||
raw: { | ||
children: [ | ||
{ | ||
type: "paragraph", | ||
children: [ | ||
{ | ||
text: "has stuff", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
expect(isEmptyRichText(obj)).toBe(false); | ||
expect(isEmptyRichText({})).toBe(false); | ||
expect(isEmptyRichText(null)).toBe(false); | ||
expect(isEmptyRichText([])).toBe(false); | ||
expect(isEmptyRichText("")).toBe(false); | ||
expect(isEmptyRichText(0)).toBe(false); | ||
expect(isEmptyRichText(true)).toBe(false); | ||
expect(isEmptyRichText({ key: "val" })).toBe(false); | ||
expect(isEmptyRichText(NaN)).toBe(false); | ||
expect(isEmptyRichText(Infinity)).toBe(false); | ||
expect(isEmptyRichText(new Date())).toBe(false); | ||
expect(isEmptyRichText(undefined)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("is", () => { | ||
it("should be true", () => { | ||
expect(is({ key: "val" })).toBe(true); | ||
expect(is("stuff")).toBe(true); | ||
expect(is(1)).toBe(true); | ||
}); | ||
it("should be false", () => { | ||
expect(is({})).toBe(false); | ||
expect(is(null)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.