1
|
import $, { isArray } from 'jquery';
|
2
|
import { getCssVar, hasPromise, isJQuery, query } from './index';
|
3
|
|
4
|
export { $ };
|
5
|
export { ajax, each, extend, map, merge, isArray, isNumeric, isFunction, isPlainObject } from 'jquery';
|
6
|
|
7
|
export function bind(fn, context) {
|
8
|
return function (a) {
|
9
|
var l = arguments.length;
|
10
|
return l ? l > 1 ? fn.apply(context, arguments) : fn.call(context, a) : fn.call(context);
|
11
|
};
|
12
|
}
|
13
|
|
14
|
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
15
|
export function hasOwn(obj, key) {
|
16
|
return hasOwnProperty.call(obj, key);
|
17
|
}
|
18
|
|
19
|
export function promise(executor) {
|
20
|
|
21
|
if (hasPromise) {
|
22
|
return new Promise(executor);
|
23
|
}
|
24
|
|
25
|
var def = $.Deferred();
|
26
|
|
27
|
executor(def.resolve, def.reject);
|
28
|
|
29
|
return def;
|
30
|
}
|
31
|
|
32
|
promise.resolve = function (value) {
|
33
|
return promise(function (resolve) {
|
34
|
resolve(value);
|
35
|
});
|
36
|
};
|
37
|
|
38
|
promise.reject = function (value) {
|
39
|
return promise(function (_, reject) {
|
40
|
reject(value);
|
41
|
});
|
42
|
};
|
43
|
|
44
|
promise.all = function (iterable) {
|
45
|
return hasPromise
|
46
|
? Promise.all(iterable)
|
47
|
: $.when.apply($, iterable);
|
48
|
};
|
49
|
|
50
|
export function classify(str) {
|
51
|
return str.replace(/(?:^|[-_\/])(\w)/g, (_, c) => c ? c.toUpperCase() : '');
|
52
|
}
|
53
|
|
54
|
export function hyphenate(str) {
|
55
|
return str
|
56
|
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
|
57
|
.toLowerCase()
|
58
|
}
|
59
|
|
60
|
const camelizeRE = /-(\w)/g;
|
61
|
export function camelize(str) {
|
62
|
return str.replace(camelizeRE, toUpper)
|
63
|
}
|
64
|
|
65
|
function toUpper(_, c) {
|
66
|
return c ? c.toUpperCase() : ''
|
67
|
}
|
68
|
|
69
|
export function isString(value) {
|
70
|
return typeof value === 'string';
|
71
|
}
|
72
|
|
73
|
export function isNumber(value) {
|
74
|
return typeof value === 'number';
|
75
|
}
|
76
|
|
77
|
export function isUndefined(value) {
|
78
|
return value === undefined;
|
79
|
}
|
80
|
|
81
|
export function isContextSelector(selector) {
|
82
|
return isString(selector) && selector.match(/^[!>+-]/);
|
83
|
}
|
84
|
|
85
|
export function getContextSelectors(selector) {
|
86
|
return isContextSelector(selector) && selector.split(/(?=\s[!>+-])/g).map(value => value.trim());
|
87
|
}
|
88
|
|
89
|
const contextSelectors = {'!': 'closest', '+': 'nextAll', '-': 'prevAll'};
|
90
|
export function toJQuery(element, context) {
|
91
|
|
92
|
if (element === true) {
|
93
|
return null;
|
94
|
}
|
95
|
|
96
|
try {
|
97
|
|
98
|
if (context && isContextSelector(element) && element[0] !== '>') {
|
99
|
|
100
|
var fn = contextSelectors[element[0]], selector = element.substr(1);
|
101
|
|
102
|
context = $(context);
|
103
|
|
104
|
if (fn === 'closest') {
|
105
|
context = context.parent();
|
106
|
selector = selector || '*';
|
107
|
}
|
108
|
|
109
|
element = context[fn](selector);
|
110
|
|
111
|
} else {
|
112
|
element = $(element, context);
|
113
|
}
|
114
|
|
115
|
} catch (e) {
|
116
|
return null;
|
117
|
}
|
118
|
|
119
|
return element.length ? element : null;
|
120
|
}
|
121
|
|
122
|
export function toNode(element) {
|
123
|
return element && (isJQuery(element) ? element[0] : element);
|
124
|
}
|
125
|
|
126
|
export function toBoolean(value) {
|
127
|
return typeof value === 'boolean'
|
128
|
? value
|
129
|
: value === 'true' || value == '1' || value === ''
|
130
|
? true
|
131
|
: value === 'false' || value == '0'
|
132
|
? false
|
133
|
: value;
|
134
|
}
|
135
|
|
136
|
export function toNumber(value) {
|
137
|
var number = Number(value);
|
138
|
return !isNaN(number) ? number : false;
|
139
|
}
|
140
|
|
141
|
export function toList(value) {
|
142
|
return isArray(value)
|
143
|
? value
|
144
|
: isString(value)
|
145
|
? value.split(',').map(value => toBoolean(value.trim()))
|
146
|
: [value];
|
147
|
}
|
148
|
|
149
|
var vars = {};
|
150
|
export function toMedia(value) {
|
151
|
|
152
|
if (isString(value)) {
|
153
|
if (value[0] === '@') {
|
154
|
var name = `media-${value.substr(1)}`;
|
155
|
value = vars[name] || (vars[name] = parseFloat(getCssVar(name)));
|
156
|
} else if (value.match(/^\(min-width:/)) {
|
157
|
return value;
|
158
|
}
|
159
|
}
|
160
|
|
161
|
return value && !isNaN(value) ? `(min-width: ${value}px)` : false;
|
162
|
}
|
163
|
|
164
|
export function coerce(type, value, context) {
|
165
|
|
166
|
if (type === Boolean) {
|
167
|
return toBoolean(value);
|
168
|
} else if (type === Number) {
|
169
|
return toNumber(value);
|
170
|
} else if (type === 'jQuery') {
|
171
|
return query(value, context);
|
172
|
} else if (type === 'list') {
|
173
|
return toList(value);
|
174
|
} else if (type === 'media') {
|
175
|
return toMedia(value);
|
176
|
}
|
177
|
|
178
|
return type ? type(value) : value;
|
179
|
}
|
180
|
|
181
|
export function toMs(time) {
|
182
|
return !time
|
183
|
? 0
|
184
|
: time.substr(-2) === 'ms'
|
185
|
? parseFloat(time)
|
186
|
: parseFloat(time) * 1000;
|
187
|
}
|
188
|
|
189
|
export function swap(value, a, b) {
|
190
|
return value.replace(new RegExp(`${a}|${b}`, 'mg'), function (match) {
|
191
|
return match === a ? b : a
|
192
|
});
|
193
|
}
|