Skip to content

Commit f440a60

Browse files
authored
Replace typeofs (codex-team#1434)
* Replace typeofs * save eslint fixes * fix * update * remove sourcemap * update * update changelog * fix typo
1 parent e319e04 commit f440a60

15 files changed

Lines changed: 159 additions & 92 deletions

File tree

dist/editor.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `Fix` — Fix problem with entering to Editor.js by Tab key [#1393](https://github.com/codex-team/editor.js/issues/1393)
88
- `Fix` - Sanitize pasted block data [#1396](https://github.com/codex-team/editor.js/issues/1396).
99
- `Fix` - Unnecessary block creation after arrow navigation at last non-default block[#1414](https://github.com/codex-team/editor.js/issues/1414)
10+
- `Impovements` - Native `typeof`replaced with custom utils methods
1011

1112
### 2.19
1213

src/codex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class EditorJS {
5353
/**
5454
* If `onReady` was passed in `configuration` then redefine onReady function
5555
*/
56-
if (typeof configuration === 'object' && _.isFunction(configuration.onReady)) {
56+
if (_.isObject(configuration) && _.isFunction(configuration.onReady)) {
5757
onReady = configuration.onReady;
5858
}
5959

src/components/block/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export default class Block {
203203
* @param {BlockToolData} options.data - Tool's initial data
204204
* @param {BlockToolConstructable} options.Tool — Tool's class
205205
* @param {ToolSettings} options.settings - default tool's config
206-
* @param {Module} options.api - Editor API module for pass it to the Block Tunes
206+
* @param options.api - Editor API module for pass it to the Block Tunes
207207
* @param {boolean} options.readOnly - Read-Only flag
208208
*/
209209
constructor({

src/components/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class Core {
119119
* Process zero-configuration or with only holderId
120120
* Make config object
121121
*/
122-
if (typeof config !== 'object') {
122+
if (!_.isObject(config)) {
123123
config = {
124124
holder: config,
125125
};
@@ -246,11 +246,11 @@ export default class Core {
246246
/**
247247
* Check for a holder element's existence
248248
*/
249-
if (typeof holder === 'string' && !$.get(holder)) {
249+
if (_.isString(holder) && !$.get(holder)) {
250250
throw Error(`element with ID «${holder}» is missing. Pass correct holder's ID.`);
251251
}
252252

253-
if (holder && typeof holder === 'object' && !$.isElement(holder)) {
253+
if (holder && _.isObject(holder) && !$.isElement(holder)) {
254254
throw Error('holder as HTMLElement if provided must be inherit from Element class.');
255255
}
256256
}

src/components/dom.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ export default class Dom {
292292
*/
293293
// eslint-disable-next-line @typescript-eslint/no-explicit-any
294294
public static isElement(node: any): node is Element {
295-
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
295+
if (_.isNumber(node)) {
296+
return false;
297+
}
298+
299+
return node && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
296300
}
297301

298302
/**
@@ -303,7 +307,11 @@ export default class Dom {
303307
*/
304308
// eslint-disable-next-line @typescript-eslint/no-explicit-any
305309
public static isFragment(node: any): node is DocumentFragment {
306-
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
310+
if (_.isNumber(node)) {
311+
return false;
312+
}
313+
314+
return node && node.nodeType && node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
307315
}
308316

309317
/**
@@ -532,7 +540,7 @@ export default class Dom {
532540
public static containsOnlyInlineElements(data: string | HTMLElement): boolean {
533541
let wrapper: HTMLElement;
534542

535-
if (typeof data === 'string') {
543+
if (_.isString(data)) {
536544
wrapper = document.createElement('div');
537545
wrapper.innerHTML = data;
538546
} else {
@@ -572,7 +580,7 @@ export default class Dom {
572580
* @returns {HTMLElement}
573581
*/
574582
public static getHolder(element: string | HTMLElement): HTMLElement {
575-
if (typeof element === 'string') {
583+
if (_.isString(element)) {
576584
return document.getElementById(element);
577585
}
578586

src/components/flipper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class Flipper {
6868
* @param {FlipperOptions} options - different constructing settings
6969
*/
7070
constructor(options: FlipperOptions) {
71-
this.allowArrows = typeof options.allowArrows === 'boolean' ? options.allowArrows : true;
71+
this.allowArrows = _.isBoolean(options.allowArrows) ? options.allowArrows : true;
7272
this.iterator = new DomIterator(options.items, options.focusedItemClass);
7373
this.activateCallback = options.activateCallback;
7474
}

src/components/i18n/namespace-internal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import defaultDictionary from './locales/en/messages.json';
22
import { DictNamespaces } from '../../types-internal/i18n-internal-namespace';
3-
import { typeOf } from '../utils';
3+
import { isObject, isString } from '../utils';
44

55
/**
66
* Evaluate messages dictionary and return object for namespace chaining
@@ -12,14 +12,14 @@ function getNamespaces(dict: object, keyPath?: string): DictNamespaces<typeof de
1212
const result = {};
1313

1414
Object.entries(dict).forEach(([key, section]) => {
15-
if (typeOf(section) === 'object') {
15+
if (isObject(section)) {
1616
const newPath = keyPath ? `${keyPath}.${key}` : key;
1717

1818
/**
1919
* Check current section values, if all of them are strings, so there is the last section
2020
*/
2121
const isLastSection = Object.values(section).every((sectionValue) => {
22-
return typeOf(sectionValue) === 'string';
22+
return isString(sectionValue);
2323
});
2424

2525
/**

src/components/modules/paste.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ export default class Paste extends Module {
304304
return;
305305
}
306306

307-
if (typeof toolInstance.onPaste !== 'function') {
307+
if (!_.isFunction(toolInstance.onPaste)) {
308308
return;
309309
}
310310

src/components/modules/sanitizer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default class Sanitizer extends Module {
9494
* Array: call sanitize for each item
9595
*/
9696
return this.cleanArray(dataToSanitize, rules);
97-
} else if (typeof dataToSanitize === 'object') {
97+
} else if (_.isObject(dataToSanitize)) {
9898
/**
9999
* Objects: just clean object deeper.
100100
*/
@@ -105,7 +105,7 @@ export default class Sanitizer extends Module {
105105
*
106106
* Clean only strings
107107
*/
108-
if (typeof dataToSanitize === 'string') {
108+
if (_.isString(dataToSanitize)) {
109109
return this.cleanOneItem(dataToSanitize, rules);
110110
}
111111

@@ -169,7 +169,7 @@ export default class Sanitizer extends Module {
169169
if (Object.prototype.hasOwnProperty.call(toolRules, fieldName)) {
170170
const rule = toolRules[fieldName];
171171

172-
if (typeof rule === 'object') {
172+
if (_.isObject(rule)) {
173173
toolConfig[fieldName] = Object.assign({}, baseConfig, rule);
174174
} else {
175175
toolConfig[fieldName] = rule;
@@ -195,7 +195,7 @@ export default class Sanitizer extends Module {
195195

196196
let config = {} as SanitizerConfig;
197197

198-
if (typeof enableInlineTools === 'boolean' && enableInlineTools) {
198+
if (_.isBoolean(enableInlineTools) && enableInlineTools) {
199199
/**
200200
* getting all tools sanitizer rule
201201
*/
@@ -292,7 +292,7 @@ export default class Sanitizer extends Module {
292292
* @returns {string}
293293
*/
294294
private cleanOneItem(taintString: string, rule: SanitizerConfig|boolean): string {
295-
if (typeof rule === 'object') {
295+
if (_.isObject(rule)) {
296296
return this.clean(taintString, rule);
297297
} else if (rule === false) {
298298
return this.clean(taintString, {} as SanitizerConfig);
@@ -309,7 +309,7 @@ export default class Sanitizer extends Module {
309309
* @param {SanitizerConfig} config - config to check
310310
*/
311311
private isRule(config: SanitizerConfig): boolean {
312-
return typeof config === 'object' || typeof config === 'boolean' || _.isFunction(config);
312+
return _.isObject(config) || _.isBoolean(config) || _.isFunction(config);
313313
}
314314

315315
/**

0 commit comments

Comments
 (0)