docomo-utils

1.3.0 • Public • Published

docomo-utils.js

A set of utilities function for docomo products. Exports as Javascript Universal Module Definition (UMD)

Import in html

// Specific range version
<script src="https://unpkg.com/docomo-utils@^1.2.0/dist/docomo-utils.min.js"></script>
//Last version
<script src="https://unpkg.com/docomo-utils/dist/docomo-utils.min.js"></script>
<script type="text/javascript">
    var settings = window.docomoUtils.merge({a:1}, {b:2}); // => { a:1, b:2 }
</script>

Import in CommonJS style

npm install docomot-utils
import docomoUtils from 'docomo-utils';
//OR
import { queryfy, dequeryfy, merge, JSONPRequest } from 'docomo-utils';

API

Iterator

Iterator

Parameters

  • array Array the array you want to transform in iterator

Examples

var myArray = ["pippo", "pluto", "paperino"];
var it = Iterator(myArray);
it.next().value === "pippo"; //true
it.next().value === "pluto"; //true
it.next(true).value === "paperino" //false because with true you can reset it!

Returns Object an iterator-like object

debounce

Debounce. Wait ms to execute a function

Parameters

  • fn Function the function to be wrapped
  • ms Number the number of ms to wait (optional, default 300)
  • immediate Boolean execute immediate and wait ms. If false only the last call
  • scope Object the this object. default is this-generated function (optional, default this)

Returns Function returns the function decorated

throttle

Throttle. Useful for resize event or scroll

Parameters

  • fn Function the function to be wrapped
  • limit Number only x call for ms (optional, default 300)
  • scope Object the this object. default to this-generated function (optional, default this)

Returns Function returns the function decorated

JSONPRequest

Make a jsonp request, remember only GET The function create a tag script and append a callback param in querystring. The promise will be reject after 3s if the url fail to respond

Parameters

  • url String the url with querystring but without &callback at the end or &function
  • timeout Number ms range for the response (optional, default 3000)

Examples

const request = new JSONPRequest("http://www.someapi.com/asd?somequery=1");
request.promise.then((data) => {});
request.prom.then((data) => {});

Returns Promise<(Object | String)>

getType

getType

Parameters

  • obj

Returns string the type of the object. date for date array etc

memoize

Simple memoization function. A memoizated function cache the results of a function computation when it receives the same params

Parameters

  • fn Function the function to memoize
  • deps Function should returns an array with dependencies
  • scope Object the this object. default to this-generated function (optional, default this)

Examples

function Person(){
this.name = 'aldo';
this.surname = 'baglio';
};
 
Person.prototype.getFullName = function(title) {
var _title = title ? title : '';
return _title + this.name + ' ' + this.surname;
};
 
var person = new Person();
//remember to bind it
var memoized = memoize(person.getFullName, function(){ return [this.name, this.surname];}.bind(person), person);

Returns any what the real function returns

merge

Merge n objects

Parameters

  • N ...Object object to merge together

Returns Object

extend

extend: this function merge two objects in a new one with the properties of both

Parameters

Returns Object a brand new object results of the merging

queryfy

A function to compose query string

Parameters

  • _api Strinq the endpoint
  • query Object a key value object: will be append to ?key=value&key2=value2

Examples

var API = "http://jsonplaceholder.typicode.com/comments"
var url = queryfy(API, {postId:1});
// url will be "http://jsonplaceholder.typicode.com/comments?postId=1"

Returns String the string composed

dequeryfy

A function to dequerify query string

Parameters

  • _url Strinq

Examples

var url = "http://jsonplaceholder.typicode.com/comments?postId=1"
var obj = dequeryfy(url); //obj is {"postId":"1"}

Returns Object the object with key-value pairs, empty if no querystring is present

addEvent

Cross browsing addEvent

Parameters

Returns HTMLElement

isLocalStorageSupported

Check if local storage is supported

Returns Boolean

checkObject

Check if the object has the nested keys list

Parameters

  • object Object the object to be checked
  • keys (Array | String) a string point separated or a list of string keys
  • defaultReturn any (optional, default null)

Examples

checkObject({a:{b:[1,2,3]}}, ["a", "b", 2])); // returns 3
checkObject({a:{b:[1,2,3]}}),"a.b.0"); // returns the b[0]

Returns (any | null) returns any value for that key or null if the key is undefined

arrayContains

Returns true if the first array is contained in the second one.

Parameters

Examples

arrayContains([1,2,3] [4,5,6,1,2,3]) // true
Works even with array of objects and string

Returns Boolean

setFingerPrint

setFingerPrint

Parameters

  • Config object the vhost configuration
  • pony string the token of the logged user
  • returnUrl string the url where to return once relogged

Returns Promise

contents_inapp

God forgive them because they don't know what they do

ponyToken

generatePony

Parameters

  • Config object the vhost configuration
  • options object - (optional, default {return_url:''})

Returns Promise<String> the pony string

readCookies

Read the cookies in document.cookie string

Parameters

  • cookies string the document.cookie string in this format key=val;

Returns object the cookies as object key value pairs

Package Sidebar

Install

npm i docomo-utils

Weekly Downloads

6

Version

1.3.0

License

MIT

Last publish

Collaborators

  • eatsjobs