This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
utility.js
104 lines (89 loc) · 2.45 KB
/
utility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
(function (window) {
var microp = {};
/**
* 'throttles' the execution of a funtion.
* will only call the function passed to throttle
* once every @threshold milliseconds
*
* @example
* window.addEventListener('resize', throttle(someFunction, 150, window));
*/
microp.throttle = 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);
}
};
};
microp.format = function(format) {
return (typeof format === 'function') ? format : d3.format(format);
};
microp.format.transform = function(format, transform) {
format = microp.format(format);
return function(d) {
return transform(format(d) || '');
};
};
microp.format.stardardizeUrl = function(str) {
var lastChar = str.length - 1;
if (str.charAt(lastChar) === '/') {
str = str.substring(0, lastChar);
}
str = str.toLowerCase();
return str;
};
microp.format.removeGitPrefix = function(str) {
return str.includes('https')
? str.split("https://github.com/").join("")
: str;
};
microp.format.transformDollars = function(str) {
str = String(str);
if (str.charAt(0) === '-') {
return '-$' + str.substr(1, str.length);
} else {
return '$' + str;
}
};
microp.format.commaSeparatedDollars = microp.format.transform(
',.0f',
microp.format.transformDollars
);
// Input: string of a date or Date Object
// Outputs a date object formatted like so: %Y-%m-%d
// Or like so %m/%d if a separator is included
microp.format.date = function (date, seperator) {
var dateObj,
date;
if (typeof(date) === 'string') {
dateObj = new Date(date);
} else if (typeof(date) === 'object') {
dateObj = date;
}
var day = dateObj.getDate(),
month = dateObj.getMonth() + 1,
year = dateObj.getFullYear();
if (seperator === '/') {
date = [month,day].join('/');
} else {
date = [year,month,day].join('-');
}
return date;
};
window.microp = microp;
})(this);