UNPKG

diffusion

Version:

Diffusion JavaScript client

165 lines (149 loc) 4.41 kB
var Set = require('core-js/library/fn/set'); /** * Split a string around a given delimiter. * <P> * This differs from String.split in two ways: * <ol> * <li>If the input string is empty, this function will return an empty array, * not a single-element array containing an empty string. * </li> * <li>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 {String} str - The value to split * @param {String} delim - The delimiter to split on * @returns {Array} The split parts */ function split(str, delim) { if (str === "") { return []; } var parts = [], l = delim.length, 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(); } function isQuote(c) { return c === "'" || c === '"'; } /** * Converts a formatted multi value property to an array. * <p> * Unescapes any escaped characters in the string. * * @param {String} stringValue the string with quoted substrings separated by whitespace or * commas * @return {Array<String>} array of strings */ 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 Error("Mis-quoted input"); } } return arr; } /** * Inserts an escape character '\' before any of the special characters ' " * or \. * @param {String} string the string to be escaped * @returns {String} the string value with escape characters inserted as appropriate */ function escape(string) { for (var i = 0; i < string.length; i++) { var ci = string.charAt(i); if (ci === '"' || ci === '\\' || ci === '\'') { var sb = ""; for (var j = 0; j < string.length; j++) { var cj = string.charAt(j); switch(cj) { case '\\': sb += "\\\\"; break; case '"': sb += "\\\""; break; case '\'': sb += "\\'"; break; default: sb += cj; break; } } return sb; } } return string; } /** * Converts a formatted multi value property to an immutable set. * <p> * Unescapes any escaped characters in the string. * @param {String} stringValue the string with quoted substrings separated by whitespace or * commas * @return {Array<String>} set of strings */ function stringToSet(stringValue) { return new Set(stringToArray(stringValue)); } /** * Converts a set of values to a canonicalised property string * representation, escaping as required. * <p> * Uses comma separators and single quotes. * * @param {Set<String>|Array<String>} values a set of roles * * @return {String} a string with each of the members escaped, quoted and concatenated */ function collectionToString(values) { var arr = []; values.forEach(function(string) { arr.push('\"' + escape(string) + '\"'); }); return arr.join(","); } module.exports = { split : split, stringToArray: stringToArray, stringToSet: stringToSet, collectionToString: collectionToString, escape: escape };