-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathutil.js
152 lines (128 loc) · 3.24 KB
/
util.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const dateUtil = require('fecha');
export function merge(target) {
for (var i = 1, j = arguments.length; i < j; i++) {
var source = arguments[i];
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
var value = source[prop];
if (value !== undefined) {
target[prop] = value;
}
}
}
}
return target;
}
export function formatDate(date, format) {
if (!(date instanceof Date)) return '';
return dateUtil.format(date, format || 'YYYY-MM-DD');
}
export function parseDate(string, format) {
return dateUtil.parse(string, format || 'YYYY-MM-DD');
}
export function debounce(fn, delay) {
var timer;
return function() {
var context = this;
var args = arguments;
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(function() {
fn.apply(context, args);
timer = null;
}, delay);
};
}
export function throttle(fn, delay) {
var now, lastExec, timer, context, args;
var execute = function() {
fn.apply(context, args);
lastExec = now;
};
return function() {
context = this;
args = arguments;
now = Date.now();
if (timer) {
clearTimeout(timer);
timer = null;
}
if (!lastExec) {
execute();
} else {
var diff = delay - (now - lastExec);
if (diff < 0) {
execute();
} else {
timer = setTimeout(function() {
execute();
}, diff);
}
}
};
}
export function getNestedPath(object, nestedProp) {
let propertyArr = nestedProp.split('.');
let property = propertyArr.pop();
return getPath(object, propertyArr.join('.')).fields[property];
}
export function getPath(object, prop) {
prop = prop || '';
var paths = prop.split('.');
var current = object;
var result = null;
for (var i = 0, j = paths.length; i < j; i++) {
var path = paths[i];
if (!current) break;
if (i === j - 1) {
result = current[path];
break;
}
current = current[path];
}
return result;
}
export function setPath(object, prop, value) {
if (prop === undefined || prop === null) return;
if (typeof prop === 'object') {
var target = prop;
for (prop in target) {
if (target.hasOwnProperty(prop)) {
setPath(object, prop, target[prop]);
}
}
} else {
prop = prop || '';
var paths = prop.split('.');
var current = object;
for (var i = 0, j = paths.length; i < j; i++) {
var path = paths[i];
if (!current) break;
if (i === j - 1) {
current[path] = value;
break;
}
current = current[path];
}
}
}
var scrollbarWidth;
export function getScrollbarWidth() {
if (scrollbarWidth !== undefined) return scrollbarWidth;
var outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.width = '100px';
outer.style.position = 'absolute';
outer.style.top = '-9999px';
document.body.appendChild(outer);
var widthNoScroll = outer.offsetWidth;
outer.style.overflow = 'scroll';
var inner = document.createElement('div');
inner.style.width = '100%';
outer.appendChild(inner);
var widthWithScroll = inner.offsetWidth;
outer.parentNode.removeChild(outer);
return widthNoScroll - widthWithScroll;
}