diffusion
Version:
Diffusion JavaScript client
167 lines (166 loc) • 4.95 kB
JavaScript
;
/**
* @module Util.String
*
* @brief Utility functions for string manipulation
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectionToString = exports.stringToSet = exports.escape = exports.stringToArray = exports.split = void 0;
var errors_1 = require("./../../errors/errors");
/**
* Split a string around a given delimiter.
*
* This differs from String.split in two ways:
*
* - If the input string is empty, this function will return an empty array,
* not a single-element array containing an empty string.
* - The string will be split into substrings that terminate with the specified
* delimiter, rather than splitting immediately when a delimiter is found.
*
* **Example:**
* ```
* split("foo/bar/", "/"); // Returns ["foo", "bar"];
* ```
*
* **Example:**
* ```
* split("foo///", "//"); // Returns ["foo/"];
* ```
*
* @param str the value to split
* @param delim the delimiter to split on
* @returns the split parts
*/
function split(str, delim) {
if (str === '') {
return [];
}
var parts = [];
var l = delim.length;
var i = str.lastIndexOf(delim);
while (i > -1) {
parts.push(str.substring(i + l, str.length));
str = str.substring(0, i);
i = str.lastIndexOf(delim);
}
parts.push(str);
return parts.reverse();
}
exports.split = split;
/**
* check if the string is a quote mark
*
* @param c the string to check
* @return `true` if the string is either a single ' or double " quote
*/
function isQuote(c) {
return c === '\'' || c === '"';
}
/**
* Converts a formatted multi value property to an array.
*
* Unescapes any escaped characters in the string.
*
* @param stringValue the string with quoted substrings separated by whitespace or
* commas
* @return array of strings
* @throws an {@link IllegalArgumentError} if the input is not correctly quoted
*/
function stringToArray(stringValue) {
var arr = [];
var part = '';
var pos = 0;
var escaped = false;
OUTER_LOOP: for (; pos < stringValue.length; pos++) {
var c = stringValue[pos];
if (isQuote(c)) {
pos++;
part = '';
escaped = false;
for (; pos < stringValue.length; pos++) {
var vc = stringValue[pos];
if (escaped) {
part += vc;
escaped = false;
}
else if (vc === '\\') {
escaped = true;
}
else if (vc === c) {
arr.push(part);
continue OUTER_LOOP;
}
else {
part += vc;
}
}
throw new errors_1.IllegalArgumentError('Mis-quoted input');
}
}
return arr;
}
exports.stringToArray = stringToArray;
/**
* Inserts an escape character '\' before any of the special characters ' "
* or \.
* @param s the string to be escaped
* @returns {String} the string value with escape characters inserted as appropriate
*/
function escape(s) {
for (var i = 0; i < s.length; i++) {
var ci = s.charAt(i);
if (ci === '"' || ci === '\\' || ci === '\'') {
var sb = '';
for (var j = 0; j < s.length; j++) {
var cj = s.charAt(j);
switch (cj) {
case '\\':
sb += '\\\\';
break;
case '"':
sb += '\\"';
break;
case '\'':
sb += '\\\'';
break;
default:
sb += cj;
break;
}
}
return sb;
}
}
return s;
}
exports.escape = escape;
/**
* Converts a formatted multi value property to an immutable set.
*
* Unescapes any escaped characters in the string.
* @param stringValue the string with quoted substrings separated by whitespace or
* commas
* @return set of strings
* @throws an {@link IllegalArgumentError} if the input is not correctly quoted
*/
function stringToSet(stringValue) {
return new Set(stringToArray(stringValue));
}
exports.stringToSet = stringToSet;
/**
* Converts a set of values to a canonicalised property string
* representation, escaping as required.
*
* Uses comma separators and single quotes.
*
* @param values a set of strings
* @return a string with each of the members escaped, quoted and concatenated
*/
function collectionToString(values) {
var arr = [];
values.forEach(function (s) {
arr.push('"' + escape(s) + '"');
});
return arr.join(',');
}
exports.collectionToString = collectionToString;