forked from pagekit/vue-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
183 lines (133 loc) · 3.69 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Utility functions.
*/
import Promise from './promise';
var {hasOwnProperty} = {}, {slice} = [], debug = false, ntick;
export const inBrowser = typeof window !== 'undefined';
export default function ({config, nextTick}) {
ntick = nextTick;
debug = config.debug || !config.silent;
}
export function warn(msg) {
if (typeof console !== 'undefined' && debug) {
console.warn('[VueResource warn]: ' + msg);
}
}
export function error(msg) {
if (typeof console !== 'undefined') {
console.error(msg);
}
}
export function nextTick(cb, ctx) {
return ntick(cb, ctx);
}
export function trim(str) {
return str ? str.replace(/^\s*|\s*$/g, '') : '';
}
export function trimEnd(str, chars) {
if (str && chars === undefined) {
return str.replace(/\s+$/, '');
}
if (!str || !chars) {
return str;
}
return str.replace(new RegExp(`[${chars}]+$`), '');
}
export function toLower(str) {
return str ? str.toLowerCase() : '';
}
export function toUpper(str) {
return str ? str.toUpperCase() : '';
}
export const isArray = Array.isArray;
export function isString(val) {
return typeof val === 'string';
}
export function isBoolean(val) {
return val === true || val === false;
}
export function isFunction(val) {
return typeof val === 'function';
}
export function isObject(obj) {
return obj !== null && typeof obj === 'object';
}
export function isPlainObject(obj) {
return isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype;
}
export function isBlob(obj) {
return typeof Blob !== 'undefined' && obj instanceof Blob;
}
export function isFormData(obj) {
return typeof FormData !== 'undefined' && obj instanceof FormData;
}
export function when(value, fulfilled, rejected) {
var promise = Promise.resolve(value);
if (arguments.length < 2) {
return promise;
}
return promise.then(fulfilled, rejected);
}
export function options(fn, obj, opts) {
opts = opts || {};
if (isFunction(opts)) {
opts = opts.call(obj);
}
return merge(fn.bind({$vm: obj, $options: opts}), fn, {$options: opts});
}
export function each(obj, iterator) {
var i, key;
if (isArray(obj)) {
for (i = 0; i < obj.length; i++) {
iterator.call(obj[i], obj[i], i);
}
} else if (isObject(obj)) {
for (key in obj) {
if (hasOwnProperty.call(obj, key)) {
iterator.call(obj[key], obj[key], key);
}
}
}
return obj;
}
export const assign = Object.assign || _assign;
export function merge(target) {
var args = slice.call(arguments, 1);
args.forEach(source => {
_merge(target, source, true);
});
return target;
}
export function defaults(target) {
var args = slice.call(arguments, 1);
args.forEach(source => {
for (var key in source) {
if (target[key] === undefined) {
target[key] = source[key];
}
}
});
return target;
}
function _assign(target) {
var args = slice.call(arguments, 1);
args.forEach(source => {
_merge(target, source);
});
return target;
}
function _merge(target, source, deep) {
for (var key in source) {
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
target[key] = {};
}
if (isArray(source[key]) && !isArray(target[key])) {
target[key] = [];
}
_merge(target[key], source[key], deep);
} else if (source[key] !== undefined) {
target[key] = source[key];
}
}
}