2015-11-23 23:27:55 +03:00
|
|
|
'use strict';
|
2016-07-21 13:35:27 +03:00
|
|
|
|
2017-01-24 00:29:52 +03:00
|
|
|
export interface StringMap<T> {
|
|
|
|
[key: string]: T;
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:31:56 +03:00
|
|
|
export function stringify(obj:any) {
|
|
|
|
return JSON.stringify(obj);
|
|
|
|
}
|
|
|
|
|
2017-03-09 21:58:10 +03:00
|
|
|
export function isString(str:any):str is String {
|
2016-10-29 12:31:56 +03:00
|
|
|
return typeof str === 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isFunction(func: any) {
|
|
|
|
return typeof func === 'function';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isBlank(obj: any): boolean {
|
2016-10-30 18:56:24 +03:00
|
|
|
return obj == undefined;
|
2016-07-21 13:35:27 +03:00
|
|
|
}
|
2015-11-23 23:27:55 +03:00
|
|
|
|
2017-04-23 15:26:15 +03:00
|
|
|
export function stripTrailingSlash(path:string):string {
|
|
|
|
return path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:29:52 +03:00
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
|
|
export function groupBy<T>(array: T[], key:string):StringMap<T[]> {
|
|
|
|
return array.reduce<StringMap<T[]>>(function(res, value) {
|
|
|
|
if (hasOwnProperty.call(res, value[key])) {
|
|
|
|
res[value[key]].push(value);
|
|
|
|
} else {
|
|
|
|
res[value[key]] = [value];
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2017-02-26 01:37:57 +03:00
|
|
|
export function statusCodeType(statusCode, defaultAsError = false) {
|
|
|
|
if (statusCode === 'default') {
|
|
|
|
return defaultAsError ? 'error' : 'success';
|
|
|
|
}
|
|
|
|
|
2015-12-12 19:30:13 +03:00
|
|
|
if (statusCode < 100 || statusCode > 599) {
|
2015-12-18 11:34:17 +03:00
|
|
|
throw new Error('invalid HTTP code');
|
2015-12-12 19:30:13 +03:00
|
|
|
}
|
2015-11-23 23:27:55 +03:00
|
|
|
let res = 'success';
|
|
|
|
if (statusCode >= 300 && statusCode < 400) {
|
|
|
|
res = 'redirect';
|
2015-11-23 23:56:44 +03:00
|
|
|
} else if (statusCode >= 400) {
|
2015-11-23 23:27:55 +03:00
|
|
|
res = 'error';
|
2015-12-12 19:30:13 +03:00
|
|
|
} else if (statusCode < 200) {
|
2015-11-23 23:27:55 +03:00
|
|
|
res = 'info';
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2016-06-22 19:13:57 +03:00
|
|
|
|
|
|
|
export function defaults(target, src) {
|
|
|
|
var props = Object.keys(src);
|
|
|
|
|
|
|
|
var index = -1,
|
|
|
|
length = props.length;
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
var key = props[index];
|
|
|
|
if (target[key] === undefined) {
|
|
|
|
target[key] = src[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
2016-07-21 13:35:27 +03:00
|
|
|
|
|
|
|
export function safePush(obj, prop, val) {
|
|
|
|
if (!obj[prop]) obj[prop] = [];
|
|
|
|
obj[prop].push(val);
|
|
|
|
}
|
2016-08-22 12:18:56 +03:00
|
|
|
|
|
|
|
// credits https://remysharp.com/2010/07/21/throttling-function-calls
|
|
|
|
export function throttle(fn, threshhold, scope) {
|
|
|
|
threshhold = threshhold || 250;
|
|
|
|
var last,
|
|
|
|
deferTimer;
|
|
|
|
return function () {
|
|
|
|
var context = scope || this;
|
|
|
|
|
|
|
|
var now = +new Date,
|
|
|
|
args = arguments;
|
|
|
|
if (last && now < last + threshhold) {
|
|
|
|
// hold on to it
|
|
|
|
clearTimeout(deferTimer);
|
|
|
|
deferTimer = setTimeout(function () {
|
|
|
|
last = now;
|
|
|
|
fn.apply(context, args);
|
|
|
|
}, threshhold);
|
|
|
|
} else {
|
|
|
|
last = now;
|
|
|
|
fn.apply(context, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-11-23 02:23:32 +03:00
|
|
|
|
2017-05-12 11:21:47 +03:00
|
|
|
export function debounce(func, wait, immediate = false) {
|
|
|
|
var timeout;
|
|
|
|
return function() {
|
|
|
|
var context = this, args = arguments;
|
|
|
|
var later = function() {
|
|
|
|
timeout = null;
|
|
|
|
if (!immediate) func.apply(context, args);
|
|
|
|
};
|
|
|
|
var callNow = immediate && !timeout;
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(later, wait);
|
|
|
|
if (callNow) func.apply(context, args);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:23:32 +03:00
|
|
|
export const isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0
|
|
|
|
|| (function (p) { return p.toString() === '[object SafariRemoteNotification]'; })(!window['safari']
|
|
|
|
|| safari.pushNotification);
|
2017-01-28 14:03:51 +03:00
|
|
|
|
|
|
|
export function snapshot(obj) {
|
|
|
|
if(obj == undefined || typeof(obj) !== 'object') {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
var temp = new obj.constructor();
|
|
|
|
|
|
|
|
for(var key in obj) {
|
|
|
|
if (obj.hasOwnProperty(key)) {
|
|
|
|
temp[key] = snapshot(obj[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
2017-03-06 10:54:52 +03:00
|
|
|
|
|
|
|
export function isJsonLike(contentType: string): boolean {
|
|
|
|
return contentType.search(/json/i) !== -1;
|
|
|
|
}
|
|
|
|
|
2017-05-12 11:03:14 +03:00
|
|
|
export function isXmlLike(contentType: string): boolean {
|
|
|
|
return contentType.search(/xml/i) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getJsonLikeSample(samples: Object) {
|
|
|
|
const jsonLikeKeys = Object.keys(samples).filter(isJsonLike);
|
2017-03-06 10:54:52 +03:00
|
|
|
|
|
|
|
if (!jsonLikeKeys.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-12 11:03:14 +03:00
|
|
|
return samples[jsonLikeKeys[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getXmlLikeSample(samples: Object) {
|
|
|
|
const xmlLikeKeys = Object.keys(samples).filter(isXmlLike);
|
|
|
|
|
|
|
|
if (!xmlLikeKeys.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return samples[xmlLikeKeys[0]];
|
2017-03-06 10:54:52 +03:00
|
|
|
}
|