Skip to content

Commit

Permalink
Replace typeofs (#1434)
Browse files Browse the repository at this point in the history
* Replace typeofs

* save eslint fixes

* fix

* update

* remove sourcemap

* update

* update changelog

* fix typo
  • Loading branch information
khaydarov committed Nov 21, 2020
1 parent e319e04 commit f440a60
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 92 deletions.
2 changes: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

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

### 2.19

Expand Down
2 changes: 1 addition & 1 deletion src/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class EditorJS {
/**
* If `onReady` was passed in `configuration` then redefine onReady function
*/
if (typeof configuration === 'object' && _.isFunction(configuration.onReady)) {
if (_.isObject(configuration) && _.isFunction(configuration.onReady)) {
onReady = configuration.onReady;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class Block {
* @param {BlockToolData} options.data - Tool's initial data
* @param {BlockToolConstructable} options.Tool — Tool's class
* @param {ToolSettings} options.settings - default tool's config
* @param {Module} options.api - Editor API module for pass it to the Block Tunes
* @param options.api - Editor API module for pass it to the Block Tunes
* @param {boolean} options.readOnly - Read-Only flag
*/
constructor({
Expand Down
6 changes: 3 additions & 3 deletions src/components/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class Core {
* Process zero-configuration or with only holderId
* Make config object
*/
if (typeof config !== 'object') {
if (!_.isObject(config)) {
config = {
holder: config,
};
Expand Down Expand Up @@ -246,11 +246,11 @@ export default class Core {
/**
* Check for a holder element's existence
*/
if (typeof holder === 'string' && !$.get(holder)) {
if (_.isString(holder) && !$.get(holder)) {
throw Error(`element with ID «${holder}» is missing. Pass correct holder's ID.`);
}

if (holder && typeof holder === 'object' && !$.isElement(holder)) {
if (holder && _.isObject(holder) && !$.isElement(holder)) {
throw Error('holder as HTMLElement if provided must be inherit from Element class.');
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ export default class Dom {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static isElement(node: any): node is Element {
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
if (_.isNumber(node)) {
return false;
}

return node && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
}

/**
Expand All @@ -303,7 +307,11 @@ export default class Dom {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static isFragment(node: any): node is DocumentFragment {
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
if (_.isNumber(node)) {
return false;
}

return node && node.nodeType && node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
}

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

if (typeof data === 'string') {
if (_.isString(data)) {
wrapper = document.createElement('div');
wrapper.innerHTML = data;
} else {
Expand Down Expand Up @@ -572,7 +580,7 @@ export default class Dom {
* @returns {HTMLElement}
*/
public static getHolder(element: string | HTMLElement): HTMLElement {
if (typeof element === 'string') {
if (_.isString(element)) {
return document.getElementById(element);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/flipper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Flipper {
* @param {FlipperOptions} options - different constructing settings
*/
constructor(options: FlipperOptions) {
this.allowArrows = typeof options.allowArrows === 'boolean' ? options.allowArrows : true;
this.allowArrows = _.isBoolean(options.allowArrows) ? options.allowArrows : true;
this.iterator = new DomIterator(options.items, options.focusedItemClass);
this.activateCallback = options.activateCallback;
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/i18n/namespace-internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import defaultDictionary from './locales/en/messages.json';
import { DictNamespaces } from '../../types-internal/i18n-internal-namespace';
import { typeOf } from '../utils';
import { isObject, isString } from '../utils';

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

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

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export default class Paste extends Module {
return;
}

if (typeof toolInstance.onPaste !== 'function') {
if (!_.isFunction(toolInstance.onPaste)) {
return;
}

Expand Down
12 changes: 6 additions & 6 deletions src/components/modules/sanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class Sanitizer extends Module {
* Array: call sanitize for each item
*/
return this.cleanArray(dataToSanitize, rules);
} else if (typeof dataToSanitize === 'object') {
} else if (_.isObject(dataToSanitize)) {
/**
* Objects: just clean object deeper.
*/
Expand All @@ -105,7 +105,7 @@ export default class Sanitizer extends Module {
*
* Clean only strings
*/
if (typeof dataToSanitize === 'string') {
if (_.isString(dataToSanitize)) {
return this.cleanOneItem(dataToSanitize, rules);
}

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

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

let config = {} as SanitizerConfig;

if (typeof enableInlineTools === 'boolean' && enableInlineTools) {
if (_.isBoolean(enableInlineTools) && enableInlineTools) {
/**
* getting all tools sanitizer rule
*/
Expand Down Expand Up @@ -292,7 +292,7 @@ export default class Sanitizer extends Module {
* @returns {string}
*/
private cleanOneItem(taintString: string, rule: SanitizerConfig|boolean): string {
if (typeof rule === 'object') {
if (_.isObject(rule)) {
return this.clean(taintString, rule);
} else if (rule === false) {
return this.clean(taintString, {} as SanitizerConfig);
Expand All @@ -309,7 +309,7 @@ export default class Sanitizer extends Module {
* @param {SanitizerConfig} config - config to check
*/
private isRule(config: SanitizerConfig): boolean {
return typeof config === 'object' || typeof config === 'boolean' || _.isFunction(config);
return _.isObject(config) || _.isBoolean(config) || _.isFunction(config);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/toolbar/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {

if (_.isFunction(exportProp)) {
exportData = exportProp(blockData);
} else if (typeof exportProp === 'string') {
} else if (_.isString(exportProp)) {
exportData = blockData[exportProp];
} else {
_.log('Conversion «export» property must be a string or function. ' +
Expand All @@ -242,7 +242,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {

if (_.isFunction(importProp)) {
newBlockData = importProp(cleaned);
} else if (typeof importProp === 'string') {
} else if (_.isString(importProp)) {
newBlockData[importProp] = cleaned;
} else {
_.log('Conversion «import» property must be a string or function. ' +
Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/toolbar/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
* Returns inline toolbar settings for a particular tool
*
* @param {string} toolName - user specified name of tool
* @returns - array of ordered tool names or false
* @returns {string[] | boolean} array of ordered tool names or false
*/
private getInlineToolbarSettings(toolName): string[] | boolean {
const toolSettings = this.Editor.Tools.getToolSettings(toolName);
Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export default class Tools extends Module {
* If Tool is an object not a Tool's class then
* save class and settings separately
*/
if (typeof this.config.tools[toolName] === 'object') {
if (_.isObject(this.config.tools[toolName])) {
/**
* Save Tool's class from 'class' field
*
Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export default class UI extends Module<UINodes> {
* @param {KeyboardEvent} event - keyboard event
*/
private enterPressed(event: KeyboardEvent): void {
const { BlockManager, BlockSelection, Caret } = this.Editor;
const { BlockManager, BlockSelection } = this.Editor;
const hasPointerToBlock = BlockManager.currentBlockIndex >= 0;

/**
Expand Down
Loading

0 comments on commit f440a60

Please sign in to comment.