UNPKG

spws

Version:

SharePoint Web Services Wrapper

78 lines 3.88 kB
"use strict"; /** * @name parseUserGroupValue * @description Converts a SharePoint User/Group string with IDs into an array * @param {string} string The user and/or group string to be parsed * @param {object} options Options to expand user fields * @param {boolean} [options.expandUserField=false] Whether to expand user fields into detailed objects * @returns {Array<UserGroup | ExpandedUserGroup>} * Returns an array of objects containing the ID, label, and value keys, and optionally loginName, email, and sipAddress when expanded. */ Object.defineProperty(exports, "__esModule", { value: true }); var parseUserGroupValue = function (string, // Default the input string to an empty string if not provided options // Optional options parameter ) { if (string === void 0) { string = ""; } var _a = (options || {}).expandUserField, expandUserField = _a === void 0 ? false : _a; // Destructure and default expandUserField to false // If the input string is empty or falsy, return an empty array if (!string) return []; if (expandUserField) { // Expand user details into an array of ExpandedUserGroup objects return string .split(";#") .reduce(function (array, currentString, index) { // Replace any occurrences of double commas with a single comma currentString = currentString.replace(/,,/g, ","); if (index % 2 === 0) { // If the index is even, push a placeholder object with just the ID array.push({ ID: currentString, label: "", value: "", loginName: "", email: "", sipAddress: "", }); } else { // If the index is odd, this string contains additional details var arrayIndex = array.length - 1; // Get the index of the last element in the array var ID = array[arrayIndex].ID; // Retrieve the ID from the last element // Split the string by ",#" to extract detailed user information var dataParts = currentString.split(",#"); // Create an expanded user object using the parsed data var user = { ID: ID, label: dataParts[4] || "", value: "".concat(ID, ";#").concat(dataParts[0]), loginName: dataParts[1] || "", email: dataParts[2] || "", sipAddress: dataParts[3] || "", // SIP address, fallback to an empty string }; // Replace the placeholder object in the array with the expanded user object array[arrayIndex] = user; } return array; // Return the updated array after processing this string }, []); } else { // Parse into an array of BaseUserGroup objects var userGroups = []; var array = string.split(";#"); // Split the input string by ";#" for (var i = 0; i < array.length; i += 2) { // Process every two elements: one for ID and one for label var ID = array[i]; var label = array[i + 1] || ""; // Use the next element as the label, or fallback to an empty string // Add the parsed user/group to the result array userGroups.push({ ID: ID, label: label, value: "".concat(ID, ";#").concat(label), // Construct the value using ID and label }); } return userGroups; // Return the array of BaseUserGroup objects } }; exports.default = parseUserGroupValue; //# sourceMappingURL=parseUserGroupValue.js.map