/
utils.ts
265 lines (244 loc) · 6.05 KB
/
utils.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Svelte Router utilities module.
* @module svelte-router/utils
*/
import tc from '@spaceavocado/type-check';
/**
* String has prefix predicate.
* @param {string} s tested string.
* @param {string} prefix needle.
* @return {boolean}
*/
export function hasPrefix(s: string, prefix: string): boolean {
if (prefix.length == 0 || s.length == 0) {
return false;
}
if (prefix.length <= s.length) {
if (s.slice(0, prefix.length) == prefix) {
return true;
}
}
return false;
}
/**
* String has suffix predicate.
* @param {string} s tested string.
* @param {string} suffix needle.
* @return {boolean}
*/
export function hasSuffix(s: string, suffix: string): boolean {
if (suffix.length == 0 || s.length == 0) {
return false;
}
if (suffix.length <= s.length) {
if (s.slice(-1 * suffix.length) == suffix) {
return true;
}
}
return false;
}
/**
* Trim prefix
* @param {string} s tested string.
* @param {string} prefix needle.
* @return {string}
*/
export function trimPrefix(s: string, prefix: string): string {
if (prefix.length > 0 && hasPrefix(s, prefix)) {
return s.substr(prefix.length);
}
return s;
}
/**
* Join URL paths.
* @param {string} a URL path A.
* @param {string} b URL path B.
* @return {string}
*/
export function joinPath(a: string, b: string): string {
const aSlash = hasSuffix(a, '/');
const bSlash = hasPrefix(b, '/');
if (aSlash && bSlash) {
return a + b.slice(1);
}
if (!aSlash && !bSlash) {
return a + '/' + b;
}
return a + b;
}
/**
* URL match predicate
* @param {string} a URL a.
* @param {string} b URL b.
* @throws an error if the URL is not valid.
* @return {boolean}
*/
export function urlMatch(a: string, b: string): boolean {
let sections = a.split('?');
if (sections.length > 2) {
throw new Error('invalid URL');
}
a = sections[0];
sections = b.split('?');
if (sections.length > 2) {
throw new Error('invalid URL');
}
b = sections[0];
if (hasPrefix(a, '/') == false) {
a = `/${a}`;
}
if (hasSuffix(a, '/') == false) {
a = `${a}/`;
}
if (hasPrefix(b, '/') == false) {
b = `/${b}`;
}
if (hasSuffix(b, '/') == false) {
b = `${b}/`;
}
return a == b;
}
/**
* URL prefix predicate
* @param {string} haystack URL haystack.
* @param {string} prefix URL prefix.
* @throws an error if the URL is not valid.
* @return {boolean}
*/
export function urlPrefix(haystack: string, prefix: string): boolean {
if (haystack.length == 0 || prefix.length == 0) {
return false;
}
if (hasPrefix(haystack, '/') == false) {
haystack = `/${haystack}`;
}
if (hasPrefix(prefix, '/') == false) {
prefix = `/${prefix}`;
}
return hasPrefix(haystack, prefix);
}
/**
* Parsed URL object.
*/
interface ParsedURL {
/** URL base path without query or hash */
base: string;
/** Query params */
query: {[k: string]: string};
/** Hash string */
hash: string;
}
/**
* Extract query param and hash from URL and return
* the base URL, dictionary of query params, and the hash.
* @param {string} path full URL.
* @throws an error if the URL is not valid.
* @return {ParsedURL}
*/
export function parseURL(path: string): ParsedURL {
let hash = '';
let sections = path.split('#');
if (sections.length > 2) {
throw new Error('invalid URL');
} else if (sections.length == 2) {
path = sections[0];
hash = sections[1];
}
sections = path.split('?');
if (sections.length > 2) {
throw new Error('invalid URL');
}
const result: ParsedURL = {
base: sections[0],
query: {},
hash: hash,
};
if (sections.length == 2) {
const entries = sections[1].split('&');
for (const entry of entries) {
const keyValue = entry.split('=');
result.query[keyValue[0]] = keyValue[1];
}
}
return result;
}
/**
* Get full URL from the base URL, query object, and hash.
* @param {string} path URL base path without query or hash.
* @param {object?} query query param dictionary.
* @param {string} hash hash param.
* @return {string}
*/
export function fullURL(
path: string,
query: {[k: string]: string} | null | undefined,
hash: string): string {
let queryPath = '';
if (tc.not.isNullOrUndefined(query)) {
for (const key in query) {
if (query.hasOwnProperty(key)) {
if (queryPath.length > 0) {
queryPath += '&';
}
queryPath += `${key}=${query[key]}`;
}
}
if (queryPath.length > 0) {
path = `${path}?${queryPath}`;
}
}
if (hash.length > 0) {
path = `${path}#${hash}`;
}
return path;
}
/**
* History location object
*/
export interface HistoryLocation {
/** A unique string representing this location. */
key: string;
/** The path of the URL. */
pathname: string;
/** The URL query string. */
search: string;
/** The URL hash fragment. */
hash: string;
/** Extra state for this location. */
state: string;
}
/**
* Get full URL from the history location object.
* @param {HistoryLocation} location history location objec.
* @return {string}
*/
export function historyFullURL(location: Partial<HistoryLocation>): string {
return `${location.pathname}${location.search}${location.hash}`;
}
/**
* Is whole number predicate
* @param {string} s Tested string.
* @return {boolean}
*/
export function isWholeNumber(s: string): boolean {
return s.match(/^0$|^[1-9]\d*$/) !== null;
}
/**
* Is float number predicate
* @param {string} s Tested string.
* @return {boolean}
*/
export function isFloatNumber(s: string): boolean {
return s.match(/^\d*\.\d*$/) !== null;
}
/**
* Simple object deep clone.
* @param {object} o source object.
* @return {object}
*/
export function deepClone(o: object): object {
if (tc.isNullOrUndefined(o) || tc.not.isObject(o)) {
return {};
}
return JSON.parse(JSON.stringify(o));
}