docomo-utils v1.3.0
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-utilsimport docomoUtils from 'docomo-utils';
//OR
import { queryfy, dequeryfy, merge, JSONPRequest } from 'docomo-utils';API
Iterator
Iterator
Parameters
arrayArray 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
fnFunction the function to be wrappedmsNumber the number of ms to wait (optional, default300)immediateBoolean execute immediate and wait ms. If false only the last callscopeObject the this object. default is this-generated function (optional, defaultthis)
Returns Function returns the function decorated
throttle
Throttle. Useful for resize event or scroll
Parameters
fnFunction the function to be wrappedlimitNumber only x call for ms (optional, default300)scopeObject the this object. default to this-generated function (optional, defaultthis)
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
urlString the url with querystring but without &callback at the end or &functiontimeoutNumber ms range for the response (optional, default3000)
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
fnFunction the function to memoizedepsFunction should returns an array with dependenciesscopeObject the this object. default to this-generated function (optional, defaultthis)
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
_apiStrinq the endpointqueryObject 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
_urlStrinq
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
eventString the event nameelementHTMLElement HTMLElementcallbackFunction -
Returns HTMLElement
isLocalStorageSupported
Check if local storage is supported
Returns Boolean
checkObject
Check if the object has the nested keys list
Parameters
objectObject the object to be checkedkeys(Array | String) a string point separated or a list of string keysdefaultReturnany (optional, defaultnull)
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 stringReturns Boolean
setFingerPrint
setFingerPrint
Parameters
Configobject the vhost configurationponystring the token of the logged userreturnUrlstring 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
Configobject the vhost configurationoptionsobject - (optional, default{return_url:''})options.return_urlobject -
Returns Promise<String> the pony string
readCookies
Read the cookies in document.cookie string
Parameters
cookiesstring the document.cookie string in this format key=val;
Returns object the cookies as object key value pairs