Built-in util method api
All util functions used internally by the plugin come from @ew-color-picker/utils, which is a peer dependency of ew-message, so it is never bundled or installed twice. Install it once before use:
npm i @ew-color-picker/utils
import * as util from '@ew-color-picker/utils';
// The following util refers to here
The following is a description of some util functions. Please refer to the @ew-color-picker/utils code for details.
isFunction
This util function indicates whether it is a function and passes in a parameter that needs to be judged. For example:
const testFn = function () {
console.log('This is a function');
};
util.isFunction(testFn); // true
util.isFunction(null); // false
isDom
This util function indicates whether it is a dom element and passes in a parameter that needs to be judged. It safely returns false when window is unavailable (e.g. SSR). For example:
const div = document.querySelector('#app');
util.isDom(div); // true;
util.isDom(1); // false;
isShallowObject
This utility function determines whether it is an object (arrays included) and passes in a parameter to be determined. For example:
util.isShallowObject({}); // true;
util.isShallowObject([]); // true;
util.isShallowObject(function () {
console.log(1);
}); // false;
util.isShallowObject(111); // false;
isString
This utility function determines whether it is a string and passes in a parameter to be determined. For example:
util.isString('111'); // true;
util.isString(111); // false
isValidNumber
This utility function indicates whether it is a valid number (excluding NaN), and passes in a parameter to be judged. For example:
util.isValidNumber(123); // true;
util.isValidNumber(NaN); // false;
util.isValidNumber('1111'); // false
hasOwn
This utility function indicates whether a certain attribute is on a certain object, and passes in 2 parameters, the first parameter is an object, and the second parameter is a property name. For example:
const obj = { name: 'eveningwater' };
util.hasOwn(obj, 'name'); // true;
util.hasOwn(obj, 'age'); // false;
toArray
This utility function is used to convert a pseudo array into a real array, passing in a value to be converted. For example:
// Assume that the page has multiple <div class="list-item"></div> elements
const listItems = document.querySelectorAll('.list-item');
util.toArray(listItems); // An array containing multiple div elements
$
This utility function is used to obtain dom elements, with two parameters, the first parameter is a string, and the second parameter is a dom element. For example:
const app = util.$('#app');
util.$('.child', app);
$$
This utility function is used to obtain the DOM element collection, i.e. NodeList, with the same parameters as the $ method. Such as:
const app = util.$('#app');
util.$$('.child', app);
util.$$('.child');
createByTemplate
This util function creates a DocumentFragment from a template string. The template can include child elements, such as:
const fragment = util.createByTemplate(`<div><span></span></div>`);
console.log(fragment); // DocumentFragment, mount it with appendChild
create
This util function creates a DOM element by tag name, such as:
const div = util.create('div');
util.addClass(div, 'box');
hasClass
This util function checks whether an element contains a class name. The parameters are the element and the class name:
const app = util.$('#app');
util.hasClass(app, 'app'); // true or false
removeElement
This util function removes a DOM node. It prefers removing through the parent element and falls back to remove():
const app = util.$('#app');
util.removeElement(app);
addClass(new in 0.0.9)
This util function adds class names to a DOM element. The first parameter is the element, and the second one accepts a string or a string array:
util.addClass(util.$('#app'), 'app');
util.addClass(util.$('#app'), ['app', 'active']);
// Add app and active to the element with id app
removeClass(0.1.1 New)
This util function removes class names from a DOM element. The parameters are the same as addClass:
util.removeClass(util.$('#app'), 'app');
util.removeClass(util.$('#app'), ['app', 'active']);
// Remove app and active from the element with id app
off(New in 0.0.9)
This method is used to remove an event listener. Four parameters are passed in. The first one is the DOM element, the second one is the event name, the third one is the event listener, and the fourth one is a Boolean value. For example:
const test = document.getElementById('test');
const handler = e => console.log(e.target.tagName);
util.off(test, 'click', handler);
on(New in 0.0.9)
This method is used to add an event listener. The parameters are the same as the off method. For example:
const test = document.getElementById('test');
const handler = e => console.log(e.target.tagName);
util.on(test, 'click', handler);
isArray(new in 0.1.2)
This method is used to determine whether the value is an array. The parameter is any value. For example:
const arr = [1, 2, 3];
util.isArray(arr); // true
isRemoveNode(new in 0.1.2)
This method is used to determine whether a node can be deleted. The parameter is any DOM node element. For example:
const test = document.getElementById('test');
util.isRemoveNode(test); // true
isNode
This util function checks whether a value is a DOM node, commonly used to decide whether content can be appended directly. It does not touch the Node global in SSR.
util.isNode(document.createElement('span')); // true
util.isNode('text'); // false