forked from nathanreyes/v-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
executable file
·156 lines (135 loc) · 4.11 KB
/
helpers.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
153
154
155
156
import { isArray, isObject, isFunction, isDate } from './_';
export const pad = (val, len, char = '0') => {
val = val !== null && val !== undefined ? String(val) : '';
len = len || 2;
while (val.length < len) {
val = `${char}${val}`;
}
return val;
};
export const evalFn = (fn, args) => (isFunction(fn) ? fn(args) : fn);
export const mergeEvents = (...args) => {
const result = {};
args.forEach(e =>
Object.entries(e).forEach(([key, value]) => {
if (!result[key]) {
result[key] = value;
} else if (isArray(result[key])) {
result[key].push(value);
} else {
result[key] = [result[key], value];
}
}),
);
return result;
};
export const pageIsValid = page => !!(page && page.month && page.year);
export const pageIsBeforePage = (page, comparePage) => {
if (!pageIsValid(page) || !pageIsValid(comparePage)) return false;
if (page.year === comparePage.year) return page.month < comparePage.month;
return page.year < comparePage.year;
};
export const pageIsAfterPage = (page, comparePage) => {
if (!pageIsValid(page) || !pageIsValid(comparePage)) return false;
if (page.year === comparePage.year) return page.month > comparePage.month;
return page.year > comparePage.year;
};
export const pageIsBetweenPages = (page, fromPage, toPage) =>
(page || false) &&
!pageIsBeforePage(page, fromPage) &&
!pageIsAfterPage(page, toPage);
export const pageIsEqualToPage = (aPage, bPage) => {
if (!aPage && bPage) return false;
if (aPage && !bPage) return false;
if (!aPage && !bPage) return true;
return aPage.month === bPage.month && aPage.year === bPage.year;
};
export const addPages = ({ month, year }, count) => {
const incr = count > 0 ? 1 : -1;
for (let i = 0; i < Math.abs(count); i++) {
month += incr;
if (month > 12) {
month = 1;
year++;
} else if (month < 1) {
month = 12;
year--;
}
}
return {
month,
year,
};
};
export const pageRangeToArray = (from, to) => {
if (!pageIsValid(from) || !pageIsValid(to)) return [];
const result = [];
while (!pageIsAfterPage(from, to)) {
result.push(from);
from = addPages(from, 1);
}
return result;
};
export function datesAreEqual(a, b) {
const aIsDate = isDate(a);
const bIsDate = isDate(b);
if (!aIsDate && !bIsDate) return true;
if (aIsDate !== bIsDate) return false;
return a.getTime() === b.getTime();
}
export const arrayHasItems = array => isArray(array) && array.length;
export const mixinOptionalProps = (source, target, props) => {
const assigned = [];
props.forEach(p => {
const name = p.name || p.toString();
const mixin = p.mixin;
const validate = p.validate;
if (Object.prototype.hasOwnProperty.call(source, name)) {
const value = validate ? validate(source[name]) : source[name];
target[name] = mixin && isObject(value) ? { ...mixin, ...value } : value;
assigned.push(name);
}
});
return {
target,
assigned: assigned.length ? assigned : null,
};
};
export const on = (element, event, handler, opts) => {
if (element && event && handler) {
element.addEventListener(event, handler, opts);
}
};
export const off = (element, event, handler, opts) => {
if (element && event) {
element.removeEventListener(event, handler, opts);
}
};
export const elementContains = (element, child) =>
!!element && !!child && (element === child || element.contains(child));
export const onSpaceOrEnter = (event, handler) => {
if (event.key === ' ' || event.key === 'Enter') {
handler(event);
event.preventDefault();
}
};
/* eslint-disable no-bitwise */
export const createGuid = () => {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return `${S4() + S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`;
};
export function hash(str) {
let hashcode = 0;
let i = 0;
let chr;
if (str.length === 0) return hashcode;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hashcode = (hashcode << 5) - hashcode + chr;
hashcode |= 0; // Convert to 32bit integer
}
return hashcode;
}
/* eslint-enable no-bitwise */