JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q39231F
  • License MIT

A mini script of utilities for Node and the Browser.

Package Exports

  • mini-utils

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 (mini-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mini-utils npm Bower

A mini script of utilities for Node and the Browser.
This DOES extend the prototype of some things.
2.4kb minified and gzipped.

Install

Node:

npm install mini-utils

Bower:

bower install mini-util

Usage

Node:

var __ = require('mini-utils'); // Double underscore

Bower:

Add this to your HTML <head>:

<script src="/path/to/your/bower_components/mini-util/mini-utils.min.js"></script>

If you want to use the script in a WebWorker, in the WebWorker you must:

importScripts('/path/to/your/bower_components/mini-util/mini-utils.min.js');

Use the __ variable to access mini-utils. Example:

__.isNode(); // Again, double underscore

List of utilities:

Cross-environment Utils:

  • isNode() - Boolean - Is the script running in Node?
  • isBrowser() - Boolean - Is the script running in a browser?
  • isWebWorker() - Boolean - Is the script running in a WebWorker?
  • inherits(Class, Class) - undefined - Minimal implementation of Node's inherits. The first parameter is the class that inherits, and the second parameter is the superclass.
  • guid() - String - Generate a GUID (well, a pseudo-GUID).
  • EventEmitter() - Class - Minimal implementation of Node's EventEmitter. Full Node API is supported (including deprecated functions) (Node API link: https://nodejs.org/api/events.html).
  • nextTick(Function) - Unknown - A little nextTick shim (return depends on what method is used to achieve the shim).
  • clone(Object) - Object - Creates a clone of an object and returns it.

WebWorker-Only Utils:

  • PROTO-EXTENSION - self Extensions:
    • emit(String/data[, ...data]) - Boolean - If the first parameter is not a string, it acts like postMessage. Otherwise, it emits the event specified in the first parameter, and optionally passes the rest of the arguments as extra data. Returns true if the event has listeners, false otherwise.
    • on(String, Function) - this - Can act like onmessage when given 'message' as the event. When the event specified in the first parameter is fired, the callback in the second parameter is called.
    • once(String, Function) - this - Can act like onmessage when given 'message' as the event (except only fires once). When the event specified in the first parameter is fired, the callback in the second parameter is called. After the callback is done, it is removed from the listeners for that event.
    • removeListener(String, Function) - this - If found, and it matches the second parameter, the callback function for an the event specified in the first parameter is removed.
    • removeAllListeners(String) - this - Removes all listeners for the event specified in the first parameter.

Browser-Only Utils:

  • PROTO-EXTENSION - Worker Extensions:
    • emit(String/data[, ...data]) - Boolean - If the first parameter is not a string, it acts like postMessage. Otherwise, it emits the event specified in the first parameter, and optionally passes the rest of the arguments as extra data. Returns true if the event has listeners, false otherwise.
    • on(String, Function) - this - Can act like onmessage when given 'message' as the event. When the event specified in the first parameter is fired, the callback in the second parameter is called.
    • once(String, Function) - this - Can act like onmessage when given 'message' as the event (except only fires once). When the event specified in the first parameter is fired, the callback in the second parameter is called. After the callback is done, it is removed from the listeners for that event.
    • removeListener(String, Function) - this - If found, and it matches the second parameter, the callback function for an the event specified in the first parameter is removed.
    • removeAllListeners(String) - this - Removes all listeners for the event specified in the first parameter.
  • PROTO-EXTENSION - Document/HTMLDocument Extensions:
    • on(String, Function) - this - When the event specified in the first parameter is fired, the callback in the second parameter is called.
    • once(String, Function) - this - When the event specified in the first parameter is fired, the callback in the second parameter is called. After the callback is done, it is removed from the listeners for that event.
    • ready(Function) - this - When the document is ready, the callback function is called.
    • get(String) - HTMLElement - Get an element based on a query string (only accepts '#id', '.class', or 'tagName').
    • create(String[, Object]) - HTMLElement - Create an element from a tag, and optionally initialize it with properties from the second argument.
  • PROTO-EXTENSION - HTMLElement Extensions:
    • on(String, Function) - this - When the event specified in the first parameter is fired, the callback in the second parameter is called.
    • once(String, Function) - this - When the event specified in the first parameter is fired, the callback in the second parameter is called. After the callback is done, it is removed from the listeners for that event.
    • remove() - this - Remove the current element.
    • text([String]) - String/this - If given no parameters, returns the element's text. If given the first parameter, replaces the element's text with the new text.
    • hasMultipleOfClass(String) - Boolean - Check if the element has multiple occurrences of a class.
    • hasClass(String) - Boolean - Check if the element has a certain class.
    • addClass(String) - this - Add the specified class to the element.
    • removeClass(String) - this - Remove the specified class to the element.
    • hide() - this - Hide the element.
    • show() - this - Show the element.
    • toggle() - this - Toggle the display state of the element.
    • css(String/Object/Array[, String/Array]) - String/undefined, throws! - Easier with a table:
First parameter Second parameter Return Value
String - Property name this String - Property value
String - Property name String - Property Value this
Object - Object of properties this this
Array - Array of property names Array - Array of property values this

Node-Only Utils:

None yet.