Skip to content

Commit

Permalink
Merge pull request #131 from editor-js/improve-normalize-data-util
Browse files Browse the repository at this point in the history
imp(utils): improved normalize data util
  • Loading branch information
e11sy authored Jan 18, 2025
2 parents bbc46d5 + 7ba9701 commit ca0357c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
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

0 comments on commit ca0357c

Please sign in to comment.