-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.js
More file actions
101 lines (96 loc) · 3.69 KB
/
utils.js
File metadata and controls
101 lines (96 loc) · 3.69 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
const vscode = require('vscode');
function utf8_to_str (src, off, lim) { // https://github.com/quicbit-js/qb-utf8-to-str-tiny
lim = lim == null ? src.length : lim;
for (var i = off || 0, s = ''; i < lim; i++) {
var h = src[i].toString(16);
if (h.length < 2) h = '0' + h;
s += '%' + h;
}
return decodeURIComponent(s);
}
function str_to_utf8_array(str) { // https://gist.github.com/lihnux/2aa4a6f5a9170974f6aa
let utf8 = [];
for (let i = 0; i < str.length; ++i) {
let charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
else { // surrogate pair
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff)<<10) | (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (charcode >>18),
0x80 | ((charcode>>12) & 0x3f),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
return utf8;
}
/**
* Remove comments and trailing comma
* @param {string} text
* @returns {string}
*/
function cleanJSONString(text) {
let result = '';
let comment = false;
let comma = false;
let strRegex = new RegExp('"(\\\\.|[^"])*"', 'g');
let length = text.length;
for (let index = 0; index < length; ++index) {
const chr = text[index];
if (comment) {
if (chr === '\n' || chr === '\r') {
comment = false;
}
continue;
}
if (chr === '/') { comment = true; continue; }
if (chr === '\n' || chr === '\r') { continue; }
if (chr === ' ' || chr === '\t') { continue; }
if (comma) {
if (chr === ']' || chr === '}') { }
else { result += ','; }
comma = false;
}
if (chr === ',') { comma = true; continue; }
if (chr === '"') {
strRegex.lastIndex = index;
result += strRegex.exec(text)[0]; // it is valid JSON so will always match
index = strRegex.lastIndex - 1;
continue;
}
result += chr;
}
return result;
}
let showErrMsg = true;
function setShowErrMsg(b) { showErrMsg = b; }
function getShowErrMsg() { return showErrMsg; }
function getProperty(obj, prop, deflt) { return obj.hasOwnProperty(prop) ? obj[prop] : deflt; }
function getDefaultProperty(obj, deflt) { return getProperty(obj, 'default', deflt); }
function errorMessage(msg, noObject) {
if (getShowErrMsg()) { vscode.window.showErrorMessage(msg); }
return noObject ? noObject : "Unknown";}
function fileNotInFolderError(noObject) { return errorMessage('File not in Multi-root Workspace', noObject); }
/** @param {any} obj @returns {obj is string} */
function isString(obj) { return typeof obj === 'string';}
/** @param {any} obj @returns {obj is any[]} */
function isArray(obj) { return Array.isArray(obj);}
function isObject(obj) { return (typeof obj === 'object') && !isArray(obj);}
function range(size, startAt = 0) { return [...Array(size).keys()].map(i => i + startAt); } // https://stackoverflow.com/a/10050831/9938317
function dblQuest(value, deflt) { return value !== undefined ? value : deflt; }
function nUp(i) { return i===0 ? '' : i.toString()+'Up'; }
module.exports = {
getProperty, getDefaultProperty, errorMessage, setShowErrMsg, fileNotInFolderError, isString, isArray, isObject, range, dblQuest, nUp, utf8_to_str, str_to_utf8_array, cleanJSONString
}