From f68441ec27928f2a3d78ef53858f288ed6430cb9 Mon Sep 17 00:00:00 2001 From: dependentmadani Date: Wed, 3 Jul 2024 22:57:20 +0100 Subject: [PATCH 1/5] fix(undefined data): undefined issue in normalizeData member function --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 07be85f..14f8830 100644 --- a/src/index.ts +++ b/src/index.ts @@ -145,14 +145,14 @@ export default class Header { * @private */ normalizeData(data: HeaderData): HeaderData { - const newData: HeaderData = {text: '', level: this.defaultLevel.number }; + const newData: HeaderData = { text: '', level: this.defaultLevel.number }; if (typeof data !== 'object') { - data = {text: '', level: this.defaultLevel.number}; + data = { text: '', level: this.defaultLevel.number}; } newData.text = data.text || ''; - newData.level = parseInt(data.level.toString()) || this.defaultLevel.number; + newData.level = data.level ? parseInt(data.level.toString()) : this.defaultLevel.number; return newData; } From dbd0aa1aff409c36a0be5a09fb3c45c436aa5eec Mon Sep 17 00:00:00 2001 From: dependentmadani Date: Thu, 4 Jul 2024 02:23:58 +0100 Subject: [PATCH 2/5] fix(undefined issue): add type guard for data --- src/index.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 14f8830..48b14e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,7 @@ interface Level { */ interface ConstructorArgs { /** Previously saved data */ - data: HeaderData; + data: HeaderData | {}; /** User config for the tool */ config: HeaderConfig; /** Editor.js API */ @@ -116,7 +116,7 @@ export default class Header { * @type {HeaderData} * @private */ - this._data = this.normalizeData(data); + this._data = this.normalizeData(data as HeaderData); /** * Main Block wrapper @@ -136,6 +136,17 @@ export default class Header { }; } + /** + * Check if data is valid + * + * @param {any} data - data to check + * @returns {data is HeaderData} + * @private + */ + isHeaderData(data: any): data is HeaderData { + return (data as HeaderData).text !== undefined && (data as HeaderData).level !== undefined; + } + /** * Normalize input data * @@ -147,12 +158,12 @@ export default class Header { normalizeData(data: HeaderData): HeaderData { const newData: HeaderData = { text: '', level: this.defaultLevel.number }; - if (typeof data !== 'object') { + if (!this.isHeaderData(data)) { data = { text: '', level: this.defaultLevel.number}; } newData.text = data.text || ''; - newData.level = data.level ? parseInt(data.level.toString()) : this.defaultLevel.number; + newData.level = parseInt(data.level.toString()) || this.defaultLevel.number; return newData; } @@ -275,7 +286,7 @@ export default class Header { get data(): HeaderData { this._data.text = this._element.innerHTML; this._data.level = this.currentLevel.number; - + return this._data; } From 9cbae654f5bf9d70279a7a9fe3849f44485ca9fd Mon Sep 17 00:00:00 2001 From: dependentmadani Date: Thu, 4 Jul 2024 12:13:44 +0100 Subject: [PATCH 3/5] update(data type): reformulate the data type and function of nomalizeData --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 48b14e4..2343d24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -116,7 +116,7 @@ export default class Header { * @type {HeaderData} * @private */ - this._data = this.normalizeData(data as HeaderData); + this._data = this.normalizeData(data); /** * Main Block wrapper @@ -155,15 +155,15 @@ export default class Header { * @returns {HeaderData} * @private */ - normalizeData(data: HeaderData): HeaderData { + normalizeData(data: HeaderData | {}): HeaderData { const newData: HeaderData = { text: '', level: this.defaultLevel.number }; if (!this.isHeaderData(data)) { data = { text: '', level: this.defaultLevel.number}; } - newData.text = data.text || ''; - newData.level = parseInt(data.level.toString()) || this.defaultLevel.number; + newData.text = (data as HeaderData).text || ''; + newData.level = parseInt((data as HeaderData).level.toString()) || this.defaultLevel.number; return newData; } From 66cfd91b40cc3dd5959f9546fe8572ad89a5d0df Mon Sep 17 00:00:00 2001 From: dependentmadani Date: Thu, 4 Jul 2024 12:20:55 +0100 Subject: [PATCH 4/5] update(nomalizData): remove casting --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2343d24..79fb250 100644 --- a/src/index.ts +++ b/src/index.ts @@ -159,11 +159,11 @@ export default class Header { const newData: HeaderData = { text: '', level: this.defaultLevel.number }; if (!this.isHeaderData(data)) { - data = { text: '', level: this.defaultLevel.number}; + return { text: '', level: this.defaultLevel.number}; } - newData.text = (data as HeaderData).text || ''; - newData.level = parseInt((data as HeaderData).level.toString()) || this.defaultLevel.number; + newData.text = data.text || ''; + newData.level = parseInt(data.level.toString()) || this.defaultLevel.number; return newData; } From 2c0d37903b5012e99b2cd8dad7d102f52e4d8e40 Mon Sep 17 00:00:00 2001 From: dependentmadani Date: Thu, 4 Jul 2024 12:24:43 +0100 Subject: [PATCH 5/5] update(version): increment patch version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 28a4480..b923a9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@editorjs/header", - "version": "2.8.4", + "version": "2.8.5", "keywords": [ "codex editor", "header",