-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_util.js
More file actions
163 lines (111 loc) · 3.98 KB
/
_util.js
File metadata and controls
163 lines (111 loc) · 3.98 KB
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
157
158
159
160
161
162
163
export const hasOwnProp = Object.hasOwnProperty;
export const truthy = any => !!any;
export const falsy = any => !!!any;
export const not = func => {
return any => {
return !func(any);
};
};
export const existy = any => any != null;
export const isDefined = any => {
if (any === null || typeof any === 'undefined') return false;
return true;
};
export const isNotDef = not(isDefined);
export const isBoolean = any => {
if (isNotDef(any)) return false;
return any.constructor === Boolean;
};
export const isNumber = any => {
if (isNotDef(any)) return false;
return any.constructor === Number;
};
export const isString = any => {
if (isNotDef(any)) return false;
return any.constructor === String;
};
export const isObject = any => {
if (isNotDef(any)) return false;
return any.constructor === Object;
};
export const isFunction = any => {
if (isNotDef(any)) return false;
return any.constructor === Function;
};
export const isPromise = any => {
if (isNotDef(any)) return false;
return any.constructor === Promise;
};
export const isVideoElement = ele => {
if (isNotDef(ele)) return false;
return ele.constructor === HTMLVideoElement;
};
export const allOf = function(/*args*/) {
const args = Array.prototype.slice.call(arguments);
return args.every(val => val === true);
};
export const anyOf = (/*args*/) => {
const args = Array.prototype.slice.call(arguments);
return args.some(function(val) {
return val === true;
});
};
export const singleEle = $ele => $ele && $ele.length === 1;
export const notSingleEle = not(singleEle);
export const each = (dataCanLoop, func, context) => {
if (falsy(Array.isArray(dataCanLoop) || isString(dataCanLoop))) {
throw new TypeError('dataCanLoop parameter type of each() should be Array or String.');
}
const _context = existy(context) ? context : null;
for (let i = 0, max = dataCanLoop.length; i < max; i++) {
func.call(_context, dataCanLoop[i]);
}
};
export const curry2 = func => {
if (!isFunction(func)) throw new TypeError('func parameter type of curry2() should be Function.');
return secondArg => firstArg => func(firstArg, secondArg);
};
export const gt = curry2(function(lhs, rhs) {
if (!allOf(isNumber(lhs), isNumber(rhs))) throw new TypeError('gt requires Number parameters.');
return lhs > rhs;
});
export const gte = curry2(function(lhs, rhs) {
if (!allOf(isNumber(lhs), isNumber(rhs))) throw new TypeError('gte requires Number parameters.');
return lhs >= rhs;
});
export const lt = curry2(function(lhs, rhs) {
if (!allOf(isNumber(lhs), isNumber(rhs))) throw new TypeError('lt requires Number parameters.');
return lhs < rhs;
});
export const lte = curry2(function(lhs, rhs) {
if (!allOf(isNumber(lhs), isNumber(rhs))) throw new TypeError('lte requires Number parameters.');
return lhs <= rhs;
});
export const eq = curry2(function(lhs, rhs) {
return lhs === rhs;
});
export const getSizeAspectFill = (srcWidth, srcHeight, fillWidth, fillHeight) => {
if (!allOf(isNumber(srcWidth), isNumber(srcHeight), isNumber(fillWidth), isNumber(fillHeight))) {
throw new TypeError('getSizeAspectFill() requires Number parameters.');
}
let modifiedSizeW = fillWidth,
modifiedSizeH = Math.ceil((fillWidth / srcWidth) * srcHeight);
if (modifiedSizeH < fillHeight) {
modifiedSizeW = Math.ceil((fillHeight / srcHeight) * srcWidth);
modifiedSizeH = fillHeight;
}
return { width: modifiedSizeW, height: modifiedSizeH };
};
export const getUriCombinedParams = (uri = '', params = {}) => {
if (!isString(uri)) throw new TypeError('uri parameter type of getUriCombinedParams() should be string.');
if (!uri) return '';
if (!params) return uri;
let str = '';
for (let key in params) str += `&${key}=${String(params[key])}`;
if (str === '') return uri;
const uris = uri.split('#');
uri = uris[0];
const hashStr = isDefined(uris[1]) ? '#' + uris[1] : '';
uri = (uri.indexOf('?') >= 0 ? uri + str : uri + '?' + str.substr(1)) + hashStr;
return uri;
};