Skip to content

Commit

Permalink
fix(undefined issue): add type guard for data
Browse files Browse the repository at this point in the history
  • Loading branch information
dependentmadani committed Jul 4, 2024
1 parent f68441e commit dbd0aa1
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand All @@ -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
*
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit dbd0aa1

Please sign in to comment.