{"version":3,"file":"index.mjs","sources":["../../../src/govuk/common/index.mjs"],"sourcesContent":["/**\n * Common helpers which do not require polyfill.\n *\n * IMPORTANT: If a helper require a polyfill, please isolate it in its own module\n * so that the polyfill can be properly tree-shaken and does not burden\n * the components that do not need that helper\n *\n * @module common/index\n */\n\n/**\n * TODO: Ideally this would be a NodeList.prototype.forEach polyfill\n * This seems to fail in IE8, requires more investigation.\n * See: https://github.com/imagitama/nodelist-foreach-polyfill\n *\n * @deprecated Will be made private in v5.0\n * @template {Node} ElementType\n * @param {NodeListOf} nodes - NodeList from querySelectorAll()\n * @param {nodeListIterator} callback - Callback function to run for each node\n * @returns {void}\n */\nexport function nodeListForEach (nodes, callback) {\n if (window.NodeList.prototype.forEach) {\n return nodes.forEach(callback)\n }\n for (var i = 0; i < nodes.length; i++) {\n callback.call(window, nodes[i], i, nodes)\n }\n}\n\n/**\n * Used to generate a unique string, allows multiple instances of the component\n * without them conflicting with each other.\n * https://stackoverflow.com/a/8809472\n *\n * @deprecated Will be made private in v5.0\n * @returns {string} Unique ID\n */\nexport function generateUniqueID () {\n var d = new Date().getTime()\n if (typeof window.performance !== 'undefined' && typeof window.performance.now === 'function') {\n d += window.performance.now() // use high-precision timer if available\n }\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {\n var r = (d + Math.random() * 16) % 16 | 0\n d = Math.floor(d / 16)\n return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)\n })\n}\n\n/**\n * Config flattening function\n *\n * Takes any number of objects, flattens them into namespaced key-value pairs,\n * (e.g. {'i18n.showSection': 'Show section'}) and combines them together, with\n * greatest priority on the LAST item passed in.\n *\n * @deprecated Will be made private in v5.0\n * @returns {Object} A flattened object of key-value pairs.\n */\nexport function mergeConfigs (/* configObject1, configObject2, ...configObjects */) {\n /**\n * Function to take nested objects and flatten them to a dot-separated keyed\n * object. Doing this means we don't need to do any deep/recursive merging of\n * each of our objects, nor transform our dataset from a flat list into a\n * nested object.\n *\n * @param {Object} configObject - Deeply nested object\n * @returns {Object} Flattened object with dot-separated keys\n */\n var flattenObject = function (configObject) {\n // Prepare an empty return object\n /** @type {Object} */\n var flattenedObject = {}\n\n /**\n * Our flattening function, this is called recursively for each level of\n * depth in the object. At each level we prepend the previous level names to\n * the key using `prefix`.\n *\n * @param {Partial>} obj - Object to flatten\n * @param {string} [prefix] - Optional dot-separated prefix\n */\n var flattenLoop = function (obj, prefix) {\n // Loop through keys...\n for (var key in obj) {\n // Check to see if this is a prototypical key/value,\n // if it is, skip it.\n if (!Object.prototype.hasOwnProperty.call(obj, key)) {\n continue\n }\n var value = obj[key]\n var prefixedKey = prefix ? prefix + '.' + key : key\n if (typeof value === 'object') {\n // If the value is a nested object, recurse over that too\n flattenLoop(value, prefixedKey)\n } else {\n // Otherwise, add this value to our return object\n flattenedObject[prefixedKey] = value\n }\n }\n }\n\n // Kick off the recursive loop\n flattenLoop(configObject)\n return flattenedObject\n }\n\n // Start with an empty object as our base\n /** @type {Object} */\n var formattedConfigObject = {}\n\n // Loop through each of the remaining passed objects and push their keys\n // one-by-one into configObject. Any duplicate keys will override the existing\n // key with the new value.\n for (var i = 0; i < arguments.length; i++) {\n var obj = flattenObject(arguments[i])\n for (var key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n formattedConfigObject[key] = obj[key]\n }\n }\n }\n\n return formattedConfigObject\n}\n\n/**\n * Extracts keys starting with a particular namespace from a flattened config\n * object, removing the namespace in the process.\n *\n * @deprecated Will be made private in v5.0\n * @param {Object} configObject - The object to extract key-value pairs from.\n * @param {string} namespace - The namespace to filter keys with.\n * @returns {Object} Flattened object with dot-separated key namespace removed\n * @throws {Error} Config object required\n * @throws {Error} Namespace string required\n */\nexport function extractConfigByNamespace (configObject, namespace) {\n // Check we have what we need\n if (!configObject || typeof configObject !== 'object') {\n throw new Error('Provide a `configObject` of type \"object\".')\n }\n\n if (!namespace || typeof namespace !== 'string') {\n throw new Error('Provide a `namespace` of type \"string\" to filter the `configObject` by.')\n }\n\n /** @type {Object} */\n var newObject = {}\n\n for (var key in configObject) {\n // Split the key into parts, using . as our namespace separator\n var keyParts = key.split('.')\n // Check if the first namespace matches the configured namespace\n if (Object.prototype.hasOwnProperty.call(configObject, key) && keyParts[0] === namespace) {\n // Remove the first item (the namespace) from the parts array,\n // but only if there is more than one part (we don't want blank keys!)\n if (keyParts.length > 1) {\n keyParts.shift()\n }\n // Join the remaining parts back together\n var newKey = keyParts.join('.')\n // Add them to our new object\n newObject[newKey] = configObject[key]\n }\n }\n return newObject\n}\n\n/**\n * @template {Node} ElementType\n * @callback nodeListIterator\n * @param {ElementType} value - The current node being iterated on\n * @param {number} index - The current index in the iteration\n * @param {NodeListOf} nodes - NodeList from querySelectorAll()\n * @returns {void}\n */\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;AAqBA,AAAO,SAAS,eAAe,EAAE,KAAK,EAAE,QAAQ,EAAE;EAChD,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,EAAE;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;GAC/B;EACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;IACrC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAC;GAC1C;CACF;;;;;;;;;;AAUD,AAAO,SAAS,gBAAgB,IAAI;EAClC,IAAI,CAAC,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,GAAE;EAC5B,IAAI,OAAO,MAAM,CAAC,WAAW,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,WAAW,CAAC,GAAG,KAAK,UAAU,EAAE;IAC7F,CAAC,IAAI,MAAM,CAAC,WAAW,CAAC,GAAG,GAAE;GAC9B;EACD,OAAO,sCAAsC,CAAC,OAAO,C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