Package Exports
- @andrewcaires/utils.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@andrewcaires/utils.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
utils.js
JavaScript utility library for web and nodejs development
Installation
npm i @andrewcaires/utils.js
Api
EventEmitter
Class for custom events
import { EventEmitter } from '@andrewcaires/utils.js';
const emitter = new EventEmitter();
// OR
class CustomEvent extends EventEmitter {}
const custom = new CustomEvent();
EventEmitter.onAdds the listener function to the end of the listeners array for the event named.
const cb = (data) => {
console.log(data);
}
emitter.on('event', cb);EventEmitter.onceAdds the listener function to the end of the listeners array for the event named. The next time event is triggered, this listener is removed.
const cb = (data) => {
console.log(data);
}
emitter.once('event', cb);EventEmitter.offRemoves the specified listener from the listener array for the event named.
emitter.off('event', cb);EventEmitter.emitSynchronously calls each of the listeners registered for the event named.
emitter.emit('event', 'on emit event');
// > on emit eventtype
Determine the internal JavaScript [[Class]] of an object.
import { isArray, isBoolean, isFunction, isNumber, isObject, isString, isFloat, isInteger, isNull, isUndefined, isValid, type } from '@andrewcaires/utils.js';isArrayFinds whether a variable is an array.
isArray([]) // > trueisBooleanFinds out whether a variable is a boolean.
isBoolean(true) // > true
isBoolean(false) // > trueisFunctionFind whether the type of a variable is function.
isFunction(function() {}) // > trueisNumberFinds whether a variable is an number.
isNumber(1) // > true
isNumber(1.7) // > trueisObjectFinds whether a variable is an object.
isObject({}) // > trueisStringFind whether the type of a variable is string.
isString('') // > trueisFloatFinds whether the type of a variable is float.
isFloat(1.7) // > trueisIntegerFind whether the type of a variable is integer.
isInteger(1) // > trueisNullFinds whether a variable is null.
isNull(null) // > trueisUndefinedFinds whether a variable is undefined.
isUndefined(undefined) // > trueisValidFind out if a variable is valid.
isValid(any) // > truetypeGet the type of a variable.
type([]); // => 'array'
type(true); // => 'boolean'
type(false); // => 'boolean'
type(frunction() {}); // => 'function'
type(1); // => 'number'
type(1.7); // => 'number'
type({}); // => 'object'
type(''); // => 'string'
type('test'); // => 'string'
type(); // => 'null'
type(null); // => 'null'