-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
186 lines (161 loc) · 4.7 KB
/
utils.js
File metadata and controls
186 lines (161 loc) · 4.7 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"use strict";
/**
Created by Complynx on 22.03.2019,
http://complynx.net
<complynx@yandex.ru> Daniel Drizhuk
*/
import {arrayLike} from "./type_checks.js";
/**
* hasOwnProperty caller
*/
let own = ((own)=>(x, i)=>own.call(x, i))(Object.prototype.hasOwnProperty);
/**
* Capitalizes the first letter of a provided string
* @param {string} string
* @returns {string} String
*/
export function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Fetching json->json
* Performed tests for 200 resp, and ensured options.body is json, and request method is POST if necessary.
* And automatic resp JSON interpretation.
* @param {string} url
* @param {*=} options
* @returns {Promise<Response | never>}
*/
export function fetch_json(url, options={}) {
if(options && options.body && typeof options.body !== "string")
options.body = JSON.stringify(options.body);
return fetch_test(url, options).then(r=>r.json());
}
/**
* Fetching json->json
* Performed tests for 200 resp, and request method is POST if necessary.
* @param {string} url
* @param {*=} options
* @returns {Promise<Response | never>}
*/
export function fetch_test(url, options={}) {
if(options){
if(!options.method && options.body)
options.method = 'POST';
}
return fetch(url, options).then(r=>{
if(!r.ok) throw new Error('HTTP error, status = ' + response.status);
return r;
});
}
/**
* Splits a string once with a delimiter
* @param {string} str what to split
* @param {string} splitter what to search
* @returns {[string, string]} start and rest of the str
*/
export function splitOnce(str, splitter) {
let ind = str.indexOf(splitter);
if(ind < 0) return [str];
let start = str.substring(0, ind);
let rest = str.substring(ind + splitter.length);
return [start, rest];
}
/**
* Parses form-data format.
* @param {string} query
* @param {boolean=} tuples return array of k-v pairs rather then an object
* @returns {*}
*/
export function parseQuery(query, tuples) {
if(query[0] === '?') query = query.substring(1);
if(arguments.length < 2) tuples = false;
let args = query.split('&');
let ret = tuples ? [] : {};
function add(key, value) {
if(tuples){
ret.push([key, value]);
}else{
ret[key] = value;
}
}
for (let i=0; i < args.length; i++) {
let arg = args[i];
if (-1 === arg.indexOf('=')) {
add(decodeURIComponent(arg).trim(), true);
}
else {
let kvp = splitOnce(arg, '=');
add(decodeURIComponent(kvp[0]).trim(), decodeURIComponent(kvp[1]).trim());
}
}
return ret;
}
let escapeRegExpRe = /[-\/\\^$*+?.()|[\]{}]/g;
/**
* Escapes regexp special characters.
* @param {string} s
* @returns {string}
*/
export function escapeRegExp(s) {
return s.replace(escapeRegExpRe, '\\$&');
}
/**
* C++ special characters escaper
* @param {string} str
* @returns {string}
*/
export function escapeSpecialChars(str) {
return str.replace(/[\\]/g, '\\\\')
.replace(/["]/g, '\\"')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t');
}
let htmlEscapeTextConverter = document.createElement('div');
/**
* Escapes HTML entities using browser engine
* @param {string} text
* @returns {string}
*/
export function escapeHtml(text) {
htmlEscapeTextConverter.innerText = text;
return htmlEscapeTextConverter.innerHTML;
}
/**
* splits path string (unix) and returns basename
* @param {string} path
* @returns {string}
*/
export function basename(path) {
return path.replace( /\\/g, '/' ).replace( /.*\//, '' );
}
/**
* splits path string (unix) and returns dirname
* @param {string} path
* @returns {string}
*/
export function dirname(path) {
return path.replace( /\\/g, '/' ).replace( /\/[^\/]*$/, '' );
}
/**
* Returns array from item, wraps it if necessary.
* @param {*} item
* @returns {Array}
*/
export function toArray(item){
if (item == null) return [];
return (item instanceof Array) ? item : arrayLike(item) ? Array.prototype.slice.call(item) : [item];
}
/**
* Returns string that is cleaned from it's linked ancestors (see V8 string manipulation optimization)
* To save speed, with a link to ' ' + str, but not more.
* @param {String} str
* @returns {String} str without links
*/
export function cleanString(str){
if(str.length<13) return str;
return (' ' + str).slice(1);
}
export {own};