-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.mjs
48 lines (37 loc) · 1.47 KB
/
string.mjs
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
"use strict"
import { makeCharList } from "./coreJs.mjs";
export const stringJoin = (elements, delimiter) => elements.join(delimiter);
export const stringDataFormat = (mime, content, encoding = 'utf8') => `data:${mime};${encoding},` + content;
export const urlToString = text => decodeURIComponent(text);
export const stringToObject = text => JSON.parse(text);
export const stringToUrl = text => encodeURIComponent(text);
const emptyChar = makeCharList(' \t');
const newLineChar = makeCharList('\n\r');
const emptyOrNewLineChar = makeCharList(emptyChar, newLineChar);
export const stringUnfold = text => {
let result = [], previousChar = '';
let spaceStart = -1;
let skipSpacesMode = 0;
for (let i = 0; i < text.length; i++) {
const char = text[i];
if (spaceStart < 0 && emptyOrNewLineChar[char] && !emptyOrNewLineChar[previousChar]) {
spaceStart = i;
}
if (newLineChar[char]) {
skipSpacesMode = 1;
if (spaceStart < 0) spaceStart = i;
}
if (!emptyOrNewLineChar[char] && emptyOrNewLineChar[previousChar]) {
if (skipSpacesMode) {
result.length && result.push(' ');
skipSpacesMode = 0;
} else {
result.length && result.push(text.substring(spaceStart, i));
}
spaceStart = -1;
}
if (!emptyOrNewLineChar[char]) result.push(char)
previousChar = char;
}
return result.join('');
}