Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(utils): improved normalize data util #131

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/list",
"version": "2.0.2",
"version": "2.0.3",
"keywords": [
"codex editor",
"list",
Expand Down
5 changes: 5 additions & 0 deletions src/types/ListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface OldListData {
items: string[];
}

/**
* Type that represents data of the List tool
*/
export type OldNestedListData = Omit<ListData, 'meta'>;

/**
* Interface that represents old checklist data format
*/
Expand Down
25 changes: 20 additions & 5 deletions src/utils/normalizeData.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import type { OldListData, ListData, ListItem, OldChecklistData } from '../types/ListParams';
import type { OldListData, ListData, ListItem, OldChecklistData, OldNestedListData } from '../types/ListParams';

/**
* Method that checks if data is result of the Old list tool save mtehod
* @param data - data of the OldList, Checklist or Editorjs List tool
* @param data - data of the OldList, Checklist, OldNestedList or Editorjs List tool
* @returns true if data related to the List tool, false otherwise
*/
function instanceOfOldListData(data: ListData | OldListData | OldChecklistData): data is OldListData {
function instanceOfOldListData(data: ListData | OldListData | OldChecklistData | OldNestedListData): data is OldListData {
return (typeof data.items[0] === 'string');
}

/**
* Method that checks if data is result of the Old nested list tool save method
* @param data - data of the OldList, Checklist, OldNestedList or Editorjs List tool
* @returns true if data is related to the Nested List tool, false otherwise
*/
function instanceOfOldNestedListData(data: ListData | OldListData | OldChecklistData | OldNestedListData): data is OldNestedListData {
return !('meta' in data);
}

/**
* Method that checks if data is result of the Old checklist tool save method
* @param data - data of the Checklist, OldList or Editorjs List tool
* @param data - data of the Checklist, OldList, OldNestedList or Editorjs List tool
* @returns true if data is related to the Checklist tool, false otherwise
*/
function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData): data is OldChecklistData {
function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData | OldNestedListData): data is OldChecklistData {
return (
typeof data.items[0] !== 'string'
&& 'text' in data.items[0]
Expand Down Expand Up @@ -62,6 +71,12 @@ export default function normalizeData(data: ListData | OldListData | OldChecklis
meta: {},
items: normalizedDataItems,
};
} else if (instanceOfOldNestedListData(data)) {
return {
style: data.style,
meta: {},
items: data.items,
};
} else {
return data;
}
Expand Down
Loading