Replace typeofs (#1434)

* Replace typeofs

* save eslint fixes

* fix

* update

* remove sourcemap

* update

* update changelog

* fix typo
This commit is contained in:
Murod Khaydarov 2020-11-21 20:54:01 +03:00 committed by GitHub
parent e319e04350
commit f440a60ead
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 159 additions and 92 deletions

2
dist/editor.js vendored

File diff suppressed because one or more lines are too long

View file

@ -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

View file

@ -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;
}

View file

@ -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({

View file

@ -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,
};
@ -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.');
}
}

View file

@ -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;
}
/**
@ -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;
}
/**
@ -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 {
@ -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);
}

View file

@ -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;
}

View file

@ -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
@ -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);
});
/**

View file

@ -304,7 +304,7 @@ export default class Paste extends Module {
return;
}
if (typeof toolInstance.onPaste !== 'function') {
if (!_.isFunction(toolInstance.onPaste)) {
return;
}

View file

@ -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.
*/
@ -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);
}
@ -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;
@ -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
*/
@ -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);
@ -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);
}
/**

View file

@ -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. ' +
@ -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. ' +

View file

@ -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);

View file

@ -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
*

View file

@ -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;
/**

View file

@ -173,6 +173,128 @@ export const log = _log.bind(window, false);
*/
export const logLabeled = _log.bind(window, true);
/**
* Return string representation of the object type
*
* @param {*} object - object to get type
*
* @returns {string}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function typeOf(object: any): string {
return Object.prototype.toString.call(object).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
/**
* Check if passed variable is a function
*
* @param {*} fn - function to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isFunction(fn: any): fn is Function {
return typeOf(fn) === 'function';
}
/**
* Checks if passed argument is an object
*
* @param {*} v - object to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isObject(v: any): v is object {
return typeOf(v) === 'object';
}
/**
* Checks if passed argument is a string
*
* @param {*} v - variable to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isString(v: any): v is string {
return typeOf(v) === 'string';
}
/**
* Checks if passed argument is boolean
*
* @param {*} v - variable to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isBoolean(v: any): v is boolean {
return typeOf(v) === 'boolean';
}
/**
* Checks if passed argument is number
*
* @param {*} v - variable to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isNumber(v: any): v is number {
return typeOf(v) === 'number';
}
/**
* Checks if passed argument is undefined
*
* @param {*} v - variable to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isUndefined(v: any): v is undefined {
return typeOf(v) === 'undefined';
}
/**
* Check if passed function is a class
*
* @param {Function} fn - function to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isClass(fn: any): boolean {
return isFunction(fn) && /^\s*class\s+/.test(fn.toString());
}
/**
* Checks if object is empty
*
* @param {object} object - object to check
*
* @returns {boolean}
*/
export function isEmpty(object: object): boolean {
if (!object) {
return true;
}
return Object.keys(object).length === 0 && object.constructor === Object;
}
/**
* Check if passed object is a Promise
*
* @param {*} object - object to check
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isPromise(object: any): object is Promise<any> {
return Promise.resolve(object) === object;
}
/**
* Returns true if passed key code is printable (a-Z, 0-9, etc) character.
*
@ -223,9 +345,9 @@ export async function sequence(
): Promise<void> {
try {
await chainData.function(chainData.data);
await successCallback(typeof chainData.data !== 'undefined' ? chainData.data : {});
await successCallback(!isUndefined(chainData.data) ? chainData.data : {});
} catch (e) {
fallbackCallback(typeof chainData.data !== 'undefined' ? chainData.data : {});
fallbackCallback(!isUndefined(chainData.data) ? chainData.data : {});
}
}
@ -255,56 +377,6 @@ export function array(collection: ArrayLike<any>): any[] {
return Array.prototype.slice.call(collection);
}
/**
* Check if passed variable is a function
*
* @param {*} fn - function to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isFunction(fn: any): fn is Function {
return typeof fn === 'function';
}
/**
* Check if passed function is a class
*
* @param {Function} fn - function to check
*
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isClass(fn: any): boolean {
return isFunction(fn) && /^\s*class\s+/.test(fn.toString());
}
/**
* Checks if object is empty
*
* @param {object} object - object to check
*
* @returns {boolean}
*/
export function isEmpty(object: object): boolean {
if (!object) {
return true;
}
return Object.keys(object).length === 0 && object.constructor === Object;
}
/**
* Check if passed object is a Promise
*
* @param {*} object - object to check
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isPromise(object: any): object is Promise<any> {
return Promise.resolve(object) === object;
}
/**
* Delays method execution
*
@ -440,18 +512,6 @@ export function capitalize(text: string): string {
return text[0].toUpperCase() + text.slice(1);
}
/**
* Return string representation of the object type
*
* @param {*} object - object to get type
*
* @returns {string}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function typeOf(object: any): string {
return Object.prototype.toString.call(object).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
/**
* Merge to objects recursively
*
@ -460,8 +520,6 @@ export function typeOf(object: any): string {
* @returns {object}
*/
export function deepMerge<T extends object>(target, ...sources): T {
const isObject = (item): item is object => item && typeOf(item) === 'object';
if (!sources.length) {
return target;
}