1.0.0 • Published 4 years ago

@arturdoruch/tool v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Tool

A collection of JavaScript tools.

Install

yarn add @arturdoruch/tool

Contents

  • ajax
  • event-dispatcher
  • local-storage

ajax

jQuery AJAX request wrapper. Added functionalities:

  • Displaying notice message and loader while request is sending.
  • Request options:
    • json - Adding option for specifying JSON data to send. If is specify request Content-Type header is automatically set to application/json; charset=UTF-8. If specified JSON is an object, then will be encoded into string.
    • method - If is not specified and the data or json option is set, then option method will be automatically set to POST.

Usage

import ajax from '@arturdoruch/tool/lib/ajax';
  
// Optionally set ProcessNoticer to display notice message and loader while sending request.
import ProcessNoticer from '@arturdoruch/process-noticer';
import '@arturdoruch/process-noticer/styles/process-notice.css';

const processNoticer = new ProcessNoticer();
ajax.setProcessNoticer(processNoticer);

// Send POST request with JSON data and header "Content-Type: application/json; charset=UTF-8".
ajax.send({
    url: "https://httpbin.org/post",
    json: {
        "attr": "value"
    }   
}, 'Request notice', true)
    .done(function (responseContent) {
        console.log(responseContent);
    });

event-listener

Dispatches custom events to registered listeners.

Usage

import eventDispatcher from '@arturdoruch/event-dispatcher/lib/event-dispatcher';

eventDispatcher.addListener('upload.complete', uploadCompleteListener);
eventDispatcher.addListener('upload.complete', uploadCompleteListener2);

eventDispatcher.dispatch('upload.complete', ['firstArgument', 'secondArgument']);

eventDispatcher.removeListener('upload.complete', uploadCompleteListener);

eventDispatcher.dispatch('upload.complete', [1, 2]);

function uploadCompleteListener(argument1, argument2) {
    console.log('First listener: ', argument1, argument2);
}

function uploadCompleteListener2(argument1, argument2) {
    console.log('Second listener: ', argument1, argument2);
}

local-storage

A wrapper for localStorage. Adds encoding object and array into string of setting localStorage item.

Usage

import _localStorage from '@arturdoruch/tool/lib/local-storage';

_localStorage.setItem('object', {"foo": "bar"});
_localStorage.setItem('array', ['foo', 'bar']);

console.log(_localStorage.getItem('object'));
console.log(_localStorage.getItem('array'));