diff --git a/dist/browser.js b/dist/browser.js index accf127..f923d60 100644 --- a/dist/browser.js +++ b/dist/browser.js @@ -553,17 +553,21 @@ const _ = require('lodash'); */ module.exports = class MeasureHelpers { /** - * Builds a map of define statement name to the statement's text from a measure. - * @public - * @param {Measure} measure - The measure to build the map from. - * @return {Hash} Map of statement definitions to full statement - */ + * Builds a map of define statement name to the statement's text from a measure. + * @public + * @param {Measure} measure - The measure to build the map from. + * @return {Hash} Map of statement definitions to full statement + */ static buildDefineToFullStatement(measure) { const ret = {}; for (const lib in measure.get('elm_annotations')) { const libStatements = {}; - for (const statement of Array.from(measure.get('elm_annotations')[lib].statements)) { - libStatements[statement.define_name] = this.parseAnnotationTree(statement.children); + for (const statement of Array.from( + measure.get('elm_annotations')[lib].statements, + )) { + libStatements[statement.define_name] = this.parseAnnotationTree( + statement.children, + ); } ret[lib] = libStatements; } @@ -571,18 +575,17 @@ module.exports = class MeasureHelpers { } /** - * Recursive function that parses an annotation tree to extract text statements. - * @param {Node} children - the node to be traversed. - * @return {String} the text of the node or its children. - */ + * Recursive function that parses an annotation tree to extract text statements. + * @param {Node} children - the node to be traversed. + * @return {String} the text of the node or its children. + */ static parseAnnotationTree(children) { let child; let ret = ''; if (children.text !== undefined) { - return _.unescape(children.text) - .replace(' ', '') - .replace(';', ''); - } if (children.children !== undefined) { + return _.unescape(children.text).replace(' ', '').replace(';', ''); + } + if (children.children !== undefined) { for (child of Array.from(children.children)) { ret += this.parseAnnotationTree(child); } @@ -595,19 +598,21 @@ module.exports = class MeasureHelpers { } /** - * Finds all localIds in a statement by it's library and statement name. - * @public - * @param {Measure} measure - The measure to find localIds in. - * @param {string} libraryName - The name of the library the statement belongs to. - * @param {string} statementName - The statement name to search for. - * @return {Hash} List of local ids in the statement. - */ + * Finds all localIds in a statement by it's library and statement name. + * @public + * @param {Measure} measure - The measure to find localIds in. + * @param {string} libraryName - The name of the library the statement belongs to. + * @param {string} statementName - The statement name to search for. + * @return {Hash} List of local ids in the statement. + */ static findAllLocalIdsInStatementByName(libraryElm, statementName) { // create place for aliases and their usages to be placed to be filled in later. Aliases and their usages (aka scope) // and returns do not have localIds in the elm but do in elm_annotations at a consistent calculable offset. // BE WEARY of this calaculable offset. const emptyResultClauses = []; - const statement = libraryElm.library.statements.def.find((stat) => stat.name === statementName); + const statement = libraryElm.library.statements.def.find( + (stat) => stat.name === statementName, + ); const libraryName = libraryElm.library.identifier.id; const aliasMap = {}; @@ -619,13 +624,16 @@ module.exports = class MeasureHelpers { {}, aliasMap, emptyResultClauses, - null + null, ); // Create/change the clause for all aliases and their usages for (const alias of Array.from(emptyResultClauses)) { // Only do it if we have a clause for where the result should be fetched from // and have a localId for the clause that the result should map to - if (localIds[alias.expressionLocalId] != null && alias.aliasLocalId != null) { + if ( + localIds[alias.expressionLocalId] != null + && alias.aliasLocalId != null + ) { localIds[alias.aliasLocalId] = { localId: alias.aliasLocalId, sourceLocalId: alias.expressionLocalId, @@ -645,18 +653,18 @@ module.exports = class MeasureHelpers { } /** - * Finds all localIds in the statement structure recursively. - * @private - * @param {Object} statement - The statement structure or child parts of it. - * @param {String} libraryName - The name of the library we are looking at. - * @param {Array} annotation - The JSON annotation for the entire structure, this is occasionally needed. - * @param {Object} localIds - The hash of localIds we are filling. - * @param {Object} aliasMap - The map of aliases. - * @param {Array} emptyResultClauses - List of clauses that will have empty results from the engine. Each object on - * this has info on where to find the actual result. - * @param {Object} parentNode - The parent node, used for some special situations. - * @return {Array[Integer]} List of local ids in the statement. This is same array, localIds, that is passed in. - */ + * Finds all localIds in the statement structure recursively. + * @private + * @param {Object} statement - The statement structure or child parts of it. + * @param {String} libraryName - The name of the library we are looking at. + * @param {Array} annotation - The JSON annotation for the entire structure, this is occasionally needed. + * @param {Object} localIds - The hash of localIds we are filling. + * @param {Object} aliasMap - The map of aliases. + * @param {Array} emptyResultClauses - List of clauses that will have empty results from the engine. Each object on + * this has info on where to find the actual result. + * @param {Object} parentNode - The parent node, used for some special situations. + * @return {Array[Integer]} List of local ids in the statement. This is same array, localIds, that is passed in. + */ static findAllLocalIdsInStatement( statement, libraryName, @@ -664,8 +672,18 @@ module.exports = class MeasureHelpers { localIds, aliasMap, emptyResultClauses, - parentNode + parentNode, ) { + // Stop recursing if this node happens to be any TypeSpecifier. We do not want to collect localIds for these clauses + // as they are not executed and will negatively affect clause coverage if captured here. ChoiceTypeSpecifiers do not + // identify their type and instead put [] at the `type` attribute which is a deprecated field. + if ( + statement.type + && (Array.isArray(statement.type) + || statement.type.endsWith('TypeSpecifier')) + ) { + return localIds; + } // looking at the key and value of everything on this object or array for (const k in statement) { let alId; @@ -677,24 +695,63 @@ module.exports = class MeasureHelpers { aliasMap[v] = statement.return.expression.localId; alId = statement.return.localId; if (alId) { - emptyResultClauses.push({ lib: libraryName, aliasLocalId: alId, expressionLocalId: aliasMap[v] }); + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: aliasMap[v], + }); } } - this.findAllLocalIdsInStatement(v, libraryName, annotation, localIds, aliasMap, emptyResultClauses, statement); + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); } else if (k === 'alias') { - if (statement.expression != null && statement.expression.localId != null) { + if ( + statement.expression != null + && statement.expression.localId != null + ) { // Keep track of the localId of the expression that the alias references aliasMap[v] = statement.expression.localId; // Determine the localId in the elm_annotation for this alias. - alId = parseInt(statement.expression.localId, 10) + 1; - emptyResultClauses.push({ lib: libraryName, aliasLocalId: alId, expressionLocalId: aliasMap[v] }); + // Determine the localId for this alias. + if (statement.localId) { + // Older translator versions require with statements to use the statement.expression.localId + 1 as the alias Id + // even if the statement already has a localId. There is not a clear mapping for alias with statements in the new + // translator, so they will go un highlighted but this will not affect coverage calculation + if (statement.type === 'With' || statement.type === 'Without') { + alId = ( + parseInt(statement.expression.localId, 10) + 1 + ).toString(); + } else { + alId = statement.localId; + } + } else { + // Older translator versions created an elm_annotation localId that was not always in the elm. This was a + // single increment up from the expression that defines the alias. + alId = (parseInt(statement.expression.localId, 10) + 1).toString(); + } + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: aliasMap[v], + }); } } else if (k === 'scope') { // The scope entry references an alias but does not have an ELM local ID. Hoever it DOES have an elm_annotations localId // The elm_annotation localId of the alias variable is the localId of it's parent (one less than) // because the result of the scope clause should be equal to the clause that the scope is referencing alId = parseInt(statement.localId, 10) - 1; - emptyResultClauses.push({ lib: libraryName, aliasLocalId: alId, expressionLocalId: aliasMap[v] }); + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: aliasMap[v], + }); } else if (k === 'asTypeSpecifier') { // Map the localId of the asTypeSpecifier (Code, Quantity...) to the result of the result it is referencing // For example, in the CQL code 'Variable.result as Code' the typeSpecifier does not produce a result, therefore @@ -702,18 +759,32 @@ module.exports = class MeasureHelpers { alId = statement.asTypeSpecifier.localId; if (alId != null) { const typeClauseId = parseInt(statement.asTypeSpecifier.localId, 10) - 1; - emptyResultClauses.push({ lib: libraryName, aliasLocalId: alId, expressionLocalId: typeClauseId }); + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: typeClauseId, + }); } } else if (k === 'sort') { // Sort is a special case that we need to recurse into separately and set the results to the result of the statement the sort clause is in - this.findAllLocalIdsInSort(v, libraryName, localIds, aliasMap, emptyResultClauses, parentNode); + this.findAllLocalIdsInSort( + v, + libraryName, + localIds, + aliasMap, + emptyResultClauses, + parentNode, + ); } else if (k === 'let') { // let is a special case where it is an array, one for each defined alias. These aliases work slightly different // and execution engine does return results for them on use. The Initial naming of them needs to be properly pointed // to what they are set to. for (const aLet of Array.from(v)) { // Add the localId for the definition of this let to it's source. - localIds[aLet.localId] = { localId: aLet.localId, sourceLocalId: aLet.expression.localId }; + localIds[aLet.localId] = { + localId: aLet.localId, + sourceLocalId: aLet.expression.localId, + }; this.findAllLocalIdsInStatement( aLet.expression, libraryName, @@ -721,47 +792,96 @@ module.exports = class MeasureHelpers { localIds, aliasMap, emptyResultClauses, - statement + statement, ); } - // handle the `when` pieces of Case expression aka CaseItems. They have a `when` key that should be mapped to get a result from the expression that defines them + // handle the `when` pieces of Case expression aka CaseItems. They have a `when` key that should be mapped to get a result from the expression that defines them } else if (k === 'when' && statement.localId && v.localId) { - localIds[statement.localId] = { localId: statement.localId, sourceLocalId: v.localId }; - this.findAllLocalIdsInStatement(v, libraryName, annotation, localIds, aliasMap, emptyResultClauses, statement); + localIds[statement.localId] = { + localId: statement.localId, + sourceLocalId: v.localId, + }; + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); // If 'First' and 'Last' expressions, the result of source of the clause should be set to the expression } else if (k === 'type' && (v === 'First' || v === 'Last')) { if (statement.source && statement.source.localId != null) { alId = statement.source.localId; - emptyResultClauses.push({ lib: libraryName, aliasLocalId: alId, expressionLocalId: statement.localId }); + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: statement.localId, + }); } // Continue to recurse into the 'First' or 'Last' expression - this.findAllLocalIdsInStatement(v, libraryName, annotation, localIds, aliasMap, emptyResultClauses, statement); + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); // If this is a FunctionRef or ExpressionRef and it references a library, find the clause for the library reference and add it. - } else if (k === 'type' && (v === 'FunctionRef' || v === 'ExpressionRef') && statement.libraryName != null) { + } else if ( + k === 'type' + && (v === 'FunctionRef' || v === 'ExpressionRef') + && statement.libraryName != null + ) { const libraryClauseLocalId = this.findLocalIdForLibraryRef( annotation, statement.localId, - statement.libraryName + statement.libraryName, ); if (libraryClauseLocalId !== null) { // only add the clause if the localId for it is found // the sourceLocalId is the FunctionRef itself to match how library statement references work. - localIds[libraryClauseLocalId] = { localId: libraryClauseLocalId, sourceLocalId: statement.localId }; + localIds[libraryClauseLocalId] = { + localId: libraryClauseLocalId, + sourceLocalId: statement.localId, + }; } // handle the `when` pieces of Case expression aka CaseItems. // They have a `when` key that should be mapped to get a result from the expression that defines them } else if (k === 'type' && v === 'Null' && statement.localId) { // If this is a "Null" expression, mark that it `isFalsyLiteral` so we can interpret final results differently. - localIds[statement.localId] = { localId: statement.localId, isFalsyLiteral: true }; - } else if (k === 'type' && v === 'Literal' && statement.localId && statement.value === 'false') { + localIds[statement.localId] = { + localId: statement.localId, + isFalsyLiteral: true, + }; + } else if ( + k === 'type' + && v === 'Literal' + && statement.localId + && statement.value === 'false' + ) { // If this is a "Literal" expression whose value is false, mark that it `isFalsyLiteral` so we can interpret final results differently - localIds[statement.localId] = { localId: statement.localId, isFalsyLiteral: true }; + localIds[statement.localId] = { + localId: statement.localId, + isFalsyLiteral: true, + }; // else if the key is localId, push the value } else if (k === 'localId') { localIds[v] = { localId: v }; // if the value is an array or object, recurse } else if (Array.isArray(v) || typeof v === 'object') { - this.findAllLocalIdsInStatement(v, libraryName, annotation, localIds, aliasMap, emptyResultClauses, statement); + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); } } @@ -769,25 +889,45 @@ module.exports = class MeasureHelpers { } /** - * Finds all localIds in the sort structure recursively and sets the expressionLocalId to the parent statement. - * @private - * @param {Object} statement - The statement structure or child parts of it. - * @param {String} libraryName - The name of the library we are looking at. - * @param {Object} localIds - The hash of localIds we are filling. - * @param {Object} aliasMap - The map of aliases. - * @param {Array} emptyResultClauses - List of clauses that will have empty results from the engine. Each object on - * this has info on where to find the actual result. - * @param {Object} rootStatement - The rootStatement. - */ - static findAllLocalIdsInSort(statement, libraryName, localIds, aliasMap, emptyResultClauses, rootStatement) { + * Finds all localIds in the sort structure recursively and sets the expressionLocalId to the parent statement. + * @private + * @param {Object} statement - The statement structure or child parts of it. + * @param {String} libraryName - The name of the library we are looking at. + * @param {Object} localIds - The hash of localIds we are filling. + * @param {Object} aliasMap - The map of aliases. + * @param {Array} emptyResultClauses - List of clauses that will have empty results from the engine. Each object on + * this has info on where to find the actual result. + * @param {Object} rootStatement - The rootStatement. + */ + static findAllLocalIdsInSort( + statement, + libraryName, + localIds, + aliasMap, + emptyResultClauses, + rootStatement, + ) { const alId = statement.localId; - emptyResultClauses.push({ lib: libraryName, aliasLocalId: alId, expressionLocalId: rootStatement.localId }); + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: rootStatement.localId, + }); return (() => { const result = []; for (const k in statement) { const v = statement[k]; if (Array.isArray(v) || typeof v === 'object') { - result.push(this.findAllLocalIdsInSort(v, libraryName, localIds, aliasMap, emptyResultClauses, rootStatement)); + result.push( + this.findAllLocalIdsInSort( + v, + libraryName, + localIds, + aliasMap, + emptyResultClauses, + rootStatement, + ), + ); } else { result.push(undefined); } @@ -797,53 +937,53 @@ module.exports = class MeasureHelpers { } /** - * Find the localId of the library reference in the JSON elm annotation. This recursively searches the annotation structure - * for the clause of the library ref. When that is found it knows where to look inside of that for where the library - * reference may be. - * - * Consider the following example of looking for function ref with id "55" and library "global". - * CQL for this is "global.CalendarAgeInYearsAt(...)". The following annotation snippet covers the call of the - * function. - * - * { - * "r": "55", - * "s": [ - * { - * "r": "49", - * "s": [ - * { - * "value": [ - * "global" - * ] - * } - * ] - * }, - * { - * "value": [ - * "." - * ] - * }, - * { - * "r": "55", - * "s": [ - * { - * "value": [ - * "\"CalendarAgeInYearsAt\"", - * "(" - * ] - * }, - * - * This method will recurse through the structure until it stops on this snippet that has "r": "55". Then it will check - * if the value of the first child is simply an array with a single string equaling "global". If that is indeed the - * case then it will return the "r" value of that first child, which is the clause localId for the library part of the - * function reference. If that is not the case, it will keep recursing and may eventually return null. - * - * @private - * @param {Object|Array} annotation - The annotation structure or child in the annotation structure. - * @param {String} refLocalId - The localId of the library ref we should look for. - * @param {String} libraryName - The library reference name, used to find the clause. - * @return {String} The localId of the clause for the library reference or null if not found. - */ + * Find the localId of the library reference in the JSON elm annotation. This recursively searches the annotation structure + * for the clause of the library ref. When that is found it knows where to look inside of that for where the library + * reference may be. + * + * Consider the following example of looking for function ref with id "55" and library "global". + * CQL for this is "global.CalendarAgeInYearsAt(...)". The following annotation snippet covers the call of the + * function. + * + * { + * "r": "55", + * "s": [ + * { + * "r": "49", + * "s": [ + * { + * "value": [ + * "global" + * ] + * } + * ] + * }, + * { + * "value": [ + * "." + * ] + * }, + * { + * "r": "55", + * "s": [ + * { + * "value": [ + * "\"CalendarAgeInYearsAt\"", + * "(" + * ] + * }, + * + * This method will recurse through the structure until it stops on this snippet that has "r": "55". Then it will check + * if the value of the first child is simply an array with a single string equaling "global". If that is indeed the + * case then it will return the "r" value of that first child, which is the clause localId for the library part of the + * function reference. If that is not the case, it will keep recursing and may eventually return null. + * + * @private + * @param {Object|Array} annotation - The annotation structure or child in the annotation structure. + * @param {String} refLocalId - The localId of the library ref we should look for. + * @param {String} libraryName - The library reference name, used to find the clause. + * @return {String} The localId of the clause for the library reference or null if not found. + */ static findLocalIdForLibraryRef(annotation, refLocalId, libraryName) { // if this is an object it should have an "r" for localId and "s" for children or leaf nodes let child; @@ -862,7 +1002,10 @@ module.exports = class MeasureHelpers { // check if the first child has the first leaf node with the library name // refer to the method comment for why this is done. if ( - this.__guard__(annotation.s[0].s != null ? annotation.s[0].s[0].value : undefined, (x) => x[0]) === libraryName + this.__guard__( + annotation.s[0].s != null ? annotation.s[0].s[0].value : undefined, + (x) => x[0], + ) === libraryName ) { // return the localId if there is one if (annotation.s[0].r != null) { @@ -884,7 +1027,11 @@ module.exports = class MeasureHelpers { } } } else if (typeof annotation.s === 'object') { - return this.findLocalIdForLibraryRef(annotation.s, refLocalId, libraryName); + return this.findLocalIdForLibraryRef( + annotation.s, + refLocalId, + libraryName, + ); } } @@ -893,16 +1040,18 @@ module.exports = class MeasureHelpers { } /** - * Figure out if a statement is a function given the measure, library name and statement name. - * @public - * @param {Measure} measure - The measure to find localIds in. - * @param {string} libraryName - The name of the library the statement belongs to. - * @param {string} statementName - The statement name to search for. - * @return {boolean} If the statement is a function or not. - */ + * Figure out if a statement is a function given the measure, library name and statement name. + * @public + * @param {Measure} measure - The measure to find localIds in. + * @param {string} libraryName - The name of the library the statement belongs to. + * @param {string} statementName - The statement name to search for. + * @return {boolean} If the statement is a function or not. + */ static isStatementFunction(library, statementName) { // find the library and statement in the elm - const statement = library.elm.library.statements.def.find((def) => def.name === statementName); + const statement = library.elm.library.statements.def.find( + (def) => def.name === statementName, + ); if (statement != null) { return statement.type === 'FunctionDef'; } @@ -910,16 +1059,18 @@ module.exports = class MeasureHelpers { } /** - * Figure out if a statement is in a Supplemental Data Element given the statement name. - * @public - * @param [PopulationSet] populationSets - * @param {string} statement - The statement to search for. - * @return {boolean} Statement does or does not belong to a Supplemental Data Element. - */ + * Figure out if a statement is in a Supplemental Data Element given the statement name. + * @public + * @param [PopulationSet] populationSets + * @param {string} statement - The statement to search for. + * @return {boolean} Statement does or does not belong to a Supplemental Data Element. + */ static isSupplementalDataElementStatement(populationSets, statementName) { for (const populationSet of populationSets) { if (populationSet.supplemental_data_elements) { - const sdeStatement = populationSet.supplemental_data_elements.find((sde) => sde.statement_name === statementName); + const sdeStatement = populationSet.supplemental_data_elements.find( + (sde) => sde.statement_name === statementName, + ); if (sdeStatement !== undefined) { return true; } @@ -929,7 +1080,9 @@ module.exports = class MeasureHelpers { } static __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null ? transform(value) : undefined; + return typeof value !== 'undefined' && value !== null + ? transform(value) + : undefined; } }; diff --git a/dist/browser.js-e b/dist/browser.js-e new file mode 100644 index 0000000..52bc588 --- /dev/null +++ b/dist/browser.js-e @@ -0,0 +1,100722 @@ +(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i { + populationResults[popCode] = 0; + if (populationSet.observations.length > 0) { + populationResults.observation_values = []; + } + }); + + // count up all population results for a patient level count + Object.keys(episodeResults).forEach((e) => { + const episodeResult = episodeResults[e]; + Object.keys(episodeResult).forEach((popCode) => { + const popResult = episodeResult[popCode]; + if (popCode === 'observation_values') { + popResult.forEach((value) => { + populationResults.observation_values.push(value); + }); + } else { + populationResults[popCode] += popResult; + } + }); + }); + } + return [populationResults, episodeResults]; + } + + /** + * Takes in the initial values from result object and checks to see if some values should not be calculated. These + * values that should not be calculated are zeroed out. + * @param {object} populationResults - The population results. Map of "POPNAME" to Integer result. Except for OBSERVs, + * their key is 'value' and value is an array of results. + * @param {String} measureScoring - can be PROPORTION RATIO CONTINUOUS_VARIABLE COHORT. RATIO measures follow a different + * logic path + * @returns {object} Population results in the same structure as passed in, but the appropiate values are zeroed out. + */ + static handlePopulationValues(populationResults, measureScoring) { + /* Setting values of populations if the correct populations are not set based on the following logic guidelines + * Initial Population (IPP): The set of patients or episodes of care to be evaluated by the measure. + * Denominator (DENOM): A subset of the IPP. + * Denominator Exclusions (DENEX): A subset of the Denominator that should not be considered for inclusion in the Numerator. + * Denominator Exceptions (DEXCEP): A subset of the Denominator. Only those members of the Denominator that are considered + * for Numerator membership and are not included are considered for membership in the Denominator Exceptions. + * Numerator (NUMER): A subset of the Denominator. The Numerator criteria are the processes or outcomes expected for each patient, + * procedure, or other unit of measurement defined in the Denominator. + * Numerator Exclusions (NUMEX): A subset of the Numerator that should not be considered for calculation. + * Measure Poplation Exclusions (MSRPOPLEX): Identify that subset of the MSRPOPL that meet the MSRPOPLEX criteria. + */ + const populationResultsHandled = populationResults; + + // RATIO measures do not follow the standard flow from IPP->DENOM->NUMER + // RATIO measures flow from IPP->DENOM-DENEX and IPP->NUMER->NUMEX + if (measureScoring === 'RATIO') { + if (populationResultsHandled.STRAT != null && this.isValueZero('STRAT', populationResults)) { + // Set all values to 0 + Object.keys(populationResults).forEach((key) => { + if (key === 'observation_values') { + populationResultsHandled.observation_values = []; + } else { + populationResultsHandled[key] = 0; + } + }); + } else if (this.isValueZero('IPP', populationResults)) { + Object.keys(populationResults).forEach((key) => { + if (key !== 'STRAT') { + if (key === 'observation_values') { + populationResultsHandled.observation_values = []; + } else { + populationResultsHandled[key] = 0; + } + } + }); + } + if (this.isValueZero('DENOM', populationResults)) { + if ('DENEX' in populationResults) { + populationResultsHandled.DENEX = 0; + } + if ('observation_values' in populationResults) { + // DENOM observation will be the first of 2 observations + populationResultsHandled.observation_values[1] = populationResultsHandled.observation_values[0]; + populationResultsHandled.observation_values[0] = 0; + } + } + if (populationResults.DENEX != null && !this.isValueZero('DENEX', populationResults) && populationResults.DENEX >= populationResults.DENOM) { + if ('observation_values' in populationResults) { + populationResultsHandled.observation_values[0] = 0; + } + } + if (this.isValueZero('NUMER', populationResults)) { + if ('NUMEX' in populationResults) { + populationResultsHandled.NUMEX = 0; + } + if ('observation_values' in populationResults) { + // NUMER observation will be the second of 2 observations + populationResultsHandled.observation_values[1] = 0; + } + } + if (populationResults.NUMER != null && !this.isValueZero('NUMEX', populationResults) && populationResults.NUMEX >= populationResults.NUMER) { + if ('observation_values' in populationResults) { + // NUMER observation will be the second of 2 observations + populationResultsHandled.observation_values[1] = 0; + } + } + return populationResultsHandled; + } + if (populationResultsHandled.STRAT != null && this.isValueZero('STRAT', populationResults)) { + // Set all values to 0 + Object.keys(populationResults).forEach((key) => { + if (key === 'observation_values') { + populationResultsHandled.observation_values = []; + } else { + populationResultsHandled[key] = 0; + } + }); + } else if (this.isValueZero('IPP', populationResults)) { + Object.keys(populationResults).forEach((key) => { + if (key !== 'STRAT') { + if (key === 'observation_values') { + populationResultsHandled.observation_values = []; + } else { + populationResultsHandled[key] = 0; + } + } + }); + } else if (this.isValueZero('DENOM', populationResults) || this.isValueZero('MSRPOPL', populationResults)) { + if ('DENEX' in populationResults) { + populationResultsHandled.DENEX = 0; + } + if ('DENEXCEP' in populationResults) { + populationResultsHandled.DENEXCEP = 0; + } + if ('NUMER' in populationResults) { + populationResultsHandled.NUMER = 0; + } + if ('NUMEX' in populationResults) { + populationResultsHandled.NUMEX = 0; + } + if ('MSRPOPLEX' in populationResults) { + populationResultsHandled.MSRPOPLEX = 0; + } + if ('observation_values' in populationResults) { + populationResultsHandled.observation_values = []; + } + // Can not be in the numerator if the same or more are excluded from the denominator + } else if (populationResults.DENEX != null && !this.isValueZero('DENEX', populationResults) && populationResults.DENEX >= populationResults.DENOM) { + if ('NUMER' in populationResults) { + populationResultsHandled.NUMER = 0; + } + if ('NUMEX' in populationResults) { + populationResultsHandled.NUMEX = 0; + } + if ('DENEXCEP' in populationResults) { + populationResultsHandled.DENEXCEP = 0; + } + if ('observation_values' in populationResults) { + populationResultsHandled.observation_values = []; + } + } else if (populationResults.MSRPOPLEX != null && !this.isValueZero('MSRPOPLEX', populationResults)) { + if ('observation_values' in populationResults) { + populationResultsHandled.observation_values = []; + } + } else if (this.isValueZero('NUMER', populationResults)) { + if ('NUMEX' in populationResults) { + populationResultsHandled.NUMEX = 0; + } + } else if (!this.isValueZero('NUMER', populationResults)) { + if ('DENEXCEP' in populationResults) { + populationResultsHandled.DENEXCEP = 0; + } + } + return populationResultsHandled; + } + + /** + * Create patient population values (aka results) for all populations in the population set using the results from the + * calculator. + * @param {Population} populationSet - The population set we are getting the values for. + * @param {Object} patientResults - The raw results object for a patient from the calculation engine. + * @param {Array} observationDefs - List of observation defines we add to the elm for calculation OBSERVs. + * @returns {Object} The population results. Map of "POPNAME" to Integer result. Except for OBSERVs, + * their key is 'value' and value is an array of results. + */ + static createPatientPopulationValues(populationSet, patientResults, observationDefs) { + const populationResults = {}; + + // Loop over all population codes ("IPP", "DENOM", etc.) + Object.keys(populationSet.populations.toObject()).forEach((popCode) => { + const cqlPopulation = populationSet.populations[popCode].statement_name; + // Is there a patient result for this population? and does this populationCriteria contain the population + // We need to check if the populationCriteria contains the population so that a STRAT is not set to zero if there is not a STRAT in the populationCriteria + // Grab CQL result value and adjust for ECQME + const value = patientResults[cqlPopulation]; + if (Array.isArray(value) && value.length > 0) { + populationResults[popCode] = value.length; + } else if (typeof value === 'boolean' && value) { + populationResults[popCode] = 1; + } else { + populationResults[popCode] = 0; + } + }); + if ((observationDefs != null ? observationDefs.length : undefined) > 0) { + // Handle observations using the names of the define statements that + // were added to the ELM to call the observation functions. + observationDefs.forEach((obDef) => { + if (!populationResults.observation_values) { + populationResults.observation_values = []; + } + // Observations only have one result, based on how the HQMF is + // structured (note the single 'value' section in the + // measureObservationDefinition clause). + const obsResults = patientResults != null + ? patientResults[obDef] + : undefined; + if (Array.isArray(obsResults)) { + obsResults.forEach((obsResult) => { + // Add the single result value to the values array on the results of + // this calculation (allowing for more than one possible observation). + if (obsResult != null ? Object.prototype.hasOwnProperty.call(obsResult, 'value') : undefined) { + // If result is a Cql.Quantity type, add its value + populationResults.observation_values.push(obsResult.observation.value); + } else { + // In all other cases, add result + populationResults.observation_values.push(obsResult.observation); + } + }); + } else { + populationResults.observation_values.push(obsResults); + } + }); + } + + return populationResults; + } + + /** + * Create population values (aka results) for all episodes using the results from the calculator. This is + * used only for the episode of care measures + * @param {Population} populationSet - The populationSet we are getting the values for. + * @param {Object} patientResults - The raw results object for the patient from the calculation engine. + * @param {Array} observationDefs - List of observation defines we add to the elm for calculation OBSERVs. + * @param {String} measureScoring - can be PROPORTION RATIO CONTINUOUS_VARIABLE COHORT. + * @returns {Object} The episode results. Map of episode id to population results which is a map of "POPNAME" + * to Integer result. Except for OBSERVs, their key is 'value' and value is an array of results. + */ + static createEpisodePopulationValues(populationSet, patientResults, observationDefs, measureScoring) { + const episodeResults = {}; + + for (const popCode in populationSet.populations.toObject()) { + let newEpisode; + const cqlPopulation = populationSet.populations[popCode]; + // Is there a patient result for this population? and does this populationCriteria contain the population + // We need to check if the populationCriteria contains the population so that a STRAT is not set to zero if there is not a STRAT in the populationCriteria + // Grab CQL result value and store for each episode found + const qdmDataElements = patientResults[cqlPopulation.statement_name]; + if (Array.isArray(qdmDataElements)) { + qdmDataElements.forEach((qdmDataElement) => { + if (qdmDataElement.id != null) { + // if an episode has already been created set the result for the population to 1 + if (episodeResults[qdmDataElement.id]) { + episodeResults[qdmDataElement.id][popCode] = 1; + // else create a new episode using the list of all popcodes for the population + } else { + newEpisode = {}; + for (const pc in populationSet.populations.toObject()) { + newEpisode[pc] = 0; + } + + newEpisode[popCode] = 1; + episodeResults[qdmDataElement.id] = newEpisode; + } + } + }); + } + } + + if ((observationDefs != null ? observationDefs.length : undefined) > 0) { + // Handle observations using the names of the define statements that + // were added to the ELM to call the observation functions. + observationDefs.forEach((obDef) => { + // Observations only have one result, based on how the HQMF is + // structured (note the single 'value' section in the + // measureObservationDefinition clause). + const obsResults = patientResults != null + ? patientResults[obDef] + : undefined; + + obsResults.forEach((obsResult) => { + let resultValue = null; + const episodeId = obsResult.episode.id; + // Add the single result value to the values array on the results of + // this calculation (allowing for more than one possible observation). + if (obsResult != null ? Object.prototype.hasOwnProperty.call(obsResult, 'value') : undefined) { + // If result is a Cql.Quantity type, add its value + resultValue = obsResult.observation.value; + } else { + // In all other cases, add result + resultValue = obsResult.observation; + } + + // if the episodeResult object already exist create or add to to the values structure + if (episodeResults[episodeId] != null) { + if (episodeResults[episodeId].observation_values != null) { + episodeResults[episodeId].observation_values.push(resultValue); + } else { + episodeResults[episodeId].observation_values = [resultValue]; + } + // else create a new episodeResult structure + } else { + const newEpisode = {}; + for (const pc in populationSet.populations.toObject()) { + newEpisode[pc] = 0; + } + newEpisode.observation_values = [resultValue]; + episodeResults[episodeId] = newEpisode; + } + }); + }); + } + + // Correct any inconsistencies. ex. In DENEX but also in NUMER using same function used for patients. + Object.keys(episodeResults).forEach((episodeId) => { + const episodeResult = episodeResults[episodeId]; + // ensure that an empty 'observation_values' array exists for continuous variable measures if there were no observations + if (populationSet.observations.length > 0) { + if (!episodeResult.observation_values) { + episodeResult.observation_values = []; + } + } + // Correct any inconsistencies. ex. In DENEX but also in NUMER using same function used for patients. + episodeResults[episodeId] = this.handlePopulationValues(episodeResult, measureScoring); + }); + + return episodeResults; + } + + // Set all value set versions to 'undefined' so the execution engine does not grab the specified version in the ELM + static setValueSetVersionsToUndefined(elm) { + Array.from(elm).forEach((elmLibrary) => { + if (elmLibrary.library.valueSets != null) { + Array.from(elmLibrary.library.valueSets.def).forEach((valueSet) => { + if (valueSet.version != null) { + valueSet.version = undefined; + } + }); + } + }); + return elm; + } + + // Format ValueSets for use by the execution engine + static valueSetsForCodeService(valueSetsArray) { + const valueSets = {}; + valueSetsArray.forEach((valueSet) => { + if (valueSet.concepts) { + if (!valueSets[valueSet.oid]) { + valueSets[valueSet.oid] = {}; + } + valueSet.concepts.forEach((concept) => { + let version = valueSet.version; + if (version === 'N/A') { + version = ''; + } + if (!valueSets[valueSet.oid][version]) { + valueSets[valueSet.oid][version] = []; + } + valueSets[valueSet.oid][version].push({ + code: concept.code, + system: concept.code_system_oid, + version, + }); + }); + } + }); + return valueSets; + } + + // Create Date from UTC string date and time using momentJS + static parseTimeStringAsUTC(timeValue) { + return moment.utc(timeValue, 'YYYYMDDHHmm').toDate(); + } + + // Create Date from UTC string date and time using momentJS, shifting to 11:59:59 of the given year + static parseTimeStringAsUTCConvertingToEndOfYear(timeValue) { + return moment + .utc(timeValue, 'YYYYMDDHHmm') + .add(1, 'years') + .subtract(1, 'seconds') + .toDate(); + } + + // If the given value is in the given populationSet, and its result is zero, return true. + static isValueZero(value, populationSet) { + if (value in populationSet && populationSet[value] === 0) { + return true; + } + return false; + } + + static deepCopyPopulationSet(original) { + const copy = {}; + copy.title = original.title; + copy.observations = original.observations; + copy.populations = {}; + for (const popCode in original.populations.toObject()) { + // skip codes starting with _ since they are mongoose metadata + const copyPop = {}; + copyPop.library_name = original.populations[popCode].library_name; + copyPop.statement_name = original.populations[popCode].statement_name; + copy.populations[popCode] = copyPop; + } + return new CqmModels.PopulationSet(copy); + } + + static getStratificationsAsPopulationSets(measure) { + const stratificationsAsPopulationSets = []; + measure.population_sets.forEach((populationSet) => { + if (populationSet.stratifications) { + populationSet.stratifications.forEach((stratification) => { + const clonedSet = this.deepCopyPopulationSet(populationSet); + clonedSet.population_set_id = stratification.stratification_id; + clonedSet.populations.STRAT = stratification.statement; + stratificationsAsPopulationSets.push(clonedSet); + }); + } + }); + return stratificationsAsPopulationSets; + } + + // Returns a JSON function to add to the ELM before ELM JSON is used to calculate results for episode based measures + // This ELM template was generated by the CQL-to-ELM Translation Service. + static generateEpisodeELMJSONFunction(functionName, parameter, index) { + const elmFunction = { + name: `obs_func_${functionName}_${index}`, + context: 'Patient', + accessLevel: 'Public', + expression: { + type: 'Query', + source: [ + { + alias: 'MP', + expression: { + name: parameter, + type: 'ExpressionRef', + }, + }, + ], + relationship: [], + return: { + distinct: false, + expression: { + type: 'Tuple', + element: [ + { + name: 'episode', + value: { + name: 'MP', + type: 'AliasRef', + }, + }, + { + name: 'observation', + value: { + name: functionName, + type: 'FunctionRef', + operand: [ + { + asType: '{urn:hl7-org:elm-types:r1}Tuple', + type: 'As', + operand: { + name: 'MP', + type: 'AliasRef', + }, + }, + ], + }, + }, + ], + }, + }, + }, + }; + return elmFunction; + } + + // Returns a JSON function to add to the ELM before ELM JSON is used to calculate results for patient based measures + // This ELM template was generated by the CQL-to-ELM Translation Service. + static generatePatientELMJSONFunction(functionName, index) { + const elmFunction = { + name: `obs_func_${functionName}_${index}`, + context: 'Patient', + accessLevel: 'Public', + expression: { + name: functionName, + type: 'FunctionRef', + operand: [], + }, + }; + return elmFunction; + } +}; + +},{"cqm-models":192,"moment":218}],3:[function(require,module,exports){ +const _ = require('lodash'); + +/** + * Contains helpers that generate additional data for CQL measures. These functions provide extra data useful in working + * with calculation results. + */ +module.exports = class MeasureHelpers { + /** + * Builds a map of define statement name to the statement's text from a measure. + * @public + * @param {Measure} measure - The measure to build the map from. + * @return {Hash} Map of statement definitions to full statement + */ + static buildDefineToFullStatement(measure) { + const ret = {}; + for (const lib in measure.get('elm_annotations')) { + const libStatements = {}; + for (const statement of Array.from( + measure.get('elm_annotations')[lib].statements, + )) { + libStatements[statement.define_name] = this.parseAnnotationTree( + statement.children, + ); + } + ret[lib] = libStatements; + } + return ret; + } + + /** + * Recursive function that parses an annotation tree to extract text statements. + * @param {Node} children - the node to be traversed. + * @return {String} the text of the node or its children. + */ + static parseAnnotationTree(children) { + let child; + let ret = ''; + if (children.text !== undefined) { + return _.unescape(children.text).replace(' ', '').replace(';', ''); + } + if (children.children !== undefined) { + for (child of Array.from(children.children)) { + ret += this.parseAnnotationTree(child); + } + } else { + for (child of Array.from(children)) { + ret += this.cparseAnnotationTree(child); + } + } + return ret; + } + + /** + * Finds all localIds in a statement by it's library and statement name. + * @public + * @param {Measure} measure - The measure to find localIds in. + * @param {string} libraryName - The name of the library the statement belongs to. + * @param {string} statementName - The statement name to search for. + * @return {Hash} List of local ids in the statement. + */ + static findAllLocalIdsInStatementByName(libraryElm, statementName) { + // create place for aliases and their usages to be placed to be filled in later. Aliases and their usages (aka scope) + // and returns do not have localIds in the elm but do in elm_annotations at a consistent calculable offset. + // BE WEARY of this calaculable offset. + const emptyResultClauses = []; + const statement = libraryElm.library.statements.def.find( + (stat) => stat.name === statementName, + ); + const libraryName = libraryElm.library.identifier.id; + + const aliasMap = {}; + // recurse through the statement elm for find all localIds + const localIds = this.findAllLocalIdsInStatement( + statement, + libraryName, + statement.annotation, + {}, + aliasMap, + emptyResultClauses, + null, + ); + // Create/change the clause for all aliases and their usages + for (const alias of Array.from(emptyResultClauses)) { + // Only do it if we have a clause for where the result should be fetched from + // and have a localId for the clause that the result should map to + if ( + localIds[alias.expressionLocalId] != null + && alias.aliasLocalId != null + ) { + localIds[alias.aliasLocalId] = { + localId: alias.aliasLocalId, + sourceLocalId: alias.expressionLocalId, + }; + } + } + + // We do not yet support coverage/coloring of Function statements + // Mark all the clauses as unsupported so we can mark them 'NA' in the clause_results + if (statement.type === 'FunctionDef') { + for (const localId in localIds) { + const clause = localIds[localId]; + clause.isUnsupported = true; + } + } + return localIds; + } + + /** + * Finds all localIds in the statement structure recursively. + * @private + * @param {Object} statement - The statement structure or child parts of it. + * @param {String} libraryName - The name of the library we are looking at. + * @param {Array} annotation - The JSON annotation for the entire structure, this is occasionally needed. + * @param {Object} localIds - The hash of localIds we are filling. + * @param {Object} aliasMap - The map of aliases. + * @param {Array} emptyResultClauses - List of clauses that will have empty results from the engine. Each object on + * this has info on where to find the actual result. + * @param {Object} parentNode - The parent node, used for some special situations. + * @return {Array[Integer]} List of local ids in the statement. This is same array, localIds, that is passed in. + */ + static findAllLocalIdsInStatement( + statement, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + parentNode, + ) { + // Stop recursing if this node happens to be any TypeSpecifier. We do not want to collect localIds for these clauses + // as they are not executed and will negatively affect clause coverage if captured here. ChoiceTypeSpecifiers do not + // identify their type and instead put [] at the `type` attribute which is a deprecated field. + if ( + statement.type + && (Array.isArray(statement.type) + || statement.type.endsWith('TypeSpecifier')) + ) { + return localIds; + } + // looking at the key and value of everything on this object or array + for (const k in statement) { + let alId; + const v = statement[k]; + if (k === 'return') { + // Keep track of the localId of the expression that the return references. 'from's without a 'return' dont have + // localId's. So it doesn't make sense to mark them. + if (statement.return.expression.localId != null) { + aliasMap[v] = statement.return.expression.localId; + alId = statement.return.localId; + if (alId) { + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: aliasMap[v], + }); + } + } + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); + } else if (k === 'alias') { + if ( + statement.expression != null + && statement.expression.localId != null + ) { + // Keep track of the localId of the expression that the alias references + aliasMap[v] = statement.expression.localId; + // Determine the localId in the elm_annotation for this alias. + // Determine the localId for this alias. + if (statement.localId) { + // Older translator versions require with statements to use the statement.expression.localId + 1 as the alias Id + // even if the statement already has a localId. There is not a clear mapping for alias with statements in the new + // translator, so they will go un highlighted but this will not affect coverage calculation + if (statement.type === 'With' || statement.type === 'Without') { + alId = ( + parseInt(statement.expression.localId, 10) + 1 + ).toString(); + } else { + alId = statement.localId; + } + } else { + // Older translator versions created an elm_annotation localId that was not always in the elm. This was a + // single increment up from the expression that defines the alias. + alId = (parseInt(statement.expression.localId, 10) + 1).toString(); + } + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: aliasMap[v], + }); + } + } else if (k === 'scope') { + // The scope entry references an alias but does not have an ELM local ID. Hoever it DOES have an elm_annotations localId + // The elm_annotation localId of the alias variable is the localId of it's parent (one less than) + // because the result of the scope clause should be equal to the clause that the scope is referencing + alId = parseInt(statement.localId, 10) - 1; + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: aliasMap[v], + }); + } else if (k === 'asTypeSpecifier') { + // Map the localId of the asTypeSpecifier (Code, Quantity...) to the result of the result it is referencing + // For example, in the CQL code 'Variable.result as Code' the typeSpecifier does not produce a result, therefore + // we will set its result to whatever the result value is for 'Variable.result' + alId = statement.asTypeSpecifier.localId; + if (alId != null) { + const typeClauseId = parseInt(statement.asTypeSpecifier.localId, 10) - 1; + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: typeClauseId, + }); + } + } else if (k === 'sort') { + // Sort is a special case that we need to recurse into separately and set the results to the result of the statement the sort clause is in + this.findAllLocalIdsInSort( + v, + libraryName, + localIds, + aliasMap, + emptyResultClauses, + parentNode, + ); + } else if (k === 'let') { + // let is a special case where it is an array, one for each defined alias. These aliases work slightly different + // and execution engine does return results for them on use. The Initial naming of them needs to be properly pointed + // to what they are set to. + for (const aLet of Array.from(v)) { + // Add the localId for the definition of this let to it's source. + localIds[aLet.localId] = { + localId: aLet.localId, + sourceLocalId: aLet.expression.localId, + }; + this.findAllLocalIdsInStatement( + aLet.expression, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); + } + // handle the `when` pieces of Case expression aka CaseItems. They have a `when` key that should be mapped to get a result from the expression that defines them + } else if (k === 'when' && statement.localId && v.localId) { + localIds[statement.localId] = { + localId: statement.localId, + sourceLocalId: v.localId, + }; + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); + // If 'First' and 'Last' expressions, the result of source of the clause should be set to the expression + } else if (k === 'type' && (v === 'First' || v === 'Last')) { + if (statement.source && statement.source.localId != null) { + alId = statement.source.localId; + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: statement.localId, + }); + } + // Continue to recurse into the 'First' or 'Last' expression + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); + // If this is a FunctionRef or ExpressionRef and it references a library, find the clause for the library reference and add it. + } else if ( + k === 'type' + && (v === 'FunctionRef' || v === 'ExpressionRef') + && statement.libraryName != null + ) { + const libraryClauseLocalId = this.findLocalIdForLibraryRef( + annotation, + statement.localId, + statement.libraryName, + ); + if (libraryClauseLocalId !== null) { + // only add the clause if the localId for it is found + // the sourceLocalId is the FunctionRef itself to match how library statement references work. + localIds[libraryClauseLocalId] = { + localId: libraryClauseLocalId, + sourceLocalId: statement.localId, + }; + } + // handle the `when` pieces of Case expression aka CaseItems. + // They have a `when` key that should be mapped to get a result from the expression that defines them + } else if (k === 'type' && v === 'Null' && statement.localId) { + // If this is a "Null" expression, mark that it `isFalsyLiteral` so we can interpret final results differently. + localIds[statement.localId] = { + localId: statement.localId, + isFalsyLiteral: true, + }; + } else if ( + k === 'type' + && v === 'Literal' + && statement.localId + && statement.value === 'false' + ) { + // If this is a "Literal" expression whose value is false, mark that it `isFalsyLiteral` so we can interpret final results differently + localIds[statement.localId] = { + localId: statement.localId, + isFalsyLiteral: true, + }; + // else if the key is localId, push the value + } else if (k === 'localId') { + localIds[v] = { localId: v }; + // if the value is an array or object, recurse + } else if (Array.isArray(v) || typeof v === 'object') { + this.findAllLocalIdsInStatement( + v, + libraryName, + annotation, + localIds, + aliasMap, + emptyResultClauses, + statement, + ); + } + } + + return localIds; + } + + /** + * Finds all localIds in the sort structure recursively and sets the expressionLocalId to the parent statement. + * @private + * @param {Object} statement - The statement structure or child parts of it. + * @param {String} libraryName - The name of the library we are looking at. + * @param {Object} localIds - The hash of localIds we are filling. + * @param {Object} aliasMap - The map of aliases. + * @param {Array} emptyResultClauses - List of clauses that will have empty results from the engine. Each object on + * this has info on where to find the actual result. + * @param {Object} rootStatement - The rootStatement. + */ + static findAllLocalIdsInSort( + statement, + libraryName, + localIds, + aliasMap, + emptyResultClauses, + rootStatement, + ) { + const alId = statement.localId; + emptyResultClauses.push({ + lib: libraryName, + aliasLocalId: alId, + expressionLocalId: rootStatement.localId, + }); + return (() => { + const result = []; + for (const k in statement) { + const v = statement[k]; + if (Array.isArray(v) || typeof v === 'object') { + result.push( + this.findAllLocalIdsInSort( + v, + libraryName, + localIds, + aliasMap, + emptyResultClauses, + rootStatement, + ), + ); + } else { + result.push(undefined); + } + } + return result; + })(); + } + + /** + * Find the localId of the library reference in the JSON elm annotation. This recursively searches the annotation structure + * for the clause of the library ref. When that is found it knows where to look inside of that for where the library + * reference may be. + * + * Consider the following example of looking for function ref with id "55" and library "global". + * CQL for this is "global.CalendarAgeInYearsAt(...)". The following annotation snippet covers the call of the + * function. + * + * { + * "r": "55", + * "s": [ + * { + * "r": "49", + * "s": [ + * { + * "value": [ + * "global" + * ] + * } + * ] + * }, + * { + * "value": [ + * "." + * ] + * }, + * { + * "r": "55", + * "s": [ + * { + * "value": [ + * "\"CalendarAgeInYearsAt\"", + * "(" + * ] + * }, + * + * This method will recurse through the structure until it stops on this snippet that has "r": "55". Then it will check + * if the value of the first child is simply an array with a single string equaling "global". If that is indeed the + * case then it will return the "r" value of that first child, which is the clause localId for the library part of the + * function reference. If that is not the case, it will keep recursing and may eventually return null. + * + * @private + * @param {Object|Array} annotation - The annotation structure or child in the annotation structure. + * @param {String} refLocalId - The localId of the library ref we should look for. + * @param {String} libraryName - The library reference name, used to find the clause. + * @return {String} The localId of the clause for the library reference or null if not found. + */ + static findLocalIdForLibraryRef(annotation, refLocalId, libraryName) { + // if this is an object it should have an "r" for localId and "s" for children or leaf nodes + let child; + let ret; + if (Array.isArray(annotation)) { + for (child of Array.from(annotation)) { + // in the case of a list of children only return if there is a non null result + ret = this.findLocalIdForLibraryRef(child, refLocalId, libraryName); + if (ret !== null) { + return ret; + } + } + } else if (typeof annotation === 'object') { + // if we found the function ref + if (annotation.r != null && annotation.r === refLocalId) { + // check if the first child has the first leaf node with the library name + // refer to the method comment for why this is done. + if ( + this.__guard__( + annotation.s[0].s != null ? annotation.s[0].s[0].value : undefined, + (x) => x[0], + ) === libraryName + ) { + // return the localId if there is one + if (annotation.s[0].r != null) { + return annotation.s[0].r; + } + // otherwise return null because the library ref is in the same clause as extpression ref. + // this is common with expressionRefs for some reason. + return null; + } + } + + // if we made it here, we should travserse down the child nodes + if (Array.isArray(annotation.s)) { + for (child of Array.from(annotation.s)) { + // in the case of a list of children only return if there is a non null result + ret = this.findLocalIdForLibraryRef(child, refLocalId, libraryName); + if (ret !== null) { + return ret; + } + } + } else if (typeof annotation.s === 'object') { + return this.findLocalIdForLibraryRef( + annotation.s, + refLocalId, + libraryName, + ); + } + } + + // if nothing above caused this to return, then we are at a leaf node and should return null + return null; + } + + /** + * Figure out if a statement is a function given the measure, library name and statement name. + * @public + * @param {Measure} measure - The measure to find localIds in. + * @param {string} libraryName - The name of the library the statement belongs to. + * @param {string} statementName - The statement name to search for. + * @return {boolean} If the statement is a function or not. + */ + static isStatementFunction(library, statementName) { + // find the library and statement in the elm + const statement = library.elm.library.statements.def.find( + (def) => def.name === statementName, + ); + if (statement != null) { + return statement.type === 'FunctionDef'; + } + return false; + } + + /** + * Figure out if a statement is in a Supplemental Data Element given the statement name. + * @public + * @param [PopulationSet] populationSets + * @param {string} statement - The statement to search for. + * @return {boolean} Statement does or does not belong to a Supplemental Data Element. + */ + static isSupplementalDataElementStatement(populationSets, statementName) { + for (const populationSet of populationSets) { + if (populationSet.supplemental_data_elements) { + const sdeStatement = populationSet.supplemental_data_elements.find( + (sde) => sde.statement_name === statementName, + ); + if (sdeStatement !== undefined) { + return true; + } + } + } + return false; + } + + static __guard__(value, transform) { + return typeof value !== 'undefined' && value !== null + ? transform(value) + : undefined; + } +}; + +},{"lodash":216}],4:[function(require,module,exports){ +/* eslint-disable camelcase */ +const _ = require('lodash'); +const CqmModels = require('cqm-models'); + +const cql = CqmModels.CQL; + +const moment = require('moment'); +const MeasureHelpers = require('./measure_helpers'); + +/** + * Contains helpers that generate useful data for coverage and highlighing. These structures are added to the Result + * object in the CQLCalculator. + */ +module.exports = class ResultsHelpers { + /** + * Builds the `statement_relevance` map. This map gets added to the Result attributes that the calculator returns. + * + * The statement_relevance map indicates which define statements were actually relevant to a population inclusion + * consideration. This makes use of the 'population_relevance' map. This is actually a two level map. The top level is + * a map of the CQL libraries, keyed by library name. The second level is a map for statement relevance in that library, + * which maps each statement to its relevance status. The values in this map differ from the `population_relevance` + * because we also need to track statements that are not used for any population calculation. Therefore the values are + * a string that is one of the following: 'NA', 'TRUE', 'FALSE'. Here is what they mean: + * + * 'NA' - Not applicable. This statement is not relevant to any population calculation in this population_set. Common + * for unused library statements or statements only used for other population sets. + * + * 'FALSE' - This statement is not relevant to any of this patient's population inclusion calculations. + * + * 'TRUE' - This statement is relevant for one or more of the population inclusion calculations. + * + * Here is an example structure this function returns. (the `statement_relevance` map) + * { + * "Test158": { + * "Patient": "NA", + * "SDE Ethnicity": "NA", + * "SDE Payer": "NA", + * "SDE Race": "NA", + * "SDE Sex": "NA", + * "Most Recent Delivery": "TRUE", + * "Most Recent Delivery Overlaps Diagnosis": "TRUE", + * "Initial Population": "TRUE", + * "Numerator": "TRUE", + * "Denominator Exceptions": "FALSE" + * }, + * "TestLibrary": { + * "Numer Helper": "TRUE", + * "Denom Excp Helper": "FALSE", + * "Unused statement": "NA" + * } + * } + * + * This function relies heavily on the cql_statement_dependencies map on the Measure to recursively determine which + * statements are used in the relevant population statements. It also uses the 'population_relevance' map to determine + * the relevance of the population defining statement and its dependent statements. + * @public + * @param {object} populationRelevance - The `population_relevance` map, used at the starting point. + * @param {Measure} measure - The measure. + * @param {population} populationSet - The population set being calculated. + * @returns {object} The `statement_relevance` map that maps each statement to its relevance status for a calculation. + * This structure is put in the Result object's attributes. + */ + static buildStatementRelevanceMap(populationRelevance, measure, populationSet) { + // build map defaulting to not applicable (NA) using cql_statement_dependencies structure + const statementRelevance = {}; + for (const lib of measure.cql_libraries) { + statementRelevance[lib.library_name] = {}; + for (const statement of lib.statement_dependencies) { + statementRelevance[lib.library_name][statement.statement_name] = 'NA'; + } + } + + if (measure.calculate_sdes && populationSet.supplemental_data_elements) { + for (const statement of Array.from(populationSet.supplemental_data_elements)) { + // Mark all Supplemental Data Elements as relevant + this.markStatementRelevant( + measure.cql_libraries, + statementRelevance, + measure.main_cql_library, + statement.statement_name, + 'TRUE' + ); + } + } + + for (const population in populationRelevance) { + // If the population is values, that means we need to mark relevance for the OBSERVs + const relevance = populationRelevance[population]; + if (population === 'observation_values') { + for (const observation of Array.from(populationSet.observations)) { + this.markStatementRelevant( + measure.cql_libraries, + statementRelevance, + measure.main_cql_library, + observation.observation_function.statement_name, + relevance + ); + } + } else { + const relevantStatement = populationSet.populations[population].statement_name; + this.markStatementRelevant( + measure.cql_libraries, + statementRelevance, + measure.main_cql_library, + relevantStatement, + relevance + ); + } + } + + return statementRelevance; + } + + /** + * Recursive helper function for the _buildStatementRelevanceMap function. This marks a statement as relevant (or not + * relevant but applicable) in the `statement_relevance` map. It recurses and marks dependent statements also relevant + * unless they have already been marked as 'TRUE' for their relevance statue. This function will never be called on + * statements that are 'NA'. + * @private + * @param {Array} cqlLibraries - Dependency map from the measure object. The thing we recurse over + * even though it is flat, it represents a tree. + * @param {object} statementRelevance - The `statement_relevance` map to mark. + * @param {string} libraryName - The library name of the statement we are marking. + * @param {string} statementName - The name of the statement we are marking. + * @param {boolean} relevant - true if the statement should be marked 'TRUE', false if it should be marked 'FALSE'. + */ + static markStatementRelevant(cqlLibraries, statementRelevance, libraryName, statementName, relevant) { + // only mark the statement if it is currently 'NA' or 'FALSE'. Otherwise it already has been marked 'TRUE' + if ( + statementRelevance[libraryName][statementName] === 'NA' + || statementRelevance[libraryName][statementName] === 'FALSE' + ) { + statementRelevance[libraryName][statementName] = relevant ? 'TRUE' : 'FALSE'; + const library = cqlLibraries.find((lib) => lib.library_name === libraryName); + const statement = library.statement_dependencies.find((stat) => stat.statement_name === statementName); + if (!statement || !statement.statement_references) { + return []; + } + return statement.statement_references.map((dependentStatement) => this.markStatementRelevant( + cqlLibraries, + statementRelevance, + dependentStatement.library_name, + dependentStatement.statement_name, + relevant + )); + } + return []; + } + + /** + * Builds the result structures for the statements and the clauses. These are named `statement_results` and + * `clause_results` respectively when added Result object's attributes. + * + * The `statement_results` structure indicates the result for each statement taking into account the statement + * relevance in determining the result. This is a two level map just like `statement_relevance`. The first level key is + * the library name and the second key level is the statement name. The value is an object that has three properties, + * 'raw', 'final' and 'pretty'. 'raw' is the raw result from the execution engine for that statement. 'final' is the final + * result that takes into account the relevance in this calculation. 'pretty' is a human readable description of the result + * that is only generated if doPretty is true. + * The value of 'final' will be one of the following strings: + * 'NA', 'UNHIT', 'TRUE', 'FALSE'. + * + * Here's what they mean: + * + * 'NA' - Not applicable. This statement is not relevant to any population calculation in this population_set. Common + * for unused library statements or statements only used for other population sets. + * !!!IMPORTANT NOTE!!! All define function statements are marked 'NA' since we don't have a strategy for + * highlighting or coverage when it comes to functions. + * + * 'UNHIT' - This statement wasn't hit. This is most likely because the statement was not relevant to population + * calculation for this patient. i.e. 'FALSE' in the the `statement_relevance` map. + * + * 'TRUE' - This statement is relevant and has a truthy result. + * + * 'FALSE' - This statement is relevant and has a falsey result. + * + * Here is an example of the `statement_results` structure: (raw results have been turned into "???" for this example) + * { + * "Test158": { + * "Patient": { "raw": "???", "final": "NA", "pretty": "NA" }, + * "SDE Ethnicity": { "raw": "???", "final": "NA", "pretty": "NA" }, + * "SDE Payer": { "raw": "???", "final": "NA", "pretty": "NA" }, + * "SDE Race": { "raw": "???", "final": "NA", "pretty": "NA" }, + * "SDE Sex": { "raw": "???", "final": "NA", "pretty": "NA" }, + * "Most Recent Delivery": { "raw": "???", "final": "TRUE", "pretty": "???" }, + * "Most Recent Delivery Overlaps Diagnosis": { "raw": "???", "final": "TRUE", "pretty": "???" }, + * "Initial Population": { "raw": "???", "final": "TRUE", "pretty": "???" }, + * "Numerator": { "raw": "???", "final": "TRUE", "pretty": "???" }, + * "Denominator Exceptions": { "raw": "???", "final": "UNHIT", "pretty": "UNHIT" }, + * }, + * "TestLibrary": { + * "Numer Helper": { "raw": "???", "final": "TRUE", "pretty": "???" }, + * "Denom Excp Helper": { "raw": "???", "final": "UNHIT", "pretty": "UNHIT" }, + * "Unused statement": { "raw": "???", "final": "NA", "pretty": "???" }, + * "false statement": { "raw": "???", "final": "FALSE", "pretty": "FALSE: []" }, + * } + * } + * + * + * The `clause_results` structure is the same as the `statement_results` but it indicates the result for each clause. + * The second level key is the localId for the clause. The result object is the same with the same 'raw' and 'final' + * properties but it also includes the name of the statement it resides in as 'statementName'. + * + * This function relies very heavily on the `statement_relevance` map to determine the final results. This function + * returns the two structures together in an object ready to be added directly to the Result attributes. + * @public + * @param {Measure} measure - The measure. + * @param {object} rawClauseResults - The raw clause results from the calculation engine. + * @param {object} statementRelevance - The `statement_relevance` map. Used to determine if they were hit or not. + * @param {boolean} doPretty - If true, also generate pretty versions of result. + * @param {boolean} requestDocument - If true, returns result as IndividualResult Mongoose Document + * @returns {object} Object with the statement_results and clause_results structures, keyed as such. + */ + static buildStatementAndClauseResults(measure, rawClauseResults, statementRelevance, doPretty, includeClauseResults, requestDocument) { + if (doPretty == null) { + doPretty = false; + } + + let statementResults = {}; + let clauseResults = {}; + + if (requestDocument) { + statementResults = []; + clauseResults = []; + } + for (const library of measure.cql_libraries) { + const statements = library.statement_dependencies; + const library_name = library.library_name; // eslint-disable-line camelcase + if (!requestDocument) { + statementResults[library.library_name] = {}; + clauseResults[library.library_name] = {}; + } + for (const statement of statements) { + const statement_name = statement.statement_name; // eslint-disable-line camelcase + const relevance = statementRelevance[library_name][statement_name]; + let rawStatementResult = this.findResultForStatementClause(library, statement, rawClauseResults); + if (rawStatementResult && rawStatementResult.isMongooseArray) { + rawStatementResult = [].concat(rawStatementResult); + } + const statementResult = { + raw: rawStatementResult, library_name, statement_name, relevance, + }; + const isSDE = MeasureHelpers.isSupplementalDataElementStatement(measure.population_sets, statement_name); + if ((!measure.calculate_sdes && isSDE) || relevance === 'NA') { + statementResult.final = 'NA'; + if (doPretty) { + statementResult.pretty = 'NA'; + } + } else if (relevance === 'FALSE' || rawClauseResults[library_name] == null) { + statementResult.final = 'UNHIT'; + // even if the statement wasn't hit, we want the pretty result to just + // be FUNCTION for functions + if (doPretty) { + if (MeasureHelpers.isStatementFunction(library, statement_name)) { + statementResult.pretty = 'FUNCTION'; + } else { + statementResult.pretty = 'UNHIT'; + } + } + } else if (this.doesResultPass(rawStatementResult)) { + statementResult.final = 'TRUE'; + if (doPretty) { + statementResult.pretty = this.prettyResult(rawStatementResult); + } + } else { + statementResult.final = 'FALSE'; + if (rawStatementResult instanceof Array && rawStatementResult.length === 0) { + // Special case, handle empty array. + if (doPretty) { + statementResult.pretty = 'FALSE ([])'; + } + } else if (MeasureHelpers.isStatementFunction(library, statement_name)) { + if (doPretty) { + statementResult.pretty = 'FUNCTION'; + } + } else if (doPretty) { + statementResult.pretty = `FALSE (${rawStatementResult})`; + } + } + + if (requestDocument) { + const statementResultDocument = new CqmModels.StatementResult(statementResult); + statementResults.push(statementResultDocument); + } else { + statementResults[library_name][statement_name] = statementResult; + } + + if (includeClauseResults) { + // create clause results for all localIds in this statement + const localIds = MeasureHelpers.findAllLocalIdsInStatementByName(library.elm, statement_name); + for (const localId in localIds) { + const clause = localIds[localId]; + let rawClauseResult = rawClauseResults[library_name] != null + ? rawClauseResults[library_name][clause.sourceLocalId != null ? clause.sourceLocalId : localId] + : undefined; + if (rawClauseResult && rawClauseResult.isMongooseArray) { + rawClauseResult = [].concat(rawClauseResult); + } + const clauseResult = { + // if this clause is an alias or a usage of alias it will get the raw result from the sourceLocalId. + raw: rawClauseResult, + statement_name, + library_name, + localId, + }; + + clauseResult.final = this.setFinalResults({ + statementRelevance, + statement_name, + rawClauseResults, + library_name, + localId, + clause, + rawResult: clauseResult.raw, + }); + if (requestDocument) { + const clauseResultDocument = new CqmModels.ClauseResult(clauseResult); + clauseResults.push(clauseResultDocument); + } else { + clauseResults[library_name][localId] = clauseResult; + } + } + } + } + } + const response = { statement_results: statementResults }; + response.clause_results = includeClauseResults ? clauseResults : null; + return response; + } + + /** + * Generates a pretty human readable representation of a result. + * + * @param {(Array|object|boolean|???)} result - The result from the calculation engine. + * @param {Integer} indentLevel - For nested objects, the indentLevel indicates how far to indent. + * Note that 1 is the base because Array(1).join ' ' returns ''. + * @returns {String} a pretty version of the given result + */ + static prettyResult(result, indentLevel, keyIndent) { + const nameOidHash = { + '2.16.840.1.113883.6.96': 'SNOMEDCT', + '2.16.840.1.113883.6.1': 'LOINC', + '2.16.840.1.113883.6.238': 'CDCREC', + '2.16.840.1.113883.6.14': 'HCP', + '2.16.840.1.113883.6.285': 'HCPCS', + '2.16.840.1.113883.6.103': 'ICD-9-CM', + '2.16.840.1.113883.6.104': 'ICD-9-PCS', + '2.16.840.1.113883.6.90': 'ICD-10-CM', + '2.16.840.1.113883.6.4': 'ICD-10-PCS', + '2.16.840.1.113883.6.88': 'RxNorm', + '2.16.840.1.113883.3.221.5': 'Source of Payment Typology', + '2.16.840.1.113883.6.12': 'CPT', + '2.16.840.1.113883.5.1': 'AdministrativeGender', + '2.16.840.1.113883.4.642.3.921': 'HL7 Relationship Code', + '2.16.840.1.113883.5.2': 'HL7 Marital Status', + '2.16.840.1.113883.12.292': 'CVX', + '2.16.840.1.113883.5.83': 'HITSP C80 Observation Status', + '2.16.840.1.113883.3.26.1.1': 'NCI Thesaurus', + '2.16.840.1.113883.3.88.12.80.20': 'FDA', + '2.16.840.1.113883.4.9': 'UNII', + '2.16.840.1.113883.6.69': 'NDC', + '2.16.840.1.113883.5.14': 'HL7 ActStatus', + '2.16.840.1.113883.6.259': 'HL7 Healthcare Service Location', + '2.16.840.1.113883.12.112': 'DischargeDisposition', + '2.16.840.1.113883.5.4': 'HL7 Act Code', + '2.16.840.1.113883.6.177': 'NLM MeSH', + '2.16.840.1.113883.5.1076': 'Religious Affiliation', + '2.16.840.1.113883.1.11.19717': 'HL7 ActNoImmunicationReason', + '2.16.840.1.113883.3.88.12.80.33': 'NUBC', + '2.16.840.1.113883.1.11.78': 'HL7 Observation Interpretation', + '2.16.840.1.113883.6.13': 'CDT', + '2.16.840.1.113883.18.2': 'AdministrativeSex', + }; + let prettyResult; + if (indentLevel == null) { + indentLevel = 1; + } + if (keyIndent == null) { + keyIndent = 1; + } + const keyIndentation = Array(keyIndent).join(' '); + const currentIndentation = Array(indentLevel).join(' '); + if (result instanceof cql.DateTime) { + return moment.utc(result.toString()).format('MM/DD/YYYY h:mm A'); + } if (result instanceof cql.Interval) { + return `INTERVAL: ${this.prettyResult(result.low)} - ${this.prettyResult(result.high)}`; + } if (result instanceof cql.Code) { + return `CODE: ${nameOidHash[result.system] || result.system} ${result.code}`; + } if (result instanceof cql.Quantity) { + let quantityResult = `QUANTITY: ${result.value}`; + if (result.unit) { + quantityResult += ` ${result.unit}`; + } + return quantityResult; + } if (result && typeof result._type === 'string' && result._type.includes('QDM::')) { + // If there isn't a description, use the type name as a fallback. This mirrors the frontend where we do + // result.constructor.name. + const description = result.description ? `${result.description}\n` : `${result._type.replace('QDM::', '')}\n`; + let startDateTime = null; + let endDateTime = null; + let startTimeString = ''; + let endTimeString = ''; + // Start time of data element is start of relevant period, if data element does not have relevant period, use authorDatetime + if (result.relevantPeriod) { + if (result.relevantPeriod.low) { + startDateTime = result.relevantPeriod.low; + } + if (result.relevantPeriod.high) { + endDateTime = result.relevantPeriod.high; + } + } else if (result.prevalencePeriod) { + if (result.prevalencePeriod.low) { + startDateTime = result.prevalencePeriod.low; + } + if (result.prevalencePeriod.high) { + endDateTime = result.prevalencePeriod.high; + } + } else if (result.authorDatetime) { + // TODO: start result string will need to be updated to AUTHORED once bonnie frontend + // updates its pretty printer to do so. + startDateTime = result.authorDatetime; + } + + if (startDateTime) { + startTimeString = `START: ${moment.utc(startDateTime.toString()).format('MM/DD/YYYY h:mm A')}\n`; + } + // If endTime is the infinity dateTime, clear it out because we do not want to export it + if (endDateTime && endDateTime.year !== 9999) { + endTimeString = `STOP: ${moment.utc(endDateTime.toString()).format('MM/DD/YYYY h:mm A')}\n`; + } + const system = result.dataElementCodes[0].system; + const codeDisplay = result.dataElementCodes && result.dataElementCodes[0] ? `CODE: ${nameOidHash[system] || system} ${result.dataElementCodes[0].code}` : ''; + // Add indentation + const returnString = `${description}${startTimeString}${endTimeString}${codeDisplay}`; + return returnString.replace(/\n/g, `\n${currentIndentation}${keyIndentation}`); + } if (result instanceof String || typeof result === 'string') { + return `"${result}"`; + } if (result instanceof Array) { + prettyResult = _.map(result, (value) => this.prettyResult(value, indentLevel, keyIndent)); + return `[${prettyResult.join(`,\n${currentIndentation}${keyIndentation}`)}]`; + } if (result instanceof Object) { + // if the object has it's own custom toString method, use that instead + if (typeof result.toString === 'function' && result.toString !== Object.prototype.toString) { + return result.toString(); + } + prettyResult = '{\n'; + const baseIndentation = Array(3).join(' '); + const sortedKeys = Object.keys(result).sort().filter((key) => key !== '_type' && key !== 'qdmVersion'); + for (const key of sortedKeys) { + // add 2 spaces per indent + const value = result[key]; + const nextIndentLevel = indentLevel + 2; + // key length + ': ' + keyIndent = key.length + 3; + prettyResult = prettyResult.concat(`${baseIndentation}${currentIndentation}${key}: ${this.prettyResult(value, nextIndentLevel, keyIndent)}`); + // append commas if it isn't the last key + if (key === sortedKeys[sortedKeys.length - 1]) { + prettyResult += '\n'; + } else { + prettyResult += ',\n'; + } + } + prettyResult += `${currentIndentation}}`; + return prettyResult; + } + if (result || result === 0) { + return JSON.stringify(result, null, 2); + } + return 'null'; + } + + /** + * Determines the final result (for coloring and coverage) for a clause. The result fills the 'final' property for the + * clause result. Look at the comments for buildStatementAndClauseResults to get a description of what each of the + * string results of this function are. + * @private + * @param {object} rawClauseResults - The raw clause results from the calculation engine. + * @param {object} statementRelevance - The statement relevance map. + * @param {object} statement_name - The name of the statement the clause is in + * @param {object} library_name - The name of the libarary the clause is in + * @param {object} localId - The localId of the current clause + * @param {object} clause - The clause we are getting the final result of + * @param {Array|Object|Interval|??} rawResult - The raw result from the calculation engine. + * @returns {string} The final result for the clause. + */ + static setFinalResults(params) { + let finalResult = 'FALSE'; + if (params.clause.isUnsupported != null) { + finalResult = 'NA'; + } else if (params.statementRelevance[params.library_name][params.statement_name] === 'NA') { + finalResult = 'NA'; + } else if ( + params.statementRelevance[params.library_name][params.statement_name] === 'FALSE' + || params.rawClauseResults[params.library_name] == null + ) { + finalResult = 'UNHIT'; + } else if ( + params.clause.isFalsyLiteral + && Object.prototype.hasOwnProperty.call(params.rawClauseResults[params.library_name], params.clause.localId) + ) { + // If this clause is a Null or Literal False we need to look for the existence of a result for the localId in the + // rawClauseResults instead. If the key for the localId exists then it was executed, and we will want to treat the + // `final` result as `TRUE` instead of `FALSE`. If the key is totally absent then it was not executed therefore + // `final` result should stay `FALSE`. + finalResult = 'TRUE'; + } else if (this.doesResultPass(params.rawResult)) { + finalResult = 'TRUE'; + } + return finalResult; + } + + /** + * Finds the clause localId for a statement and gets the raw result for it from the raw clause results. + * @private + * @param {string} library - The library + * @param {string} statement - The statement + * @param {object} rawClauseResults - The raw clause results from the engine. + * @returns {(Array|object|Interval|??)} The raw result from the calculation engine for the given statement. + */ + static findResultForStatementClause(library, statement, rawClauseResults) { + const elmStatement = library.elm.library.statements.def.find((def) => def.name === statement.statement_name); + const libraryName = library.library_name; + return rawClauseResults[libraryName] != null ? rawClauseResults[libraryName][elmStatement.localId] : undefined; + } + + /** + * Determines if a result (for a statement or clause) from the execution engine is a pass or fail. + * @private + * @param {(Array|object|boolean|???)} result - The result from the calculation engine. + * @returns {boolean} true or false + */ + static doesResultPass(result) { + if (result === true) { + // Specifically a boolean true + return true; + } if (result === false) { + // Specifically a boolean false + return false; + } if (Array.isArray(result)) { + // Check if result is an array + if (result.length === 0) { + // Result is true if the array is not empty + return false; + } if (result.length === 1 && result[0] === null) { + // But if the array has one element that is null. Then we should make it red. + return false; + } + return true; + } if (result instanceof cql.Interval) { + // make it green if and Interval is returned + return true; + // Return false if an empty cql.Code is the result + } if (result instanceof cql.Code && result.code == null) { + return false; + } if (result === null || result === undefined) { + // Specifically no result + return false; + } + return true; + } + + /* + * Iterate over episode results, call _buildPopulationRelevanceMap for each result + * OR population relevances together so that populations are marked as relevant + * based on all episodes instead of just one + * @private + * @param {episodeResults} result - Population_results for each episode + * @param {String} measureScoring - can be PROPORTION RATIO CONTINUOUS_VARIABLE COHORT. + * @returns {object} Map that tells if a population calculation was considered/relevant in any episode + */ + static populationRelevanceForAllEpisodes(episodeResults, measureScoring) { + const masterRelevanceMap = {}; + for (const key in episodeResults) { + const episodeResult = episodeResults[key]; + const popRelMap = this.buildPopulationRelevanceMap(episodeResult, measureScoring); + for (const pop in popRelMap) { + const popRel = popRelMap[pop]; + if (masterRelevanceMap[pop] == null) { + masterRelevanceMap[pop] = false; + } + masterRelevanceMap[pop] = masterRelevanceMap[pop] || popRel; + } + } + return masterRelevanceMap; + } + + /** + * Builds the `population_relevance` map. This map gets added to the Result attributes that the calculator returns. + * + * The population_relevance map indicates which populations the patient was actually considered for inclusion in. It + * is a simple map of "POPNAME" to true or false. true if the population was relevant/considered, false if + * NOT relevant/considered. This is used later to determine which define statements are relevant in the calculation. + * + * For example: If they aren't in the IPP then they are not going to be considered for any other population and all other + * populations will be marked NOT relevant. + * + * Below is an example result of this function (the 'population_relevance' map). DENEXCEP is not relevant because in + * the population_results the NUMER was greater than zero: + * { + * "IPP": true, + * "DENOM": true, + * "NUMER": true, + * "DENEXCEP": false + * } + * + * This function is extremely verbose because this is an important and confusing calculation to make. The verbosity + * was kept to make it more maintainable. + * @private + * @param {Result} result - The `population_results` object. + * @param {String} measureScoring - can be PROPORTION RATIO CONTINUOUS_VARIABLE COHORT. + * @returns {object} Map that tells if a population calculation was considered/relevant. + */ + static buildPopulationRelevanceMap(result, measureScoring) { + // initialize to true for every population + const resultShown = {}; + _.each(Object.keys(result), (population) => { resultShown[population] = true; }); + + // If STRAT is 0 then everything else is not calculated + if (result.STRAT != null && result.STRAT === 0) { + if (resultShown.IPP != null) { + resultShown.IPP = false; + } + if (resultShown.NUMER != null) { + resultShown.NUMER = false; + } + if (resultShown.NUMEX != null) { + resultShown.NUMEX = false; + } + if (resultShown.DENOM != null) { + resultShown.DENOM = false; + } + if (resultShown.DENEX != null) { + resultShown.DENEX = false; + } + if (resultShown.DENEXCEP != null) { + resultShown.DENEXCEP = false; + } + if (resultShown.MSRPOPL != null) { + resultShown.MSRPOPL = false; + } + if (resultShown.MSRPOPLEX != null) { + resultShown.MSRPOPLEX = false; + } + if (resultShown.observation_values != null) { + resultShown.observation_values = false; + } + } + + // If IPP is 0 then everything else is not calculated + if (result.IPP === 0) { + if (resultShown.NUMER != null) { + resultShown.NUMER = false; + } + if (resultShown.NUMEX != null) { + resultShown.NUMEX = false; + } + if (resultShown.DENOM != null) { + resultShown.DENOM = false; + } + if (resultShown.DENEX != null) { + resultShown.DENEX = false; + } + if (resultShown.DENEXCEP != null) { + resultShown.DENEXCEP = false; + } + if (resultShown.MSRPOPL != null) { + resultShown.MSRPOPL = false; + } + if (resultShown.MSRPOPLEX != null) { + resultShown.MSRPOPLEX = false; + } + // values is the OBSERVs + if (resultShown.observation_values != null) { + resultShown.observation_values = false; + } + } + + // If DENOM is 0 then DENEX, DENEXCEP, NUMER and NUMEX are not calculated + if (result.DENOM != null && result.DENOM === 0) { + // Skip for RATIO measures. NUMER inclusion is not dependent on DENOM for RATIO measures. + if (measureScoring !== 'RATIO') { + if (resultShown.NUMER != null) { + resultShown.NUMER = false; + } + if (resultShown.NUMEX != null) { + resultShown.NUMEX = false; + } + } + if (resultShown.DENEX != null) { + resultShown.DENEX = false; + } + if (resultShown.DENEXCEP != null) { + resultShown.DENEXCEP = false; + } + } + + // If DENEX is greater than or equal to DENOM then NUMER, NUMEX and DENEXCEP not calculated + if (result.DENEX != null && result.DENEX >= result.DENOM) { + // Skip for RATIO measures. NUMER inclusion is not dependent on DENOM for RATIO measures. + if (measureScoring !== 'RATIO') { + if (resultShown.NUMER != null) { + resultShown.NUMER = false; + } + if (resultShown.NUMEX != null) { + resultShown.NUMEX = false; + } + } + if (resultShown.DENEXCEP != null) { + resultShown.DENEXCEP = false; + } + } + + // If NUMER is 0 then NUMEX is not calculated + if (result.NUMER != null && result.NUMER === 0) { + if (resultShown.NUMEX != null) { + resultShown.NUMEX = false; + } + } + + // If NUMER is 1 then DENEXCEP is not calculated + if (result.NUMER != null && result.NUMER >= 1) { + if (resultShown.DENEXCEP != null) { + resultShown.DENEXCEP = false; + } + } + + // If MSRPOPL is 0 then OBSERVs and MSRPOPLEX are not calculateed + if (result.MSRPOPL != null && result.MSRPOPL === 0) { + // values is the OBSERVs + if (resultShown.observation_values != null) { + resultShown.observation_values = false; + } + if (resultShown.MSRPOPLEX) { + resultShown.MSRPOPLEX = false; + } + } + + // If MSRPOPLEX is greater than or equal to MSRPOPL then OBSERVs are not calculated + if (result.MSRPOPLEX != null && result.MSRPOPLEX >= result.MSRPOPL) { + if (resultShown.observation_values != null) { + resultShown.observation_values = false; + } + } + + return resultShown; + } +}; + +},{"./measure_helpers":3,"cqm-models":192,"lodash":216,"moment":218}],5:[function(require,module,exports){ +module.exports.CalculatorHelpers = require('./helpers/calculator_helpers'); +module.exports.MeasureHelpers = require('./helpers/measure_helpers'); +module.exports.ResultsHelpers = require('./helpers/results_helpers'); +module.exports.PatientSource = require('./models/patient_source'); +module.exports.Calculator = require('./models/calculator'); + +},{"./helpers/calculator_helpers":2,"./helpers/measure_helpers":3,"./helpers/results_helpers":4,"./models/calculator":6,"./models/patient_source":7}],6:[function(require,module,exports){ +/** + * The CQL calculator. This calls the cql-execution framework and formats the results as neccesary. + */ +const _ = require('lodash'); +const CqmModels = require('cqm-models'); + +const cql = CqmModels.CQL; +const QDMPatient = CqmModels.QDMPatient; +const Measure = CqmModels.Measure; +const ResultsHelpers = require('../helpers/results_helpers'); +const CalculatorHelpers = require('../helpers/calculator_helpers'); +const PatientSource = require('./patient_source'); + +module.exports = class Calculator { + /** + * Generate calculation results for patients against a CQL measure. + * + * @param {Object|Measure} measure - The measure population to calculate on. + * @param {Array} patients - The array of QDM patients to run calcs on. + * @param {Array} valueSets - all ValueSets relevant to the measure. + * @param {Hash} options - contains options for measure calculation. + * @returns {patientId, results} results - mapping from patient to calculation results for each patient. + */ + static calculate( + measure, + patients, + valueSets, + // default values for the passed in options object + { + includeClauseResults = false, // whether or not to include the individual clause results (note - these can be large for some measures e.g. opioids) + doPretty = false, // whether or not to include the .pretty attribute + timezoneOffset = 0, + effectiveDate = null, // will default to measure.measure_period.low.value if not included + effectiveDateEnd = null, // will default to measure.measure_period.high.value if this and effective_date not provided, if effective_date is provided will default to end of the effective_date year + executionDate = null, // will default to today + requestDocument = false, // If true, returns results as IndividualResult Mongoose Documents + } = {} + ) { + // We store both the calculation result and the calculation code based on keys derived from the arguments + const resultsByPatient = {}; + + // check if patients are cqm-model documents or not + const qdmPatients = patients.map((patient) => { + if (patient.validateSync !== undefined) { + return patient; // return the patient if it needs no conversion + } + return new QDMPatient(patient); + }); + const patientSource = new PatientSource(qdmPatients); + + // convert the measure to a document if needed + if (measure.validateSync === undefined) { + measure = new Measure(measure); + } + + // Grab start and end of Measurement Period + let start; + let end; + // Override default measure_period with effective_date if available + if (effectiveDate != null) { + start = CalculatorHelpers.parseTimeStringAsUTC(effectiveDate); + if (effectiveDateEnd != null) { + end = CalculatorHelpers.parseTimeStringAsUTC(effectiveDateEnd); + } else { + end = CalculatorHelpers.parseTimeStringAsUTCConvertingToEndOfYear(effectiveDate); + } + } else { + start = CalculatorHelpers.parseTimeStringAsUTC(measure.measure_period.low.value); + end = CalculatorHelpers.parseTimeStringAsUTC(measure.measure_period.high.value); + } + + const startCql = cql.DateTime.fromJSDate(start, 0); // No timezone offset for start + const endCql = cql.DateTime.fromJSDate(end, 0); // No timezone offset for stop + + // Construct CQL params + const params = { 'Measurement Period': new cql.Interval(startCql, endCql) }; + + // Set execution_date if available else default to new Date() + const executionDateOption = (executionDate != null) ? CalculatorHelpers.parseTimeStringAsUTC(effectiveDate) : new Date(); + + // Create the execution DateTime that we pass into the engine + const executionDateTime = cql.DateTime.fromJSDate(executionDateOption, timezoneOffset); + + // Grab ELM JSON from measure, use clone so that the function added from observations does not get added over and over again + // Set all value set versions to 'undefined' so the execution engine does not grab the specified version in the ELM + const allElm = CalculatorHelpers.setValueSetVersionsToUndefined(_.clone(measure.cql_libraries.map((lib) => lib.elm))); + // Find the main library (the library that is the "measure") + const mainLibraryElm = allElm.find((elm) => elm.library.identifier.id === measure.main_cql_library); + + const observations = measure.population_sets[0].observations; + const observationDefs = []; + let generatedELMJSON; + if (observations) { + observations.forEach((obs, index) => { + const funcName = obs.observation_function.statement_name; + const param = obs.observation_parameter.statement_name; + generatedELMJSON = (measure.calculation_method === 'PATIENT' && !measure.composite) + ? CalculatorHelpers.generatePatientELMJSONFunction(funcName, index) + : CalculatorHelpers.generateEpisodeELMJSONFunction(funcName, param, index); + // Save the name of the generated define statement, so we can check + // its result later in the CQL calculation process. These added + // define statements are called 'obs_func_' followed by the + // name of the function - see the 'generateELMJSONFunction' function. + observationDefs.push(`obs_func_${funcName}_${index}`); + // Add the generated elm representing the observation function into the elm + mainLibraryElm.library.statements.def.push(generatedELMJSON); + }); + } + + // Grab the correct version of value sets to pass into the execution engine. + const measureValueSets = CalculatorHelpers.valueSetsForCodeService(valueSets); + + // Clear cache of execution friendly dataElements + CqmModels.QDMPatientSchema.clearDataElementCache(); + + // Calculate results for each CQL statement + const resultsRaw = Calculator.executeEngine( + allElm, + patientSource, + measureValueSets, + measure.main_cql_library, + mainLibraryElm.library.identifier.version, + executionDateTime, + params + ); + + const stratificationPopulations = CalculatorHelpers.getStratificationsAsPopulationSets(measure); + const populationSetsWithStratifcations = measure.population_sets.concat(stratificationPopulations); + + Object.keys(resultsRaw.patientResults).forEach((patientId) => { + let populationResults; + let episodeResults; + let populationRelevance; + const patientResults = resultsRaw.patientResults[patientId]; + // Parse CQL statement results into population values + populationSetsWithStratifcations.forEach((populationSet) => { + [populationResults, episodeResults] = Array.from(CalculatorHelpers.createPopulationValues( + measure, + populationSet, + patientResults, + observationDefs + )); + if (populationResults) { + let result = {}; // new CqmModels.IndividualResult(); + Object.assign(result, populationResults); + // result.set(populationResults); + if (episodeResults != null) { + result.episode_results = episodeResults; + if (Object.keys(episodeResults).length > 0) { + /* In episode of care based measures, episode_results contains the population results + * for EACH episode, so we need to build population_relevance based on a combonation + * of the episode_results. IE: If DENEX is irrelevant for one episode but relevant for + * another, the logic view should not highlight it as irrelevant + */ + populationRelevance = ResultsHelpers.populationRelevanceForAllEpisodes(episodeResults, measure.measure_scoring); + } else { + // Use the patient based relevance if there are no episodes. This will properly set IPP or STRAT to true. + populationRelevance = ResultsHelpers.buildPopulationRelevanceMap(populationResults, measure.measure_scoring); + } + } else { + // Calculate relevance for patient based measure + populationRelevance = ResultsHelpers.buildPopulationRelevanceMap(populationResults, measure.measure_scoring); + } + + const statementRelevance = ResultsHelpers.buildStatementRelevanceMap( + populationRelevance, + measure, + populationSet + ); + + if (!requestDocument) { + // If not in a Mongoose Document, put statement relevance at top level so that it does not have + // to be parsed out of result hashes which can be time consuming for larger measures + result.statement_relevance = statementRelevance; + } + result.population_relevance = populationRelevance; + + const statementAndClauseResults = ResultsHelpers.buildStatementAndClauseResults( + measure, + resultsRaw.localIdPatientResultsMap[patientId], + statementRelevance, + doPretty, + includeClauseResults, + requestDocument + ); + + Object.assign(result, statementAndClauseResults); + + // Populate result with info + result.patient = patientId; + result.measure = measure._id; + result.state = 'complete'; + + // Add result of population set, hashed by population set id + if (!resultsByPatient[patientId]) { + resultsByPatient[patientId] = {}; + } + + if (requestDocument) { + result = new CqmModels.IndividualResult(result); + } + + resultsByPatient[patientId][populationSet.population_set_id] = result; + } + }); + }); + return resultsByPatient; + } + + /** + * Call into the CQL execution engine for raw results. + */ + static executeEngine(elm, patientSource, valueSets, libraryName, version, executionDateTime, parameters = {}) { + let lib; + let rep; + if (Array.isArray(elm)) { + if (elm.length > 1) { + rep = new cql.Repository(elm); + lib = rep.resolve(libraryName, version); + } else { + lib = new cql.Library(elm[0]); + } + } else { + lib = new cql.Library(elm); + } + const codeService = new cql.CodeService(valueSets); + const executor = new cql.Executor(lib, codeService, parameters); + return executor.exec(patientSource, executionDateTime); + } +}; + +},{"../helpers/calculator_helpers":2,"../helpers/results_helpers":4,"./patient_source":7,"cqm-models":192,"lodash":216}],7:[function(require,module,exports){ +// This is a wrapper class for an array of QDM Patients +// This class adds functions used by the execution engine to +// traverse an array of QDM Patients +module.exports = class PatientSource { + constructor(patients) { + this.patients = patients; + this.index = 0; + } + + currentPatient() { + if (this.index < this.patients.length) { + return this.patients[this.index]; + } + return null; + } + + nextPatient() { + this.index += 1; + } +}; + +},{}],8:[function(require,module,exports){ +module.exports={"license":"The following data (prefixes and units) was generated by the UCUM LHC code from the UCUM data and selected LOINC combinations of UCUM units. The license for the UCUM LHC code (demo and library code as well as the combined units) is located at https://github.com/lhncbc/ucum-lhc/blob/LICENSE.md.","prefixes":{"config":["code_","ciCode_","name_","printSymbol_","value_","exp_"],"data":[["E","EX","exa","E",1000000000000000000,"18"],["G","GA","giga","G",1000000000,"9"],["Gi","GIB","gibi","Gi",1073741824,null],["Ki","KIB","kibi","Ki",1024,null],["M","MA","mega","M",1000000,"6"],["Mi","MIB","mebi","Mi",1048576,null],["P","PT","peta","P",1000000000000000,"15"],["T","TR","tera","T",1000000000000,"12"],["Ti","TIB","tebi","Ti",1099511627776,null],["Y","YA","yotta","Y",1e+24,"24"],["Z","ZA","zetta","Z",1e+21,"21"],["a","A","atto","a",1e-18,"-18"],["c","C","centi","c",0.01,"-2"],["d","D","deci","d",0.1,"-1"],["da","DA","deka","da",10,"1"],["f","F","femto","f",1e-15,"-15"],["h","H","hecto","h",100,"2"],["k","K","kilo","k",1000,"3"],["m","M","milli","m",0.001,"-3"],["n","N","nano","n",1e-9,"-9"],["p","P","pico","p",1e-12,"-12"],["u","U","micro","μ",0.000001,"-6"],["y","YO","yocto","y",1.0000000000000001e-24,"-24"],["z","ZO","zepto","z",1e-21,"-21"]]},"units":{"config":["isBase_","name_","csCode_","ciCode_","property_","magnitude_",["dim_","dimVec_"],"printSymbol_","class_","isMetric_","variable_","cnv_","cnvPfx_","isSpecial_","isArbitrary_","moleExp_","synonyms_","source_","loincProperty_","category_","guidance_","csUnitString_","ciUnitString_","baseFactorStr_","baseFactor_","defError_"],"data":[[true,"meter","m","M","length",1,[1,0,0,0,0,0,0],"m",null,false,"L",null,1,false,false,0,"meters; metres; distance","UCUM","Len","Clinical","unit of length = 1.09361 yards",null,null,null,null,false],[true,"second - time","s","S","time",1,[0,1,0,0,0,0,0],"s",null,false,"T",null,1,false,false,0,"seconds","UCUM","Time","Clinical","",null,null,null,null,false],[true,"gram","g","G","mass",1,[0,0,1,0,0,0,0],"g",null,false,"M",null,1,false,false,0,"grams; gm","UCUM","Mass","Clinical","",null,null,null,null,false],[true,"radian","rad","RAD","plane angle",1,[0,0,0,1,0,0,0],"rad",null,false,"A",null,1,false,false,0,"radians","UCUM","Angle","Clinical","unit of angular measure where 1 radian = 1/2π turn = 57.296 degrees. ",null,null,null,null,false],[true,"degree Kelvin","K","K","temperature",1,[0,0,0,0,1,0,0],"K",null,false,"C",null,1,false,false,0,"Kelvin; degrees","UCUM","Temp","Clinical","absolute, thermodynamic temperature scale ",null,null,null,null,false],[true,"coulomb","C","C","electric charge",1,[0,0,0,0,0,1,0],"C",null,false,"Q",null,1,false,false,0,"coulombs","UCUM","","Clinical","defined as amount of 1 electron charge = 6.2415093×10^18 e, and equivalent to 1 Ampere-second",null,null,null,null,false],[true,"candela","cd","CD","luminous intensity",1,[0,0,0,0,0,0,1],"cd",null,false,"F",null,1,false,false,0,"candelas","UCUM","","Clinical","SI base unit of luminous intensity",null,null,null,null,false],[false,"the number ten for arbitrary powers","10*","10*","number",10,[0,0,0,0,0,0,0],"10","dimless",false,null,null,1,false,false,0,"10^; 10 to the arbitrary powers","UCUM","Num","Clinical","10* by itself is the same as 10, but users can add digits after the *. For example, 10*3 = 1000.","1","1","10",10,false],[false,"the number ten for arbitrary powers","10^","10^","number",10,[0,0,0,0,0,0,0],"10","dimless",false,null,null,1,false,false,0,"10*; 10 to the arbitrary power","UCUM","Num","Clinical","10* by itself is the same as 10, but users can add digits after the *. For example, 10*3 = 1000.","1","1","10",10,false],[false,"the number pi","[pi]","[PI]","number",3.141592653589793,[0,0,0,0,0,0,0],"π","dimless",false,null,null,1,false,false,0,"π","UCUM","","Constant","a mathematical constant; the ratio of a circle's circumference to its diameter ≈ 3.14159","1","1","3.1415926535897932384626433832795028841971693993751058209749445923",3.141592653589793,false],[false,"","%","%","fraction",0.01,[0,0,0,0,0,0,0],"%","dimless",false,null,null,1,false,false,0,"percents","UCUM","FR; NFR; MFR; CFR; SFR Rto; etc. ","Clinical","","10*-2","10*-2","1",1,false],[false,"parts per thousand","[ppth]","[PPTH]","fraction",0.001,[0,0,0,0,0,0,0],"ppth","dimless",false,null,null,1,false,false,0,"ppth; 10^-3","UCUM","MCnc; MCnt","Clinical","[ppth] is often used in solution concentrations as 1 g/L or 1 g/kg.\n\nCan be ambigous and would be better if the metric units was used directly. ","10*-3","10*-3","1",1,false],[false,"parts per million","[ppm]","[PPM]","fraction",0.000001,[0,0,0,0,0,0,0],"ppm","dimless",false,null,null,1,false,false,0,"ppm; 10^-6","UCUM","MCnt; MCnc; SFr","Clinical","[ppm] is often used in solution concentrations as 1 mg/L or 1 mg/kg. Also used to express mole fractions as 1 mmol/mol.\n\n[ppm] is also used in nuclear magnetic resonance (NMR) to represent chemical shift - the difference of a measured frequency in parts per million from the reference frequency.\n\nCan be ambigous and would be better if the metric units was used directly. ","10*-6","10*-6","1",1,false],[false,"parts per billion","[ppb]","[PPB]","fraction",1e-9,[0,0,0,0,0,0,0],"ppb","dimless",false,null,null,1,false,false,0,"ppb; 10^-9","UCUM","MCnt; MCnc; SFr","Clinical","[ppb] is often used in solution concentrations as 1 ug/L or 1 ug/kg. Also used to express mole fractions as 1 umol/mol.\n\nCan be ambigous and would be better if the metric units was used directly. ","10*-9","10*-9","1",1,false],[false,"parts per trillion","[pptr]","[PPTR]","fraction",1e-12,[0,0,0,0,0,0,0],"pptr","dimless",false,null,null,1,false,false,0,"pptr; 10^-12","UCUM","MCnt; MCnc; SFr","Clinical","[pptr] is often used in solution concentrations as 1 ng/L or 1 ng/kg. Also used to express mole fractions as 1 nmol/mol.\n\nCan be ambigous and would be better if the metric units was used directly. ","10*-12","10*-12","1",1,false],[false,"mole","mol","MOL","amount of substance",6.0221367e+23,[0,0,0,0,0,0,0],"mol","si",true,null,null,1,false,false,1,"moles","UCUM","Sub","Clinical","Measure the number of molecules ","10*23","10*23","6.0221367",6.0221367,false],[false,"steradian - solid angle","sr","SR","solid angle",1,[0,0,0,2,0,0,0],"sr","si",true,null,null,1,false,false,0,"square radian; rad2; rad^2","UCUM","Angle","Clinical","unit of solid angle in three-dimensional geometry analagous to radian; used in photometry which measures the perceived brightness of object by human eye (e.g. radiant intensity = watt/steradian)","rad2","RAD2","1",1,false],[false,"hertz","Hz","HZ","frequency",1,[0,-1,0,0,0,0,0],"Hz","si",true,null,null,1,false,false,0,"Herz; frequency; frequencies","UCUM","Freq; Num","Clinical","equal to one cycle per second","s-1","S-1","1",1,false],[false,"newton","N","N","force",1000,[1,-2,1,0,0,0,0],"N","si",true,null,null,1,false,false,0,"Newtons","UCUM","Force","Clinical","unit of force with base units kg.m/s2","kg.m/s2","KG.M/S2","1",1,false],[false,"pascal","Pa","PAL","pressure",1000,[-1,-2,1,0,0,0,0],"Pa","si",true,null,null,1,false,false,0,"pascals","UCUM","Pres","Clinical","standard unit of pressure equal to 1 newton per square meter (N/m2)","N/m2","N/M2","1",1,false],[false,"joule","J","J","energy",1000,[2,-2,1,0,0,0,0],"J","si",true,null,null,1,false,false,0,"joules","UCUM","Enrg","Clinical","unit of energy defined as the work required to move an object 1 m with a force of 1 N (N.m) or an electric charge of 1 C through 1 V (C.V), or to produce 1 W for 1 s (W.s) ","N.m","N.M","1",1,false],[false,"watt","W","W","power",1000,[2,-3,1,0,0,0,0],"W","si",true,null,null,1,false,false,0,"watts","UCUM","EngRat","Clinical","unit of power equal to 1 Joule per second (J/s) = kg⋅m2⋅s−3","J/s","J/S","1",1,false],[false,"Ampere","A","A","electric current",1,[0,-1,0,0,0,1,0],"A","si",true,null,null,1,false,false,0,"Amperes","UCUM","ElpotRat","Clinical","unit of electric current equal to flow rate of electrons equal to 16.2415×10^18 elementary charges moving past a boundary in one second or 1 Coulomb/second","C/s","C/S","1",1,false],[false,"volt","V","V","electric potential",1000,[2,-2,1,0,0,-1,0],"V","si",true,null,null,1,false,false,0,"volts","UCUM","Elpot","Clinical","unit of electric potential (voltage) = 1 Joule per Coulomb (J/C)","J/C","J/C","1",1,false],[false,"farad","F","F","electric capacitance",0.001,[-2,2,-1,0,0,2,0],"F","si",true,null,null,1,false,false,0,"farads; electric capacitance","UCUM","","Clinical","CGS unit of electric capacitance with base units C/V (Coulomb per Volt)","C/V","C/V","1",1,false],[false,"ohm","Ohm","OHM","electric resistance",1000,[2,-1,1,0,0,-2,0],"Ω","si",true,null,null,1,false,false,0,"Ω; resistance; ohms","UCUM","","Clinical","unit of electrical resistance with units of Volt per Ampere","V/A","V/A","1",1,false],[false,"siemens","S","SIE","electric conductance",0.001,[-2,1,-1,0,0,2,0],"S","si",true,null,null,1,false,false,0,"Reciprocal ohm; mho; Ω−1; conductance","UCUM","","Clinical","unit of electric conductance (the inverse of electrical resistance) equal to ohm^-1","Ohm-1","OHM-1","1",1,false],[false,"weber","Wb","WB","magnetic flux",1000,[2,-1,1,0,0,-1,0],"Wb","si",true,null,null,1,false,false,0,"magnetic flux; webers","UCUM","","Clinical","unit of magnetic flux equal to Volt second","V.s","V.S","1",1,false],[false,"degree Celsius","Cel","CEL","temperature",1,[0,0,0,0,1,0,0],"°C","si",true,null,"Cel",1,true,false,0,"°C; degrees","UCUM","Temp","Clinical","","K",null,null,1,false],[false,"tesla","T","T","magnetic flux density",1000,[0,-1,1,0,0,-1,0],"T","si",true,null,null,1,false,false,0,"Teslas; magnetic field","UCUM","","Clinical","SI unit of magnetic field strength for magnetic field B equal to 1 Weber/square meter = 1 kg/(s2*A)","Wb/m2","WB/M2","1",1,false],[false,"henry","H","H","inductance",1000,[2,0,1,0,0,-2,0],"H","si",true,null,null,1,false,false,0,"henries; inductance","UCUM","","Clinical","unit of electrical inductance; usually expressed in millihenrys (mH) or microhenrys (uH).","Wb/A","WB/A","1",1,false],[false,"lumen","lm","LM","luminous flux",1,[0,0,0,2,0,0,1],"lm","si",true,null,null,1,false,false,0,"luminous flux; lumens","UCUM","","Clinical","unit of luminous flux defined as 1 lm = 1 cd⋅sr (candela times sphere)","cd.sr","CD.SR","1",1,false],[false,"lux","lx","LX","illuminance",1,[-2,0,0,2,0,0,1],"lx","si",true,null,null,1,false,false,0,"illuminance; luxes","UCUM","","Clinical","unit of illuminance equal to one lumen per square meter. ","lm/m2","LM/M2","1",1,false],[false,"becquerel","Bq","BQ","radioactivity",1,[0,-1,0,0,0,0,0],"Bq","si",true,null,null,1,false,false,0,"activity; radiation; becquerels","UCUM","","Clinical","measure of the atomic radiation rate with units s^-1","s-1","S-1","1",1,false],[false,"gray","Gy","GY","energy dose",1,[2,-2,0,0,0,0,0],"Gy","si",true,null,null,1,false,false,0,"absorbed doses; ionizing radiation doses; kerma; grays","UCUM","EngCnt","Clinical","unit of ionizing radiation dose with base units of 1 joule of radiation energy per kilogram of matter","J/kg","J/KG","1",1,false],[false,"sievert","Sv","SV","dose equivalent",1,[2,-2,0,0,0,0,0],"Sv","si",true,null,null,1,false,false,0,"sieverts; radiation dose quantities; equivalent doses; effective dose; operational dose; committed dose","UCUM","","Clinical","SI unit for radiation dose equivalent equal to 1 Joule/kilogram.","J/kg","J/KG","1",1,false],[false,"degree - plane angle","deg","DEG","plane angle",0.017453292519943295,[0,0,0,1,0,0,0],"°","iso1000",false,null,null,1,false,false,0,"°; degree of arc; arc degree; arcdegree; angle","UCUM","Angle","Clinical","one degree is equivalent to π/180 radians.","[pi].rad/360","[PI].RAD/360","2",2,false],[false,"gon","gon","GON","plane angle",0.015707963267948967,[0,0,0,1,0,0,0],"□g","iso1000",false,null,null,1,false,false,0,"gon (grade); gons","UCUM","Angle","Nonclinical","unit of plane angle measurement equal to 1/400 circle","deg","DEG","0.9",0.9,false],[false,"arc minute","'","'","plane angle",0.0002908882086657216,[0,0,0,1,0,0,0],"'","iso1000",false,null,null,1,false,false,0,"arcminutes; arcmin; arc minutes; arc mins","UCUM","Angle","Clinical","equal to 1/60 degree; used in optometry and opthamology (e.g. visual acuity tests)","deg/60","DEG/60","1",1,false],[false,"arc second","''","''","plane angle",0.00000484813681109536,[0,0,0,1,0,0,0],"''","iso1000",false,null,null,1,false,false,0,"arcseconds; arcsecs","UCUM","Angle","Clinical","equal to 1/60 arcminute = 1/3600 degree; used in optometry and opthamology (e.g. visual acuity tests)","'/60","'/60","1",1,false],[false,"Liters","l","L","volume",0.001,[3,0,0,0,0,0,0],"l","iso1000",true,null,null,1,false,false,0,"cubic decimeters; decimeters cubed; decimetres; dm3; dm^3; litres; liters, LT ","UCUM","Vol","Clinical","Because lower case \"l\" can be read as the number \"1\", though this is a valid UCUM units. UCUM strongly reccomends using \"L\"","dm3","DM3","1",1,false],[false,"Liters","L","L","volume",0.001,[3,0,0,0,0,0,0],"L","iso1000",true,null,null,1,false,false,0,"cubic decimeters; decimeters cubed; decimetres; dm3; dm^3; litres; liters, LT ","UCUM","Vol","Clinical","Because lower case \"l\" can be read as the number \"1\", though this is a valid UCUM units. UCUM strongly reccomends using \"L\"","l",null,"1",1,false],[false,"are","ar","AR","area",100,[2,0,0,0,0,0,0],"a","iso1000",true,null,null,1,false,false,0,"100 m2; 100 m^2; 100 square meter; meters squared; metres","UCUM","Area","Clinical","metric base unit for area defined as 100 m^2","m2","M2","100",100,false],[false,"minute","min","MIN","time",60,[0,1,0,0,0,0,0],"min","iso1000",false,null,null,1,false,false,0,"minutes","UCUM","Time","Clinical","","s","S","60",60,false],[false,"hour","h","HR","time",3600,[0,1,0,0,0,0,0],"h","iso1000",false,null,null,1,false,false,0,"hours; hrs; age","UCUM","Time","Clinical","","min","MIN","60",60,false],[false,"day","d","D","time",86400,[0,1,0,0,0,0,0],"d","iso1000",false,null,null,1,false,false,0,"days; age; dy; 24 hours; 24 hrs","UCUM","Time","Clinical","","h","HR","24",24,false],[false,"tropical year","a_t","ANN_T","time",31556925.216,[0,1,0,0,0,0,0],"at","iso1000",false,null,null,1,false,false,0,"solar years; a tropical; years","UCUM","Time","Clinical","has an average of 365.242181 days but is constantly changing.","d","D","365.24219",365.24219,false],[false,"mean Julian year","a_j","ANN_J","time",31557600,[0,1,0,0,0,0,0],"aj","iso1000",false,null,null,1,false,false,0,"mean Julian yr; a julian; years","UCUM","Time","Clinical","has an average of 365.25 days, and in everyday use, has been replaced by the Gregorian year. However, this unit is used in astronomy to calculate light year. ","d","D","365.25",365.25,false],[false,"mean Gregorian year","a_g","ANN_G","time",31556952,[0,1,0,0,0,0,0],"ag","iso1000",false,null,null,1,false,false,0,"mean Gregorian yr; a gregorian; years","UCUM","Time","Clinical","has an average of 365.2425 days and is the most internationally used civil calendar.","d","D","365.2425",365.2425,false],[false,"year","a","ANN","time",31557600,[0,1,0,0,0,0,0],"a","iso1000",false,null,null,1,false,false,0,"years; a; yr, yrs; annum","UCUM","Time","Clinical","","a_j","ANN_J","1",1,false],[false,"week","wk","WK","time",604800,[0,1,0,0,0,0,0],"wk","iso1000",false,null,null,1,false,false,0,"weeks; wks","UCUM","Time","Clinical","","d","D","7",7,false],[false,"synodal month","mo_s","MO_S","time",2551442.976,[0,1,0,0,0,0,0],"mos","iso1000",false,null,null,1,false,false,0,"Moon; synodic month; lunar month; mo-s; mo s; months; moons","UCUM","Time","Nonclinical","has an average of 29.53 days per month, unit used in astronomy","d","D","29.53059",29.53059,false],[false,"mean Julian month","mo_j","MO_J","time",2629800,[0,1,0,0,0,0,0],"moj","iso1000",false,null,null,1,false,false,0,"mo-julian; mo Julian; months","UCUM","Time","Clinical","has an average of 30.435 days per month","a_j/12","ANN_J/12","1",1,false],[false,"mean Gregorian month","mo_g","MO_G","time",2629746,[0,1,0,0,0,0,0],"mog","iso1000",false,null,null,1,false,false,0,"months; month-gregorian; mo-gregorian","UCUM","Time","Clinical","has an average 30.436875 days per month and is from the most internationally used civil calendar.","a_g/12","ANN_G/12","1",1,false],[false,"month","mo","MO","time",2629800,[0,1,0,0,0,0,0],"mo","iso1000",false,null,null,1,false,false,0,"months; duration","UCUM","Time","Clinical","based on Julian calendar which has an average of 30.435 days per month (this unit is used in astronomy but not in everyday life - see mo_g)","mo_j","MO_J","1",1,false],[false,"metric ton","t","TNE","mass",1000000,[0,0,1,0,0,0,0],"t","iso1000",true,null,null,1,false,false,0,"tonnes; megagrams; tons","UCUM","Mass","Nonclinical","equal to 1000 kg used in the US (recognized by NIST as metric ton), and internationally (recognized as tonne)","kg","KG","1e3",1000,false],[false,"bar","bar","BAR","pressure",100000000,[-1,-2,1,0,0,0,0],"bar","iso1000",true,null,null,1,false,false,0,"bars","UCUM","Pres","Nonclinical","unit of pressure equal to 10^5 Pascals, primarily used by meteorologists and in weather forecasting","Pa","PAL","1e5",100000,false],[false,"unified atomic mass unit","u","AMU","mass",1.6605402e-24,[0,0,1,0,0,0,0],"u","iso1000",true,null,null,1,false,false,0,"unified atomic mass units; amu; Dalton; Da","UCUM","Mass","Clinical","the mass of 1/12 of an unbound Carbon-12 atom nuclide equal to 1.6606x10^-27 kg ","g","G","1.6605402e-24",1.6605402e-24,false],[false,"astronomic unit","AU","ASU","length",149597870691,[1,0,0,0,0,0,0],"AU","iso1000",false,null,null,1,false,false,0,"AU; units","UCUM","Len","Clinical","unit of length used in astronomy for measuring distance in Solar system","Mm","MAM","149597.870691",149597.870691,false],[false,"parsec","pc","PRS","length",30856780000000000,[1,0,0,0,0,0,0],"pc","iso1000",true,null,null,1,false,false,0,"parsecs","UCUM","Len","Clinical","unit of length equal to 3.26 light years, nad used to measure large distances to objects outside our Solar System","m","M","3.085678e16",30856780000000000,false],[false,"velocity of light in a vacuum","[c]","[C]","velocity",299792458,[1,-1,0,0,0,0,0],"c","const",true,null,null,1,false,false,0,"speed of light","UCUM","Vel","Constant","equal to 299792458 m/s (approximately 3 x 10^8 m/s)","m/s","M/S","299792458",299792458,false],[false,"Planck constant","[h]","[H]","action",6.6260755e-31,[2,-1,1,0,0,0,0],"h","const",true,null,null,1,false,false,0,"Planck's constant","UCUM","","Constant","constant = 6.62607004 × 10-34 m2.kg/s; defined as quantum of action","J.s","J.S","6.6260755e-34",6.6260755e-34,false],[false,"Boltzmann constant","[k]","[K]","(unclassified)",1.380658e-20,[2,-2,1,0,-1,0,0],"k","const",true,null,null,1,false,false,0,"k; kB","UCUM","","Constant","physical constant relating energy at the individual particle level with temperature = 1.38064852 ×10^−23 J/K","J/K","J/K","1.380658e-23",1.380658e-23,false],[false,"permittivity of vacuum - electric","[eps_0]","[EPS_0]","electric permittivity",8.854187817000001e-15,[-3,2,-1,0,0,2,0],"ε0","const",true,null,null,1,false,false,0,"ε0; Electric Constant; vacuum permittivity; permittivity of free space ","UCUM","","Constant","approximately equal to 8.854 × 10^−12 F/m (farads per meter)","F/m","F/M","8.854187817e-12",8.854187817e-12,false],[false,"permeability of vacuum - magnetic","[mu_0]","[MU_0]","magnetic permeability",0.0012566370614359172,[1,0,1,0,0,-2,0],"μ0","const",true,null,null,1,false,false,0,"μ0; vacuum permeability; permeability of free space; magnetic constant","UCUM","","Constant","equal to 4π×10^−7 N/A2 (Newtons per square ampere) ≈ 1.2566×10^−6 H/m (Henry per meter)","N/A2","4.[PI].10*-7.N/A2","1",0.0000012566370614359173,false],[false,"elementary charge","[e]","[E]","electric charge",1.60217733e-19,[0,0,0,0,0,1,0],"e","const",true,null,null,1,false,false,0,"e; q; electric charges","UCUM","","Constant","the magnitude of the electric charge carried by a single electron or proton ≈ 1.60217×10^-19 Coulombs","C","C","1.60217733e-19",1.60217733e-19,false],[false,"electronvolt","eV","EV","energy",1.60217733e-16,[2,-2,1,0,0,0,0],"eV","iso1000",true,null,null,1,false,false,0,"Electron Volts; electronvolts","UCUM","Eng","Clinical","unit of kinetic energy = 1 V * 1.602×10^−19 C = 1.6×10−19 Joules","[e].V","[E].V","1",1,false],[false,"electron mass","[m_e]","[M_E]","mass",9.1093897e-28,[0,0,1,0,0,0,0],"me","const",true,null,null,1,false,false,0,"electron rest mass; me","UCUM","Mass","Constant","approximately equal to 9.10938356 × 10-31 kg; defined as the mass of a stationary electron","g","g","9.1093897e-28",9.1093897e-28,false],[false,"proton mass","[m_p]","[M_P]","mass",1.6726231e-24,[0,0,1,0,0,0,0],"mp","const",true,null,null,1,false,false,0,"mp; masses","UCUM","Mass","Constant","approximately equal to 1.672622×10−27 kg","g","g","1.6726231e-24",1.6726231e-24,false],[false,"Newtonian constant of gravitation","[G]","[GC]","(unclassified)",6.67259e-14,[3,-2,-1,0,0,0,0],"G","const",true,null,null,1,false,false,0,"G; gravitational constant; Newton's constant","UCUM","","Constant","gravitational constant = 6.674×10−11 N⋅m2/kg2","m3.kg-1.s-2","M3.KG-1.S-2","6.67259e-11",6.67259e-11,false],[false,"standard acceleration of free fall","[g]","[G]","acceleration",9.80665,[1,-2,0,0,0,0,0],"gn","const",true,null,null,1,false,false,0,"standard gravity; g; ɡ0; ɡn","UCUM","Accel","Constant","defined by standard = 9.80665 m/s2","m/s2","M/S2","980665e-5",9.80665,false],[false,"Torr","Torr","Torr","pressure",133322,[-1,-2,1,0,0,0,0],"Torr","const",false,null,null,1,false,false,0,"torrs","UCUM","Pres","Clinical","1 torr = 1 mmHg; unit used to measure blood pressure","Pa","PAL","133.322",133.322,false],[false,"standard atmosphere","atm","ATM","pressure",101325000,[-1,-2,1,0,0,0,0],"atm","const",false,null,null,1,false,false,0,"reference pressure; atmos; std atmosphere","UCUM","Pres","Clinical","defined as being precisely equal to 101,325 Pa","Pa","PAL","101325",101325,false],[false,"light-year","[ly]","[LY]","length",9460730472580800,[1,0,0,0,0,0,0],"l.y.","const",true,null,null,1,false,false,0,"light years; ly","UCUM","Len","Constant","unit of astronomal distance = 5.88×10^12 mi","[c].a_j","[C].ANN_J","1",1,false],[false,"gram-force","gf","GF","force",9.80665,[1,-2,1,0,0,0,0],"gf","const",true,null,null,1,false,false,0,"Newtons; gram forces","UCUM","Force","Clinical","May be specific to unit related to cardiac output","g.[g]","G.[G]","1",1,false],[false,"Kayser","Ky","KY","lineic number",100,[-1,0,0,0,0,0,0],"K","cgs",true,null,null,1,false,false,0,"wavenumbers; kaysers","UCUM","InvLen","Clinical","unit of wavelength equal to cm^-1","cm-1","CM-1","1",1,false],[false,"Gal","Gal","GL","acceleration",0.01,[1,-2,0,0,0,0,0],"Gal","cgs",true,null,null,1,false,false,0,"galileos; Gals","UCUM","Accel","Clinical","unit of acceleration used in gravimetry; equivalent to cm/s2 ","cm/s2","CM/S2","1",1,false],[false,"dyne","dyn","DYN","force",0.01,[1,-2,1,0,0,0,0],"dyn","cgs",true,null,null,1,false,false,0,"dynes","UCUM","Force","Clinical","unit of force equal to 10^-5 Newtons","g.cm/s2","G.CM/S2","1",1,false],[false,"erg","erg","ERG","energy",0.0001,[2,-2,1,0,0,0,0],"erg","cgs",true,null,null,1,false,false,0,"10^-7 Joules, 10-7 Joules; 100 nJ; 100 nanoJoules; 1 dyne cm; 1 g.cm2/s2","UCUM","Eng","Clinical","unit of energy = 1 dyne centimeter = 10^-7 Joules","dyn.cm","DYN.CM","1",1,false],[false,"Poise","P","P","dynamic viscosity",100,[-1,-1,1,0,0,0,0],"P","cgs",true,null,null,1,false,false,0,"dynamic viscosity; poises","UCUM","Visc","Clinical","unit of dynamic viscosity where 1 Poise = 1/10 Pascal second","dyn.s/cm2","DYN.S/CM2","1",1,false],[false,"Biot","Bi","BI","electric current",10,[0,-1,0,0,0,1,0],"Bi","cgs",true,null,null,1,false,false,0,"Bi; abamperes; abA","UCUM","ElpotRat","Clinical","equal to 10 amperes","A","A","10",10,false],[false,"Stokes","St","ST","kinematic viscosity",0.0001,[2,-1,0,0,0,0,0],"St","cgs",true,null,null,1,false,false,0,"kinematic viscosity","UCUM","Visc","Clinical","unit of kimematic viscosity with units cm2/s","cm2/s","CM2/S","1",1,false],[false,"Maxwell","Mx","MX","flux of magnetic induction",0.00001,[2,-1,1,0,0,-1,0],"Mx","cgs",true,null,null,1,false,false,0,"magnetix flux; Maxwells","UCUM","","Clinical","unit of magnetic flux","Wb","WB","1e-8",1e-8,false],[false,"Gauss","G","GS","magnetic flux density",0.1,[0,-1,1,0,0,-1,0],"Gs","cgs",true,null,null,1,false,false,0,"magnetic fields; magnetic flux density; induction; B","UCUM","magnetic","Clinical","CGS unit of magnetic flux density, known as magnetic field B; defined as one maxwell unit per square centimeter (see Oersted for CGS unit for H field)","T","T","1e-4",0.0001,false],[false,"Oersted","Oe","OE","magnetic field intensity",79.57747154594767,[-1,-1,0,0,0,1,0],"Oe","cgs",true,null,null,1,false,false,0,"H magnetic B field; Oersteds","UCUM","","Clinical","CGS unit of the auxiliary magnetic field H defined as 1 dyne per unit pole = 1000/4π amperes per meter (see Gauss for CGS unit for B field)","A/m","/[PI].A/M","250",79.57747154594767,false],[false,"Gilbert","Gb","GB","magnetic tension",0.7957747154594768,[0,-1,0,0,0,1,0],"Gb","cgs",true,null,null,1,false,false,0,"Gi; magnetomotive force; Gilberts","UCUM","","Clinical","unit of magnetomotive force (magnetic potential)","Oe.cm","OE.CM","1",1,false],[false,"stilb","sb","SB","lum. intensity density",10000,[-2,0,0,0,0,0,1],"sb","cgs",true,null,null,1,false,false,0,"stilbs","UCUM","","Obsolete","unit of luminance; equal to and replaced by unit candela per square centimeter (cd/cm2)","cd/cm2","CD/CM2","1",1,false],[false,"Lambert","Lmb","LMB","brightness",3183.098861837907,[-2,0,0,0,0,0,1],"L","cgs",true,null,null,1,false,false,0,"luminance; lamberts","UCUM","","Clinical","unit of luminance defined as 1 lambert = 1/ π candela per square meter","cd/cm2/[pi]","CD/CM2/[PI]","1",1,false],[false,"phot","ph","PHT","illuminance",0.0001,[-2,0,0,2,0,0,1],"ph","cgs",true,null,null,1,false,false,0,"phots","UCUM","","Clinical","CGS photometric unit of illuminance, or luminous flux through an area equal to 10000 lumens per square meter = 10000 lux","lx","LX","1e-4",0.0001,false],[false,"Curie","Ci","CI","radioactivity",37000000000,[0,-1,0,0,0,0,0],"Ci","cgs",true,null,null,1,false,false,0,"curies","UCUM","","Obsolete","unit for measuring atomic disintegration rate; replaced by the Bequerel (Bq) unit","Bq","BQ","37e9",37000000000,false],[false,"Roentgen","R","ROE","ion dose",2.58e-7,[0,0,-1,0,0,1,0],"R","cgs",true,null,null,1,false,false,0,"röntgen; Roentgens","UCUM","","Clinical","unit of exposure of X-rays and gamma rays in air; unit used primarily in the US but strongly discouraged by NIST","C/kg","C/KG","2.58e-4",0.000258,false],[false,"radiation absorbed dose","RAD","[RAD]","energy dose",0.01,[2,-2,0,0,0,0,0],"RAD","cgs",true,null,null,1,false,false,0,"doses","UCUM","","Clinical","unit of radiation absorbed dose used primarily in the US with base units 100 ergs per gram of material. Also see the SI unit Gray (Gy).","erg/g","ERG/G","100",100,false],[false,"radiation equivalent man","REM","[REM]","dose equivalent",0.01,[2,-2,0,0,0,0,0],"REM","cgs",true,null,null,1,false,false,0,"Roentgen Equivalent in Man; rems; dose equivalents","UCUM","","Clinical","unit of equivalent dose which measures the effect of radiation on humans equal to 0.01 sievert. Used primarily in the US. Also see SI unit Sievert (Sv)","RAD","[RAD]","1",1,false],[false,"inch","[in_i]","[IN_I]","length",0.025400000000000002,[1,0,0,0,0,0,0],"in","intcust",false,null,null,1,false,false,0,"inches; in; international inch; body height","UCUM","Len","Clinical","standard unit for inch in the US and internationally","cm","CM","254e-2",2.54,false],[false,"foot","[ft_i]","[FT_I]","length",0.3048,[1,0,0,0,0,0,0],"ft","intcust",false,null,null,1,false,false,0,"ft; fts; foot; international foot; feet; international feet; height","UCUM","Len","Clinical","unit used in the US and internationally","[in_i]","[IN_I]","12",12,false],[false,"yard","[yd_i]","[YD_I]","length",0.9144000000000001,[1,0,0,0,0,0,0],"yd","intcust",false,null,null,1,false,false,0,"international yards; yds; distance","UCUM","Len","Clinical","standard unit used in the US and internationally","[ft_i]","[FT_I]","3",3,false],[false,"mile","[mi_i]","[MI_I]","length",1609.344,[1,0,0,0,0,0,0],"mi","intcust",false,null,null,1,false,false,0,"international miles; mi I; statute mile","UCUM","Len","Clinical","standard unit used in the US and internationally","[ft_i]","[FT_I]","5280",5280,false],[false,"fathom","[fth_i]","[FTH_I]","depth of water",1.8288000000000002,[1,0,0,0,0,0,0],"fth","intcust",false,null,null,1,false,false,0,"international fathoms","UCUM","Len","Nonclinical","unit used in the US and internationally to measure depth of water; same length as the US fathom","[ft_i]","[FT_I]","6",6,false],[false,"nautical mile","[nmi_i]","[NMI_I]","length",1852,[1,0,0,0,0,0,0],"n.mi","intcust",false,null,null,1,false,false,0,"nautical mile; nautical miles; international nautical mile; international nautical miles; nm; n.m.; nmi","UCUM","Len","Nonclinical","standard unit used in the US and internationally","m","M","1852",1852,false],[false,"knot","[kn_i]","[KN_I]","velocity",0.5144444444444445,[1,-1,0,0,0,0,0],"knot","intcust",false,null,null,1,false,false,0,"kn; kt; international knots","UCUM","Vel","Nonclinical","defined as equal to one nautical mile (1.852 km) per hour","[nmi_i]/h","[NMI_I]/H","1",1,false],[false,"square inch","[sin_i]","[SIN_I]","area",0.0006451600000000001,[2,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"in2; in^2; inches squared; sq inch; inches squared; international","UCUM","Area","Clinical","standard unit used in the US and internationally","[in_i]2","[IN_I]2","1",1,false],[false,"square foot","[sft_i]","[SFT_I]","area",0.09290304,[2,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"ft2; ft^2; ft squared; sq ft; feet; international","UCUM","Area","Clinical","standard unit used in the US and internationally","[ft_i]2","[FT_I]2","1",1,false],[false,"square yard","[syd_i]","[SYD_I]","area",0.8361273600000002,[2,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"yd2; yd^2; sq. yds; yards squared; international","UCUM","Area","Clinical","standard unit used in the US and internationally","[yd_i]2","[YD_I]2","1",1,false],[false,"cubic inch","[cin_i]","[CIN_I]","volume",0.000016387064000000003,[3,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"in3; in^3; in*3; inches^3; inches*3; cu. in; cu in; cubic inches; inches cubed; cin","UCUM","Vol","Clinical","standard unit used in the US and internationally","[in_i]3","[IN_I]3","1",1,false],[false,"cubic foot","[cft_i]","[CFT_I]","volume",0.028316846592000004,[3,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"ft3; ft^3; ft*3; cu. ft; cubic feet; cubed; [ft_i]3; international","UCUM","Vol","Clinical","","[ft_i]3","[FT_I]3","1",1,false],[false,"cubic yard","[cyd_i]","[CYD_I]","volume",0.7645548579840002,[3,0,0,0,0,0,0],"cu.yd","intcust",false,null,null,1,false,false,0,"cubic yards; cubic yds; cu yards; CYs; yards^3; yd^3; yds^3; yd3; yds3","UCUM","Vol","Nonclinical","standard unit used in the US and internationally","[yd_i]3","[YD_I]3","1",1,false],[false,"board foot","[bf_i]","[BF_I]","volume",0.002359737216,[3,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"BDFT; FBM; BF; board feet; international","UCUM","Vol","Nonclinical","unit of volume used to measure lumber","[in_i]3","[IN_I]3","144",144,false],[false,"cord","[cr_i]","[CR_I]","volume",3.6245563637760005,[3,0,0,0,0,0,0],null,"intcust",false,null,null,1,false,false,0,"crd I; international cords","UCUM","Vol","Nonclinical","unit of measure of dry volume used to measure firewood equal 128 ft3","[ft_i]3","[FT_I]3","128",128,false],[false,"mil","[mil_i]","[MIL_I]","length",0.000025400000000000004,[1,0,0,0,0,0,0],"mil","intcust",false,null,null,1,false,false,0,"thou, thousandth; mils; international","UCUM","Len","Clinical","equal to 0.001 international inch","[in_i]","[IN_I]","1e-3",0.001,false],[false,"circular mil","[cml_i]","[CML_I]","area",5.067074790974979e-10,[2,0,0,0,0,0,0],"circ.mil","intcust",false,null,null,1,false,false,0,"circular mils; cml I; international","UCUM","Area","Clinical","","[pi]/4.[mil_i]2","[PI]/4.[MIL_I]2","1",1,false],[false,"hand","[hd_i]","[HD_I]","height of horses",0.10160000000000001,[1,0,0,0,0,0,0],"hd","intcust",false,null,null,1,false,false,0,"hands; international","UCUM","Len","Nonclinical","used to measure horse height","[in_i]","[IN_I]","4",4,false],[false,"foot - US","[ft_us]","[FT_US]","length",0.3048006096012192,[1,0,0,0,0,0,0],"ftus","us-lengths",false,null,null,1,false,false,0,"US foot; foot US; us ft; ft us; height; visual distance; feet","UCUM","Len","Obsolete","Better to use [ft_i] which refers to the length used worldwide, including in the US; [ft_us] may be confused with land survey units. ","m/3937","M/3937","1200",1200,false],[false,"yard - US","[yd_us]","[YD_US]","length",0.9144018288036575,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"US yards; us yds; distance","UCUM","Len; Nrat","Obsolete","Better to use [yd_i] which refers to the length used worldwide, including in the US; [yd_us] refers to unit used in land surveys in the US","[ft_us]","[FT_US]","3",3,false],[false,"inch - US","[in_us]","[IN_US]","length",0.0254000508001016,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"US inches; in us; us in; inch US","UCUM","Len","Obsolete","Better to use [in_i] which refers to the length used worldwide, including in the US","[ft_us]/12","[FT_US]/12","1",1,false],[false,"rod - US","[rd_us]","[RD_US]","length",5.029210058420117,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"US rod; US rods; rd US; US rd","UCUM","Len","Obsolete","","[ft_us]","[FT_US]","16.5",16.5,false],[false,"Gunter's chain - US","[ch_us]","[CH_US]","length",20.116840233680467,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"surveyor's chain; Surveyor's chain USA; Gunter’s measurement; surveyor’s measurement; Gunter's Chain USA","UCUM","Len","Obsolete","historical unit used for land survey used only in the US","[rd_us]","[RD_US]","4",4,false],[false,"link for Gunter's chain - US","[lk_us]","[LK_US]","length",0.20116840233680466,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"Links for Gunter's Chain USA","UCUM","Len","Obsolete","","[ch_us]/100","[CH_US]/100","1",1,false],[false,"Ramden's chain - US","[rch_us]","[RCH_US]","length",30.480060960121918,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"Ramsden's chain; engineer's chains","UCUM","Len","Obsolete","distance measuring device used for land survey","[ft_us]","[FT_US]","100",100,false],[false,"link for Ramden's chain - US","[rlk_us]","[RLK_US]","length",0.3048006096012192,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"links for Ramsden's chain","UCUM","Len","Obsolete","","[rch_us]/100","[RCH_US]/100","1",1,false],[false,"fathom - US","[fth_us]","[FTH_US]","length",1.828803657607315,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"US fathoms; fathom USA; fth us","UCUM","Len","Obsolete","same length as the international fathom - better to use international fathom ([fth_i])","[ft_us]","[FT_US]","6",6,false],[false,"furlong - US","[fur_us]","[FUR_US]","length",201.16840233680466,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"US furlongs; fur us","UCUM","Len","Nonclinical","distance unit in horse racing","[rd_us]","[RD_US]","40",40,false],[false,"mile - US","[mi_us]","[MI_US]","length",1609.3472186944373,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"U.S. Survey Miles; US statute miles; survey mi; US mi; distance","UCUM","Len","Nonclinical","Better to use [mi_i] which refers to the length used worldwide, including in the US","[fur_us]","[FUR_US]","8",8,false],[false,"acre - US","[acr_us]","[ACR_US]","area",4046.872609874252,[2,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"Acre USA Survey; Acre USA; survey acres","UCUM","Area","Nonclinical","an older unit based on pre 1959 US statute lengths that is still sometimes used in the US only for land survey purposes. ","[rd_us]2","[RD_US]2","160",160,false],[false,"square rod - US","[srd_us]","[SRD_US]","area",25.292953811714074,[2,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"rod2; rod^2; sq. rod; rods squared","UCUM","Area","Nonclinical","Used only in the US to measure land area, based on US statute land survey length units","[rd_us]2","[RD_US]2","1",1,false],[false,"square mile - US","[smi_us]","[SMI_US]","area",2589998.470319521,[2,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"mi2; mi^2; sq mi; miles squared","UCUM","Area","Nonclinical","historical unit used only in the US for land survey purposes (based on the US survey mile), not the internationally recognized [mi_i]","[mi_us]2","[MI_US]2","1",1,false],[false,"section","[sct]","[SCT]","area",2589998.470319521,[2,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"sct; sections","UCUM","Area","Nonclinical","tract of land approximately equal to 1 mile square containing 640 acres","[mi_us]2","[MI_US]2","1",1,false],[false,"township","[twp]","[TWP]","area",93239944.93150276,[2,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"twp; townships","UCUM","Area","Nonclinical","land measurement equal to 6 mile square","[sct]","[SCT]","36",36,false],[false,"mil - US","[mil_us]","[MIL_US]","length",0.0000254000508001016,[1,0,0,0,0,0,0],null,"us-lengths",false,null,null,1,false,false,0,"thou, thousandth; mils","UCUM","Len","Obsolete","better to use [mil_i] which is based on the internationally recognized inch","[in_us]","[IN_US]","1e-3",0.001,false],[false,"inch - British","[in_br]","[IN_BR]","length",0.025399980000000003,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"imperial inches; imp in; br in; british inches","UCUM","Len","Obsolete","","cm","CM","2.539998",2.539998,false],[false,"foot - British","[ft_br]","[FT_BR]","length",0.30479976000000003,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British Foot; Imperial Foot; feet; imp fts; br fts","UCUM","Len","Obsolete","","[in_br]","[IN_BR]","12",12,false],[false,"rod - British","[rd_br]","[RD_BR]","length",5.02919604,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British rods; br rd","UCUM","Len","Obsolete","","[ft_br]","[FT_BR]","16.5",16.5,false],[false,"Gunter's chain - British","[ch_br]","[CH_BR]","length",20.11678416,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"Gunter's Chain British; Gunters Chain British; Surveyor's Chain British","UCUM","Len","Obsolete","historical unit used for land survey used only in Great Britain","[rd_br]","[RD_BR]","4",4,false],[false,"link for Gunter's chain - British","[lk_br]","[LK_BR]","length",0.2011678416,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"Links for Gunter's Chain British","UCUM","Len","Obsolete","","[ch_br]/100","[CH_BR]/100","1",1,false],[false,"fathom - British","[fth_br]","[FTH_BR]","length",1.82879856,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British fathoms; imperial fathoms; br fth; imp fth","UCUM","Len","Obsolete","","[ft_br]","[FT_BR]","6",6,false],[false,"pace - British","[pc_br]","[PC_BR]","length",0.7619994000000001,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British paces; br pc","UCUM","Len","Nonclinical","traditional unit of length equal to 152.4 centimeters, or 1.52 meter. ","[ft_br]","[FT_BR]","2.5",2.5,false],[false,"yard - British","[yd_br]","[YD_BR]","length",0.91439928,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British yards; Br yds; distance","UCUM","Len","Obsolete","","[ft_br]","[FT_BR]","3",3,false],[false,"mile - British","[mi_br]","[MI_BR]","length",1609.3427328000002,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"imperial miles; British miles; English statute miles; imp mi, br mi","UCUM","Len","Obsolete","","[ft_br]","[FT_BR]","5280",5280,false],[false,"nautical mile - British","[nmi_br]","[NMI_BR]","length",1853.1825408000002,[1,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British nautical miles; Imperial nautical miles; Admiralty miles; n.m. br; imp nm","UCUM","Len","Obsolete","","[ft_br]","[FT_BR]","6080",6080,false],[false,"knot - British","[kn_br]","[KN_BR]","velocity",0.5147729280000001,[1,-1,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"British knots; kn br; kt","UCUM","Vel","Obsolete","based on obsolete British nautical mile ","[nmi_br]/h","[NMI_BR]/H","1",1,false],[false,"acre","[acr_br]","[ACR_BR]","area",4046.850049400269,[2,0,0,0,0,0,0],null,"brit-length",false,null,null,1,false,false,0,"Imperial acres; British; a; ac; ar; acr","UCUM","Area","Nonclinical","the standard unit for acre used in the US and internationally","[yd_br]2","[YD_BR]2","4840",4840,false],[false,"gallon - US","[gal_us]","[GAL_US]","fluid volume",0.0037854117840000006,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US gallons; US liquid gallon; gal us; Queen Anne's wine gallon","UCUM","Vol","Nonclinical","only gallon unit used in the US; [gal_us] is only used in some other countries in South American and Africa to measure gasoline volume","[in_i]3","[IN_I]3","231",231,false],[false,"barrel - US","[bbl_us]","[BBL_US]","fluid volume",0.158987294928,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"bbl","UCUM","Vol","Nonclinical","[bbl_us] is the standard unit for oil barrel, which is a unit only used in the US to measure the volume oil. ","[gal_us]","[GAL_US]","42",42,false],[false,"quart - US","[qt_us]","[QT_US]","fluid volume",0.0009463529460000001,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US quarts; us qts","UCUM","Vol","Clinical","Used only in the US","[gal_us]/4","[GAL_US]/4","1",1,false],[false,"pint - US","[pt_us]","[PT_US]","fluid volume",0.00047317647300000007,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US pints; pint US; liquid pint; pt us; us pt","UCUM","Vol","Clinical","Used only in the US","[qt_us]/2","[QT_US]/2","1",1,false],[false,"gill - US","[gil_us]","[GIL_US]","fluid volume",0.00011829411825000002,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US gills; gil us","UCUM","Vol","Nonclinical","only used in the context of alcohol volume in the US","[pt_us]/4","[PT_US]/4","1",1,false],[false,"fluid ounce - US","[foz_us]","[FOZ_US]","fluid volume",0.000029573529562500005,[3,0,0,0,0,0,0],"oz fl","us-volumes",false,null,null,1,false,false,0,"US fluid ounces; fl ozs; FO; fl. oz.; foz us","UCUM","Vol","Clinical","unit used only in the US","[gil_us]/4","[GIL_US]/4","1",1,false],[false,"fluid dram - US","[fdr_us]","[FDR_US]","fluid volume",0.0000036966911953125006,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US fluid drams; fdr us","UCUM","Vol","Nonclinical","equal to 1/8 US fluid ounce = 3.69 mL; used informally to mean small amount of liquor, especially Scotch whiskey","[foz_us]/8","[FOZ_US]/8","1",1,false],[false,"minim - US","[min_us]","[MIN_US]","fluid volume",6.1611519921875e-8,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"min US; US min; ♏ US","UCUM","Vol","Obsolete","","[fdr_us]/60","[FDR_US]/60","1",1,false],[false,"cord - US","[crd_us]","[CRD_US]","fluid volume",3.6245563637760005,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US cord; US cords; crd us; us crd","UCUM","Vol","Nonclinical","unit of measure of dry volume used to measure firewood equal 128 ft3 (the same as international cord [cr_i])","[ft_i]3","[FT_I]3","128",128,false],[false,"bushel - US","[bu_us]","[BU_US]","dry volume",0.03523907016688001,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US bushels; US bsh; US bu","UCUM","Vol","Obsolete","Historical unit of dry volume that is rarely used today","[in_i]3","[IN_I]3","2150.42",2150.42,false],[false,"gallon - historical","[gal_wi]","[GAL_WI]","dry volume",0.004404883770860001,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"Corn Gallon British; Dry Gallon US; Gallons Historical; Grain Gallon British; Winchester Corn Gallon; historical winchester gallons; wi gal","UCUM","Vol","Obsolete","historical unit of dry volume no longer used","[bu_us]/8","[BU_US]/8","1",1,false],[false,"peck - US","[pk_us]","[PK_US]","dry volume",0.008809767541720002,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"US pecks; US pk","UCUM","Vol","Nonclinical","unit of dry volume rarely used today (can be used to measure volume of apples)","[bu_us]/4","[BU_US]/4","1",1,false],[false,"dry quart - US","[dqt_us]","[DQT_US]","dry volume",0.0011012209427150002,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"dry quarts; dry quart US; US dry quart; dry qt; us dry qt; dqt; dqt us","UCUM","Vol","Nonclinical","historical unit of dry volume only in the US, but is rarely used today","[pk_us]/8","[PK_US]/8","1",1,false],[false,"dry pint - US","[dpt_us]","[DPT_US]","dry volume",0.0005506104713575001,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"dry pints; dry pint US; US dry pint; dry pt; dpt; dpt us","UCUM","Vol","Nonclinical","historical unit of dry volume only in the US, but is rarely used today","[dqt_us]/2","[DQT_US]/2","1",1,false],[false,"tablespoon - US","[tbs_us]","[TBS_US]","volume",0.000014786764781250002,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"Tbs; tbsp; tbs us; US tablespoons","UCUM","Vol","Clinical","unit defined as 0.5 US fluid ounces or 3 teaspoons - used only in the US. See [tbs_m] for the unit used internationally and in the US for nutrional labelling. ","[foz_us]/2","[FOZ_US]/2","1",1,false],[false,"teaspoon - US","[tsp_us]","[TSP_US]","volume",0.0000049289215937500005,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"tsp; t; US teaspoons","UCUM","Vol","Nonclinical","unit defined as 1/6 US fluid ounces - used only in the US. See [tsp_m] for the unit used internationally and in the US for nutrional labelling. ","[tbs_us]/3","[TBS_US]/3","1",1,false],[false,"cup - US customary","[cup_us]","[CUP_US]","volume",0.00023658823650000004,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"cup us; us cups","UCUM","Vol","Nonclinical","Unit defined as 1/2 US pint or 16 US tablespoons ≈ 236.59 mL, which is not the standard unit defined by the FDA of 240 mL - see [cup_m] (metric cup)","[tbs_us]","[TBS_US]","16",16,false],[false,"fluid ounce - metric","[foz_m]","[FOZ_M]","fluid volume",0.000029999999999999997,[3,0,0,0,0,0,0],"oz fl","us-volumes",false,null,null,1,false,false,0,"metric fluid ounces; fozs m; fl ozs m","UCUM","Vol","Clinical","unit used only in the US for nutritional labelling, as set by the FDA","mL","ML","30",30,false],[false,"cup - US legal","[cup_m]","[CUP_M]","volume",0.00023999999999999998,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"cup m; metric cups","UCUM","Vol","Clinical","standard unit equal to 240 mL used in the US for nutritional labelling, as defined by the FDA. Note that this is different from the US customary cup (236.59 mL) and the metric cup used in Commonwealth nations (250 mL).","mL","ML","240",240,false],[false,"teaspoon - metric","[tsp_m]","[TSP_M]","volume",0.0000049999999999999996,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"tsp; t; metric teaspoons","UCUM","Vol","Clinical","standard unit used in the US and internationally","mL","mL","5",5,false],[false,"tablespoon - metric","[tbs_m]","[TBS_M]","volume",0.000014999999999999999,[3,0,0,0,0,0,0],null,"us-volumes",false,null,null,1,false,false,0,"metric tablespoons; Tbs; tbsp; T; tbs m","UCUM","Vol","Clinical","standard unit used in the US and internationally","mL","mL","15",15,false],[false,"gallon- British","[gal_br]","[GAL_BR]","volume",0.004546090000000001,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"imperial gallons, UK gallons; British gallons; br gal; imp gal","UCUM","Vol","Nonclinical","Used only in Great Britain and other Commonwealth countries","l","L","4.54609",4.54609,false],[false,"peck - British","[pk_br]","[PK_BR]","volume",0.009092180000000002,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"imperial pecks; British pecks; br pk; imp pk","UCUM","Vol","Nonclinical","unit of dry volume rarely used today (can be used to measure volume of apples)","[gal_br]","[GAL_BR]","2",2,false],[false,"bushel - British","[bu_br]","[BU_BR]","volume",0.03636872000000001,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"British bushels; imperial; br bsh; br bu; imp","UCUM","Vol","Obsolete","Historical unit of dry volume that is rarely used today","[pk_br]","[PK_BR]","4",4,false],[false,"quart - British","[qt_br]","[QT_BR]","volume",0.0011365225000000002,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"British quarts; imperial quarts; br qts","UCUM","Vol","Clinical","Used only in Great Britain and other Commonwealth countries","[gal_br]/4","[GAL_BR]/4","1",1,false],[false,"pint - British","[pt_br]","[PT_BR]","volume",0.0005682612500000001,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"British pints; imperial pints; pt br; br pt; imp pt; pt imp","UCUM","Vol","Clinical","Used only in Great Britain and other Commonwealth countries","[qt_br]/2","[QT_BR]/2","1",1,false],[false,"gill - British","[gil_br]","[GIL_BR]","volume",0.00014206531250000003,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"imperial gills; British gills; imp gill, br gill","UCUM","Vol","Nonclinical","only used in the context of alcohol volume in Great Britain","[pt_br]/4","[PT_BR]/4","1",1,false],[false,"fluid ounce - British","[foz_br]","[FOZ_BR]","volume",0.000028413062500000005,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"British fluid ounces; Imperial fluid ounces; br fozs; imp fozs; br fl ozs","UCUM","Vol","Clinical","Used only in Great Britain and other Commonwealth countries","[gil_br]/5","[GIL_BR]/5","1",1,false],[false,"fluid dram - British","[fdr_br]","[FDR_BR]","volume",0.0000035516328125000006,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"British fluid drams; fdr br","UCUM","Vol","Nonclinical","equal to 1/8 Imperial fluid ounce = 3.55 mL; used informally to mean small amount of liquor, especially Scotch whiskey","[foz_br]/8","[FOZ_BR]/8","1",1,false],[false,"minim - British","[min_br]","[MIN_BR]","volume",5.919388020833334e-8,[3,0,0,0,0,0,0],null,"brit-volumes",false,null,null,1,false,false,0,"min br; br min; ♏ br","UCUM","Vol","Obsolete","","[fdr_br]/60","[FDR_BR]/60","1",1,false],[false,"grain","[gr]","[GR]","mass",0.06479891,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"gr; grains","UCUM","Mass","Nonclinical","an apothecary measure of mass rarely used today","mg","MG","64.79891",64.79891,false],[false,"pound","[lb_av]","[LB_AV]","mass",453.59237,[0,0,1,0,0,0,0],"lb","avoirdupois",false,null,null,1,false,false,0,"avoirdupois pounds, international pounds; av lbs; pounds","UCUM","Mass","Clinical","standard unit used in the US and internationally","[gr]","[GR]","7000",7000,false],[false,"pound force - US","[lbf_av]","[LBF_AV]","force",4448.2216152605,[1,-2,1,0,0,0,0],"lbf","const",false,null,null,1,false,false,0,"lbfs; US lbf; US pound forces","UCUM","Force","Clinical","only rarely needed in health care - see [lb_av] which is the more common unit to express weight","[lb_av].[g]","[LB_AV].[G]","1",1,false],[false,"ounce","[oz_av]","[OZ_AV]","mass",28.349523125,[0,0,1,0,0,0,0],"oz","avoirdupois",false,null,null,1,false,false,0,"ounces; international ounces; avoirdupois ounces; av ozs","UCUM","Mass","Clinical","standard unit used in the US and internationally","[lb_av]/16","[LB_AV]/16","1",1,false],[false,"Dram mass unit","[dr_av]","[DR_AV]","mass",1.7718451953125,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"Dram; drams avoirdupois; avoidupois dram; international dram","UCUM","Mass","Clinical","unit from the avoirdupois system, which is used in the US and internationally","[oz_av]/16","[OZ_AV]/16","1",1,false],[false,"short hundredweight","[scwt_av]","[SCWT_AV]","mass",45359.237,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"hundredweights; s cwt; scwt; avoirdupois","UCUM","Mass","Nonclinical","Used only in the US to equal 100 pounds","[lb_av]","[LB_AV]","100",100,false],[false,"long hundredweight","[lcwt_av]","[LCWT_AV]","mass",50802.345440000005,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"imperial hundredweights; imp cwt; lcwt; avoirdupois","UCUM","Mass","Obsolete","","[lb_av]","[LB_AV]","112",112,false],[false,"short ton - US","[ston_av]","[STON_AV]","mass",907184.74,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"ton; US tons; avoirdupois tons","UCUM","Mass","Clinical","Used only in the US","[scwt_av]","[SCWT_AV]","20",20,false],[false,"long ton - British","[lton_av]","[LTON_AV]","mass",1016046.9088000001,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"imperial tons; weight tons; British long tons; long ton avoirdupois","UCUM","Mass","Nonclinical","Used only in Great Britain and other Commonwealth countries","[lcwt_av]","[LCWT_AV]","20",20,false],[false,"stone - British","[stone_av]","[STONE_AV]","mass",6350.293180000001,[0,0,1,0,0,0,0],null,"avoirdupois",false,null,null,1,false,false,0,"British stones; avoirdupois","UCUM","Mass","Nonclinical","Used primarily in the UK and Ireland to measure body weight","[lb_av]","[LB_AV]","14",14,false],[false,"pennyweight - troy","[pwt_tr]","[PWT_TR]","mass",1.5551738400000001,[0,0,1,0,0,0,0],null,"troy",false,null,null,1,false,false,0,"dwt; denarius weights","UCUM","Mass","Obsolete","historical unit used to measure mass and cost of precious metals","[gr]","[GR]","24",24,false],[false,"ounce - troy","[oz_tr]","[OZ_TR]","mass",31.103476800000003,[0,0,1,0,0,0,0],null,"troy",false,null,null,1,false,false,0,"troy ounces; tr ozs","UCUM","Mass","Nonclinical","unit of mass for precious metals and gemstones only","[pwt_tr]","[PWT_TR]","20",20,false],[false,"pound - troy","[lb_tr]","[LB_TR]","mass",373.2417216,[0,0,1,0,0,0,0],null,"troy",false,null,null,1,false,false,0,"troy pounds; tr lbs","UCUM","Mass","Nonclinical","only used for weighing precious metals","[oz_tr]","[OZ_TR]","12",12,false],[false,"scruple","[sc_ap]","[SC_AP]","mass",1.2959782,[0,0,1,0,0,0,0],null,"apoth",false,null,null,1,false,false,0,"scruples; sc ap","UCUM","Mass","Obsolete","","[gr]","[GR]","20",20,false],[false,"dram - apothecary","[dr_ap]","[DR_AP]","mass",3.8879346,[0,0,1,0,0,0,0],null,"apoth",false,null,null,1,false,false,0,"ʒ; drachm; apothecaries drams; dr ap; dram ap","UCUM","Mass","Nonclinical","unit still used in the US occasionally to measure amount of drugs in pharmacies","[sc_ap]","[SC_AP]","3",3,false],[false,"ounce - apothecary","[oz_ap]","[OZ_AP]","mass",31.1034768,[0,0,1,0,0,0,0],null,"apoth",false,null,null,1,false,false,0,"apothecary ounces; oz ap; ap ozs; ozs ap","UCUM","Mass","Obsolete","","[dr_ap]","[DR_AP]","8",8,false],[false,"pound - apothecary","[lb_ap]","[LB_AP]","mass",373.2417216,[0,0,1,0,0,0,0],null,"apoth",false,null,null,1,false,false,0,"apothecary pounds; apothecaries pounds; ap lb; lb ap; ap lbs; lbs ap","UCUM","Mass","Obsolete","","[oz_ap]","[OZ_AP]","12",12,false],[false,"ounce - metric","[oz_m]","[OZ_M]","mass",28,[0,0,1,0,0,0,0],null,"apoth",false,null,null,1,false,false,0,"metric ounces; m ozs","UCUM","Mass","Clinical","see [oz_av] (the avoirdupois ounce) for the standard ounce used internationally; [oz_m] is equal to 28 grams and is based on the apothecaries' system of mass units which is used in some US pharmacies. ","g","g","28",28,false],[false,"line","[lne]","[LNE]","length",0.002116666666666667,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"British lines; br L; L; l","UCUM","Len","Obsolete","","[in_i]/12","[IN_I]/12","1",1,false],[false,"point (typography)","[pnt]","[PNT]","length",0.0003527777777777778,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"DTP points; desktop publishing point; pt; pnt","UCUM","Len","Nonclinical","typography unit for typesetter's length","[lne]/6","[LNE]/6","1",1,false],[false,"pica (typography)","[pca]","[PCA]","length",0.004233333333333334,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"picas","UCUM","Len","Nonclinical","typography unit for typesetter's length","[pnt]","[PNT]","12",12,false],[false,"Printer's point (typography)","[pnt_pr]","[PNT_PR]","length",0.00035145980000000004,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"pnt pr","UCUM","Len","Nonclinical","typography unit for typesetter's length","[in_i]","[IN_I]","0.013837",0.013837,false],[false,"Printer's pica (typography)","[pca_pr]","[PCA_PR]","length",0.004217517600000001,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"pca pr; Printer's picas","UCUM","Len","Nonclinical","typography unit for typesetter's length","[pnt_pr]","[PNT_PR]","12",12,false],[false,"pied","[pied]","[PIED]","length",0.3248,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"pieds du roi; Paris foot; royal; French; feet","UCUM","Len","Obsolete","","cm","CM","32.48",32.48,false],[false,"pouce","[pouce]","[POUCE]","length",0.027066666666666666,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"historical French inches; French royal inches","UCUM","Len","Obsolete","","[pied]/12","[PIED]/12","1",1,false],[false,"ligne","[ligne]","[LIGNE]","length",0.0022555555555555554,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"Paris lines; lignes","UCUM","Len","Obsolete","","[pouce]/12","[POUCE]/12","1",1,false],[false,"didot","[didot]","[DIDOT]","length",0.0003759259259259259,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"Didot point; dd; Didots Point; didots; points","UCUM","Len","Obsolete","typography unit for typesetter's length","[ligne]/6","[LIGNE]/6","1",1,false],[false,"cicero","[cicero]","[CICERO]","length",0.004511111111111111,[1,0,0,0,0,0,0],null,"typeset",false,null,null,1,false,false,0,"Didot's pica; ciceros; picas","UCUM","Len","Obsolete","typography unit for typesetter's length","[didot]","[DIDOT]","12",12,false],[false,"degrees Fahrenheit","[degF]","[DEGF]","temperature",0.5555555555555556,[0,0,0,0,1,0,0],"°F","heat",false,null,"degF",1,true,false,0,"°F; deg F","UCUM","Temp","Clinical","","K",null,null,0.5555555555555556,false],[false,"degrees Rankine","[degR]","[degR]","temperature",0.5555555555555556,[0,0,0,0,1,0,0],"°R","heat",false,null,null,1,false,false,0,"°R; °Ra; Rankine","UCUM","Temp","Obsolete","Replaced by Kelvin","K/9","K/9","5",5,false],[false,"degrees Réaumur","[degRe]","[degRe]","temperature",1.25,[0,0,0,0,1,0,0],"°Ré","heat",false,null,"degRe",1,true,false,0,"°Ré, °Re, °r; Réaumur; degree Reaumur; Reaumur","UCUM","Temp","Obsolete","replaced by Celsius","K",null,null,1.25,false],[false,"calorie at 15°C","cal_[15]","CAL_[15]","energy",4185.8,[2,-2,1,0,0,0,0],"cal15°C","heat",true,null,null,1,false,false,0,"calorie 15 C; cals 15 C; calories at 15 C","UCUM","Enrg","Nonclinical","equal to 4.1855 joules; calorie most often used in engineering","J","J","4.18580",4.1858,false],[false,"calorie at 20°C","cal_[20]","CAL_[20]","energy",4181.9,[2,-2,1,0,0,0,0],"cal20°C","heat",true,null,null,1,false,false,0,"calorie 20 C; cal 20 C; calories at 20 C","UCUM","Enrg","Clinical","equal to 4.18190 joules. ","J","J","4.18190",4.1819,false],[false,"mean calorie","cal_m","CAL_M","energy",4190.0199999999995,[2,-2,1,0,0,0,0],"calm","heat",true,null,null,1,false,false,0,"mean cals; mean calories","UCUM","Enrg","Clinical","equal to 4.19002 joules. ","J","J","4.19002",4.19002,false],[false,"international table calorie","cal_IT","CAL_IT","energy",4186.8,[2,-2,1,0,0,0,0],"calIT","heat",true,null,null,1,false,false,0,"calories IT; IT cals; international steam table calories","UCUM","Enrg","Nonclinical","used in engineering steam tables and defined as 1/860 international watt-hour; equal to 4.1868 joules","J","J","4.1868",4.1868,false],[false,"thermochemical calorie","cal_th","CAL_TH","energy",4184,[2,-2,1,0,0,0,0],"calth","heat",true,null,null,1,false,false,0,"thermochemical calories; th cals","UCUM","Enrg","Clinical","equal to 4.184 joules; used as the unit in medicine and biochemistry (equal to cal)","J","J","4.184",4.184,false],[false,"calorie","cal","CAL","energy",4184,[2,-2,1,0,0,0,0],"cal","heat",true,null,null,1,false,false,0,"gram calories; small calories","UCUM","Enrg","Clinical","equal to 4.184 joules (the same value as the thermochemical calorie, which is the most common calorie used in medicine and biochemistry)","cal_th","CAL_TH","1",1,false],[false,"nutrition label Calories","[Cal]","[CAL]","energy",4184000,[2,-2,1,0,0,0,0],"Cal","heat",false,null,null,1,false,false,0,"food calories; Cal; kcal","UCUM","Eng","Clinical","","kcal_th","KCAL_TH","1",1,false],[false,"British thermal unit at 39°F","[Btu_39]","[BTU_39]","energy",1059670,[2,-2,1,0,0,0,0],"Btu39°F","heat",false,null,null,1,false,false,0,"BTU 39F; BTU 39 F; B.T.U. 39 F; B.Th.U. 39 F; BThU 39 F; British thermal units","UCUM","Eng","Nonclinical","equal to 1.05967 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","kJ","kJ","1.05967",1.05967,false],[false,"British thermal unit at 59°F","[Btu_59]","[BTU_59]","energy",1054800,[2,-2,1,0,0,0,0],"Btu59°F","heat",false,null,null,1,false,false,0,"BTU 59 F; BTU 59F; B.T.U. 59 F; B.Th.U. 59 F; BThU 59F; British thermal units","UCUM","Eng","Nonclinical","equal to 1.05480 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","kJ","kJ","1.05480",1.0548,false],[false,"British thermal unit at 60°F","[Btu_60]","[BTU_60]","energy",1054680,[2,-2,1,0,0,0,0],"Btu60°F","heat",false,null,null,1,false,false,0,"BTU 60 F; BTU 60F; B.T.U. 60 F; B.Th.U. 60 F; BThU 60 F; British thermal units 60 F","UCUM","Eng","Nonclinical","equal to 1.05468 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","kJ","kJ","1.05468",1.05468,false],[false,"mean British thermal unit","[Btu_m]","[BTU_M]","energy",1055870,[2,-2,1,0,0,0,0],"Btum","heat",false,null,null,1,false,false,0,"BTU mean; B.T.U. mean; B.Th.U. mean; BThU mean; British thermal units mean; ","UCUM","Eng","Nonclinical","equal to 1.05587 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","kJ","kJ","1.05587",1.05587,false],[false,"international table British thermal unit","[Btu_IT]","[BTU_IT]","energy",1055055.85262,[2,-2,1,0,0,0,0],"BtuIT","heat",false,null,null,1,false,false,0,"BTU IT; B.T.U. IT; B.Th.U. IT; BThU IT; British thermal units IT","UCUM","Eng","Nonclinical","equal to 1.055 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","kJ","kJ","1.05505585262",1.05505585262,false],[false,"thermochemical British thermal unit","[Btu_th]","[BTU_TH]","energy",1054350,[2,-2,1,0,0,0,0],"Btuth","heat",false,null,null,1,false,false,0,"BTU Th; B.T.U. Th; B.Th.U. Th; BThU Th; thermochemical British thermal units","UCUM","Eng","Nonclinical","equal to 1.054350 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","kJ","kJ","1.054350",1.05435,false],[false,"British thermal unit","[Btu]","[BTU]","energy",1054350,[2,-2,1,0,0,0,0],"btu","heat",false,null,null,1,false,false,0,"BTU; B.T.U. ; B.Th.U.; BThU; British thermal units","UCUM","Eng","Nonclinical","equal to the thermochemical British thermal unit equal to 1.054350 kJ; used as a measure of power in the electric power, steam generation, heating, and air conditioning industries","[Btu_th]","[BTU_TH]","1",1,false],[false,"horsepower - mechanical","[HP]","[HP]","power",745699.8715822703,[2,-3,1,0,0,0,0],null,"heat",false,null,null,1,false,false,0,"imperial horsepowers","UCUM","EngRat","Nonclinical","refers to mechanical horsepower, which is unit used to measure engine power primarily in the US. ","[ft_i].[lbf_av]/s","[FT_I].[LBF_AV]/S","550",550,false],[false,"tex","tex","TEX","linear mass density (of textile thread)",0.001,[-1,0,1,0,0,0,0],"tex","heat",true,null,null,1,false,false,0,"linear mass density; texes","UCUM","","Clinical","unit of linear mass density for fibers equal to gram per 1000 meters","g/km","G/KM","1",1,false],[false,"Denier (linear mass density)","[den]","[DEN]","linear mass density (of textile thread)",0.0001111111111111111,[-1,0,1,0,0,0,0],"den","heat",false,null,null,1,false,false,0,"den; deniers","UCUM","","Nonclinical","equal to the mass in grams per 9000 meters of the fiber (1 denier = 1 strand of silk)","g/9/km","G/9/KM","1",1,false],[false,"meter of water column","m[H2O]","M[H2O]","pressure",9806650,[-1,-2,1,0,0,0,0],"m HO2","clinical",true,null,null,1,false,false,0,"mH2O; m H2O; meters of water column; metres; pressure","UCUM","Pres","Clinical","","kPa","KPAL","980665e-5",9.80665,false],[false,"meter of mercury column","m[Hg]","M[HG]","pressure",133322000,[-1,-2,1,0,0,0,0],"m Hg","clinical",true,null,null,1,false,false,0,"mHg; m Hg; meters of mercury column; metres; pressure","UCUM","Pres","Clinical","","kPa","KPAL","133.3220",133.322,false],[false,"inch of water column","[in_i'H2O]","[IN_I'H2O]","pressure",249088.91000000003,[-1,-2,1,0,0,0,0],"in HO2","clinical",false,null,null,1,false,false,0,"inches WC; inAq; in H2O; inch of water gauge; iwg; pressure","UCUM","Pres","Clinical","unit of pressure, especially in respiratory and ventilation care","m[H2O].[in_i]/m","M[H2O].[IN_I]/M","1",1,false],[false,"inch of mercury column","[in_i'Hg]","[IN_I'HG]","pressure",3386378.8000000003,[-1,-2,1,0,0,0,0],"in Hg","clinical",false,null,null,1,false,false,0,"inHg; in Hg; pressure; inches","UCUM","Pres","Clinical","unit of pressure used in US to measure barometric pressure and occasionally blood pressure (see mm[Hg] for unit used internationally)","m[Hg].[in_i]/m","M[HG].[IN_I]/M","1",1,false],[false,"peripheral vascular resistance unit","[PRU]","[PRU]","fluid resistance",133322000000,[-4,-1,1,0,0,0,0],"P.R.U.","clinical",false,null,null,1,false,false,0,"peripheral vascular resistance units; peripheral resistance unit; peripheral resistance units; PRU","UCUM","FldResist","Clinical","used to assess blood flow in the capillaries; equal to 1 mmH.min/mL = 133.3 Pa·min/mL","mm[Hg].s/ml","MM[HG].S/ML","1",1,false],[false,"Wood unit","[wood'U]","[WOOD'U]","fluid resistance",7999320000,[-4,-1,1,0,0,0,0],"Wood U.","clinical",false,null,null,1,false,false,0,"hybrid reference units; HRU; mmHg.min/L; vascular resistance","UCUM","Pres","Clinical","simplified unit of measurement for for measuring pulmonary vascular resistance that uses pressure; equal to mmHg.min/L","mm[Hg].min/L","MM[HG].MIN/L","1",1,false],[false,"diopter (lens)","[diop]","[DIOP]","refraction of a lens",1,[1,0,0,0,0,0,0],"dpt","clinical",false,null,"inv",1,false,false,0,"diopters; diop; dioptre; dpt; refractive power","UCUM","InvLen","Clinical","unit of optical power of lens represented by inverse meters (m^-1)","m","/M","1",1,false],[false,"prism diopter (magnifying power)","[p'diop]","[P'DIOP]","refraction of a prism",1,[0,0,0,1,0,0,0],"PD","clinical",false,null,"tanTimes100",1,true,false,0,"diopters; dioptres; p diops; pdiop; dpt; pdptr; Δ; cm/m; centimeter per meter; centimetre; metre","UCUM","Angle","Clinical","unit for prism correction in eyeglass prescriptions","rad",null,null,1,false],[false,"percent of slope","%[slope]","%[SLOPE]","slope",0.017453292519943295,[0,0,0,1,0,0,0],"%","clinical",false,null,"100tan",1,true,false,0,"% slope; %slope; percents slopes","UCUM","VelFr; ElpotRatFr; VelRtoFr; AccelFr","Clinical","","deg",null,null,1,false],[false,"mesh","[mesh_i]","[MESH_I]","lineic number",0.025400000000000002,[1,0,0,0,0,0,0],null,"clinical",false,null,"inv",1,false,false,0,"meshes","UCUM","NLen (lineic number)","Clinical","traditional unit of length defined as the number of strands or particles per inch","[in_i]","/[IN_I]","1",1,false],[false,"French (catheter gauge) ","[Ch]","[CH]","gauge of catheters",0.0003333333333333333,[1,0,0,0,0,0,0],"Ch","clinical",false,null,null,1,false,false,0,"Charrières, French scales; French gauges; Fr, Fg, Ga, FR, Ch","UCUM","Len; Circ; Diam","Clinical","","mm/3","MM/3","1",1,false],[false,"drop - metric (1/20 mL)","[drp]","[DRP]","volume",5e-8,[3,0,0,0,0,0,0],"drp","clinical",false,null,null,1,false,false,0,"drop dosing units; metric drops; gtt","UCUM","Vol","Clinical","standard unit used in the US and internationally for clinical medicine but note that although [drp] is defined as 1/20 milliliter, in practice, drop sizes will vary due to external factors","ml/20","ML/20","1",1,false],[false,"Hounsfield unit","[hnsf'U]","[HNSF'U]","x-ray attenuation",1,[0,0,0,0,0,0,0],"HF","clinical",false,null,null,1,false,false,0,"HU; units","UCUM","","Clinical","used to measure X-ray attenuation, especially in CT scans.","1","1","1",1,false],[false,"Metabolic Equivalent of Task ","[MET]","[MET]","metabolic cost of physical activity",5.833333333333334e-11,[3,-1,-1,0,0,0,0],"MET","clinical",false,null,null,1,false,false,0,"metabolic equivalents","UCUM","RelEngRat","Clinical","unit used to measure rate of energy expenditure per power in treadmill and other functional tests","mL/min/kg","ML/MIN/KG","3.5",3.5,false],[false,"homeopathic potency of decimal series (retired)","[hp'_X]","[HP'_X]","homeopathic potency (retired)",1,[0,0,0,0,0,0,0],"X","clinical",false,null,"hpX",1,true,false,0,null,"UCUM",null,null,null,"1",null,null,1,false],[false,"homeopathic potency of centesimal series (retired)","[hp'_C]","[HP'_C]","homeopathic potency (retired)",1,[0,0,0,0,0,0,0],"C","clinical",false,null,"hpC",1,true,false,0,null,"UCUM",null,null,null,"1",null,null,1,false],[false,"homeopathic potency of millesimal series (retired)","[hp'_M]","[HP'_M]","homeopathic potency (retired)",1,[0,0,0,0,0,0,0],"M","clinical",false,null,"hpM",1,true,false,0,null,"UCUM",null,null,null,"1",null,null,1,false],[false,"homeopathic potency of quintamillesimal series (retired)","[hp'_Q]","[HP'_Q]","homeopathic potency (retired)",1,[0,0,0,0,0,0,0],"Q","clinical",false,null,"hpQ",1,true,false,0,null,"UCUM",null,null,null,"1",null,null,1,false],[false,"homeopathic potency of decimal hahnemannian series","[hp_X]","[HP_X]","homeopathic potency (Hahnemann)",1,[0,0,0,0,0,0,0],"X","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of centesimal hahnemannian series","[hp_C]","[HP_C]","homeopathic potency (Hahnemann)",1,[0,0,0,0,0,0,0],"C","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of millesimal hahnemannian series","[hp_M]","[HP_M]","homeopathic potency (Hahnemann)",1,[0,0,0,0,0,0,0],"M","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of quintamillesimal hahnemannian series","[hp_Q]","[HP_Q]","homeopathic potency (Hahnemann)",1,[0,0,0,0,0,0,0],"Q","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of decimal korsakovian series","[kp_X]","[KP_X]","homeopathic potency (Korsakov)",1,[0,0,0,0,0,0,0],"X","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of centesimal korsakovian series","[kp_C]","[KP_C]","homeopathic potency (Korsakov)",1,[0,0,0,0,0,0,0],"C","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of millesimal korsakovian series","[kp_M]","[KP_M]","homeopathic potency (Korsakov)",1,[0,0,0,0,0,0,0],"M","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"homeopathic potency of quintamillesimal korsakovian series","[kp_Q]","[KP_Q]","homeopathic potency (Korsakov)",1,[0,0,0,0,0,0,0],"Q","clinical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"equivalent","eq","EQ","amount of substance",6.0221367e+23,[0,0,0,0,0,0,0],"eq","chemical",true,null,null,1,false,false,1,"equivalents","UCUM","Sub","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"osmole","osm","OSM","amount of substance (dissolved particles)",6.0221367e+23,[0,0,0,0,0,0,0],"osm","chemical",true,null,null,1,false,false,1,"osmoles; osmols","UCUM","Osmol","Clinical","the number of moles of solute that contribute to the osmotic pressure of a solution","mol","MOL","1",1,false],[false,"pH","[pH]","[PH]","acidity",6.0221366999999994e+26,[-3,0,0,0,0,0,0],"pH","chemical",false,null,"pH",1,true,false,0,"pH scale","UCUM","LogCnc","Clinical","Log concentration of H+","mol/l",null,null,1,false],[false,"gram percent","g%","G%","mass concentration",10000,[-3,0,1,0,0,0,0],"g%","chemical",true,null,null,1,false,false,0,"gram %; gram%; grams per deciliter; g/dL; gm per dL; gram percents","UCUM","MCnc","Clinical","equivalent to unit gram per deciliter (g/dL), a unit often used in medical tests to represent solution concentrations","g/dl","G/DL","1",1,false],[false,"Svedberg unit","[S]","[S]","sedimentation coefficient",1e-13,[0,1,0,0,0,0,0],"S","chemical",false,null,null,1,false,false,0,"Sv; 10^-13 seconds; 100 fs; 100 femtoseconds","UCUM","Time","Clinical","unit of time used in measuring particle's sedimentation rate, usually after centrifugation. ","s","10*-13.S","1",1e-13,false],[false,"high power field (microscope)","[HPF]","[HPF]","view area in microscope",1,[0,0,0,0,0,0,0],"HPF","chemical",false,null,null,1,false,false,0,"HPF","UCUM","Area","Clinical","area visible under the maximum magnification power of the objective in microscopy (usually 400x)\n","1","1","1",1,false],[false,"low power field (microscope)","[LPF]","[LPF]","view area in microscope",1,[0,0,0,0,0,0,0],"LPF","chemical",false,null,null,1,false,false,0,"LPF; fields","UCUM","Area","Clinical","area visible under the low magnification of the objective in microscopy (usually 100 x)\n","1","1","100",100,false],[false,"katal","kat","KAT","catalytic activity",6.0221367e+23,[0,-1,0,0,0,0,0],"kat","chemical",true,null,null,1,false,false,1,"mol/secs; moles per second; mol*sec-1; mol*s-1; mol.s-1; katals; catalytic activity; enzymatic; enzyme units; activities","UCUM","CAct","Clinical","kat is a unit of catalytic activity with base units = mol/s. Rarely used because its units are too large to practically express catalytic activity. See enzyme unit [U] which is the standard unit for catalytic activity.","mol/s","MOL/S","1",1,false],[false,"enzyme unit","U","U","catalytic activity",10036894500000000,[0,-1,0,0,0,0,0],"U","chemical",true,null,null,1,false,false,1,"micromoles per minute; umol/min; umol per minute; umol min-1; enzymatic activity; enzyme activity","UCUM","CAct","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"international unit - arbitrary","[iU]","[IU]","arbitrary",1,[0,0,0,0,0,0,0],"IU","chemical",true,null,null,1,false,true,0,"international units; IE; F2","UCUM","Arb","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","1","1","1",1,false],[false,"international unit - arbitrary","[IU]","[IU]","arbitrary",1,[0,0,0,0,0,0,0],"i.U.","chemical",true,null,null,1,false,true,0,"international units; IE; F2","UCUM","Arb","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"arbitary unit","[arb'U]","[ARB'U]","arbitrary",1,[0,0,0,0,0,0,0],"arb. U","chemical",false,null,null,1,false,true,0,"arbitary units; arb units; arbU","UCUM","Arb","Clinical","relative unit of measurement to show the ratio of test measurement to reference measurement","1","1","1",1,false],[false,"United States Pharmacopeia unit","[USP'U]","[USP'U]","arbitrary",1,[0,0,0,0,0,0,0],"U.S.P.","chemical",false,null,null,1,false,true,0,"USP U; USP'U","UCUM","Arb","Clinical","a dose unit to express potency of drugs and vitamins defined by the United States Pharmacopoeia; usually 1 USP = 1 IU","1","1","1",1,false],[false,"GPL unit","[GPL'U]","[GPL'U]","biologic activity of anticardiolipin IgG",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"GPL Units; GPL U; IgG anticardiolipin units; IgG Phospholipid","UCUM","ACnc; AMass","Clinical","Units for an antiphospholipid test","1","1","1",1,false],[false,"MPL unit","[MPL'U]","[MPL'U]","biologic activity of anticardiolipin IgM",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"MPL units; MPL U; MPL'U; IgM anticardiolipin units; IgM Phospholipid Units ","UCUM","ACnc","Clinical","units for antiphospholipid test","1","1","1",1,false],[false,"APL unit","[APL'U]","[APL'U]","biologic activity of anticardiolipin IgA",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"APL units; APL U; IgA anticardiolipin; IgA Phospholipid; biologic activity of","UCUM","AMass; ACnc","Clinical","Units for an anti phospholipid syndrome test","1","1","1",1,false],[false,"Bethesda unit","[beth'U]","[BETH'U]","biologic activity of factor VIII inhibitor",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"BU","UCUM","ACnc","Clinical","measures of blood coagulation inhibitior for many blood factors","1","1","1",1,false],[false,"anti factor Xa unit","[anti'Xa'U]","[ANTI'XA'U]","biologic activity of factor Xa inhibitor (heparin)",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"units","UCUM","ACnc","Clinical","[anti'Xa'U] unit is equivalent to and can be converted to IU/mL. ","1","1","1",1,false],[false,"Todd unit","[todd'U]","[TODD'U]","biologic activity antistreptolysin O",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"units","UCUM","InvThres; RtoThres","Clinical","the unit for the results of the testing for antistreptolysin O (ASO)","1","1","1",1,false],[false,"Dye unit","[dye'U]","[DYE'U]","biologic activity of amylase",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"units","UCUM","CCnc","Obsolete","equivalent to the Somogyi unit, which is an enzyme unit for amylase but better to use U, the standard enzyme unit for measuring catalytic activity","1","1","1",1,false],[false,"Somogyi unit","[smgy'U]","[SMGY'U]","biologic activity of amylase",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"Somogyi units; smgy U","UCUM","CAct","Clinical","measures the enzymatic activity of amylase in blood serum - better to use base units mg/mL ","1","1","1",1,false],[false,"Bodansky unit","[bdsk'U]","[BDSK'U]","biologic activity of phosphatase",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"","UCUM","ACnc","Obsolete","Enzyme unit specific to alkaline phosphatase - better to use standard enzyme unit of U","1","1","1",1,false],[false,"King-Armstrong unit","[ka'U]","[KA'U]","biologic activity of phosphatase",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"King-Armstrong Units; King units","UCUM","AMass","Obsolete","enzyme units for acid phosphatase - better to use enzyme unit [U]","1","1","1",1,false],[false,"Kunkel unit","[knk'U]","[KNK'U]","arbitrary biologic activity",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,null,"UCUM",null,null,null,"1","1","1",1,false],[false,"Mac Lagan unit","[mclg'U]","[MCLG'U]","arbitrary biologic activity",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"galactose index; galactose tolerance test; thymol turbidity test unit; mclg U; units; indexes","UCUM","ACnc","Obsolete","unit for liver tests - previously used in thymol turbidity tests for liver disease diagnoses, and now is sometimes referred to in the oral galactose tolerance test","1","1","1",1,false],[false,"tuberculin unit","[tb'U]","[TB'U]","biologic activity of tuberculin",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"TU; units","UCUM","Arb","Clinical","amount of tuberculin antigen -usually in reference to a TB skin test ","1","1","1",1,false],[false,"50% cell culture infectious dose","[CCID_50]","[CCID_50]","biologic activity (infectivity) of an infectious agent preparation",1,[0,0,0,0,0,0,0],"CCID50","chemical",false,null,null,1,false,true,0,"CCID50; 50% cell culture infective doses","UCUM","NumThres","Clinical","","1","1","1",1,false],[false,"50% tissue culture infectious dose","[TCID_50]","[TCID_50]","biologic activity (infectivity) of an infectious agent preparation",1,[0,0,0,0,0,0,0],"TCID50","chemical",false,null,null,1,false,true,0,"TCID50; 50% tissue culture infective dose","UCUM","NumThres","Clinical","","1","1","1",1,false],[false,"50% embryo infectious dose","[EID_50]","[EID_50]","biologic activity (infectivity) of an infectious agent preparation",1,[0,0,0,0,0,0,0],"EID50","chemical",false,null,null,1,false,true,0,"EID50; 50% embryo infective doses; EID50 Egg Infective Dosage","UCUM","thresNum","Clinical","","1","1","1",1,false],[false,"plaque forming units","[PFU]","[PFU]","amount of an infectious agent",1,[0,0,0,0,0,0,0],"PFU","chemical",false,null,null,1,false,true,0,"PFU","UCUM","ACnc","Clinical","tests usually report unit as number of PFU per unit volume","1","1","1",1,false],[false,"focus forming units (cells)","[FFU]","[FFU]","amount of an infectious agent",1,[0,0,0,0,0,0,0],"FFU","chemical",false,null,null,1,false,true,0,"FFU","UCUM","EntNum","Clinical","","1","1","1",1,false],[false,"colony forming units","[CFU]","[CFU]","amount of a proliferating organism",1,[0,0,0,0,0,0,0],"CFU","chemical",false,null,null,1,false,true,0,"CFU","UCUM","Num","Clinical","","1","1","1",1,false],[false,"index of reactivity (allergen)","[IR]","[IR]","amount of an allergen callibrated through in-vivo testing using the Stallergenes® method.",1,[0,0,0,0,0,0,0],"IR","chemical",false,null,null,1,false,true,0,"IR; indexes","UCUM","Acnc","Clinical","amount of an allergen callibrated through in-vivo testing using the Stallergenes method. Usually reported in tests as IR/mL","1","1","1",1,false],[false,"bioequivalent allergen unit","[BAU]","[BAU]","amount of an allergen callibrated through in-vivo testing based on the ID50EAL method of (intradermal dilution for 50mm sum of erythema diameters",1,[0,0,0,0,0,0,0],"BAU","chemical",false,null,null,1,false,true,0,"BAU; Bioequivalent Allergy Units; bioequivalent allergen units","UCUM","Arb","Clinical","","1","1","1",1,false],[false,"allergy unit","[AU]","[AU]","procedure defined amount of an allergen using some reference standard",1,[0,0,0,0,0,0,0],"AU","chemical",false,null,null,1,false,true,0,"allergy units; allergen units; AU","UCUM","Arb","Clinical","Most standard test allergy units are reported as [IU] or as %. ","1","1","1",1,false],[false,"allergen unit for Ambrosia artemisiifolia","[Amb'a'1'U]","[AMB'A'1'U]","procedure defined amount of the major allergen of ragweed.",1,[0,0,0,0,0,0,0],"Amb a 1 U","chemical",false,null,null,1,false,true,0,"Amb a 1 unit; Antigen E; AgE U; allergen units","UCUM","Arb","Clinical","Amb a 1 is the major allergen in short ragweed, and can be converted Bioequivalent allergen units (BAU) where 350 Amb a 1 U/mL = 100,000 BAU/mL","1","1","1",1,false],[false,"protein nitrogen unit (allergen testing)","[PNU]","[PNU]","procedure defined amount of a protein substance",1,[0,0,0,0,0,0,0],"PNU","chemical",false,null,null,1,false,true,0,"protein nitrogen units; PNU","UCUM","Mass","Clinical","defined as 0.01 ug of phosphotungstic acid-precipitable protein nitrogen. Being replaced by bioequivalent allergy units (BAU).","1","1","1",1,false],[false,"Limit of flocculation","[Lf]","[LF]","procedure defined amount of an antigen substance",1,[0,0,0,0,0,0,0],"Lf","chemical",false,null,null,1,false,true,0,"Lf doses","UCUM","Arb","Clinical","the antigen content forming 1:1 ratio against 1 unit of antitoxin","1","1","1",1,false],[false,"D-antigen unit (polio)","[D'ag'U]","[D'AG'U]","procedure defined amount of a poliomyelitis d-antigen substance",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"DAgU; units","UCUM","Acnc","Clinical","unit of potency of poliovirus vaccine used for poliomyelitis prevention reported as D antigen units/mL. The unit is poliovirus type-specific.","1","1","1",1,false],[false,"fibrinogen equivalent units","[FEU]","[FEU]","amount of fibrinogen broken down into the measured d-dimers",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"FEU","UCUM","MCnc","Clinical","Note both the FEU and DDU units are used to report D-dimer measurements. 1 DDU = 1/2 FFU","1","1","1",1,false],[false,"ELISA unit","[ELU]","[ELU]","arbitrary ELISA unit",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"Enzyme-Linked Immunosorbent Assay Units; ELU; EL. U","UCUM","ACnc","Clinical","","1","1","1",1,false],[false,"Ehrlich units (urobilinogen)","[EU]","[EU]","Ehrlich unit",1,[0,0,0,0,0,0,0],null,"chemical",false,null,null,1,false,true,0,"EU/dL; mg{urobilinogen}/dL","UCUM","ACnc","Clinical","","1","1","1",1,false],[false,"neper","Np","NEP","level",1,[0,0,0,0,0,0,0],"Np","levels",true,null,"ln",1,true,false,0,"nepers","UCUM","LogRto","Clinical","logarithmic unit for ratios of measurements of physical field and power quantities, such as gain and loss of electronic signals","1",null,null,1,false],[false,"bel","B","B","level",1,[0,0,0,0,0,0,0],"B","levels",true,null,"lg",1,true,false,0,"bels","UCUM","LogRto","Clinical","Logarithm of the ratio of power- or field-type quantities; usually expressed in decibels ","1",null,null,1,false],[false,"bel sound pressure","B[SPL]","B[SPL]","pressure level",0.02,[-1,-2,1,0,0,0,0],"B(SPL)","levels",true,null,"lgTimes2",1,true,false,0,"bel SPL; B SPL; sound pressure bels","UCUM","LogRto","Clinical","used to measure sound level in acoustics","Pa",null,null,0.00002,false],[false,"bel volt","B[V]","B[V]","electric potential level",1000,[2,-2,1,0,0,-1,0],"B(V)","levels",true,null,"lgTimes2",1,true,false,0,"bel V; B V; volts bels","UCUM","LogRtoElp","Clinical","used to express power gain in electrical circuits","V",null,null,1,false],[false,"bel millivolt","B[mV]","B[MV]","electric potential level",1,[2,-2,1,0,0,-1,0],"B(mV)","levels",true,null,"lgTimes2",1,true,false,0,"bel mV; B mV; millivolt bels; 10^-3V bels; 10*-3V ","UCUM","LogRtoElp","Clinical","used to express power gain in electrical circuits","mV",null,null,1,false],[false,"bel microvolt","B[uV]","B[UV]","electric potential level",0.001,[2,-2,1,0,0,-1,0],"B(μV)","levels",true,null,"lgTimes2",1,true,false,0,"bel uV; B uV; microvolts bels; 10^-6V bel; 10*-6V bel","UCUM","LogRto","Clinical","used to express power gain in electrical circuits","uV",null,null,1,false],[false,"bel 10 nanovolt","B[10.nV]","B[10.NV]","electric potential level",0.000010000000000000003,[2,-2,1,0,0,-1,0],"B(10 nV)","levels",true,null,"lgTimes2",1,true,false,0,"bel 10 nV; B 10 nV; 10 nanovolts bels","UCUM","LogRtoElp","Clinical","used to express power gain in electrical circuits","nV",null,null,10,false],[false,"bel watt","B[W]","B[W]","power level",1000,[2,-3,1,0,0,0,0],"B(W)","levels",true,null,"lg",1,true,false,0,"bel W; b W; b Watt; Watts bels","UCUM","LogRto","Clinical","used to express power","W",null,null,1,false],[false,"bel kilowatt","B[kW]","B[KW]","power level",1000000,[2,-3,1,0,0,0,0],"B(kW)","levels",true,null,"lg",1,true,false,0,"bel kW; B kW; kilowatt bel; kW bel; kW B","UCUM","LogRto","Clinical","used to express power","kW",null,null,1,false],[false,"stere","st","STR","volume",1,[3,0,0,0,0,0,0],"st","misc",true,null,null,1,false,false,0,"stère; m3; cubic meter; m^3; meters cubed; metre","UCUM","Vol","Nonclinical","equal to one cubic meter, usually used for measuring firewoord","m3","M3","1",1,false],[false,"Ångström","Ao","AO","length",1.0000000000000002e-10,[1,0,0,0,0,0,0],"Å","misc",false,null,null,1,false,false,0,"Å; Angstroms; Ao; Ångströms","UCUM","Len","Clinical","equal to 10^-10 meters; used to express wave lengths and atom scaled differences ","nm","NM","0.1",0.1,false],[false,"barn","b","BRN","action area",9.999999999999999e-29,[2,0,0,0,0,0,0],"b","misc",false,null,null,1,false,false,0,"barns","UCUM","Area","Clinical","used in high-energy physics to express cross-sectional areas","fm2","FM2","100",100,false],[false,"technical atmosphere","att","ATT","pressure",98066499.99999999,[-1,-2,1,0,0,0,0],"at","misc",false,null,null,1,false,false,0,"at; tech atm; tech atmosphere; kgf/cm2; atms; atmospheres","UCUM","Pres","Obsolete","non-SI unit of pressure equal to one kilogram-force per square centimeter","kgf/cm2","KGF/CM2","1",1,false],[false,"mho","mho","MHO","electric conductance",0.001,[-2,1,-1,0,0,2,0],"mho","misc",true,null,null,1,false,false,0,"siemens; ohm reciprocals; Ω^−1; Ω-1 ","UCUM","","Obsolete","unit of electric conductance (the inverse of electrical resistance) equal to ohm^-1","S","S","1",1,false],[false,"pound per square inch","[psi]","[PSI]","pressure",6894757.293168359,[-1,-2,1,0,0,0,0],"psi","misc",false,null,null,1,false,false,0,"psi; lb/in2; lb per in2","UCUM","Pres","Clinical","","[lbf_av]/[in_i]2","[LBF_AV]/[IN_I]2","1",1,false],[false,"circle - plane angle","circ","CIRC","plane angle",6.283185307179586,[0,0,0,1,0,0,0],"circ","misc",false,null,null,1,false,false,0,"angles; circles","UCUM","Angle","Clinical","","[pi].rad","[PI].RAD","2",2,false],[false,"spere - solid angle","sph","SPH","solid angle",12.566370614359172,[0,0,0,2,0,0,0],"sph","misc",false,null,null,1,false,false,0,"speres","UCUM","Angle","Clinical","equal to the solid angle of an entire sphere = 4πsr (sr = steradian) ","[pi].sr","[PI].SR","4",4,false],[false,"metric carat","[car_m]","[CAR_M]","mass",0.2,[0,0,1,0,0,0,0],"ctm","misc",false,null,null,1,false,false,0,"carats; ct; car m","UCUM","Mass","Nonclinical","unit of mass for gemstones","g","G","2e-1",0.2,false],[false,"carat of gold alloys","[car_Au]","[CAR_AU]","mass fraction",0.041666666666666664,[0,0,0,0,0,0,0],"ctAu","misc",false,null,null,1,false,false,0,"karats; k; kt; car au; carats","UCUM","MFr","Nonclinical","unit of purity for gold alloys","/24","/24","1",1,false],[false,"Smoot","[smoot]","[SMOOT]","length",1.7018000000000002,[1,0,0,0,0,0,0],null,"misc",false,null,null,1,false,false,0,"","UCUM","Len","Nonclinical","prank unit of length from MIT","[in_i]","[IN_I]","67",67,false],[false,"meter per square seconds per square root of hertz","[m/s2/Hz^(1/2)]","[M/S2/HZ^(1/2)]","amplitude spectral density",1,[2,-3,0,0,0,0,0],null,"misc",false,null,"sqrt",1,true,false,0,"m/s2/(Hz^.5); m/s2/(Hz^(1/2)); m per s2 per Hz^1/2","UCUM","","Constant","measures amplitude spectral density, and is equal to the square root of power spectral density\n ","m2/s4/Hz",null,null,1,false],[false,"bit - logarithmic","bit_s","BIT_S","amount of information",1,[0,0,0,0,0,0,0],"bits","infotech",false,null,"ld",1,true,false,0,"bit-s; bit s; bit logarithmic","UCUM","LogA","Nonclinical","defined as the log base 2 of the number of distinct signals; cannot practically be used to express more than 1000 bits\n\nIn information theory, the definition of the amount of self-information and information entropy is often expressed with the binary logarithm (log base 2)","1",null,null,1,false],[false,"bit","bit","BIT","amount of information",1,[0,0,0,0,0,0,0],"bit","infotech",true,null,null,1,false,false,0,"bits","UCUM","","Nonclinical","dimensionless information unit of 1 used in computing and digital communications","1","1","1",1,false],[false,"byte","By","BY","amount of information",8,[0,0,0,0,0,0,0],"B","infotech",true,null,null,1,false,false,0,"bytes","UCUM","","Nonclinical","equal to 8 bits","bit","bit","8",8,false],[false,"baud","Bd","BD","signal transmission rate",1,[0,1,0,0,0,0,0],"Bd","infotech",true,null,"inv",1,false,false,0,"Bd; bauds","UCUM","Freq","Nonclinical","unit to express rate in symbols per second or pulses per second. ","s","/s","1",1,false],[false,"per twelve hour","/(12.h)","/HR","",0.000023148148148148147,[0,-1,0,0,0,0,0],"/h",null,false,null,null,1,false,false,0,"per 12 hours; 12hrs; 12 hrs; /12hrs","LOINC","Rat","Clinical","",null,null,null,null,false],[false,"per arbitrary unit","/[arb'U]","/[ARB'U]","",1,[0,0,0,0,0,0,0],"/arb/ U",null,false,null,null,1,false,true,0,"/arbU","LOINC","InvA ","Clinical","",null,null,null,null,false],[false,"per high power field","/[HPF]","/[HPF]","",1,[0,0,0,0,0,0,0],"/HPF",null,false,null,null,1,false,false,0,"/HPF; per HPF","LOINC","Naric","Clinical","",null,null,null,null,false],[false,"per international unit","/[IU]","/[IU]","",1,[0,0,0,0,0,0,0],"/i/U.",null,false,null,null,1,false,true,0,"international units; /IU; per IU","LOINC","InvA","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)",null,null,null,null,false],[false,"per low power field","/[LPF]","/[LPF]","",1,[0,0,0,0,0,0,0],"/LPF",null,false,null,null,1,false,false,0,"/LPF; per LPF","LOINC","Naric","Clinical","",null,null,null,null,false],[false,"per 10 billion ","/10*10","/10*10","",1e-10,[0,0,0,0,0,0,0],"/1010<.sup>",null,false,null,null,1,false,false,0,"/10^10; per 10*10","LOINC","NFr","Clinical","used for counting entities, e.g. blood cells; usually these kinds of terms have numerators such as moles or milligrams, and counting that amount per the number in the denominator",null,null,null,null,false],[false,"per trillion ","/10*12","/10*12","",1e-12,[0,0,0,0,0,0,0],"/1012<.sup>",null,false,null,null,1,false,false,0,"/10^12; per 10*12","LOINC","NFr","Clinical","used for counting entities, e.g. blood cells; usually these kinds of terms have numerators such as moles or milligrams, and counting that amount per the number in the denominator",null,null,null,null,false],[false,"per thousand","/10*3","/10*3","",0.001,[0,0,0,0,0,0,0],"/103<.sup>",null,false,null,null,1,false,false,0,"/10^3; per 10*3","LOINC","NFr","Clinical","used for counting entities, e.g. blood cells; usually these kinds of terms have numerators such as moles or milligrams, and counting that amount per the number in the denominator",null,null,null,null,false],[false,"per million","/10*6","/10*6","",0.000001,[0,0,0,0,0,0,0],"/106<.sup>",null,false,null,null,1,false,false,0,"/10^6; per 10*6;","LOINC","NFr","Clinical","used for counting entities, e.g. blood cells; usually these kinds of terms have numerators such as moles or milligrams, and counting that amount per the number in the denominator",null,null,null,null,false],[false,"per billion","/10*9","/10*9","",1e-9,[0,0,0,0,0,0,0],"/109<.sup>",null,false,null,null,1,false,false,0,"/10^9; per 10*9","LOINC","NFr","Clinical","used for counting entities, e.g. blood cells; usually these kinds of terms have numerators such as moles or milligrams, and counting that amount per the number in the denominator",null,null,null,null,false],[false,"per 100","/100","","",0.01,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"per hundred; 10^2; 10*2","LOINC","NFr","Clinical","used for counting entities, e.g. blood cells; usually these kinds of terms have numerators such as moles or milligrams, and counting that amount per the number in the denominator",null,null,null,null,false],[false,"per 100 cells","/100{cells}","","",0.01,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"/100 cells; /100cells; per hundred","LOINC","EntMass; EntNum; NFr","Clinical","",null,null,null,null,false],[false,"per 100 neutrophils","/100{neutrophils}","","",0.01,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"/100 neutrophils; /100neutrophils; per hundred","LOINC","EntMass; EntNum; NFr","Clinical","",null,null,null,null,false],[false,"per 100 spermatozoa","/100{spermatozoa}","","",0.01,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"/100 spermatozoa; /100spermatozoa; per hundred","LOINC","NFr","Clinical","",null,null,null,null,false],[false,"per 100 white blood cells","/100{WBCs}","","",0.01,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"/100 WBCs; /100WBCs; per hundred","LOINC","Ratio; NFr","Clinical","",null,null,null,null,false],[false,"per year","/a","/ANN","",3.168808781402895e-8,[0,-1,0,0,0,0,0],"/a",null,false,null,null,1,false,false,0,"/Years; /yrs; yearly","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"per centimeter of water","/cm[H2O]","/CM[H2O]","",0.000010197162129779282,[1,2,-1,0,0,0,0],"/cm HO2<.r>",null,false,null,null,1,false,false,0,"/cmH2O; /cm H2O; centimeters; centimetres","LOINC","InvPress","Clinical","",null,null,null,null,false],[false,"per day","/d","/D","",0.000011574074074074073,[0,-1,0,0,0,0,0],"/d",null,false,null,null,1,false,false,0,"/dy; per day","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"per deciliter","/dL","/DL","",10000,[-3,0,0,0,0,0,0],"/dL",null,false,null,null,1,false,false,0,"per dL; /deciliter; decilitre","LOINC","NCnc","Clinical","",null,null,null,null,false],[false,"per gram","/g","/G","",1,[0,0,-1,0,0,0,0],"/g",null,false,null,null,1,false,false,0,"/gm; /gram; per g","LOINC","NCnt","Clinical","",null,null,null,null,false],[false,"per hour","/h","/HR","",0.0002777777777777778,[0,-1,0,0,0,0,0],"/h",null,false,null,null,1,false,false,0,"/hr; /hour; per hr","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"per kilogram","/kg","/KG","",0.001,[0,0,-1,0,0,0,0],"/kg",null,false,null,null,1,false,false,0,"per kg; per kilogram","LOINC","NCnt","Clinical","",null,null,null,null,false],[false,"per liter","/L","/L","",1000,[-3,0,0,0,0,0,0],"/L",null,false,null,null,1,false,false,0,"/liter; litre","LOINC","NCnc","Clinical","",null,null,null,null,false],[false,"per square meter","/m2","/M2","",1,[-2,0,0,0,0,0,0],"/m2<.sup>",null,false,null,null,1,false,false,0,"/m^2; /m*2; /sq. m; per square meter; meter squared; metre","LOINC","Naric","Clinical","",null,null,null,null,false],[false,"per cubic meter","/m3","/M3","",1,[-3,0,0,0,0,0,0],"/m3<.sup>",null,false,null,null,1,false,false,0,"/m^3; /m*3; /cu. m; per cubic meter; meter cubed; per m3; metre","LOINC","NCncn","Clinical","",null,null,null,null,false],[false,"per milligram","/mg","/MG","",1000,[0,0,-1,0,0,0,0],"/mg",null,false,null,null,1,false,false,0,"/milligram; per mg","LOINC","NCnt","Clinical","",null,null,null,null,false],[false,"per minute","/min","/MIN","",0.016666666666666666,[0,-1,0,0,0,0,0],"/min",null,false,null,null,1,false,false,0,"/minute; per mins; breaths beats per minute","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"per milliliter","/mL","/ML","",1000000,[-3,0,0,0,0,0,0],"/mL",null,false,null,null,1,false,false,0,"/milliliter; per mL; millilitre","LOINC","NCncn","Clinical","",null,null,null,null,false],[false,"per millimeter","/mm","/MM","",1000,[-1,0,0,0,0,0,0],"/mm",null,false,null,null,1,false,false,0,"/millimeter; per mm; millimetre","LOINC","InvLen","Clinical","",null,null,null,null,false],[false,"per month","/mo","/MO","",3.802570537683474e-7,[0,-1,0,0,0,0,0],"/mo",null,false,null,null,1,false,false,0,"/month; per mo; monthly; month","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"per second","/s","/S","",1,[0,-1,0,0,0,0,0],"/s",null,false,null,null,1,false,false,0,"/second; /sec; per sec; frequency; Hertz; Herz; Hz; becquerels; Bq; s-1; s^-1","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"per enzyme unit","/U","/U","",9.963241120049633e-17,[0,1,0,0,0,0,0],"/U",null,false,null,null,1,false,false,-1,"/enzyme units; per U","LOINC","InvC; NCat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)",null,null,null,null,false],[false,"per microliter","/uL","/UL","",999999999.9999999,[-3,0,0,0,0,0,0],"/μL",null,false,null,null,1,false,false,0,"/microliter; microlitre; /mcl; per uL","LOINC","ACnc","Clinical","",null,null,null,null,false],[false,"per week","/wk","/WK","",0.0000016534391534391535,[0,-1,0,0,0,0,0],"/wk",null,false,null,null,1,false,false,0,"/week; per wk; weekly, weeks","LOINC","NRat","Clinical","",null,null,null,null,false],[false,"APL unit per milliliter","[APL'U]/mL","[APL'U]/ML","biologic activity of anticardiolipin IgA",1000000,[-3,0,0,0,0,0,0],"/mL","chemical",false,null,null,1,false,true,0,"APL/mL; APL'U/mL; APL U/mL; APL/milliliter; IgA anticardiolipin units per milliliter; IgA Phospholipid Units; millilitre; biologic activity of","LOINC","ACnc","Clinical","Units for an anti phospholipid syndrome test","1","1","1",1,false],[false,"arbitrary unit per milliliter","[arb'U]/mL","[ARB'U]/ML","arbitrary",1000000,[-3,0,0,0,0,0,0],"(arb. U)/mL","chemical",false,null,null,1,false,true,0,"arb'U/mL; arbU/mL; arb U/mL; arbitrary units per milliliter; millilitre","LOINC","ACnc","Clinical","relative unit of measurement to show the ratio of test measurement to reference measurement","1","1","1",1,false],[false,"colony forming units per liter","[CFU]/L","[CFU]/L","amount of a proliferating organism",1000,[-3,0,0,0,0,0,0],"CFU/L","chemical",false,null,null,1,false,true,0,"CFU per Liter; CFU/L","LOINC","NCnc","Clinical","","1","1","1",1,false],[false,"colony forming units per milliliter","[CFU]/mL","[CFU]/ML","amount of a proliferating organism",1000000,[-3,0,0,0,0,0,0],"CFU/mL","chemical",false,null,null,1,false,true,0,"CFU per mL; CFU/mL","LOINC","NCnc","Clinical","","1","1","1",1,false],[false,"foot per foot - US","[ft_us]/[ft_us]","[FT_US]/[FT_US]","length",1,[0,0,0,0,0,0,0],"(ftus)/(ftus)","us-lengths",false,null,null,1,false,false,0,"ft/ft; ft per ft; feet per feet; visual acuity","","LenRto","Clinical","distance ratio to measure 20:20 vision","m/3937","M/3937","1200",1200,false],[false,"GPL unit per milliliter","[GPL'U]/mL","[GPL'U]/ML","biologic activity of anticardiolipin IgG",1000000,[-3,0,0,0,0,0,0],"/mL","chemical",false,null,null,1,false,true,0,"GPL U/mL; GPL'U/mL; GPL/mL; GPL U per mL; IgG Phospholipid Units per milliliters; IgG anticardiolipin units; millilitres ","LOINC","ACnc; AMass","Clinical","Units for an antiphospholipid test","1","1","1",1,false],[false,"international unit per 2 hour","[IU]/(2.h)","[IU]/HR","arbitrary",0.0001388888888888889,[0,-1,0,0,0,0,0],"(i.U.)/h","chemical",true,null,null,1,false,true,0,"IU/2hrs; IU/2 hours; IU per 2 hrs; international units per 2 hours","LOINC","ARat","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per 24 hour","[IU]/(24.h)","[IU]/HR","arbitrary",0.000011574074074074073,[0,-1,0,0,0,0,0],"(i.U.)/h","chemical",true,null,null,1,false,true,0,"IU/24hr; IU/24 hours; IU per 24 hrs; international units per 24 hours","LOINC","ARat","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per day","[IU]/d","[IU]/D","arbitrary",0.000011574074074074073,[0,-1,0,0,0,0,0],"(i.U.)/d","chemical",true,null,null,1,false,true,0,"IU/dy; IU/days; IU per dys; international units per day","LOINC","ARat","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per deciliter","[IU]/dL","[IU]/DL","arbitrary",10000,[-3,0,0,0,0,0,0],"(i.U.)/dL","chemical",true,null,null,1,false,true,0,"IU/dL; IU per dL; international units per deciliters; decilitres","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per gram","[IU]/g","[IU]/G","arbitrary",1,[0,0,-1,0,0,0,0],"(i.U.)/g","chemical",true,null,null,1,false,true,0,"IU/gm; IU/gram; IU per gm; IU per g; international units per gram","LOINC","ACnt","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per hour","[IU]/h","[IU]/HR","arbitrary",0.0002777777777777778,[0,-1,0,0,0,0,0],"(i.U.)/h","chemical",true,null,null,1,false,true,0,"IU/hrs; IU per hours; international units per hour","LOINC","ARat","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per kilogram","[IU]/kg","[IU]/KG","arbitrary",0.001,[0,0,-1,0,0,0,0],"(i.U.)/kg","chemical",true,null,null,1,false,true,0,"IU/kg; IU/kilogram; IU per kg; units","LOINC","ACnt","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per kilogram per day","[IU]/kg/d","([IU]/KG)/D","arbitrary",1.1574074074074074e-8,[0,-1,-1,0,0,0,0],"((i.U.)/kg)/d","chemical",true,null,null,1,false,true,0,"IU/kg/dy; IU/kg/day; IU/kilogram/day; IU per kg per day; units","LOINC","ACntRat","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per liter","[IU]/L","[IU]/L","arbitrary",1000,[-3,0,0,0,0,0,0],"(i.U.)/L","chemical",true,null,null,1,false,true,0,"IU/L; IU/liter; IU per liter; units; litre","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per minute","[IU]/min","[IU]/MIN","arbitrary",0.016666666666666666,[0,-1,0,0,0,0,0],"(i.U.)/min","chemical",true,null,null,1,false,true,0,"IU/min; IU/minute; IU per minute; international units","LOINC","ARat","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"international unit per milliliter","[IU]/mL","[IU]/ML","arbitrary",1000000,[-3,0,0,0,0,0,0],"(i.U.)/mL","chemical",true,null,null,1,false,true,0,"IU/mL; IU per mL; international units per milliliter; millilitre","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"MPL unit per milliliter","[MPL'U]/mL","[MPL'U]/ML","biologic activity of anticardiolipin IgM",1000000,[-3,0,0,0,0,0,0],"/mL","chemical",false,null,null,1,false,true,0,"MPL/mL; MPL U/mL; MPL'U/mL; IgM anticardiolipin units; IgM Phospholipid Units; millilitre ","LOINC","ACnc","Clinical","units for antiphospholipid test\n","1","1","1",1,false],[false,"number per high power field","{#}/[HPF]","/[HPF]","",1,[0,0,0,0,0,0,0],"/HPF",null,false,null,null,1,false,false,0,"#/HPF; # per HPF; number/HPF; numbers per high power field","LOINC","Naric","Clinical","",null,null,null,null,false],[false,"number per low power field","{#}/[LPF]","/[LPF]","",1,[0,0,0,0,0,0,0],"/LPF",null,false,null,null,1,false,false,0,"#/LPF; # per LPF; number/LPF; numbers per low power field","LOINC","Naric","Clinical","",null,null,null,null,false],[false,"IgA antiphosphatidylserine unit ","{APS'U}","","",1,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"APS Unit; Phosphatidylserine Antibody IgA Units","LOINC","ACnc","Clinical","unit for antiphospholipid test",null,null,null,null,false],[false,"EIA index","{EIA_index}","","",1,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"enzyme immunoassay index","LOINC","ACnc","Clinical","",null,null,null,null,false],[false,"kaolin clotting time","{KCT'U}","","",1,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"KCT","LOINC","Time","Clinical","sensitive test to detect lupus anticoagulants; measured in seconds",null,null,null,null,false],[false,"IgM antiphosphatidylserine unit","{MPS'U}","","",1,[0,0,0,0,0,0,0],null,null,false,null,null,1,false,false,0,"Phosphatidylserine Antibody IgM Measurement ","LOINC","ACnc","Clinical","",null,null,null,null,false],[false,"trillion per liter","10*12/L","(10*12)/L","number",1000000000000000,[-3,0,0,0,0,0,0],"(1012)/L","dimless",false,null,null,1,false,false,0,"10^12/L; 10*12 per Liter; trillion per liter; litre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"10^3 (used for cell count)","10*3","10*3","number",1000,[0,0,0,0,0,0,0],"103","dimless",false,null,null,1,false,false,0,"10^3; thousand","LOINC","Num","Clinical","usually used for counting entities (e.g. blood cells) per volume","1","1","10",10,false],[false,"thousand per liter","10*3/L","(10*3)/L","number",1000000,[-3,0,0,0,0,0,0],"(103)/L","dimless",false,null,null,1,false,false,0,"10^3/L; 10*3 per liter; litre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"thousand per milliliter","10*3/mL","(10*3)/ML","number",1000000000,[-3,0,0,0,0,0,0],"(103)/mL","dimless",false,null,null,1,false,false,0,"10^3/mL; 10*3 per mL; thousand per milliliter; millilitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"thousand per microliter","10*3/uL","(10*3)/UL","number",999999999999.9999,[-3,0,0,0,0,0,0],"(103)/μL","dimless",false,null,null,1,false,false,0,"10^3/uL; 10*3 per uL; thousand per microliter; microlitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"10 thousand per microliter","10*4/uL","(10*4)/UL","number",10000000000000,[-3,0,0,0,0,0,0],"(104)/μL","dimless",false,null,null,1,false,false,0,"10^4/uL; 10*4 per uL; microlitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"10^5 ","10*5","10*5","number",100000,[0,0,0,0,0,0,0],"105","dimless",false,null,null,1,false,false,0,"one hundred thousand","LOINC","Num","Clinical","","1","1","10",10,false],[false,"10^6","10*6","10*6","number",1000000,[0,0,0,0,0,0,0],"106","dimless",false,null,null,1,false,false,0,"","LOINC","Num","Clinical","","1","1","10",10,false],[false,"million colony forming unit per liter","10*6.[CFU]/L","((10*6).[CFU])/L","number",1000000000,[-3,0,0,0,0,0,0],"((106).CFU)/L","dimless",false,null,null,1,false,true,0,"10*6 CFU/L; 10^6 CFU/L; 10^6CFU; 10^6 CFU per liter; million colony forming units; litre","LOINC","ACnc","Clinical","","1","1","10",10,false],[false,"million international unit","10*6.[IU]","(10*6).[IU]","number",1000000,[0,0,0,0,0,0,0],"(106).(i.U.)","dimless",false,null,null,1,false,true,0,"10*6 IU; 10^6 IU; international units","LOINC","arb","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","1","1","10",10,false],[false,"million per 24 hour","10*6/(24.h)","(10*6)/HR","number",11.574074074074074,[0,-1,0,0,0,0,0],"(106)/h","dimless",false,null,null,1,false,false,0,"10*6/24hrs; 10^6/24 hrs; 10*6 per 24 hrs; 10^6 per 24 hours","LOINC","NRat","Clinical","","1","1","10",10,false],[false,"million per kilogram","10*6/kg","(10*6)/KG","number",1000,[0,0,-1,0,0,0,0],"(106)/kg","dimless",false,null,null,1,false,false,0,"10^6/kg; 10*6 per kg; 10*6 per kilogram; millions","LOINC","NCnt","Clinical","","1","1","10",10,false],[false,"million per liter","10*6/L","(10*6)/L","number",1000000000,[-3,0,0,0,0,0,0],"(106)/L","dimless",false,null,null,1,false,false,0,"10^6/L; 10*6 per Liter; 10^6 per Liter; litre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"million per milliliter","10*6/mL","(10*6)/ML","number",1000000000000,[-3,0,0,0,0,0,0],"(106)/mL","dimless",false,null,null,1,false,false,0,"10^6/mL; 10*6 per mL; 10*6 per milliliter; millilitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"million per microliter","10*6/uL","(10*6)/UL","number",1000000000000000,[-3,0,0,0,0,0,0],"(106)/μL","dimless",false,null,null,1,false,false,0,"10^6/uL; 10^6 per uL; 10^6/mcl; 10^6 per mcl; 10^6 per microliter; microlitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"10^8","10*8","10*8","number",100000000,[0,0,0,0,0,0,0],"108","dimless",false,null,null,1,false,false,0,"100 million; one hundred million; 10^8","LOINC","Num","Clinical","","1","1","10",10,false],[false,"billion per liter","10*9/L","(10*9)/L","number",1000000000000,[-3,0,0,0,0,0,0],"(109)/L","dimless",false,null,null,1,false,false,0,"10^9/L; 10*9 per Liter; litre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"billion per milliliter","10*9/mL","(10*9)/ML","number",1000000000000000,[-3,0,0,0,0,0,0],"(109)/mL","dimless",false,null,null,1,false,false,0,"10^9/mL; 10*9 per mL; 10^9 per mL; 10*9 per milliliter; millilitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"billion per microliter","10*9/uL","(10*9)/UL","number",1000000000000000000,[-3,0,0,0,0,0,0],"(109)/μL","dimless",false,null,null,1,false,false,0,"10^9/uL; 10^9 per uL; 10^9/mcl; 10^9 per mcl; 10*9 per uL; 10*9 per mcl; 10*9/mcl; 10^9 per microliter; microlitre","LOINC","NCncn","Clinical","","1","1","10",10,false],[false,"10 liter per minute per square meter","10.L/(min.m2)","L/(MIN.M2)","",0.00016666666666666666,[1,-1,0,0,0,0,0],"L/(min.(m2))",null,false,null,null,1,false,false,0,"10 liters per minutes per square meter; 10 L per min per m2; m^2; 10 L/(min*m2); 10L/(min*m^2); litres; sq. meter; metre; meters squared","LOINC","ArVRat","Clinical","",null,null,null,null,false],[false,"10 liter per minute","10.L/min","L/MIN","",0.00016666666666666666,[3,-1,0,0,0,0,0],"L/min",null,false,null,null,1,false,false,0,"10 liters per minute; 10 L per min; 10L; 10 L/min; litre","LOINC","VRat","Clinical","",null,null,null,null,false],[false,"10 micronewton second per centimeter to the fifth power per square meter","10.uN.s/(cm5.m2)","(UN.S)/(CM5.M2)","",100000000,[-6,-1,1,0,0,0,0],"(μN.s)/(cm5).(m2)",null,false,null,null,1,false,false,0,"dyne seconds per centimeter5 and square meter; dyn.s/(cm5.m2); dyn.s/cm5/m2; cm^5; m^2","LOINC","","Clinical","unit to measure systemic vascular resistance per body surface area",null,null,null,null,false],[false,"24 hour","24.h","HR","",86400,[0,1,0,0,0,0,0],"h",null,false,null,null,1,false,false,0,"24hrs; 24 hrs; 24 hours; days; dy","LOINC","Time","Clinical","",null,null,null,null,false],[false,"ampere per meter","A/m","A/M","electric current",1,[-1,-1,0,0,0,1,0],"A/m","si",true,null,null,1,false,false,0,"A/m; amp/meter; magnetic field strength; H; B; amperes per meter; metre","LOINC","","Clinical","unit of magnetic field strength","C/s","C/S","1",1,false],[false,"centigram","cg","CG","mass",0.01,[0,0,1,0,0,0,0],"cg",null,false,"M",null,1,false,false,0,"centigrams; cg; cgm","LOINC","Mass","Clinical","",null,null,null,null,false],[false,"centiliter","cL","CL","volume",0.00001,[3,0,0,0,0,0,0],"cL","iso1000",true,null,null,1,false,false,0,"centiliters; centilitres","LOINC","Vol","Clinical","","l",null,"1",1,false],[false,"centimeter","cm","CM","length",0.01,[1,0,0,0,0,0,0],"cm",null,false,"L",null,1,false,false,0,"centimeters; centimetres","LOINC","Len","Clinical","",null,null,null,null,false],[false,"centimeter of water","cm[H2O]","CM[H2O]","pressure",98066.5,[-1,-2,1,0,0,0,0],"cm HO2","clinical",true,null,null,1,false,false,0,"cm H2O; cmH2O; centimetres; pressure","LOINC","Pres","Clinical","unit of pressure mostly applies to blood pressure","kPa","KPAL","980665e-5",9.80665,false],[false,"centimeter of water per liter per second","cm[H2O]/L/s","(CM[H2O]/L)/S","pressure",98066500,[-4,-3,1,0,0,0,0],"((cm HO2)/L)/s","clinical",true,null,null,1,false,false,0,"cm[H2O]/(L/s); cm[H2O].s/L; cm H2O/L/sec; cmH2O/L/sec; cmH2O/Liter; cmH2O per L per secs; centimeters of water per liters per second; centimetres; litres; cm[H2O]/(L/s)","LOINC","PresRat","Clinical","unit used to measure mean pulmonary resistance","kPa","KPAL","980665e-5",9.80665,false],[false,"centimeter of water per second per meter","cm[H2O]/s/m","(CM[H2O]/S)/M","pressure",98066.5,[-2,-3,1,0,0,0,0],"((cm HO2)/s)/m","clinical",true,null,null,1,false,false,0,"cm[H2O]/(s.m); cm H2O/s/m; cmH2O; cmH2O/sec/m; cmH2O per secs per meters; centimeters of water per seconds per meter; centimetres; metre","LOINC","PresRat","Clinical","unit used to measure pulmonary pressure time product","kPa","KPAL","980665e-5",9.80665,false],[false,"centimeter of mercury","cm[Hg]","CM[HG]","pressure",1333220,[-1,-2,1,0,0,0,0],"cm Hg","clinical",true,null,null,1,false,false,0,"centimeters of mercury; centimetres; cmHg; cm Hg","LOINC","Pres","Clinical","unit of pressure where 1 cmHg = 10 torr","kPa","KPAL","133.3220",133.322,false],[false,"square centimeter","cm2","CM2","length",0.0001,[2,0,0,0,0,0,0],"cm2",null,false,"L",null,1,false,false,0,"cm^2; sq cm; centimeters squared; square centimeters; centimetre; area","LOINC","Area","Clinical","",null,null,null,null,false],[false,"square centimeter per second","cm2/s","CM2/S","length",0.0001,[2,-1,0,0,0,0,0],"(cm2)/s",null,false,"L",null,1,false,false,0,"cm^2/sec; square centimeters per second; sq cm per sec; cm2; centimeters squared; centimetres","LOINC","AreaRat","Clinical","",null,null,null,null,false],[false,"centipoise","cP","CP","dynamic viscosity",1,[-1,-1,1,0,0,0,0],"cP","cgs",true,null,null,1,false,false,0,"cps; centiposes","LOINC","Visc","Clinical","unit of dynamic viscosity in the CGS system with base units: 10^−3 Pa.s = 1 mPa·.s (1 millipascal second)","dyn.s/cm2","DYN.S/CM2","1",1,false],[false,"centistoke","cSt","CST","kinematic viscosity",0.0000010000000000000002,[2,-1,0,0,0,0,0],"cSt","cgs",true,null,null,1,false,false,0,"centistokes","LOINC","Visc","Clinical","unit for kinematic viscosity with base units of mm^2/s (square millimeter per second)","cm2/s","CM2/S","1",1,false],[false,"dekaliter per minute","daL/min","DAL/MIN","volume",0.00016666666666666666,[3,-1,0,0,0,0,0],"daL/min","iso1000",true,null,null,1,false,false,0,"dekalitres; dekaliters per minute; per min","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"dekaliter per minute per square meter","daL/min/m2","(DAL/MIN)/M2","volume",0.00016666666666666666,[1,-1,0,0,0,0,0],"(daL/min)/(m2)","iso1000",true,null,null,1,false,false,0,"daL/min/m^2; daL/minute/m2; sq. meter; dekaliters per minutes per square meter; meter squared; dekalitres; metre","LOINC","ArVRat","Clinical","The area usually is the body surface area used to normalize cardiovascular measures for patient's size","l",null,"1",1,false],[false,"decibel","dB","DB","level",1,[0,0,0,0,0,0,0],"dB","levels",true,null,"lg",0.1,true,false,0,"decibels","LOINC","LogRto","Clinical","unit most commonly used in acoustics as unit of sound pressure level. (also see B[SPL] or bel sound pressure level). ","1",null,null,1,false],[false,"degree per second","deg/s","DEG/S","plane angle",0.017453292519943295,[0,-1,0,1,0,0,0],"°/s","iso1000",false,null,null,1,false,false,0,"deg/sec; deg per sec; °/sec; twist rate; angular speed; rotational speed","LOINC","ARat","Clinical","unit of angular (rotational) speed used to express turning rate","[pi].rad/360","[PI].RAD/360","2",2,false],[false,"decigram","dg","DG","mass",0.1,[0,0,1,0,0,0,0],"dg",null,false,"M",null,1,false,false,0,"decigrams; dgm; 0.1 grams; 1/10 gm","LOINC","Mass","Clinical","equal to 1/10 gram",null,null,null,null,false],[false,"deciliter","dL","DL","volume",0.0001,[3,0,0,0,0,0,0],"dL","iso1000",true,null,null,1,false,false,0,"deciliters; decilitres; 0.1 liters; 1/10 L","LOINC","Vol","Clinical","equal to 1/10 liter","l",null,"1",1,false],[false,"decimeter","dm","DM","length",0.1,[1,0,0,0,0,0,0],"dm",null,false,"L",null,1,false,false,0,"decimeters; decimetres; 0.1 meters; 1/10 m; 10 cm; centimeters","LOINC","Len","Clinical","equal to 1/10 meter or 10 centimeters",null,null,null,null,false],[false,"square decimeter per square second","dm2/s2","DM2/S2","length",0.010000000000000002,[2,-2,0,0,0,0,0],"(dm2)/(s2)",null,false,"L",null,1,false,false,0,"dm2 per s2; dm^2/s^2; decimeters squared per second squared; sq dm; sq sec","LOINC","EngMass (massic energy)","Clinical","units for energy per unit mass or Joules per kilogram (J/kg = kg.m2/s2/kg = m2/s2) ",null,null,null,null,false],[false,"dyne second per centimeter per square meter","dyn.s/(cm.m2)","(DYN.S)/(CM.M2)","force",1,[-2,-1,1,0,0,0,0],"(dyn.s)/(cm.(m2))","cgs",true,null,null,1,false,false,0,"(dyn*s)/(cm*m2); (dyn*s)/(cm*m^2); dyn s per cm per m2; m^2; dyne seconds per centimeters per square meter; centimetres; sq. meter; squared","LOINC","","Clinical","","g.cm/s2","G.CM/S2","1",1,false],[false,"dyne second per centimeter","dyn.s/cm","(DYN.S)/CM","force",1,[0,-1,1,0,0,0,0],"(dyn.s)/cm","cgs",true,null,null,1,false,false,0,"(dyn*s)/cm; dyn sec per cm; seconds; centimetre; dyne seconds","LOINC","","Clinical","","g.cm/s2","G.CM/S2","1",1,false],[false,"equivalent per liter","eq/L","EQ/L","amount of substance",6.0221366999999994e+26,[-3,0,0,0,0,0,0],"eq/L","chemical",true,null,null,1,false,false,1,"eq/liter; eq/litre; eqs; equivalents per liter; litre","LOINC","SCnc","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"equivalent per milliliter","eq/mL","EQ/ML","amount of substance",6.0221367e+29,[-3,0,0,0,0,0,0],"eq/mL","chemical",true,null,null,1,false,false,1,"equivalent/milliliter; equivalents per milliliter; eq per mL; millilitre","LOINC","SCnc","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"equivalent per millimole","eq/mmol","EQ/MMOL","amount of substance",1000,[0,0,0,0,0,0,0],"eq/mmol","chemical",true,null,null,1,false,false,0,"equivalent/millimole; equivalents per millimole; eq per mmol","LOINC","SRto","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"equivalent per micromole","eq/umol","EQ/UMOL","amount of substance",1000000,[0,0,0,0,0,0,0],"eq/μmol","chemical",true,null,null,1,false,false,0,"equivalent/micromole; equivalents per micromole; eq per umol","LOINC","SRto","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"femtogram","fg","FG","mass",1e-15,[0,0,1,0,0,0,0],"fg",null,false,"M",null,1,false,false,0,"fg; fgm; femtograms; weight","LOINC","Mass","Clinical","equal to 10^-15 grams",null,null,null,null,false],[false,"femtoliter","fL","FL","volume",1e-18,[3,0,0,0,0,0,0],"fL","iso1000",true,null,null,1,false,false,0,"femtolitres; femtoliters","LOINC","Vol; EntVol","Clinical","equal to 10^-15 liters","l",null,"1",1,false],[false,"femtometer","fm","FM","length",1e-15,[1,0,0,0,0,0,0],"fm",null,false,"L",null,1,false,false,0,"femtometres; femtometers","LOINC","Len","Clinical","equal to 10^-15 meters",null,null,null,null,false],[false,"femtomole","fmol","FMOL","amount of substance",602213670,[0,0,0,0,0,0,0],"fmol","si",true,null,null,1,false,false,1,"femtomoles","LOINC","EntSub","Clinical","equal to 10^-15 moles","10*23","10*23","6.0221367",6.0221367,false],[false,"femtomole per gram","fmol/g","FMOL/G","amount of substance",602213670,[0,0,-1,0,0,0,0],"fmol/g","si",true,null,null,1,false,false,1,"femtomoles; fmol/gm; fmol per gm","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"femtomole per liter","fmol/L","FMOL/L","amount of substance",602213670000,[-3,0,0,0,0,0,0],"fmol/L","si",true,null,null,1,false,false,1,"femtomoles; fmol per liter; litre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"femtomole per milligram","fmol/mg","FMOL/MG","amount of substance",602213670000,[0,0,-1,0,0,0,0],"fmol/mg","si",true,null,null,1,false,false,1,"fmol per mg; femtomoles","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"femtomole per milliliter","fmol/mL","FMOL/ML","amount of substance",602213670000000,[-3,0,0,0,0,0,0],"fmol/mL","si",true,null,null,1,false,false,1,"femtomoles; millilitre; fmol per mL; fmol per milliliter","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"gram meter","g.m","G.M","mass",1,[1,0,1,0,0,0,0],"g.m",null,false,"M",null,1,false,false,0,"g*m; gxm; meters; metres","LOINC","Enrg","Clinical","Unit for measuring stroke work (heart work)",null,null,null,null,false],[false,"gram per 100 gram","g/(100.g)","G/G","mass",0.01,[0,0,0,0,0,0,0],"g/g",null,false,"M",null,1,false,false,0,"g/100 gm; 100gm; grams per 100 grams; gm per 100 gm","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"gram per 12 hour","g/(12.h)","G/HR","mass",0.000023148148148148147,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/12hrs; 12 hrs; gm per 12 hrs; 12hrs; grams per 12 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 24 hour","g/(24.h)","G/HR","mass",0.000011574074074074073,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/24hrs; gm/24 hrs; gm per 24 hrs; 24hrs; grams per 24 hours; gm/dy; gm per dy; grams per day","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 3 days","g/(3.d)","G/D","mass",0.000003858024691358025,[0,-1,1,0,0,0,0],"g/d",null,false,"M",null,1,false,false,0,"gm/3dy; gm/3 dy; gm per 3 days; grams","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 4 hour","g/(4.h)","G/HR","mass",0.00006944444444444444,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/4hrs; gm/4 hrs; gm per 4 hrs; 4hrs; grams per 4 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 48 hour","g/(48.h)","G/HR","mass",0.000005787037037037037,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/48hrs; gm/48 hrs; gm per 48 hrs; 48hrs; grams per 48 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 5 hour","g/(5.h)","G/HR","mass",0.00005555555555555556,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/5hrs; gm/5 hrs; gm per 5 hrs; 5hrs; grams per 5 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 6 hour","g/(6.h)","G/HR","mass",0.000046296296296296294,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/6hrs; gm/6 hrs; gm per 6 hrs; 6hrs; grams per 6 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per 72 hour","g/(72.h)","G/HR","mass",0.000003858024691358025,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/72hrs; gm/72 hrs; gm per 72 hrs; 72hrs; grams per 72 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per cubic centimeter","g/cm3","G/CM3","mass",999999.9999999999,[-3,0,1,0,0,0,0],"g/(cm3)",null,false,"M",null,1,false,false,0,"g/cm^3; gm per cm3; g per cm^3; grams per centimeter cubed; cu. cm; centimetre; g/mL; gram per milliliter; millilitre","LOINC","MCnc","Clinical","g/cm3 = g/mL",null,null,null,null,false],[false,"gram per day","g/d","G/D","mass",0.000011574074074074073,[0,-1,1,0,0,0,0],"g/d",null,false,"M",null,1,false,false,0,"gm/dy; gm per dy; grams per day; gm/24hrs; gm/24 hrs; gm per 24 hrs; 24hrs; grams per 24 hours; serving","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per deciliter","g/dL","G/DL","mass",10000,[-3,0,1,0,0,0,0],"g/dL",null,false,"M",null,1,false,false,0,"gm/dL; gm per dL; grams per deciliter; decilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"gram per gram","g/g","G/G","mass",1,[0,0,0,0,0,0,0],"g/g",null,false,"M",null,1,false,false,0,"gm; grams","LOINC","MRto ","Clinical","",null,null,null,null,false],[false,"gram per hour","g/h","G/HR","mass",0.0002777777777777778,[0,-1,1,0,0,0,0],"g/h",null,false,"M",null,1,false,false,0,"gm/hr; gm per hr; grams; intake; output","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per hour per square meter","g/h/m2","(G/HR)/M2","mass",0.0002777777777777778,[-2,-1,1,0,0,0,0],"(g/h)/(m2)",null,false,"M",null,1,false,false,0,"gm/hr/m2; gm/h/m2; /m^2; sq. m; g per hr per m2; grams per hours per square meter; meter squared; metre","LOINC","ArMRat","Clinical","",null,null,null,null,false],[false,"gram per kilogram","g/kg ","G/KG","mass",0.001,[0,0,0,0,0,0,0],"g/kg",null,false,"M",null,1,false,false,0,"g per kg; gram per kilograms","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"gram per kilogram per 8 hour ","g/kg/(8.h)","(G/KG)/HR","mass",3.472222222222222e-8,[0,-1,0,0,0,0,0],"(g/kg)/h",null,false,"M",null,1,false,false,0,"g/(8.kg.h); gm/kg/8hrs; 8 hrs; g per kg per 8 hrs; 8hrs; grams per kilograms per 8 hours; shift","LOINC","MCntRat; RelMRat","Clinical","unit often used to describe mass in grams of protein consumed in a 8 hours, divided by the subject's body weight in kilograms. Also used to measure mass dose rate per body mass",null,null,null,null,false],[false,"gram per kilogram per day","g/kg/d","(G/KG)/D","mass",1.1574074074074074e-8,[0,-1,0,0,0,0,0],"(g/kg)/d",null,false,"M",null,1,false,false,0,"g/(kg.d); gm/kg/dy; gm per kg per dy; grams per kilograms per day","LOINC","RelMRat","Clinical","unit often used to describe mass in grams of protein consumed in a day, divided by the subject's body weight in kilograms. Also used to measure mass dose rate per body mass",null,null,null,null,false],[false,"gram per kilogram per hour","g/kg/h","(G/KG)/HR","mass",2.7777777777777776e-7,[0,-1,0,0,0,0,0],"(g/kg)/h",null,false,"M",null,1,false,false,0,"g/(kg.h); g/kg/hr; g per kg per hrs; grams per kilograms per hour","LOINC","MCntRat; RelMRat","Clinical","unit used to measure mass dose rate per body mass",null,null,null,null,false],[false,"gram per kilogram per minute","g/kg/min","(G/KG)/MIN","mass",0.000016666666666666667,[0,-1,0,0,0,0,0],"(g/kg)/min",null,false,"M",null,1,false,false,0,"g/(kg.min); g/kg/min; g per kg per min; grams per kilograms per minute","LOINC","MCntRat; RelMRat","Clinical","unit used to measure mass dose rate per body mass",null,null,null,null,false],[false,"gram per liter","g/L","G/L","mass",1000,[-3,0,1,0,0,0,0],"g/L",null,false,"M",null,1,false,false,0,"gm per liter; g/liter; grams per liter; litre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"gram per square meter","g/m2","G/M2","mass",1,[-2,0,1,0,0,0,0],"g/(m2)",null,false,"M",null,1,false,false,0,"g/m^2; gram/square meter; g/sq m; g per m2; g per m^2; grams per square meter; meters squared; metre","LOINC","ArMass","Clinical","Tests measure myocardial mass (heart ventricle system) per body surface area; unit used to measure mass dose per body surface area",null,null,null,null,false],[false,"gram per milligram","g/mg","G/MG","mass",1000,[0,0,0,0,0,0,0],"g/mg",null,false,"M",null,1,false,false,0,"g per mg; grams per milligram","LOINC","MCnt; MRto","Clinical","",null,null,null,null,false],[false,"gram per minute","g/min","G/MIN","mass",0.016666666666666666,[0,-1,1,0,0,0,0],"g/min",null,false,"M",null,1,false,false,0,"g per min; grams per minute; gram/minute","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"gram per milliliter","g/mL","G/ML","mass",1000000,[-3,0,1,0,0,0,0],"g/mL",null,false,"M",null,1,false,false,0,"g per mL; grams per milliliter; millilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"gram per millimole","g/mmol","G/MMOL","mass",1.6605401866749388e-21,[0,0,1,0,0,0,0],"g/mmol",null,false,"M",null,1,false,false,-1,"grams per millimole; g per mmol","LOINC","Ratio","Clinical","",null,null,null,null,false],[false,"joule per liter","J/L","J/L","energy",1000000,[-1,-2,1,0,0,0,0],"J/L","si",true,null,null,1,false,false,0,"joules per liter; litre; J per L","LOINC","EngCnc","Clinical","","N.m","N.M","1",1,false],[false,"degree Kelvin per Watt","K/W","K/W","temperature",0.001,[-2,3,-1,0,1,0,0],"K/W",null,false,"C",null,1,false,false,0,"degree Kelvin/Watt; K per W; thermal ohm; thermal resistance; degrees","LOINC","TempEngRat","Clinical","unit for absolute thermal resistance equal to the reciprocal of thermal conductance. Unit used for tests to measure work of breathing",null,null,null,null,false],[false,"kilo international unit per liter","k[IU]/L","K[IU]/L","arbitrary",1000000,[-3,0,0,0,0,0,0],"(ki.U.)/L","chemical",true,null,null,1,false,true,0,"kIU/L; kIU per L; kIU per liter; kilo international units; litre; allergens; allergy units","LOINC","ACnc","Clinical","IgE has an WHO reference standard so IgE allergen testing can be reported as k[IU]/L","[iU]","[IU]","1",1,false],[false,"kilo international unit per milliliter","k[IU]/mL","K[IU]/ML","arbitrary",1000000000,[-3,0,0,0,0,0,0],"(ki.U.)/mL","chemical",true,null,null,1,false,true,0,"kIU/mL; kIU per mL; kIU per milliliter; kilo international units; millilitre; allergens; allergy units","LOINC","ACnc","Clinical","IgE has an WHO reference standard so IgE allergen testing can be reported as k[IU]/mL","[iU]","[IU]","1",1,false],[false,"katal per kilogram","kat/kg","KAT/KG","catalytic activity",602213670000000000000,[0,-1,-1,0,0,0,0],"kat/kg","chemical",true,null,null,1,false,false,1,"kat per kg; katals per kilogram; mol/s/kg; moles per seconds per kilogram","LOINC","CCnt","Clinical","kat is a unit of catalytic activity with base units = mol/s. Rarely used because its units are too large to practically express catalytic activity. See enzyme unit [U] which is the standard unit for catalytic activity.","mol/s","MOL/S","1",1,false],[false,"katal per liter","kat/L","KAT/L","catalytic activity",6.0221366999999994e+26,[-3,-1,0,0,0,0,0],"kat/L","chemical",true,null,null,1,false,false,1,"kat per L; katals per liter; litre; mol/s/L; moles per seconds per liter","LOINC","CCnc","Clinical","kat is a unit of catalytic activity with base units = mol/s. Rarely used because its units are too large to practically express catalytic activity. See enzyme unit [U] which is the standard unit for catalytic activity.","mol/s","MOL/S","1",1,false],[false,"kilocalorie","kcal","KCAL","energy",4184000,[2,-2,1,0,0,0,0],"kcal","heat",true,null,null,1,false,false,0,"kilogram calories; large calories; food calories; kcals","LOINC","EngRat","Clinical","It is equal to 1000 calories (equal to 4.184 kJ). But in practical usage, kcal refers to food calories which excludes caloric content in fiber and other constitutes that is not digestible by humans. Also see nutrition label Calories ([Cal])","cal_th","CAL_TH","1",1,false],[false,"kilocalorie per 24 hour","kcal/(24.h)","KCAL/HR","energy",48.425925925925924,[2,-3,1,0,0,0,0],"kcal/h","heat",true,null,null,1,false,false,0,"kcal/24hrs; kcal/24 hrs; kcal per 24hrs; kilocalories per 24 hours; kilojoules; kJ/24hr; kJ/(24.h); kJ/dy; kilojoules per days; intake; calories burned; metabolic rate; food calories","","EngRat","Clinical","","cal_th","CAL_TH","1",1,false],[false,"kilocalorie per ounce","kcal/[oz_av]","KCAL/[OZ_AV]","energy",147586.25679704445,[2,-2,0,0,0,0,0],"kcal/oz","heat",true,null,null,1,false,false,0,"kcal/oz; kcal per ozs; large calories per ounces; food calories; servings; international","LOINC","EngCnt","Clinical","used in nutrition to represent calorie of food","cal_th","CAL_TH","1",1,false],[false,"kilocalorie per day","kcal/d","KCAL/D","energy",48.425925925925924,[2,-3,1,0,0,0,0],"kcal/d","heat",true,null,null,1,false,false,0,"kcal/dy; kcal per day; kilocalories per days; kilojoules; kJ/dy; kilojoules per days; intake; calories burned; metabolic rate; food calories","LOINC","EngRat","Clinical","unit in nutrition for food intake (measured in calories) in a day","cal_th","CAL_TH","1",1,false],[false,"kilocalorie per hour","kcal/h","KCAL/HR","energy",1162.2222222222222,[2,-3,1,0,0,0,0],"kcal/h","heat",true,null,null,1,false,false,0,"kcal/hrs; kcals per hr; intake; kilocalories per hours; kilojoules","LOINC","EngRat","Clinical","used in nutrition to represent caloric requirement or consumption","cal_th","CAL_TH","1",1,false],[false,"kilocalorie per kilogram per 24 hour","kcal/kg/(24.h)","(KCAL/KG)/HR","energy",0.04842592592592593,[2,-3,0,0,0,0,0],"(kcal/kg)/h","heat",true,null,null,1,false,false,0,"kcal/kg/24hrs; 24 hrs; kcal per kg per 24hrs; kilocalories per kilograms per 24 hours; kilojoules","LOINC","EngCntRat","Clinical","used in nutrition to represent caloric requirement per day based on subject's body weight in kilograms","cal_th","CAL_TH","1",1,false],[false,"kilogram","kg","KG","mass",1000,[0,0,1,0,0,0,0],"kg",null,false,"M",null,1,false,false,0,"kilograms; kgs","LOINC","Mass","Clinical","",null,null,null,null,false],[false,"kilogram meter per second","kg.m/s","(KG.M)/S","mass",1000,[1,-1,1,0,0,0,0],"(kg.m)/s",null,false,"M",null,1,false,false,0,"kg*m/s; kg.m per sec; kg*m per sec; p; momentum","LOINC","","Clinical","unit for momentum = mass times velocity",null,null,null,null,false],[false,"kilogram per second per square meter","kg/(s.m2)","KG/(S.M2)","mass",1000,[-2,-1,1,0,0,0,0],"kg/(s.(m2))",null,false,"M",null,1,false,false,0,"kg/(s*m2); kg/(s*m^2); kg per s per m2; per sec; per m^2; kilograms per seconds per square meter; meter squared; metre","LOINC","ArMRat","Clinical","",null,null,null,null,false],[false,"kilogram per hour","kg/h","KG/HR","mass",0.2777777777777778,[0,-1,1,0,0,0,0],"kg/h",null,false,"M",null,1,false,false,0,"kg/hr; kg per hr; kilograms per hour","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"kilogram per liter","kg/L","KG/L","mass",1000000,[-3,0,1,0,0,0,0],"kg/L",null,false,"M",null,1,false,false,0,"kg per liter; litre; kilograms","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"kilogram per square meter","kg/m2","KG/M2","mass",1000,[-2,0,1,0,0,0,0],"kg/(m2)",null,false,"M",null,1,false,false,0,"kg/m^2; kg/sq. m; kg per m2; per m^2; per sq. m; kilograms; meter squared; metre; BMI","LOINC","Ratio","Clinical","units for body mass index (BMI)",null,null,null,null,false],[false,"kilogram per cubic meter","kg/m3","KG/M3","mass",1000,[-3,0,1,0,0,0,0],"kg/(m3)",null,false,"M",null,1,false,false,0,"kg/m^3; kg/cu. m; kg per m3; per m^3; per cu. m; kilograms; meters cubed; metre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"kilogram per minute","kg/min","KG/MIN","mass",16.666666666666668,[0,-1,1,0,0,0,0],"kg/min",null,false,"M",null,1,false,false,0,"kilogram/minute; kg per min; kilograms per minute","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"kilogram per mole","kg/mol","KG/MOL","mass",1.6605401866749388e-21,[0,0,1,0,0,0,0],"kg/mol",null,false,"M",null,1,false,false,-1,"kilogram/mole; kg per mol; kilograms per mole","LOINC","SCnt","Clinical","",null,null,null,null,false],[false,"kilogram per second","kg/s","KG/S","mass",1000,[0,-1,1,0,0,0,0],"kg/s",null,false,"M",null,1,false,false,0,"kg/sec; kilogram/second; kg per sec; kilograms; second","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"kiloliter","kL","KL","volume",1,[3,0,0,0,0,0,0],"kL","iso1000",true,null,null,1,false,false,0,"kiloliters; kilolitres; m3; m^3; meters cubed; metre","LOINC","Vol","Clinical","","l",null,"1",1,false],[false,"kilometer","km","KM","length",1000,[1,0,0,0,0,0,0],"km",null,false,"L",null,1,false,false,0,"kilometers; kilometres; distance","LOINC","Len","Clinical","",null,null,null,null,false],[false,"kilopascal","kPa","KPAL","pressure",1000000,[-1,-2,1,0,0,0,0],"kPa","si",true,null,null,1,false,false,0,"kilopascals; pressure","LOINC","Pres; PPresDiff","Clinical","","N/m2","N/M2","1",1,false],[false,"kilosecond","ks","KS","time",1000,[0,1,0,0,0,0,0],"ks",null,false,"T",null,1,false,false,0,"kiloseconds; ksec","LOINC","Time","Clinical","",null,null,null,null,false],[false,"kilo enzyme unit","kU","KU","catalytic activity",10036894500000000000,[0,-1,0,0,0,0,0],"kU","chemical",true,null,null,1,false,false,1,"units; mmol/min; millimoles per minute","LOINC","CAct","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 kU = 1 mmol/min","umol/min","UMOL/MIN","1",1,false],[false,"kilo enzyme unit per gram","kU/g","KU/G","catalytic activity",10036894500000000000,[0,-1,-1,0,0,0,0],"kU/g","chemical",true,null,null,1,false,false,1,"units per grams; kU per gm","LOINC","CCnt","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 kU = 1 mmol/min","umol/min","UMOL/MIN","1",1,false],[false,"kilo enzyme unit per liter","kU/L","KU/L","catalytic activity",1.00368945e+22,[-3,-1,0,0,0,0,0],"kU/L","chemical",true,null,null,1,false,false,1,"units per liter; litre; enzymatic activity; enzyme activity per volume; activities","LOINC","ACnc; CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 kU = 1 mmol/min","umol/min","UMOL/MIN","1",1,false],[false,"kilo enzyme unit per milliliter","kU/mL","KU/ML","catalytic activity",1.00368945e+25,[-3,-1,0,0,0,0,0],"kU/mL","chemical",true,null,null,1,false,false,1,"kU per mL; units per milliliter; millilitre; enzymatic activity per volume; enzyme activities","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 kU = 1 mmol/min","umol/min","UMOL/MIN","1",1,false],[false,"Liters per 24 hour","L/(24.h)","L/HR","volume",1.1574074074074074e-8,[3,-1,0,0,0,0,0],"L/h","iso1000",true,null,null,1,false,false,0,"L/24hrs; L/24 hrs; L per 24hrs; liters per 24 hours; day; dy; litres; volume flow rate","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"Liters per 8 hour","L/(8.h)","L/HR","volume",3.472222222222222e-8,[3,-1,0,0,0,0,0],"L/h","iso1000",true,null,null,1,false,false,0,"L/8hrs; L/8 hrs; L per 8hrs; liters per 8 hours; litres; volume flow rate; shift","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"Liters per minute per square meter","L/(min.m2) ","L/(MIN.M2)","volume",0.000016666666666666667,[1,-1,0,0,0,0,0],"L/(min.(m2))","iso1000",true,null,null,1,false,false,0,"L/(min.m2); L/min/m^2; L/min/sq. meter; L per min per m2; m^2; liters per minutes per square meter; meter squared; litres; metre ","LOINC","ArVRat","Clinical","unit for tests that measure cardiac output per body surface area (cardiac index)","l",null,"1",1,false],[false,"Liters per day","L/d","L/D","volume",1.1574074074074074e-8,[3,-1,0,0,0,0,0],"L/d","iso1000",true,null,null,1,false,false,0,"L/dy; L per day; 24hrs; 24 hrs; 24 hours; liters; litres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"Liters per hour","L/h","L/HR","volume",2.7777777777777776e-7,[3,-1,0,0,0,0,0],"L/h","iso1000",true,null,null,1,false,false,0,"L/hr; L per hr; litres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"Liters per kilogram","L/kg","L/KG","volume",0.000001,[3,0,-1,0,0,0,0],"L/kg","iso1000",true,null,null,1,false,false,0,"L per kg; litre","LOINC","VCnt","Clinical","","l",null,"1",1,false],[false,"Liters per liter","L/L","L/L","volume",1,[0,0,0,0,0,0,0],"L/L","iso1000",true,null,null,1,false,false,0,"L per L; liter/liter; litre","LOINC","VFr","Clinical","","l",null,"1",1,false],[false,"Liters per minute","L/min","L/MIN","volume",0.000016666666666666667,[3,-1,0,0,0,0,0],"L/min","iso1000",true,null,null,1,false,false,0,"liters per minute; litre","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"Liters per minute per square meter","L/min/m2","(L/MIN)/M2","volume",0.000016666666666666667,[1,-1,0,0,0,0,0],"(L/min)/(m2)","iso1000",true,null,null,1,false,false,0,"L/(min.m2); L/min/m^2; L/min/sq. meter; L per min per m2; m^2; liters per minutes per square meter; meter squared; litres; metre ","","ArVRat","Clinical","unit for tests that measure cardiac output per body surface area (cardiac index)","l",null,"1",1,false],[false,"Liters per second","L/s","L/S","volume",0.001,[3,-1,0,0,0,0,0],"L/s","iso1000",true,null,null,1,false,false,0,"L per sec; litres","LOINC","VRat","Clinical","unit used often to measure gas flow and peak expiratory flow","l",null,"1",1,false],[false,"Liters per second per square second","L/s/s2","(L/S)/S2","volume",0.001,[3,-3,0,0,0,0,0],"(L/s)/(s2)","iso1000",true,null,null,1,false,false,0,"L/s/s^2; L/sec/sec2; L/sec/sec^2; L/sec/sq. sec; L per s per s2; L per sec per sec2; s^2; sec^2; liters per seconds per square second; second squared; litres ","LOINC","ArVRat","Clinical","unit for tests that measure cardiac output/body surface area","l",null,"1",1,false],[false,"lumen square meter","lm.m2","LM.M2","luminous flux",1,[2,0,0,2,0,0,1],"lm.(m2)","si",true,null,null,1,false,false,0,"lm*m2; lm*m^2; lumen meters squared; lumen sq. meters; metres","LOINC","","Clinical","","cd.sr","CD.SR","1",1,false],[false,"meter per second","m/s","M/S","length",1,[1,-1,0,0,0,0,0],"m/s",null,false,"L",null,1,false,false,0,"meter/second; m per sec; meters per second; metres; velocity; speed","LOINC","Vel","Clinical","unit of velocity",null,null,null,null,false],[false,"meter per square second","m/s2","M/S2","length",1,[1,-2,0,0,0,0,0],"m/(s2)",null,false,"L",null,1,false,false,0,"m/s^2; m/sq. sec; m per s2; per s^2; meters per square second; second squared; sq second; metres; acceleration","LOINC","Accel","Clinical","unit of acceleration",null,null,null,null,false],[false,"milli international unit per liter","m[IU]/L","M[IU]/L","arbitrary",1,[-3,0,0,0,0,0,0],"(mi.U.)/L","chemical",true,null,null,1,false,true,0,"mIU/L; m IU/L; mIU per liter; units; litre","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"milli international unit per milliliter","m[IU]/mL","M[IU]/ML","arbitrary",1000.0000000000001,[-3,0,0,0,0,0,0],"(mi.U.)/mL","chemical",true,null,null,1,false,true,0,"mIU/mL; m IU/mL; mIU per mL; milli international units per milliliter; millilitre","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"square meter","m2","M2","length",1,[2,0,0,0,0,0,0],"m2",null,false,"L",null,1,false,false,0,"m^2; sq m; square meters; meters squared; metres","LOINC","Area","Clinical","unit often used to represent body surface area",null,null,null,null,false],[false,"square meter per second","m2/s","M2/S","length",1,[2,-1,0,0,0,0,0],"(m2)/s",null,false,"L",null,1,false,false,0,"m^2/sec; m2 per sec; m^2 per sec; sq m/sec; meters squared/seconds; sq m per sec; meters squared; metres","LOINC","ArRat","Clinical","",null,null,null,null,false],[false,"cubic meter per second","m3/s","M3/S","length",1,[3,-1,0,0,0,0,0],"(m3)/s",null,false,"L",null,1,false,false,0,"m^3/sec; m3 per sec; m^3 per sec; cu m/sec; cubic meters per seconds; meters cubed; metres","LOINC","VRat","Clinical","",null,null,null,null,false],[false,"milliampere","mA","MA","electric current",0.001,[0,-1,0,0,0,1,0],"mA","si",true,null,null,1,false,false,0,"mamp; milliamperes","LOINC","ElpotRat","Clinical","unit of electric current","C/s","C/S","1",1,false],[false,"millibar","mbar","MBAR","pressure",100000,[-1,-2,1,0,0,0,0],"mbar","iso1000",true,null,null,1,false,false,0,"millibars","LOINC","Pres","Clinical","unit of pressure","Pa","PAL","1e5",100000,false],[false,"millibar second per liter","mbar.s/L","(MBAR.S)/L","pressure",100000000,[-4,-1,1,0,0,0,0],"(mbar.s)/L","iso1000",true,null,null,1,false,false,0,"mbar*s/L; mbar.s per L; mbar*s per L; millibar seconds per liter; millibar second per litre","LOINC","","Clinical","unit to measure expiratory resistance","Pa","PAL","1e5",100000,false],[false,"millibar per liter per second","mbar/L/s","(MBAR/L)/S","pressure",100000000,[-4,-3,1,0,0,0,0],"(mbar/L)/s","iso1000",true,null,null,1,false,false,0,"mbar/(L.s); mbar/L/sec; mbar/liter/second; mbar per L per sec; mbar per liter per second; millibars per liters per seconds; litres","LOINC","PresCncRat","Clinical","unit to measure expiratory resistance","Pa","PAL","1e5",100000,false],[false,"milliequivalent","meq","MEQ","amount of substance",602213670000000000000,[0,0,0,0,0,0,0],"meq","chemical",true,null,null,1,false,false,1,"milliequivalents; meqs","LOINC","Sub","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per 2 hour","meq/(2.h)","MEQ/HR","amount of substance",83640787500000000,[0,-1,0,0,0,0,0],"meq/h","chemical",true,null,null,1,false,false,1,"meq/2hrs; meq/2 hrs; meq per 2 hrs; milliequivalents per 2 hours","LOINC","SRat","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per 24 hour","meq/(24.h)","MEQ/HR","amount of substance",6970065625000000,[0,-1,0,0,0,0,0],"meq/h","chemical",true,null,null,1,false,false,1,"meq/24hrs; meq/24 hrs; meq per 24 hrs; milliequivalents per 24 hours","LOINC","SRat","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per 8 hour","meq/(8.h)","MEQ/HR","amount of substance",20910196875000000,[0,-1,0,0,0,0,0],"meq/h","chemical",true,null,null,1,false,false,1,"meq/8hrs; meq/8 hrs; meq per 8 hrs; milliequivalents per 8 hours; shift","LOINC","SRat","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per day","meq/d","MEQ/D","amount of substance",6970065625000000,[0,-1,0,0,0,0,0],"meq/d","chemical",true,null,null,1,false,false,1,"meq/dy; meq per day; milliquivalents per days; meq/24hrs; meq/24 hrs; meq per 24 hrs; milliequivalents per 24 hours","LOINC","SRat","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per deciliter","meq/dL","MEQ/DL","amount of substance",6.022136699999999e+24,[-3,0,0,0,0,0,0],"meq/dL","chemical",true,null,null,1,false,false,1,"meq per dL; milliequivalents per deciliter; decilitre","LOINC","SCnc","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per gram","meq/g","MEQ/G","amount of substance",602213670000000000000,[0,0,-1,0,0,0,0],"meq/g","chemical",true,null,null,1,false,false,1,"mgq/gm; meq per gm; milliequivalents per gram","LOINC","MCnt","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per hour","meq/h","MEQ/HR","amount of substance",167281575000000000,[0,-1,0,0,0,0,0],"meq/h","chemical",true,null,null,1,false,false,1,"meq/hrs; meq per hrs; milliequivalents per hour","LOINC","SRat","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per kilogram","meq/kg","MEQ/KG","amount of substance",602213670000000000,[0,0,-1,0,0,0,0],"meq/kg","chemical",true,null,null,1,false,false,1,"meq per kg; milliequivalents per kilogram","LOINC","SCnt","Clinical","equivalence equals moles per valence; used to measure dose per patient body mass","mol","MOL","1",1,false],[false,"milliequivalent per kilogram per hour","meq/kg/h","(MEQ/KG)/HR","amount of substance",167281575000000,[0,-1,-1,0,0,0,0],"(meq/kg)/h","chemical",true,null,null,1,false,false,1,"meq/(kg.h); meq/kg/hr; meq per kg per hr; milliequivalents per kilograms per hour","LOINC","SCntRat","Clinical","equivalence equals moles per valence; unit used to measure dose rate per patient body mass","mol","MOL","1",1,false],[false,"milliequivalent per liter","meq/L","MEQ/L","amount of substance",6.0221367e+23,[-3,0,0,0,0,0,0],"meq/L","chemical",true,null,null,1,false,false,1,"milliequivalents per liter; litre; meq per l; acidity","LOINC","SCnc","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per square meter","meq/m2","MEQ/M2","amount of substance",602213670000000000000,[-2,0,0,0,0,0,0],"meq/(m2)","chemical",true,null,null,1,false,false,1,"meq/m^2; meq/sq. m; milliequivalents per square meter; meter squared; metre","LOINC","ArSub","Clinical","equivalence equals moles per valence; note that the use of m2 in clinical units ofter refers to body surface area","mol","MOL","1",1,false],[false,"milliequivalent per minute","meq/min","MEQ/MIN","amount of substance",10036894500000000000,[0,-1,0,0,0,0,0],"meq/min","chemical",true,null,null,1,false,false,1,"meq per min; milliequivalents per minute","LOINC","SRat","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milliequivalent per milliliter","meq/mL","MEQ/ML","amount of substance",6.0221367e+26,[-3,0,0,0,0,0,0],"meq/mL","chemical",true,null,null,1,false,false,1,"meq per mL; milliequivalents per milliliter; millilitre","LOINC","SCnc","Clinical","equivalence equals moles per valence","mol","MOL","1",1,false],[false,"milligram","mg","MG","mass",0.001,[0,0,1,0,0,0,0],"mg",null,false,"M",null,1,false,false,0,"milligrams","LOINC","Mass","Clinical","",null,null,null,null,false],[false,"milligram per 10 hour","mg/(10.h)","MG/HR","mass",2.7777777777777777e-8,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/10hrs; mg/10 hrs; mg per 10 hrs; milligrams per 10 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per 12 hour","mg/(12.h)","MG/HR","mass",2.3148148148148148e-8,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/12hrs; mg/12 hrs; per 12 hrs; 12hrs; milligrams per 12 hours","LOINC","MRat","Clinical","units used for tests in urine",null,null,null,null,false],[false,"milligram per 2 hour","mg/(2.h)","MG/HR","mass",1.3888888888888888e-7,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/2hrs; mg/2 hrs; mg per 2 hrs; 2hrs; milligrams per 2 hours","LOINC","MRat","Clinical","units used for tests in urine",null,null,null,null,false],[false,"milligram per 24 hour","mg/(24.h)","MG/HR","mass",1.1574074074074074e-8,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/24hrs; mg/24 hrs; milligrams per 24 hours; mg/kg/dy; mg per kg per day; milligrams per kilograms per days","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per 6 hour","mg/(6.h)","MG/HR","mass",4.6296296296296295e-8,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/6hrs; mg/6 hrs; mg per 6 hrs; 6hrs; milligrams per 6 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per 72 hour","mg/(72.h)","MG/HR","mass",3.858024691358025e-9,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/72hrs; mg/72 hrs; 72 hrs; 72hrs; milligrams per 72 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per 8 hour","mg/(8.h)","MG/HR","mass",3.472222222222222e-8,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/8hrs; mg/8 hrs; milligrams per 8 hours; shift","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per day","mg/d","MG/D","mass",1.1574074074074074e-8,[0,-1,1,0,0,0,0],"mg/d",null,false,"M",null,1,false,false,0,"mg/24hrs; mg/24 hrs; milligrams per 24 hours; mg/dy; mg per day; milligrams","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per deciliter","mg/dL","MG/DL","mass",10,[-3,0,1,0,0,0,0],"mg/dL",null,false,"M",null,1,false,false,0,"mg per dL; milligrams per deciliter; decilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"milligram per gram","mg/g","MG/G","mass",0.001,[0,0,0,0,0,0,0],"mg/g",null,false,"M",null,1,false,false,0,"mg per gm; milligrams per gram","LOINC","MCnt; MRto","Clinical","",null,null,null,null,false],[false,"milligram per hour","mg/h","MG/HR","mass",2.7777777777777776e-7,[0,-1,1,0,0,0,0],"mg/h",null,false,"M",null,1,false,false,0,"mg/hr; mg per hr; milligrams","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per kilogram","mg/kg","MG/KG","mass",0.000001,[0,0,0,0,0,0,0],"mg/kg",null,false,"M",null,1,false,false,0,"mg per kg; milligrams per kilograms","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"milligram per kilogram per 8 hour","mg/kg/(8.h)","(MG/KG)/HR","mass",3.472222222222222e-11,[0,-1,0,0,0,0,0],"(mg/kg)/h",null,false,"M",null,1,false,false,0,"mg/(8.h.kg); mg/kg/8hrs; mg/kg/8 hrs; mg per kg per 8hrs; 8 hrs; milligrams per kilograms per 8 hours; shift","LOINC","RelMRat; MCntRat","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"milligram per kilogram per day","mg/kg/d","(MG/KG)/D","mass",1.1574074074074074e-11,[0,-1,0,0,0,0,0],"(mg/kg)/d",null,false,"M",null,1,false,false,0,"mg/(kg.d); mg/(kg.24.h)mg/kg/dy; mg per kg per day; milligrams per kilograms per days; mg/kg/(24.h); mg/kg/24hrs; 24 hrs; 24 hours","LOINC","RelMRat ","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"milligram per kilogram per hour","mg/kg/h","(MG/KG)/HR","mass",2.7777777777777777e-10,[0,-1,0,0,0,0,0],"(mg/kg)/h",null,false,"M",null,1,false,false,0,"mg/(kg.h); mg/kg/hr; mg per kg per hr; milligrams per kilograms per hour","LOINC","RelMRat; MCntRat","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"milligram per kilogram per minute","mg/kg/min","(MG/KG)/MIN","mass",1.6666666666666667e-8,[0,-1,0,0,0,0,0],"(mg/kg)/min",null,false,"M",null,1,false,false,0,"mg/(kg.min); mg per kg per min; milligrams per kilograms per minute","LOINC","RelMRat; MCntRat","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"milligram per liter","mg/L","MG/L","mass",1,[-3,0,1,0,0,0,0],"mg/L",null,false,"M",null,1,false,false,0,"mg per l; milligrams per liter; litre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"milligram per square meter","mg/m2","MG/M2","mass",0.001,[-2,0,1,0,0,0,0],"mg/(m2)",null,false,"M",null,1,false,false,0,"mg/m^2; mg/sq. m; mg per m2; mg per m^2; mg per sq. milligrams; meter squared; metre","LOINC","ArMass","Clinical","",null,null,null,null,false],[false,"milligram per cubic meter","mg/m3","MG/M3","mass",0.001,[-3,0,1,0,0,0,0],"mg/(m3)",null,false,"M",null,1,false,false,0,"mg/m^3; mg/cu. m; mg per m3; milligrams per cubic meter; meter cubed; metre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"milligram per milligram","mg/mg","MG/MG","mass",1,[0,0,0,0,0,0,0],"mg/mg",null,false,"M",null,1,false,false,0,"mg per mg; milligrams; milligram/milligram","LOINC","MRto","Clinical","",null,null,null,null,false],[false,"milligram per minute","mg/min","MG/MIN","mass",0.000016666666666666667,[0,-1,1,0,0,0,0],"mg/min",null,false,"M",null,1,false,false,0,"mg per min; milligrams per minutes; milligram/minute","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"milligram per milliliter","mg/mL","MG/ML","mass",1000.0000000000001,[-3,0,1,0,0,0,0],"mg/mL",null,false,"M",null,1,false,false,0,"mg per mL; milligrams per milliliters; millilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"milligram per millimole","mg/mmol","MG/MMOL","mass",1.660540186674939e-24,[0,0,1,0,0,0,0],"mg/mmol",null,false,"M",null,1,false,false,-1,"mg per mmol; milligrams per millimole; ","LOINC","Ratio","Clinical","",null,null,null,null,false],[false,"milligram per week","mg/wk","MG/WK","mass",1.6534391534391535e-9,[0,-1,1,0,0,0,0],"mg/wk",null,false,"M",null,1,false,false,0,"mg/week; mg per wk; milligrams per weeks; milligram/week","LOINC","Mrat","Clinical","",null,null,null,null,false],[false,"milliliter","mL","ML","volume",0.000001,[3,0,0,0,0,0,0],"mL","iso1000",true,null,null,1,false,false,0,"milliliters; millilitres","LOINC","Vol","Clinical","","l",null,"1",1,false],[false,"milliliter per 10 hour","mL/(10.h)","ML/HR","volume",2.7777777777777777e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/10hrs; ml/10 hrs; mL per 10hrs; 10 hrs; milliliters per 10 hours; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 12 hour","mL/(12.h)","ML/HR","volume",2.3148148148148147e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/12hrs; ml/12 hrs; mL per 12hrs; 12 hrs; milliliters per 12 hours; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 2 hour","mL/(2.h)","ML/HR","volume",1.3888888888888888e-10,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/2hrs; ml/2 hrs; mL per 2hrs; 2 hrs; milliliters per 2 hours; millilitres ","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 24 hour","mL/(24.h)","ML/HR","volume",1.1574074074074074e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/24hrs; ml/24 hrs; mL per 24hrs; 24 hrs; milliliters per 24 hours; millilitres; ml/dy; /day; ml per dy; days; fluid outputs; fluid inputs; flow rate","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 4 hour","mL/(4.h)","ML/HR","volume",6.944444444444444e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/4hrs; ml/4 hrs; mL per 4hrs; 4 hrs; milliliters per 4 hours; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 5 hour","mL/(5.h)","ML/HR","volume",5.5555555555555553e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/5hrs; ml/5 hrs; mL per 5hrs; 5 hrs; milliliters per 5 hours; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 6 hour","mL/(6.h)","ML/HR","volume",4.6296296296296294e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/6hrs; ml/6 hrs; mL per 6hrs; 6 hrs; milliliters per 6 hours; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 72 hour","mL/(72.h)","ML/HR","volume",3.8580246913580245e-12,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/72hrs; ml/72 hrs; mL per 72hrs; 72 hrs; milliliters per 72 hours; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 8 hour","mL/(8.h)","ML/HR","volume",3.472222222222222e-11,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"ml/8hrs; ml/8 hrs; mL per 8hrs; 8 hrs; milliliters per 8 hours; millilitres; shift","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per 8 hour per kilogram","mL/(8.h)/kg","(ML/HR)/KG","volume",3.472222222222222e-14,[3,-1,-1,0,0,0,0],"(mL/h)/kg","iso1000",true,null,null,1,false,false,0,"mL/kg/(8.h); ml/8h/kg; ml/8 h/kg; ml/8hr/kg; ml/8 hr/kgr; mL per 8h per kg; 8 h; 8hr; 8 hr; milliliters per 8 hours per kilogram; millilitres; shift","LOINC","VRatCnt","Clinical","unit used to measure renal excretion volume rate per body mass","l",null,"1",1,false],[false,"milliliter per square inch (international)","mL/[sin_i]","ML/[SIN_I]","volume",0.0015500031000061998,[1,0,0,0,0,0,0],"mL","iso1000",true,null,null,1,false,false,0,"mL/sin; mL/in2; mL/in^2; mL per sin; in2; in^2; sq. in; milliliters per square inch; inch squared","LOINC","ArVol","Clinical","","l",null,"1",1,false],[false,"milliliter per centimeter of water","mL/cm[H2O]","ML/CM[H2O]","volume",1.0197162129779282e-11,[4,2,-1,0,0,0,0],"mL/(cm HO2)","iso1000",true,null,null,1,false,false,0,"milliliters per centimeter of water; millilitre per centimetre of water; millilitres per centimetre of water; mL/cmH2O; mL/cm H2O; mL per cmH2O; mL per cm H2O","LOINC","Compli","Clinical","unit used to measure dynamic lung compliance","l",null,"1",1,false],[false,"milliliter per day","mL/d","ML/D","volume",1.1574074074074074e-11,[3,-1,0,0,0,0,0],"mL/d","iso1000",true,null,null,1,false,false,0,"ml/day; ml per day; milliliters per day; 24 hours; 24hrs; millilitre;","LOINC","VRat","Clinical","usually used to measure fluid output or input; flow rate","l",null,"1",1,false],[false,"milliliter per deciliter","mL/dL","ML/DL","volume",0.009999999999999998,[0,0,0,0,0,0,0],"mL/dL","iso1000",true,null,null,1,false,false,0,"mL per dL; millilitres; decilitre; milliliters","LOINC","VFr; VFrDiff","Clinical","","l",null,"1",1,false],[false,"milliliter per hour","mL/h","ML/HR","volume",2.7777777777777777e-10,[3,-1,0,0,0,0,0],"mL/h","iso1000",true,null,null,1,false,false,0,"mL/hr; mL per hr; milliliters per hour; millilitres; fluid intake; fluid output","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per kilogram","mL/kg","ML/KG","volume",9.999999999999999e-10,[3,0,-1,0,0,0,0],"mL/kg","iso1000",true,null,null,1,false,false,0,"mL per kg; milliliters per kilogram; millilitres","LOINC","VCnt","Clinical","","l",null,"1",1,false],[false,"milliliter per kilogram per 8 hour","mL/kg/(8.h)","(ML/KG)/HR","volume",3.472222222222222e-14,[3,-1,-1,0,0,0,0],"(mL/kg)/h","iso1000",true,null,null,1,false,false,0,"mL/(8.h.kg); mL/kg/8hrs; mL/kg/8 hrs; mL per kg per 8hrs; 8 hrs; milliliters per kilograms per 8 hours; millilitres; shift","LOINC","VCntRat; RelEngRat","Clinical","unit used to measure renal excretion volume rate per body mass","l",null,"1",1,false],[false,"milliliter per kilogram per day","mL/kg/d","(ML/KG)/D","volume",1.1574074074074072e-14,[3,-1,-1,0,0,0,0],"(mL/kg)/d","iso1000",true,null,null,1,false,false,0,"mL/(kg.d); mL/kg/dy; mL per kg per day; milliliters per kilograms per day; mg/kg/24hrs; 24 hrs; per 24 hours millilitres","LOINC","VCntRat; RelEngRat","Clinical","unit used to measure renal excretion volume rate per body mass","l",null,"1",1,false],[false,"milliliter per kilogram per hour","mL/kg/h","(ML/KG)/HR","volume",2.7777777777777774e-13,[3,-1,-1,0,0,0,0],"(mL/kg)/h","iso1000",true,null,null,1,false,false,0,"mL/(kg.h); mL/kg/hr; mL per kg per hr; milliliters per kilograms per hour; millilitres","LOINC","VCntRat; RelEngRat","Clinical","unit used to measure renal excretion volume rate per body mass","l",null,"1",1,false],[false,"milliliter per kilogram per minute","mL/kg/min","(ML/KG)/MIN","volume",1.6666666666666664e-11,[3,-1,-1,0,0,0,0],"(mL/kg)/min","iso1000",true,null,null,1,false,false,0,"mL/(kg.min); mL/kg/dy; mL per kg per day; milliliters per kilograms per day; millilitres","LOINC","RelEngRat","Clinical","used for tests that measure activity metabolic rate compared to standard resting metabolic rate ","l",null,"1",1,false],[false,"milliliter per square meter","mL/m2","ML/M2","volume",0.000001,[1,0,0,0,0,0,0],"mL/(m2)","iso1000",true,null,null,1,false,false,0,"mL/m^2; mL/sq. meter; mL per m2; m^2; sq. meter; milliliters per square meter; millilitres; meter squared","LOINC","ArVol","Clinical","used for tests that relate to heart work - e.g. ventricular stroke volume; atrial volume per body surface area","l",null,"1",1,false],[false,"milliliter per millibar","mL/mbar","ML/MBAR","volume",1e-11,[4,2,-1,0,0,0,0],"mL/mbar","iso1000",true,null,null,1,false,false,0,"mL per mbar; milliliters per millibar; millilitres","LOINC","","Clinical","unit used to measure dynamic lung compliance","l",null,"1",1,false],[false,"milliliter per minute","mL/min","ML/MIN","volume",1.6666666666666667e-8,[3,-1,0,0,0,0,0],"mL/min","iso1000",true,null,null,1,false,false,0,"mL per min; milliliters; millilitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"milliliter per minute per square meter","mL/min/m2","(ML/MIN)/M2","volume",1.6666666666666667e-8,[1,-1,0,0,0,0,0],"(mL/min)/(m2)","iso1000",true,null,null,1,false,false,0,"ml/min/m^2; ml/min/sq. meter; mL per min per m2; m^2; sq. meter; milliliters per minutes per square meter; millilitres; metre; meter squared","LOINC","ArVRat","Clinical","unit used to measure volume per body surface area; oxygen consumption index","l",null,"1",1,false],[false,"milliliter per millimeter","mL/mm","ML/MM","volume",0.001,[2,0,0,0,0,0,0],"mL/mm","iso1000",true,null,null,1,false,false,0,"mL per mm; milliliters per millimeter; millilitres; millimetre","LOINC","Lineic Volume","Clinical","","l",null,"1",1,false],[false,"milliliter per second","mL/s","ML/S","volume",0.000001,[3,-1,0,0,0,0,0],"mL/s","iso1000",true,null,null,1,false,false,0,"ml/sec; mL per sec; milliliters per second; millilitres","LOINC","Vel; VelRat; VRat","Clinical","","l",null,"1",1,false],[false,"millimeter","mm","MM","length",0.001,[1,0,0,0,0,0,0],"mm",null,false,"L",null,1,false,false,0,"millimeters; millimetres; height; length; diameter; thickness; axis; curvature; size","LOINC","Len","Clinical","",null,null,null,null,false],[false,"millimeter per hour","mm/h","MM/HR","length",2.7777777777777776e-7,[1,-1,0,0,0,0,0],"mm/h",null,false,"L",null,1,false,false,0,"mm/hr; mm per hr; millimeters per hour; millimetres","LOINC","Vel","Clinical","unit to measure sedimentation rate",null,null,null,null,false],[false,"millimeter per minute","mm/min","MM/MIN","length",0.000016666666666666667,[1,-1,0,0,0,0,0],"mm/min",null,false,"L",null,1,false,false,0,"mm per min; millimeters per minute; millimetres","LOINC","Vel","Clinical","",null,null,null,null,false],[false,"millimeter of water","mm[H2O]","MM[H2O]","pressure",9806.65,[-1,-2,1,0,0,0,0],"mm HO2","clinical",true,null,null,1,false,false,0,"mmH2O; mm H2O; millimeters of water; millimetres","LOINC","Pres","Clinical","","kPa","KPAL","980665e-5",9.80665,false],[false,"millimeter of mercury","mm[Hg]","MM[HG]","pressure",133322,[-1,-2,1,0,0,0,0],"mm Hg","clinical",true,null,null,1,false,false,0,"mmHg; mm Hg; millimeters of mercury; millimetres","LOINC","Pres; PPres; Ratio","Clinical","1 mm[Hg] = 1 torr; unit to measure blood pressure","kPa","KPAL","133.3220",133.322,false],[false,"square millimeter","mm2","MM2","length",0.000001,[2,0,0,0,0,0,0],"mm2",null,false,"L",null,1,false,false,0,"mm^2; sq. mm.; sq. millimeters; millimeters squared; millimetres","LOINC","Area","Clinical","",null,null,null,null,false],[false,"millimole","mmol","MMOL","amount of substance",602213670000000000000,[0,0,0,0,0,0,0],"mmol","si",true,null,null,1,false,false,1,"millimoles","LOINC","Sub","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per 12 hour","mmol/(12.h)","MMOL/HR","amount of substance",13940131250000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/12hrs; mmol/12 hrs; mmol per 12 hrs; 12hrs; millimoles per 12 hours","LOINC","SRat","Clinical","unit for tests related to urine","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per 2 hour","mmol/(2.h)","MMOL/HR","amount of substance",83640787500000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/2hrs; mmol/2 hrs; mmol per 2 hrs; 2hrs; millimoles per 2 hours","LOINC","SRat","Clinical","unit for tests related to urine","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per 24 hour","mmol/(24.h)","MMOL/HR","amount of substance",6970065625000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/24hrs; mmol/24 hrs; mmol per 24 hrs; 24hrs; millimoles per 24 hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per 5 hour","mmol/(5.h)","MMOL/HR","amount of substance",33456315000000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/5hrs; mmol/5 hrs; mmol per 5 hrs; 5hrs; millimoles per 5 hours","LOINC","SRat","Clinical","unit for tests related to doses","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per 6 hour","mmol/(6.h)","MMOL/HR","amount of substance",27880262500000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/6hrs; mmol/6 hrs; mmol per 6 hrs; 6hrs; millimoles per 6 hours","LOINC","SRat","Clinical","unit for tests related to urine","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per 8 hour","mmol/(8.h)","MMOL/HR","amount of substance",20910196875000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/8hrs; mmol/8 hrs; mmol per 8 hrs; 8hrs; millimoles per 8 hours; shift","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per day","mmol/d","MMOL/D","amount of substance",6970065625000000,[0,-1,0,0,0,0,0],"mmol/d","si",true,null,null,1,false,false,1,"mmol/24hrs; mmol/24 hrs; mmol per 24 hrs; 24hrs; millimoles per 24 hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per deciliter","mmol/dL","MMOL/DL","amount of substance",6.022136699999999e+24,[-3,0,0,0,0,0,0],"mmol/dL","si",true,null,null,1,false,false,1,"mmol per dL; millimoles; decilitre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per gram","mmol/g","MMOL/G","amount of substance",602213670000000000000,[0,0,-1,0,0,0,0],"mmol/g","si",true,null,null,1,false,false,1,"mmol per gram; millimoles","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per hour","mmol/h","MMOL/HR","amount of substance",167281575000000000,[0,-1,0,0,0,0,0],"mmol/h","si",true,null,null,1,false,false,1,"mmol/hr; mmol per hr; millimoles per hour","LOINC","SRat","Clinical","unit for tests related to urine","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per kilogram","mmol/kg","MMOL/KG","amount of substance",602213670000000000,[0,0,-1,0,0,0,0],"mmol/kg","si",true,null,null,1,false,false,1,"mmol per kg; millimoles per kilogram","LOINC","SCnt","Clinical","unit for tests related to stool","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per kilogram per 8 hour","mmol/kg/(8.h)","(MMOL/KG)/HR","amount of substance",20910196875000,[0,-1,-1,0,0,0,0],"(mmol/kg)/h","si",true,null,null,1,false,false,1,"mmol/(8.h.kg); mmol/kg/8hrs; mmol/kg/8 hrs; mmol per kg per 8hrs; 8 hrs; millimoles per kilograms per 8 hours; shift","LOINC","CCnt","Clinical","unit used to measure molar dose rate per patient body mass","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per kilogram per day","mmol/kg/d","(MMOL/KG)/D","amount of substance",6970065625000,[0,-1,-1,0,0,0,0],"(mmol/kg)/d","si",true,null,null,1,false,false,1,"mmol/kg/dy; mmol/kg/day; mmol per kg per dy; millimoles per kilograms per day","LOINC","RelSRat","Clinical","unit used to measure molar dose rate per patient body mass","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per kilogram per hour","mmol/kg/h","(MMOL/KG)/HR","amount of substance",167281575000000,[0,-1,-1,0,0,0,0],"(mmol/kg)/h","si",true,null,null,1,false,false,1,"mmol/kg/hr; mmol per kg per hr; millimoles per kilograms per hour","LOINC","CCnt","Clinical","unit used to measure molar dose rate per patient body mass","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per kilogram per minute","mmol/kg/min","(MMOL/KG)/MIN","amount of substance",10036894500000000,[0,-1,-1,0,0,0,0],"(mmol/kg)/min","si",true,null,null,1,false,false,1,"mmol/(kg.min); mmol/kg/min; mmol per kg per min; millimoles per kilograms per minute","LOINC","CCnt","Clinical","unit used to measure molar dose rate per patient body mass; note that the unit for the enzyme unit U = umol/min. mmol/kg/min = kU/kg; ","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per liter","mmol/L","MMOL/L","amount of substance",6.0221367e+23,[-3,0,0,0,0,0,0],"mmol/L","si",true,null,null,1,false,false,1,"mmol per L; millimoles per liter; litre","LOINC","SCnc","Clinical","unit for tests related to doses","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per square meter","mmol/m2","MMOL/M2","amount of substance",602213670000000000000,[-2,0,0,0,0,0,0],"mmol/(m2)","si",true,null,null,1,false,false,1,"mmol/m^2; mmol/sq. meter; mmol per m2; m^2; sq. meter; millimoles; meter squared; metre","LOINC","ArSub","Clinical","unit used to measure molar dose per patient body surface area","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per minute","mmol/min","MMOL/MIN","amount of substance",10036894500000000000,[0,-1,0,0,0,0,0],"mmol/min","si",true,null,null,1,false,false,1,"mmol per min; millimoles per minute","LOINC","Srat; CAct","Clinical","unit for the enzyme unit U = umol/min. mmol/min = kU","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per millimole","mmol/mmol","MMOL/MMOL","amount of substance",1,[0,0,0,0,0,0,0],"mmol/mmol","si",true,null,null,1,false,false,0,"mmol per mmol; millimoles per millimole","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per mole","mmol/mol","MMOL/MOL","amount of substance",0.001,[0,0,0,0,0,0,0],"mmol/mol","si",true,null,null,1,false,false,0,"mmol per mol; millimoles per mole","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"millimole per second per liter","mmol/s/L","(MMOL/S)/L","amount of substance",6.0221367e+23,[-3,-1,0,0,0,0,0],"(mmol/s)/L","si",true,null,null,1,false,false,1,"mmol/sec/L; mmol per s per L; per sec; millimoles per seconds per liter; litre","LOINC","CCnc ","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per kilogram","mol/kg","MOL/KG","amount of substance",602213670000000000000,[0,0,-1,0,0,0,0],"mol/kg","si",true,null,null,1,false,false,1,"mol per kg; moles; mols","LOINC","SCnt","Clinical","unit for tests related to stool","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per kilogram per second","mol/kg/s","(MOL/KG)/S","amount of substance",602213670000000000000,[0,-1,-1,0,0,0,0],"(mol/kg)/s","si",true,null,null,1,false,false,1,"mol/kg/sec; mol per kg per sec; moles per kilograms per second; mols","LOINC","CCnt","Clinical","unit of catalytic activity (mol/s) per mass (kg)","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per liter","mol/L","MOL/L","amount of substance",6.0221366999999994e+26,[-3,0,0,0,0,0,0],"mol/L","si",true,null,null,1,false,false,1,"mol per L; moles per liter; litre; moles; mols","LOINC","SCnc","Clinical","unit often used in tests measuring oxygen content","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per cubic meter","mol/m3","MOL/M3","amount of substance",6.0221367e+23,[-3,0,0,0,0,0,0],"mol/(m3)","si",true,null,null,1,false,false,1,"mol/m^3; mol/cu. m; mol per m3; m^3; cu. meter; mols; moles; meters cubed; metre; mole per kiloliter; kilolitre; mol/kL","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per milliliter","mol/mL","MOL/ML","amount of substance",6.0221367e+29,[-3,0,0,0,0,0,0],"mol/mL","si",true,null,null,1,false,false,1,"mol per mL; moles; millilitre; mols","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per mole","mol/mol","MOL/MOL","amount of substance",1,[0,0,0,0,0,0,0],"mol/mol","si",true,null,null,1,false,false,0,"mol per mol; moles per mol; mols","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"mole per second","mol/s","MOL/S","amount of substance",6.0221367e+23,[0,-1,0,0,0,0,0],"mol/s","si",true,null,null,1,false,false,1,"mol per sec; moles per second; mols","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"milliosmole","mosm","MOSM","amount of substance (dissolved particles)",602213670000000000000,[0,0,0,0,0,0,0],"mosm","chemical",true,null,null,1,false,false,1,"milliosmoles","LOINC","Osmol","Clinical","equal to 1/1000 of an osmole","mol","MOL","1",1,false],[false,"milliosmole per kilogram","mosm/kg","MOSM/KG","amount of substance (dissolved particles)",602213670000000000,[0,0,-1,0,0,0,0],"mosm/kg","chemical",true,null,null,1,false,false,1,"mosm per kg; milliosmoles per kilogram","LOINC","Osmol","Clinical","","mol","MOL","1",1,false],[false,"milliosmole per liter","mosm/L","MOSM/L","amount of substance (dissolved particles)",6.0221367e+23,[-3,0,0,0,0,0,0],"mosm/L","chemical",true,null,null,1,false,false,1,"mosm per liter; litre; milliosmoles","LOINC","Osmol","Clinical","","mol","MOL","1",1,false],[false,"millipascal","mPa","MPAL","pressure",1,[-1,-2,1,0,0,0,0],"mPa","si",true,null,null,1,false,false,0,"millipascals","LOINC","Pres","Clinical","unit of pressure","N/m2","N/M2","1",1,false],[false,"millipascal second","mPa.s","MPAL.S","pressure",1,[-1,-1,1,0,0,0,0],"mPa.s","si",true,null,null,1,false,false,0,"mPa*s; millipoise; mP; dynamic viscosity","LOINC","Visc","Clinical","base units for millipoise, a measurement of dynamic viscosity","N/m2","N/M2","1",1,false],[false,"megasecond","Ms","MAS","time",1000000,[0,1,0,0,0,0,0],"Ms",null,false,"T",null,1,false,false,0,"megaseconds","LOINC","Time","Clinical","",null,null,null,null,false],[false,"millisecond","ms","MS","time",0.001,[0,1,0,0,0,0,0],"ms",null,false,"T",null,1,false,false,0,"milliseconds; duration","LOINC","Time","Clinical","",null,null,null,null,false],[false,"milli enzyme unit per gram","mU/g","MU/G","catalytic activity",10036894500000,[0,-1,-1,0,0,0,0],"mU/g","chemical",true,null,null,1,false,false,1,"mU per gm; milli enzyme units per gram; enzyme activity; enzymatic activity per mass","LOINC","CCnt","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 mU = 1 nmol/min","umol/min","UMOL/MIN","1",1,false],[false,"milli enzyme unit per liter","mU/L","MU/L","catalytic activity",10036894500000000,[-3,-1,0,0,0,0,0],"mU/L","chemical",true,null,null,1,false,false,1,"mU per liter; litre; milli enzyme units enzymatic activity per volume; enzyme activity","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 mU = 1 nmol/min","umol/min","UMOL/MIN","1",1,false],[false,"milli enzyme unit per milligram","mU/mg","MU/MG","catalytic activity",10036894500000000,[0,-1,-1,0,0,0,0],"mU/mg","chemical",true,null,null,1,false,false,1,"mU per mg; milli enzyme units per milligram","LOINC","CCnt","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 mU = 1 nmol/min","umol/min","UMOL/MIN","1",1,false],[false,"milli enzyme unit per milliliter","mU/mL","MU/ML","catalytic activity",10036894500000000000,[-3,-1,0,0,0,0,0],"mU/mL","chemical",true,null,null,1,false,false,1,"mU per mL; milli enzyme units per milliliter; millilitre; enzymatic activity per volume; enzyme activity","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 mU = 1 nmol/min","umol/min","UMOL/MIN","1",1,false],[false,"milli enzyme unit per milliliter per minute","mU/mL/min","(MU/ML)/MIN","catalytic activity",167281575000000000,[-3,-2,0,0,0,0,0],"(mU/mL)/min","chemical",true,null,null,1,false,false,1,"mU per mL per min; mU per milliliters per minute; millilitres; milli enzyme units; enzymatic activity; enzyme activity","LOINC","CCncRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 mU = 1 nmol/min","umol/min","UMOL/MIN","1",1,false],[false,"millivolt","mV","MV","electric potential",1,[2,-2,1,0,0,-1,0],"mV","si",true,null,null,1,false,false,0,"millivolts","LOINC","Elpot","Clinical","unit of electric potential (voltage)","J/C","J/C","1",1,false],[false,"Newton centimeter","N.cm","N.CM","force",10,[2,-2,1,0,0,0,0],"N.cm","si",true,null,null,1,false,false,0,"N*cm; Ncm; N cm; Newton*centimeters; Newton* centimetres; torque; work","LOINC","","Clinical","as a measurement of work, N.cm = 1/100 Joules;\nnote that N.m is the standard unit of measurement for torque (although dimensionally equivalent to Joule), and N.cm can also be thought of as a torqe unit","kg.m/s2","KG.M/S2","1",1,false],[false,"Newton second","N.s","N.S","force",1000,[1,-1,1,0,0,0,0],"N.s","si",true,null,null,1,false,false,0,"Newton*seconds; N*s; N s; Ns; impulse; imp","LOINC","","Clinical","standard unit of impulse","kg.m/s2","KG.M/S2","1",1,false],[false,"nanogram","ng","NG","mass",1e-9,[0,0,1,0,0,0,0],"ng",null,false,"M",null,1,false,false,0,"nanograms","LOINC","Mass","Clinical","",null,null,null,null,false],[false,"nanogram per 24 hour","ng/(24.h)","NG/HR","mass",1.1574074074074075e-14,[0,-1,1,0,0,0,0],"ng/h",null,false,"M",null,1,false,false,0,"ng/24hrs; ng/24 hrs; nanograms per 24 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"nanogram per 8 hour","ng/(8.h)","NG/HR","mass",3.4722222222222224e-14,[0,-1,1,0,0,0,0],"ng/h",null,false,"M",null,1,false,false,0,"ng/8hrs; ng/8 hrs; nanograms per 8 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"nanogram per million","ng/10*6","NG/(10*6)","mass",1e-15,[0,0,1,0,0,0,0],"ng/(106)",null,false,"M",null,1,false,false,0,"ng/10^6; ng per 10*6; 10^6; nanograms","LOINC","MNum","Clinical","",null,null,null,null,false],[false,"nanogram per day","ng/d","NG/D","mass",1.1574074074074075e-14,[0,-1,1,0,0,0,0],"ng/d",null,false,"M",null,1,false,false,0,"ng/dy; ng per day; nanograms ","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"nanogram per deciliter","ng/dL","NG/DL","mass",0.00001,[-3,0,1,0,0,0,0],"ng/dL",null,false,"M",null,1,false,false,0,"ng per dL; nanograms per deciliter; decilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"nanogram per gram","ng/g","NG/G","mass",1e-9,[0,0,0,0,0,0,0],"ng/g",null,false,"M",null,1,false,false,0,"ng/gm; ng per gm; nanograms per gram","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"nanogram per hour","ng/h","NG/HR","mass",2.777777777777778e-13,[0,-1,1,0,0,0,0],"ng/h",null,false,"M",null,1,false,false,0,"ng/hr; ng per hr; nanograms per hour","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"nanogram per kilogram","ng/kg","NG/KG","mass",1e-12,[0,0,0,0,0,0,0],"ng/kg",null,false,"M",null,1,false,false,0,"ng per kg; nanograms per kilogram","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"nanogram per kilogram per 8 hour","ng/kg/(8.h)","(NG/KG)/HR","mass",3.472222222222222e-17,[0,-1,0,0,0,0,0],"(ng/kg)/h",null,false,"M",null,1,false,false,0,"ng/(8.h.kg); ng/kg/8hrs; ng/kg/8 hrs; ng per kg per 8hrs; 8 hrs; nanograms per kilograms per 8 hours; shift","LOINC","MRtoRat ","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"nanogram per kilogram per hour","ng/kg/h","(NG/KG)/HR","mass",2.7777777777777775e-16,[0,-1,0,0,0,0,0],"(ng/kg)/h",null,false,"M",null,1,false,false,0,"ng/(kg.h); ng/kg/hr; ng per kg per hr; nanograms per kilograms per hour","LOINC","MRtoRat ","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"nanogram per kilogram per minute","ng/kg/min","(NG/KG)/MIN","mass",1.6666666666666667e-14,[0,-1,0,0,0,0,0],"(ng/kg)/min",null,false,"M",null,1,false,false,0,"ng/(kg.min); ng per kg per min; nanograms per kilograms per minute","LOINC","MRtoRat ","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"nanogram per liter","ng/L","NG/L","mass",0.000001,[-3,0,1,0,0,0,0],"ng/L",null,false,"M",null,1,false,false,0,"ng per L; nanograms per liter; litre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"nanogram per square meter","ng/m2","NG/M2","mass",1e-9,[-2,0,1,0,0,0,0],"ng/(m2)",null,false,"M",null,1,false,false,0,"ng/m^2; ng/sq. m; ng per m2; m^2; sq. meter; nanograms; meter squared; metre","LOINC","ArMass","Clinical","unit used to measure mass dose per patient body surface area",null,null,null,null,false],[false,"nanogram per milligram","ng/mg","NG/MG","mass",0.000001,[0,0,0,0,0,0,0],"ng/mg",null,false,"M",null,1,false,false,0,"ng per mg; nanograms","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"nanogram per milligram per hour","ng/mg/h","(NG/MG)/HR","mass",2.7777777777777777e-10,[0,-1,0,0,0,0,0],"(ng/mg)/h",null,false,"M",null,1,false,false,0,"ng/mg/hr; ng per mg per hr; nanograms per milligrams per hour","LOINC","MRtoRat ","Clinical","",null,null,null,null,false],[false,"nanogram per minute","ng/min","NG/MIN","mass",1.6666666666666667e-11,[0,-1,1,0,0,0,0],"ng/min",null,false,"M",null,1,false,false,0,"ng per min; nanograms","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"nanogram per millliiter","ng/mL","NG/ML","mass",0.001,[-3,0,1,0,0,0,0],"ng/mL",null,false,"M",null,1,false,false,0,"ng per mL; nanograms; millilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"nanogram per milliliter per hour","ng/mL/h","(NG/ML)/HR","mass",2.7777777777777776e-7,[-3,-1,1,0,0,0,0],"(ng/mL)/h",null,false,"M",null,1,false,false,0,"ng/mL/hr; ng per mL per mL; nanograms per milliliter per hour; nanogram per millilitre per hour; nanograms per millilitre per hour; enzymatic activity per volume; enzyme activity per milliliters","LOINC","CCnc","Clinical","tests that measure enzymatic activity",null,null,null,null,false],[false,"nanogram per second","ng/s","NG/S","mass",1e-9,[0,-1,1,0,0,0,0],"ng/s",null,false,"M",null,1,false,false,0,"ng/sec; ng per sec; nanograms per second","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"nanogram per enzyme unit","ng/U","NG/U","mass",9.963241120049634e-26,[0,1,1,0,0,0,0],"ng/U",null,false,"M",null,1,false,false,-1,"ng per U; nanograms per enzyme unit","LOINC","CMass","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)",null,null,null,null,false],[false,"nanokatal","nkat","NKAT","catalytic activity",602213670000000,[0,-1,0,0,0,0,0],"nkat","chemical",true,null,null,1,false,false,1,"nanokatals","LOINC","CAct","Clinical","kat is a unit of catalytic activity with base units = mol/s. Rarely used because its units are too large to practically express catalytic activity. See enzyme unit [U] which is the standard unit for catalytic activity.","mol/s","MOL/S","1",1,false],[false,"nanoliter","nL","NL","volume",1.0000000000000002e-12,[3,0,0,0,0,0,0],"nL","iso1000",true,null,null,1,false,false,0,"nanoliters; nanolitres","LOINC","Vol","Clinical","","l",null,"1",1,false],[false,"nanometer","nm","NM","length",1e-9,[1,0,0,0,0,0,0],"nm",null,false,"L",null,1,false,false,0,"nanometers; nanometres","LOINC","Len","Clinical","",null,null,null,null,false],[false,"nanometer per second per liter","nm/s/L","(NM/S)/L","length",0.000001,[-2,-1,0,0,0,0,0],"(nm/s)/L",null,false,"L",null,1,false,false,0,"nm/sec/liter; nm/sec/litre; nm per s per l; nm per sec per l; nanometers per second per liter; nanometre per second per litre; nanometres per second per litre","LOINC","VelCnc","Clinical","",null,null,null,null,false],[false,"nanomole","nmol","NMOL","amount of substance",602213670000000,[0,0,0,0,0,0,0],"nmol","si",true,null,null,1,false,false,1,"nanomoles","LOINC","Sub","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per 24 hour","nmol/(24.h)","NMOL/HR","amount of substance",6970065625,[0,-1,0,0,0,0,0],"nmol/h","si",true,null,null,1,false,false,1,"nmol/24hr; nmol/24 hr; nanomoles per 24 hours; nmol/day; nanomoles per day; nmol per day; nanomole/day; nanomol/day","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per day","nmol/d","NMOL/D","amount of substance",6970065625,[0,-1,0,0,0,0,0],"nmol/d","si",true,null,null,1,false,false,1,"nmol/day; nanomoles per day; nmol per day; nanomole/day; nanomol/day; nmol/24hr; nmol/24 hr; nanomoles per 24 hours; ","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per deciliter","nmol/dL","NMOL/DL","amount of substance",6022136700000000000,[-3,0,0,0,0,0,0],"nmol/dL","si",true,null,null,1,false,false,1,"nmol per dL; nanomoles per deciliter; nanomole per decilitre; nanomoles per decilitre; nanomole/deciliter; nanomole/decilitre; nanomol/deciliter; nanomol/decilitre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per gram","nmol/g","NMOL/G","amount of substance",602213670000000,[0,0,-1,0,0,0,0],"nmol/g","si",true,null,null,1,false,false,1,"nmol per gram; nanomoles per gram; nanomole/gram","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per hour per liter","nmol/h/L","(NMOL/HR)/L","amount of substance",167281575000000,[-3,-1,0,0,0,0,0],"(nmol/h)/L","si",true,null,null,1,false,false,1,"nmol/hrs/L; nmol per hrs per L; nanomoles per hours per liter; litre; enzymatic activity per volume; enzyme activities","LOINC","CCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per liter","nmol/L","NMOL/L","amount of substance",602213670000000000,[-3,0,0,0,0,0,0],"nmol/L","si",true,null,null,1,false,false,1,"nmol per L; nanomoles per liter; litre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per milligram","nmol/mg","NMOL/MG","amount of substance",602213670000000000,[0,0,-1,0,0,0,0],"nmol/mg","si",true,null,null,1,false,false,1,"nmol per mg; nanomoles per milligram","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per milligram per hour","nmol/mg/h","(NMOL/MG)/HR","amount of substance",167281575000000,[0,-1,-1,0,0,0,0],"(nmol/mg)/h","si",true,null,null,1,false,false,1,"nmol/mg/hr; nmol per mg per hr; nanomoles per milligrams per hour","LOINC","SCntRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per milligram of protein","nmol/mg{prot}","NMOL/MG","amount of substance",602213670000000000,[0,0,-1,0,0,0,0],"nmol/mg","si",true,null,null,1,false,false,1,"nanomoles; nmol/mg prot; nmol per mg prot","LOINC","Ratio; CCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per minute","nmol/min","NMOL/MIN","amount of substance",10036894500000,[0,-1,0,0,0,0,0],"nmol/min","si",true,null,null,1,false,false,1,"nmol per min; nanomoles per minute; milli enzyme units; enzyme activity per volume; enzymatic activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. nmol/min = mU (milli enzyme unit)","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per minute per milliliter","nmol/min/mL","(NMOL/MIN)/ML","amount of substance",10036894500000000000,[-3,-1,0,0,0,0,0],"(nmol/min)/mL","si",true,null,null,1,false,false,1,"nmol per min per mL; nanomoles per minutes per milliliter; millilitre; milli enzyme units per volume; enzyme activity; enzymatic activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. nmol/mL/min = mU/mL","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per milliliter","nmol/mL","NMOL/ML","amount of substance",602213670000000000000,[-3,0,0,0,0,0,0],"nmol/mL","si",true,null,null,1,false,false,1,"nmol per mL; nanomoles per milliliter; millilitre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per milliliter per hour","nmol/mL/h","(NMOL/ML)/HR","amount of substance",167281575000000000,[-3,-1,0,0,0,0,0],"(nmol/mL)/h","si",true,null,null,1,false,false,1,"nmol/mL/hr; nmol per mL per hr; nanomoles per milliliters per hour; millilitres; milli enzyme units per volume; enzyme activity; enzymatic activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min.","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per milliliter per minute","nmol/mL/min","(NMOL/ML)/MIN","amount of substance",10036894500000000000,[-3,-1,0,0,0,0,0],"(nmol/mL)/min","si",true,null,null,1,false,false,1,"nmol per mL per min; nanomoles per milliliters per min; millilitres; milli enzyme units per volume; enzyme activity; enzymatic activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. nmol/mL/min = mU/mL","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per millimole","nmol/mmol","NMOL/MMOL","amount of substance",0.000001,[0,0,0,0,0,0,0],"nmol/mmol","si",true,null,null,1,false,false,0,"nmol per mmol; nanomoles per millimole","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per millimole of creatinine","nmol/mmol{creat}","NMOL/MMOL","amount of substance",0.000001,[0,0,0,0,0,0,0],"nmol/mmol","si",true,null,null,1,false,false,0,"nanomoles","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per mole","nmol/mol","NMOL/MOL","amount of substance",1e-9,[0,0,0,0,0,0,0],"nmol/mol","si",true,null,null,1,false,false,0,"nmol per mole; nanomoles","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per nanomole","nmol/nmol","NMOL/NMOL","amount of substance",1,[0,0,0,0,0,0,0],"nmol/nmol","si",true,null,null,1,false,false,0,"nmol per nmol; nanomoles","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per second","nmol/s","NMOL/S","amount of substance",602213670000000,[0,-1,0,0,0,0,0],"nmol/s","si",true,null,null,1,false,false,1,"nmol/sec; nmol per sec; nanomoles per sercond; milli enzyme units; enzyme activity; enzymatic activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min.","10*23","10*23","6.0221367",6.0221367,false],[false,"nanomole per second per liter","nmol/s/L","(NMOL/S)/L","amount of substance",602213670000000000,[-3,-1,0,0,0,0,0],"(nmol/s)/L","si",true,null,null,1,false,false,1,"nmol/sec/L; nmol per s per L; nmol per sec per L; nanomoles per seconds per liter; litre; milli enzyme units per volume; enzyme activity; enzymatic activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min.","10*23","10*23","6.0221367",6.0221367,false],[false,"nanosecond","ns","NS","time",1e-9,[0,1,0,0,0,0,0],"ns",null,false,"T",null,1,false,false,0,"nanoseconds","LOINC","Time","Clinical","",null,null,null,null,false],[false,"nanoenzyme unit per milliliter","nU/mL","NU/ML","catalytic activity",10036894500000,[-3,-1,0,0,0,0,0],"nU/mL","chemical",true,null,null,1,false,false,1,"nU per mL; nanoenzyme units per milliliter; millilitre; enzymatic activity per volume; enzyme activity","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 fU = pmol/min","umol/min","UMOL/MIN","1",1,false],[false,"Ohm meter","Ohm.m","OHM.M","electric resistance",1000,[3,-1,1,0,0,-2,0],"Ω.m","si",true,null,null,1,false,false,0,"electric resistivity; meters; metres","LOINC","","Clinical","unit of electric resistivity","V/A","V/A","1",1,false],[false,"osmole per kilogram","osm/kg","OSM/KG","amount of substance (dissolved particles)",602213670000000000000,[0,0,-1,0,0,0,0],"osm/kg","chemical",true,null,null,1,false,false,1,"osm per kg; osmoles per kilogram; osmols","LOINC","Osmol","Clinical","","mol","MOL","1",1,false],[false,"osmole per liter","osm/L","OSM/L","amount of substance (dissolved particles)",6.0221366999999994e+26,[-3,0,0,0,0,0,0],"osm/L","chemical",true,null,null,1,false,false,1,"osm per L; osmoles per liter; litre; osmols","LOINC","Osmol","Clinical","","mol","MOL","1",1,false],[false,"picoampere","pA","PA","electric current",1e-12,[0,-1,0,0,0,1,0],"pA","si",true,null,null,1,false,false,0,"picoamperes","LOINC","","Clinical","equal to 10^-12 amperes","C/s","C/S","1",1,false],[false,"picogram","pg","PG","mass",1e-12,[0,0,1,0,0,0,0],"pg",null,false,"M",null,1,false,false,0,"picograms","LOINC","Mass; EntMass","Clinical","",null,null,null,null,false],[false,"picogram per deciliter","pg/dL","PG/DL","mass",9.999999999999999e-9,[-3,0,1,0,0,0,0],"pg/dL",null,false,"M",null,1,false,false,0,"pg per dL; picograms; decilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"picogram per liter","pg/L","PG/L","mass",1e-9,[-3,0,1,0,0,0,0],"pg/L",null,false,"M",null,1,false,false,0,"pg per L; picograms; litre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"picogram per milligram","pg/mg","PG/MG","mass",1e-9,[0,0,0,0,0,0,0],"pg/mg",null,false,"M",null,1,false,false,0,"pg per mg; picograms","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"picogram per milliliter","pg/mL","PG/ML","mass",0.000001,[-3,0,1,0,0,0,0],"pg/mL",null,false,"M",null,1,false,false,0,"pg per mL; picograms per milliliter; millilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"picogram per millimeter","pg/mm","PG/MM","mass",1e-9,[-1,0,1,0,0,0,0],"pg/mm",null,false,"M",null,1,false,false,0,"pg per mm; picogram/millimeter; picogram/millimetre; picograms per millimeter; millimetre","LOINC","Lineic Mass","Clinical","",null,null,null,null,false],[false,"picokatal","pkat","PKAT","catalytic activity",602213670000,[0,-1,0,0,0,0,0],"pkat","chemical",true,null,null,1,false,false,1,"pkats; picokatals","LOINC","CAct","Clinical","kat is a unit of catalytic activity with base units = mol/s. Rarely used because its units are too large to practically express catalytic activity. See enzyme unit [U] which is the standard unit for catalytic activity.","mol/s","MOL/S","1",1,false],[false,"picoliter","pL","PL","volume",1e-15,[3,0,0,0,0,0,0],"pL","iso1000",true,null,null,1,false,false,0,"picoliters; picolitres","LOINC","Vol","Clinical","","l",null,"1",1,false],[false,"picometer","pm","PM","length",1e-12,[1,0,0,0,0,0,0],"pm",null,false,"L",null,1,false,false,0,"picometers; picometres","LOINC","Len","Clinical","",null,null,null,null,false],[false,"picomole","pmol","PMOL","amount of substance",602213670000,[0,0,0,0,0,0,0],"pmol","si",true,null,null,1,false,false,1,"picomoles; pmols","LOINC","Sub","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per 24 hour","pmol/(24.h)","PMOL/HR","amount of substance",6970065.625,[0,-1,0,0,0,0,0],"pmol/h","si",true,null,null,1,false,false,1,"pmol/24hrs; pmol/24 hrs; pmol per 24 hrs; 24hrs; days; dy; picomoles per 24 hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per day","pmol/d","PMOL/D","amount of substance",6970065.625,[0,-1,0,0,0,0,0],"pmol/d","si",true,null,null,1,false,false,1,"pmol/dy; pmol per day; 24 hours; 24hrs; 24 hrs; picomoles","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per deciliter","pmol/dL","PMOL/DL","amount of substance",6022136700000000,[-3,0,0,0,0,0,0],"pmol/dL","si",true,null,null,1,false,false,1,"pmol per dL; picomoles per deciliter; decilitre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per gram","pmol/g","PMOL/G","amount of substance",602213670000,[0,0,-1,0,0,0,0],"pmol/g","si",true,null,null,1,false,false,1,"pmol per gm; picomoles per gram; picomole/gram","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per hour per milliliter ","pmol/h/mL","(PMOL/HR)/ML","amount of substance",167281575000000,[-3,-1,0,0,0,0,0],"(pmol/h)/mL","si",true,null,null,1,false,false,1,"pmol/hrs/mL; pmol per hrs per mL; picomoles per hour per milliliter; millilitre; micro enzyme units per volume; enzymatic activity; enzyme activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. ","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per liter","pmol/L","PMOL/L","amount of substance",602213670000000,[-3,0,0,0,0,0,0],"pmol/L","si",true,null,null,1,false,false,1,"picomole/liter; pmol per L; picomoles; litre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per minute","pmol/min","PMOL/MIN","amount of substance",10036894500,[0,-1,0,0,0,0,0],"pmol/min","si",true,null,null,1,false,false,1,"picomole/minute; pmol per min; picomoles per minute; micro enzyme units; enzymatic activity; enzyme activity","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. pmol/min = uU (micro enzyme unit)","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per milliliter","pmol/mL","PMOL/ML","amount of substance",602213670000000000,[-3,0,0,0,0,0,0],"pmol/mL","si",true,null,null,1,false,false,1,"picomole/milliliter; picomole/millilitre; pmol per mL; picomoles; millilitre; picomols; pmols","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picomole per micromole","pmol/umol","PMOL/UMOL","amount of substance",0.000001,[0,0,0,0,0,0,0],"pmol/μmol","si",true,null,null,1,false,false,0,"pmol/mcgmol; picomole/micromole; pmol per umol; pmol per mcgmol; picomoles ","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"picosecond","ps","PS","time",1e-12,[0,1,0,0,0,0,0],"ps",null,false,"T",null,1,false,false,0,"picoseconds; psec","LOINC","Time","Clinical","",null,null,null,null,false],[false,"picotesla","pT","PT","magnetic flux density",1e-9,[0,-1,1,0,0,-1,0],"pT","si",true,null,null,1,false,false,0,"picoteslas","LOINC","","Clinical","SI unit of magnetic field strength for magnetic field B","Wb/m2","WB/M2","1",1,false],[false,"enzyme unit per 12 hour","U/(12.h)","U/HR","catalytic activity",232335520833.33334,[0,-2,0,0,0,0,0],"U/h","chemical",true,null,null,1,false,false,1,"U/12hrs; U/ 12hrs; U per 12 hrs; 12hrs; enzyme units per 12 hours; enzyme activity; enzymatic activity per time; umol per min per 12 hours; micromoles per minute per 12 hours; umol/min/12hr","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per 2 hour","U/(2.h)","U/HR","catalytic activity",1394013125000,[0,-2,0,0,0,0,0],"U/h","chemical",true,null,null,1,false,false,1,"U/2hrs; U/ 2hrs; U per 2 hrs; 2hrs; enzyme units per 2 hours; enzyme activity; enzymatic activity per time; umol per minute per 2 hours; micromoles per minute; umol/min/2hr; umol per min per 2hr","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per 24 hour","U/(24.h)","U/HR","catalytic activity",116167760416.66667,[0,-2,0,0,0,0,0],"U/h","chemical",true,null,null,1,false,false,1,"U/24hrs; U/ 24hrs; U per 24 hrs; 24hrs; enzyme units per 24 hours; enzyme activity; enzymatic activity per time; micromoles per minute per 24 hours; umol/min/24hr; umol per min per 24hr","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per 10","U/10","U","catalytic activity",1003689450000000,[0,-1,0,0,0,0,0],"U","chemical",true,null,null,1,false,false,1,"enzyme unit/10; U per 10; enzyme units per 10; enzymatic activity; enzyme activity; micromoles per minute; umol/min/10","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per 10 billion","U/10*10","U/(10*10)","catalytic activity",1003689.45,[0,-1,0,0,0,0,0],"U/(1010)","chemical",true,null,null,1,false,false,1,"U per 10*10; enzyme units per 10*10; U per 10 billion; enzyme units; enzymatic activity; micromoles per minute per 10 billion; umol/min/10*10","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per trillion","U/10*12","U/(10*12)","catalytic activity",10036.8945,[0,-1,0,0,0,0,0],"U/(1012)","chemical",true,null,null,1,false,false,1,"enzyme unit/10*12; U per 10*12; enzyme units per 10*12; enzyme units per trillion; enzymatic activity; micromoles per minute per trillion; umol/min/10*12; umol per min per 10*12","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per million","U/10*6","U/(10*6)","catalytic activity",10036894500,[0,-1,0,0,0,0,0],"U/(106)","chemical",true,null,null,1,false,false,1,"enzyme unit/10*6; U per 10*6; enzyme units per 10*6; enzyme units; enzymatic activity per volume; micromoles per minute per million; umol/min/10*6; umol per min per 10*6","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per billion","U/10*9","U/(10*9)","catalytic activity",10036894.5,[0,-1,0,0,0,0,0],"U/(109)","chemical",true,null,null,1,false,false,1,"enzyme unit/10*9; U per 10*9; enzyme units per 10*9; enzymatic activity per volume; micromoles per minute per billion; umol/min/10*9; umol per min per 10*9","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per day","U/d","U/D","catalytic activity",116167760416.66667,[0,-2,0,0,0,0,0],"U/d","chemical",true,null,null,1,false,false,1,"U/dy; enzyme units per day; enzyme units; enzyme activity; enzymatic activity per time; micromoles per minute per day; umol/min/day; umol per min per day","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per deciliter","U/dL","U/DL","catalytic activity",100368945000000000000,[-3,-1,0,0,0,0,0],"U/dL","chemical",true,null,null,1,false,false,1,"U per dL; enzyme units per deciliter; decilitre; micromoles per minute per deciliter; umol/min/dL; umol per min per dL","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per gram","U/g","U/G","catalytic activity",10036894500000000,[0,-1,-1,0,0,0,0],"U/g","chemical",true,null,null,1,false,false,1,"U/gm; U per gm; enzyme units per gram; micromoles per minute per gram; umol/min/g; umol per min per g","LOINC","CCnt","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per hour","U/h","U/HR","catalytic activity",2788026250000,[0,-2,0,0,0,0,0],"U/h","chemical",true,null,null,1,false,false,1,"U/hr; U per hr; enzyme units per hour; micromoles per minute per hour; umol/min/hr; umol per min per hr","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per liter","U/L","U/L","catalytic activity",10036894500000000000,[-3,-1,0,0,0,0,0],"U/L","chemical",true,null,null,1,false,false,1,"enzyme unit/liter; enzyme unit/litre; U per L; enzyme units per liter; enzyme unit per litre; micromoles per minute per liter; umol/min/L; umol per min per L","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per minute","U/min","U/MIN","catalytic activity",167281575000000,[0,-2,0,0,0,0,0],"U/min","chemical",true,null,null,1,false,false,1,"enzyme unit/minute; U per min; enzyme units; umol/min/min; micromoles per minute per minute; micromoles per min per min; umol","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per milliliter","U/mL","U/ML","catalytic activity",1.00368945e+22,[-3,-1,0,0,0,0,0],"U/mL","chemical",true,null,null,1,false,false,1,"U per mL; enzyme units per milliliter; millilitre; micromoles per minute per milliliter; umol/min/mL; umol per min per mL","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"enzyme unit per second","U/s","U/S","catalytic activity",10036894500000000,[0,-2,0,0,0,0,0],"U/s","chemical",true,null,null,1,false,false,1,"U/sec; U per second; enzyme units per second; micromoles per minute per second; umol/min/sec; umol per min per sec","LOINC","CRat","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min)","umol/min","UMOL/MIN","1",1,false],[false,"micro international unit","u[IU]","U[IU]","arbitrary",0.000001,[0,0,0,0,0,0,0],"μi.U.","chemical",true,null,null,1,false,true,0,"uIU; u IU; microinternational units","LOINC","Arb","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"micro international unit per liter","u[IU]/L","U[IU]/L","arbitrary",0.001,[-3,0,0,0,0,0,0],"(μi.U.)/L","chemical",true,null,null,1,false,true,0,"uIU/L; u IU/L; uIU per L; microinternational units per liter; litre; ","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"micro international unit per milliliter","u[IU]/mL","U[IU]/ML","arbitrary",1,[-3,0,0,0,0,0,0],"(μi.U.)/mL","chemical",true,null,null,1,false,true,0,"uIU/mL; u IU/mL; uIU per mL; microinternational units per milliliter; millilitre","LOINC","ACnc","Clinical","International units (IU) are analyte and reference specimen specific arbitrary units (held at WHO)","[iU]","[IU]","1",1,false],[false,"microequivalent","ueq","UEQ","amount of substance",602213670000000000,[0,0,0,0,0,0,0],"μeq","chemical",true,null,null,1,false,false,1,"microequivalents; 10^-6 equivalents; 10-6 equivalents","LOINC","Sub","Clinical","","mol","MOL","1",1,false],[false,"microequivalent per liter","ueq/L","UEQ/L","amount of substance",602213670000000000000,[-3,0,0,0,0,0,0],"μeq/L","chemical",true,null,null,1,false,false,1,"ueq per liter; litre; microequivalents","LOINC","MCnc","Clinical","","mol","MOL","1",1,false],[false,"microequivalent per milliliter","ueq/mL","UEQ/ML","amount of substance",6.0221367000000003e+23,[-3,0,0,0,0,0,0],"μeq/mL","chemical",true,null,null,1,false,false,1,"ueq per milliliter; millilitre; microequivalents","LOINC","MCnc","Clinical","","mol","MOL","1",1,false],[false,"microgram","ug","UG","mass",0.000001,[0,0,1,0,0,0,0],"μg",null,false,"M",null,1,false,false,0,"mcg; micrograms; 10^-6 grams; 10-6 grams","LOINC","Mass","Clinical","",null,null,null,null,false],[false,"microgram per 100 gram","ug/(100.g)","UG/G","mass",1e-8,[0,0,0,0,0,0,0],"μg/g",null,false,"M",null,1,false,false,0,"ug/100gm; ug/100 gm; mcg; ug per 100g; 100 gm; mcg per 100g; micrograms per 100 grams","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"microgram per 24 hour","ug/(24.h)","UG/HR","mass",1.1574074074074074e-11,[0,-1,1,0,0,0,0],"μg/h",null,false,"M",null,1,false,false,0,"ug/24hrs; ug/24 hrs; mcg/24hrs; ug per 24hrs; mcg per 24hrs; 24 hrs; micrograms per 24 hours","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"microgram per 8 hour","ug/(8.h)","UG/HR","mass",3.472222222222222e-11,[0,-1,1,0,0,0,0],"μg/h",null,false,"M",null,1,false,false,0,"ug/8hrs; ug/8 hrs; mcg/8hrs; ug per 8hrs; mcg per 8hrs; 8 hrs; micrograms per 8 hours; shift","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"microgram per square foot (international)","ug/[sft_i]","UG/[SFT_I]","mass",0.000010763910416709721,[-2,0,1,0,0,0,0],"μg",null,false,"M",null,1,false,false,0,"ug/sft; ug/ft2; ug/ft^2; ug/sq. ft; micrograms; sq. foot; foot squared","LOINC","ArMass","Clinical","",null,null,null,null,false],[false,"microgram per day","ug/d","UG/D","mass",1.1574074074074074e-11,[0,-1,1,0,0,0,0],"μg/d",null,false,"M",null,1,false,false,0,"ug/dy; mcg/dy; ug per day; mcg; micrograms per day","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"microgram per deciliter","ug/dL","UG/DL","mass",0.009999999999999998,[-3,0,1,0,0,0,0],"μg/dL",null,false,"M",null,1,false,false,0,"ug per dL; mcg/dl; mcg per dl; micrograms per deciliter; decilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"microgram per gram","ug/g","UG/G","mass",0.000001,[0,0,0,0,0,0,0],"μg/g",null,false,"M",null,1,false,false,0,"ug per gm; mcg/gm; mcg per g; micrograms per gram","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"microgram per hour","ug/h","UG/HR","mass",2.7777777777777777e-10,[0,-1,1,0,0,0,0],"μg/h",null,false,"M",null,1,false,false,0,"ug/hr; mcg/hr; mcg per hr; ug per hr; ug per hour; micrograms","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"microgram per kilogram","ug/kg","UG/KG","mass",9.999999999999999e-10,[0,0,0,0,0,0,0],"μg/kg",null,false,"M",null,1,false,false,0,"ug per kg; mcg/kg; mcg per kg; micrograms per kilogram","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"microgram per kilogram per 8 hour","ug/kg/(8.h)","(UG/KG)/HR","mass",3.472222222222222e-14,[0,-1,0,0,0,0,0],"(μg/kg)/h",null,false,"M",null,1,false,false,0,"ug/kg/8hrs; mcg/kg/8hrs; ug/kg/8 hrs; mcg/kg/8 hrs; ug per kg per 8hrs; 8 hrs; mcg per kg per 8hrs; micrograms per kilograms per 8 hours; shift","LOINC","","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"microgram per kilogram per day","ug/kg/d","(UG/KG)/D","mass",1.1574074074074072e-14,[0,-1,0,0,0,0,0],"(μg/kg)/d",null,false,"M",null,1,false,false,0,"ug/(kg.d); ug/kg/dy; mcg/kg/day; ug per kg per dy; 24 hours; 24hrs; mcg; kilograms; microgram per kilogram and day","LOINC","","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"microgram per kilogram per hour","ug/kg/h","(UG/KG)/HR","mass",2.7777777777777774e-13,[0,-1,0,0,0,0,0],"(μg/kg)/h",null,false,"M",null,1,false,false,0,"ug/(kg.h); ug/kg/hr; mcg/kg/hr; ug per kg per hr; mcg per kg per hr; kilograms","LOINC","","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"microgram per kilogram per minute","ug/kg/min","(UG/KG)/MIN","mass",1.6666666666666664e-11,[0,-1,0,0,0,0,0],"(μg/kg)/min",null,false,"M",null,1,false,false,0,"ug/kg/min; ug/kg/min; mcg/kg/min; ug per kg per min; mcg; micrograms per kilograms per minute ","LOINC","","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"microgram per liter","ug/L","UG/L","mass",0.001,[-3,0,1,0,0,0,0],"μg/L",null,false,"M",null,1,false,false,0,"mcg/L; ug per L; mcg; micrograms per liter; litre ","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"microgram per liter per 24 hour","ug/L/(24.h)","(UG/L)/HR","mass",1.1574074074074074e-8,[-3,-1,1,0,0,0,0],"(μg/L)/h",null,false,"M",null,1,false,false,0,"ug/L/24hrs; ug/L/24 hrs; mcg/L/24hrs; ug per L per 24hrs; 24 hrs; day; dy mcg; micrograms per liters per 24 hours; litres","LOINC","","Clinical","unit used to measure mass dose rate per patient body mass",null,null,null,null,false],[false,"microgram per square meter","ug/m2","UG/M2","mass",0.000001,[-2,0,1,0,0,0,0],"μg/(m2)",null,false,"M",null,1,false,false,0,"ug/m^2; ug/sq. m; mcg/m2; mcg/m^2; mcg/sq. m; ug per m2; m^2; sq. meter; mcg; micrograms per square meter; meter squared; metre","LOINC","ArMass","Clinical","unit used to measure mass dose per patient body surface area",null,null,null,null,false],[false,"microgram per cubic meter","ug/m3","UG/M3","mass",0.000001,[-3,0,1,0,0,0,0],"μg/(m3)",null,false,"M",null,1,false,false,0,"ug/m^3; ug/cu. m; mcg/m3; mcg/m^3; mcg/cu. m; ug per m3; ug per m^3; ug per cu. m; mcg; micrograms per cubic meter; meter cubed; metre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"microgram per milligram","ug/mg","UG/MG","mass",0.001,[0,0,0,0,0,0,0],"μg/mg",null,false,"M",null,1,false,false,0,"ug per mg; mcg/mg; mcg per mg; micromilligrams per milligram","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"microgram per minute","ug/min","UG/MIN","mass",1.6666666666666667e-8,[0,-1,1,0,0,0,0],"μg/min",null,false,"M",null,1,false,false,0,"ug per min; mcg/min; mcg per min; microminutes per minute","LOINC","MRat","Clinical","",null,null,null,null,false],[false,"microgram per milliliter","ug/mL","UG/ML","mass",1,[-3,0,1,0,0,0,0],"μg/mL",null,false,"M",null,1,false,false,0,"ug per mL; mcg/mL; mcg per mL; micrograms per milliliter; millilitre","LOINC","MCnc","Clinical","",null,null,null,null,false],[false,"microgram per millimole","ug/mmol","UG/MMOL","mass",1.660540186674939e-27,[0,0,1,0,0,0,0],"μg/mmol",null,false,"M",null,1,false,false,-1,"ug per mmol; mcg/mmol; mcg per mmol; micrograms per millimole","LOINC","Ratio","Clinical","",null,null,null,null,false],[false,"microgram per nanogram","ug/ng","UG/NG","mass",999.9999999999999,[0,0,0,0,0,0,0],"μg/ng",null,false,"M",null,1,false,false,0,"ug per ng; mcg/ng; mcg per ng; micrograms per nanogram","LOINC","MCnt","Clinical","",null,null,null,null,false],[false,"microkatal","ukat","UKAT","catalytic activity",602213670000000000,[0,-1,0,0,0,0,0],"μkat","chemical",true,null,null,1,false,false,1,"microkatals; ukats","LOINC","CAct","Clinical","kat is a unit of catalytic activity with base units = mol/s. Rarely used because its units are too large to practically express catalytic activity. See enzyme unit [U] which is the standard unit for catalytic activity.","mol/s","MOL/S","1",1,false],[false,"microliter","uL","UL","volume",1e-9,[3,0,0,0,0,0,0],"μL","iso1000",true,null,null,1,false,false,0,"microliters; microlitres; mcl","LOINC","Vol","Clinical","","l",null,"1",1,false],[false,"microliter per 2 hour","uL/(2.h)","UL/HR","volume",1.388888888888889e-13,[3,-1,0,0,0,0,0],"μL/h","iso1000",true,null,null,1,false,false,0,"uL/2hrs; uL/2 hrs; mcg/2hr; mcg per 2hr; uL per 2hr; uL per 2 hrs; microliters per 2 hours; microlitres ","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"microliter per hour","uL/h","UL/HR","volume",2.777777777777778e-13,[3,-1,0,0,0,0,0],"μL/h","iso1000",true,null,null,1,false,false,0,"uL/hr; mcg/hr; mcg per hr; uL per hr; microliters per hour; microlitres","LOINC","VRat","Clinical","","l",null,"1",1,false],[false,"micrometer","um","UM","length",0.000001,[1,0,0,0,0,0,0],"μm",null,false,"L",null,1,false,false,0,"micrometers; micrometres; μm; microns","LOINC","Len","Clinical","Unit of length that is usually used in tests related to the eye",null,null,null,null,false],[false,"microns per second","um/s","UM/S","length",0.000001,[1,-1,0,0,0,0,0],"μm/s",null,false,"L",null,1,false,false,0,"um/sec; micron/second; microns/second; um per sec; micrometers per second; micrometres","LOINC","Vel","Clinical","",null,null,null,null,false],[false,"micromole","umol","UMOL","amount of substance",602213670000000000,[0,0,0,0,0,0,0],"μmol","si",true,null,null,1,false,false,1,"micromoles; umols","LOINC","Sub","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per 2 hour","umol/(2.h)","UMOL/HR","amount of substance",83640787500000,[0,-1,0,0,0,0,0],"μmol/h","si",true,null,null,1,false,false,1,"umol/2hrs; umol/2 hrs; umol per 2 hrs; 2hrs; micromoles per 2 hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per 24 hour","umol/(24.h)","UMOL/HR","amount of substance",6970065625000,[0,-1,0,0,0,0,0],"μmol/h","si",true,null,null,1,false,false,1,"umol/24hrs; umol/24 hrs; umol per 24 hrs; per 24hrs; micromoles per 24 hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per 8 hour","umol/(8.h)","UMOL/HR","amount of substance",20910196875000,[0,-1,0,0,0,0,0],"μmol/h","si",true,null,null,1,false,false,1,"umol/8hr; umol/8 hr; umol per 8 hr; umol per 8hr; umols per 8hr; umol per 8 hours; micromoles per 8 hours; shift","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per day","umol/d","UMOL/D","amount of substance",6970065625000,[0,-1,0,0,0,0,0],"μmol/d","si",true,null,null,1,false,false,1,"umol/day; umol per day; umols per day; umol per days; micromoles per days; umol/24hr; umol/24 hr; umol per 24 hr; umol per 24hr; umols per 24hr; umol per 24 hours; micromoles per 24 hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per deciliter","umol/dL","UMOL/DL","amount of substance",6.0221367e+21,[-3,0,0,0,0,0,0],"μmol/dL","si",true,null,null,1,false,false,1,"micromole/deciliter; micromole/decilitre; umol per dL; micromoles per deciliters; micromole per decilitres","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per gram","umol/g","UMOL/G","amount of substance",602213670000000000,[0,0,-1,0,0,0,0],"μmol/g","si",true,null,null,1,false,false,1,"micromole/gram; umol per g; micromoles per gram","LOINC","SCnt; Ratio","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per hour","umol/h","UMOL/HR","amount of substance",167281575000000,[0,-1,0,0,0,0,0],"μmol/h","si",true,null,null,1,false,false,1,"umol/hr; umol per hr; umol per hour; micromoles per hours","LOINC","SRat","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per kilogram","umol/kg","UMOL/KG","amount of substance",602213670000000,[0,0,-1,0,0,0,0],"μmol/kg","si",true,null,null,1,false,false,1,"umol per kg; micromoles per kilogram","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per liter","umol/L","UMOL/L","amount of substance",602213670000000000000,[-3,0,0,0,0,0,0],"μmol/L","si",true,null,null,1,false,false,1,"micromole/liter; micromole/litre; umol per liter; micromoles per liter; litre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per liter per hour","umol/L/h","(UMOL/L)/HR","amount of substance",167281575000000000,[-3,-1,0,0,0,0,0],"(μmol/L)/h","si",true,null,null,1,false,false,1,"umol/liter/hr; umol/litre/hr; umol per L per hr; umol per liter per hour; micromoles per liters per hour; litre","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min; umol/L/h is a derived unit of enzyme units","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per milligram","umol/mg","UMOL/MG","amount of substance",602213670000000000000,[0,0,-1,0,0,0,0],"μmol/mg","si",true,null,null,1,false,false,1,"micromole/milligram; umol per mg; micromoles per milligram","LOINC","SCnt","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per minute","umol/min","UMOL/MIN","amount of substance",10036894500000000,[0,-1,0,0,0,0,0],"μmol/min","si",true,null,null,1,false,false,1,"micromole/minute; umol per min; micromoles per minute; enzyme units","LOINC","CAct","Clinical","unit for the enzyme unit U = umol/min","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per minute per gram","umol/min/g","(UMOL/MIN)/G","amount of substance",10036894500000000,[0,-1,-1,0,0,0,0],"(μmol/min)/g","si",true,null,null,1,false,false,1,"umol/min/gm; umol per min per gm; micromoles per minutes per gram; U/g; enzyme units","LOINC","CCnt","Clinical","unit for the enzyme unit U = umol/min. umol/min/g = U/g","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per minute per liter","umol/min/L","(UMOL/MIN)/L","amount of substance",10036894500000000000,[-3,-1,0,0,0,0,0],"(μmol/min)/L","si",true,null,null,1,false,false,1,"umol/min/liter; umol/minute/liter; micromoles per minutes per liter; litre; enzyme units; U/L","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. umol/min/L = U/L","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per milliliter","umol/mL","UMOL/ML","amount of substance",6.0221367000000003e+23,[-3,0,0,0,0,0,0],"μmol/mL","si",true,null,null,1,false,false,1,"umol per mL; micromoles per milliliter; millilitre","LOINC","SCnc","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per milliliter per minute","umol/mL/min","(UMOL/ML)/MIN","amount of substance",1.00368945e+22,[-3,-1,0,0,0,0,0],"(μmol/mL)/min","si",true,null,null,1,false,false,1,"umol per mL per min; micromoles per milliliters per minute; millilitres","LOINC","CCnc","Clinical","unit for the enzyme unit U = umol/min. umol/mL/min = U/mL","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per millimole","umol/mmol","UMOL/MMOL","amount of substance",0.001,[0,0,0,0,0,0,0],"μmol/mmol","si",true,null,null,1,false,false,0,"umol per mmol; micromoles per millimole","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per mole","umol/mol","UMOL/MOL","amount of substance",0.000001,[0,0,0,0,0,0,0],"μmol/mol","si",true,null,null,1,false,false,0,"umol per mol; micromoles per mole","LOINC","SRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"micromole per micromole","umol/umol","UMOL/UMOL","amount of substance",1,[0,0,0,0,0,0,0],"μmol/μmol","si",true,null,null,1,false,false,0,"umol per umol; micromoles per micromole","LOINC","Srto; SFr; EntSRto","Clinical","","10*23","10*23","6.0221367",6.0221367,false],[false,"microOhm","uOhm","UOHM","electric resistance",0.001,[2,-1,1,0,0,-2,0],"μΩ","si",true,null,null,1,false,false,0,"microOhms; µΩ","LOINC","","Clinical","unit of electric resistance","V/A","V/A","1",1,false],[false,"microsecond","us","US","time",0.000001,[0,1,0,0,0,0,0],"μs",null,false,"T",null,1,false,false,0,"microseconds","LOINC","Time","Clinical","",null,null,null,null,false],[false,"micro enzyme unit per gram","uU/g","UU/G","catalytic activity",10036894500,[0,-1,-1,0,0,0,0],"μU/g","chemical",true,null,null,1,false,false,1,"uU per gm; micro enzyme units per gram; micro enzymatic activity per mass; enzyme activity","LOINC","CCnt","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 uU = 1pmol/min","umol/min","UMOL/MIN","1",1,false],[false,"micro enzyme unit per liter","uU/L","UU/L","catalytic activity",10036894500000,[-3,-1,0,0,0,0,0],"μU/L","chemical",true,null,null,1,false,false,1,"uU per L; micro enzyme units per liter; litre; enzymatic activity per volume; enzyme activity ","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 uU = 1pmol/min","umol/min","UMOL/MIN","1",1,false],[false,"micro enzyme unit per milliliter","uU/mL","UU/ML","catalytic activity",10036894500000000,[-3,-1,0,0,0,0,0],"μU/mL","chemical",true,null,null,1,false,false,1,"uU per mL; micro enzyme units per milliliter; millilitre; enzymatic activity per volume; enzyme activity","LOINC","CCnc","Clinical","1 U is the standard enzyme unit which equals 1 micromole substrate catalyzed per minute (1 umol/min); 1 uU = 1pmol/min","umol/min","UMOL/MIN","1",1,false],[false,"microvolt","uV","UV","electric potential",0.001,[2,-2,1,0,0,-1,0],"μV","si",true,null,null,1,false,false,0,"microvolts","LOINC","Elpot","Clinical","unit of electric potential (voltage)","J/C","J/C","1",1,false]]}} + +},{}],9:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Ucum = void 0; + +/* + * This defines the namespace for the UCUM classes and provides + * a place for the definition of global variables and constants. + * + * The javascript for this UCUM implementation uses syntax as + * defined by the ECMAScript 6 standard + */ +var Ucum = { + /** + * Flag indicating whether or not we're using case sensitive labels + * I don't think we need this. I think we're just going with + * case sensitive, per Clem. Gunther's code has this flag, but I + * am removing it, at least for now. lm, 6/2016 + */ + //caseSensitive_: true , + + /** + * The number of elements in a Dimension array. Currently this + * is set as a configuration variable, but when we get to the point + * of loading the unit definitions from a file, this value will be + * set from that. + */ + dimLen_: 7, + + /** + * The characters used as valid operators in a UCUM unit expression, + * where '.' is for multiplication and '/' is for division. + */ + validOps_: ['.', '/'], + + /** + * The string used to separate a unit code and unit name when they + * are displayed together + */ + codeSep_: ': ', + // Message text variations for validation methods and conversion methods + valMsgStart_: 'Did you mean ', + valMsgEnd_: '?', + cnvMsgStart_: 'We assumed you meant ', + cnvMsgEnd_: '.', + + /** + * Default opening string used to emphasize portions of error messages. + * Used when NOT displaying messages on a web site, i.e., for output + * from the library methods or to a file. + */ + openEmph_: ' ->', + + /** + * Default closing string used to emphasize portions of error messages. + * Used when NOT displaying messages on a web site, i.e., for output + * from the library methods or to a file. + */ + closeEmph_: '<- ', + + /** + * Opening HTML used to emphasize portions of error messages. Used when + * displaying messages on a web site; should be blank when output is + * to a file. + */ + openEmphHTML_: ' ', + + /** + * Closing HTML used to emphasize portions of error messages. Used when + * displaying messages on a web site; should be blank when output is + * to a file. + */ + closeEmphHTML_: ' ', + + /** + * Message that is displayed when annotations are included in a unit + * string, to let the user know how they are interpreted. + */ + bracesMsg_: 'FYI - annotations (text in curly braces {}) are ignored, ' + 'except that an annotation without a leading symbol implies ' + 'the default unit 1 (the unity).', + + /** + * Message that is displayed or returned when a conversion is requested + * for two units where (only) a mass<->moles conversion is appropriate + * but no molecular weight was specified. + */ + needMoleWeightMsg_: 'Did you wish to convert between mass and moles? The ' + 'molecular weight of the substance represented by the ' + 'units is required to perform the conversion.', + + /** + * Hash that matches unit column names to names used in the csv file + * that is submitted to the data updater. + */ + csvCols_: { + 'case-sensitive code': 'csCode_', + 'LOINC property': 'loincProperty_', + 'name (display)': 'name_', + 'synonyms': 'synonyms_', + 'source': 'source_', + 'category': 'category_', + 'Guidance': 'guidance_' + }, + + /** + * Name of the column in the csv file that serves as the key + */ + inputKey_: 'case-sensitive code', + + /** + * Special codes that contain operators within brackets. The operator + * within these codes causes them to parse incorrectly if they are preceded + * by a prefix, because the parsing algorithm splits them up on the operator. + * So we use this object to identify them and substitute placeholders to + * avoid that. + */ + specUnits_: { + 'B[10.nV]': 'specialUnitOne', + '[m/s2/Hz^(1/2)]': 'specialUnitTwo' + } +}; +exports.Ucum = Ucum; + + +},{}],10:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Dimension = void 0; + +/** + * This class implements an object containing the vector of exponents for + * a unit and its operations for addition, subtraction, and multiplication + * with a scalar. + * + * This object should exist for each unit that can be expressed as a + * vector of numbers. This excludes arbitrary units, e.g., (10*23), and + * units that are not numbers but are an expression based solely on numbers, + * e.g., mol (mole) which is based on 10*23. + * + * @author Lee Mericle, based on java version by Gunther Schadow + */ +var UC = require('./config.js'); + +var isInteger = require("is-integer"); + +class Dimension { + /** + * Constructor. + * + * @param dimSetting an optional parameter that may be: + * null, which means that the dimVec_ attribute for this object will be null; or + * an array, which must be the length defined by Ucum.dimLen_, and + * whose contents will be copied to this new object's vector; or + * an integer, which must be between 0 and 1 less than the vector length + * defined by Ucum.dimLen_. This new object's vector will be + * initialized to zero for all elements except the one whose index + * matches the number passed in. That element will be set to one. + * @throws an error if the dimSetting parameter does not meet the types + * listed above. + * An error will also be thrown if Ucum.dimLen_ has not been set yet, + * i.e., is still zero. Currently that won't happen, because the + * value is set in the config.js file. But further down the road + * the setting will come from a definitions input file, so we check + * here anyway. + * + */ + constructor(dimSetting) { + if (UC.Ucum.dimLen_ === 0) { + throw new Error('Dimension.setDimensionLen must be called before ' + 'Dimension constructor'); + } + + if (dimSetting === undefined || dimSetting === null) { + this.assignZero(); + } else if (dimSetting instanceof Array) { + if (dimSetting.length !== UC.Ucum.dimLen_) { + throw new Error('Parameter error, incorrect length of vector passed to ' + `Dimension constructor, vector = ${JSON.stringify(dimSetting)}`); + } + + this.dimVec_ = []; + + for (let d = 0; d < UC.Ucum.dimLen_; d++) this.dimVec_.push(dimSetting[d]); + } // In es6 this should be Number.isInteger(dimSetting). But Babel + // doesn't transpile that correctly, so we need to use the isInteger + // module. :0 + else if (isInteger(dimSetting)) { + if (dimSetting < 0 || dimSetting >= UC.Ucum.dimLen_) { + throw new Error('Parameter error, invalid element number specified for ' + 'Dimension constructor'); + } + + this.assignZero(); + this.dimVec_[dimSetting] = 1; + } + } // end constructor + + /** + * Sets the element at the specified position to a specified value. The + * default value is 1. If the dimension vector is null when this is called + * a zero-filled vector is created and then the indicated position is set. + * + * @param indexPos the index of the element to be set + * @param value the value to assign to the specified element; optional, + * default value is 1 + * @throws an exception if the specified position is invalid, i.e., not a + * number or is less than 0 or greater than Ucum.dimLen_ + **/ + + + setElementAt(indexPos, value) { + if (!isInteger(indexPos) || indexPos < 0 || indexPos >= UC.Ucum.dimLen_) { + throw new Error(`Dimension.setElementAt called with an invalid index ` + `position (${indexPos})`); + } + + if (!this.dimVec_) { + this.assignZero(); + } + + if (value === undefined || value === null) value = 1; + this.dimVec_[indexPos] = value; + } + /** + * Gets the value of the element at the specified position + * + * @param indexPos the index of the element whose value is to be returned + * @return the value of the element at indexPos, or null if the dimension + * vector is null + * @throws an exception if the specified position is invalid, i.e., not a + * number or is less than 0 or greater than Ucum.dimLen_ + **/ + + + getElementAt(indexPos) { + if (!isInteger(indexPos) || indexPos < 0 || indexPos >= UC.Ucum.dimLen_) { + throw new Error(`Dimension.getElementAt called with an invalid index ` + `position (${indexPos})`); + } + + let ret = null; + if (this.dimVec_) ret = this.dimVec_[indexPos]; + return ret; + } + /** + * This returns the value of the property named by the parameter + * passed in. Although we currently only have one property, dimVec_, + * that this will get, it's possible that we'll have additional + * properties. If we don't this could just be replaced by a + * getVector function. + * + * @param propertyName name of the property to be returned, with + * or without the trailing underscore. + * @return the requested property, if found for this Dimension + * @throws an error if the property is not found for this Dimension + */ + + + getProperty(propertyName) { + let uProp = propertyName.charAt(propertyName.length - 1) === '_' ? propertyName : propertyName + '_'; + return this[uProp]; + } // end getProperty + + /** + * Return a string that represents the dimension vector. Returns null if + * the dimension vector is null. + * + * @return the string that represents the dimension vector. The + * values are enclosed in square brackets, each separated + * by a comma and a space + **/ + + + toString() { + let ret = null; + if (this.dimVec_) ret = '[' + this.dimVec_.join(', ') + ']'; + return ret; + } + /** + * Adds the vector of the dimension object passed in to this + * dimension object's vector. This object's vector is changed. + * If either dimension vector is null, no changes are made to this object. + * + * + * @param dim2 the dimension whose vector is to be added to this one + * @return this object + * @throws an exception if dim2 is not a Dimension object + **/ + + + add(dim2) { + if (!dim2 instanceof Dimension) { + throw new Error(`Dimension.add called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`); + } + + if (this.dimVec_ && dim2.dimVec_) { + for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] += dim2.dimVec_[i]; + } + + return this; + } + /** + * Subtracts the vector of the dimension object passed in from this + * dimension object's vector. This object's vector is changed. + * If either dimension vector is null, no changes are made to this object. + * + * @param dim2 the dimension whose vector is to be subtracted from this one + * @return this object + * @throws an exception if dim2 is not a Dimension object + **/ + + + sub(dim2) { + if (!dim2 instanceof Dimension) { + throw new Error(`Dimension.sub called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`); + } + + if (this.dimVec_ && dim2.dimVec_) { + for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] -= dim2.dimVec_[i]; + } + + return this; + } + /** + * Inverts this dimension object's vector (by multiplying each element + * by negative 1). This object's vector is changed - unless it is null, + * in which case it stays that way. + * + * @return this object + **/ + + + minus() { + if (this.dimVec_) { + for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] = -this.dimVec_[i]; + } + + return this; + } + /** + * Multiplies this dimension object's vector with a scalar. This is used + * when a unit is raised to a power. This object's vector is changed unless + * the vector is null, in which case it stays that way. + * + * @param s the scalar to use + * @return this object + * @throws an exception if s is not a number + */ + + + mul(s) { + if (!isInteger(s)) { + throw new Error(`Dimension.sub called with an invalid parameter - ` + `${typeof dim2} instead of a number`); + } + + if (this.dimVec_) { + for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] *= s; + } + + return this; + } + /** + * Tests for equality of this dimension object's vector and that of + * the dimension object passed in. If the dimension vector for one of + * the objects is null, the dimension vector for the other object must + * also be null for the two to be equal. (I know - duh. still) + * + * @param dim2 the dimension object whose vector is to be compared to this one + * @return true if the two vectors are equal; false otherwise. + * @throws an exception if dim2 is not a Dimension object + */ + + + equals(dim2) { + if (!dim2 instanceof Dimension) { + throw new Error(`Dimension.equals called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`); + } + + let isEqual = true; + let dimVec2 = dim2.dimVec_; + + if (this.dimVec_ && dimVec2) { + for (let i = 0; isEqual && i < UC.Ucum.dimLen_; i++) isEqual = this.dimVec_[i] === dimVec2[i]; + } else { + isEqual = this.dimVec_ === null && dimVec2 === null; + } + + return isEqual; + } + /** + * Assigns the contents of the vector belonging to the dimension object + * passed in to this dimension's vector. If this dimension vector is null + * and the other is not, this one will get the contents of the other. If + * this dimension vector is not null but the one passed in is null, this + * one will be set to null. + * + * @param dim2 the dimension object with the vector whose contents are + * to be assigned to this dimension's vector + * @return this object (not sure why) + * @throws an exception if dim2 is not a Dimension object + */ + + + assignDim(dim2) { + if (!dim2 instanceof Dimension) { + throw new Error(`Dimension.assignDim called with an invalid parameter - ` + `${typeof dim2} instead of a Dimension object`); + } + + if (dim2.dimVec_ === null) this.dimVec_ = null;else { + if (this.dimVec_ === null) { + this.dimVec_ = []; + } + + for (let i = 0; i < UC.Ucum.dimLen_; i++) this.dimVec_[i] = dim2.dimVec_[i]; + } + return this; + } + /** + * Sets all elements of this dimension object's vector to zero. + * If this object's vector is null, it is created as a zero-filled vector. + * + * @return this object (not sure why) + */ + + + assignZero() { + if (this.dimVec_ === null || this.dimVec_ === undefined) this.dimVec_ = []; + + for (let i = 0; i < UC.Ucum.dimLen_; i++) { + this.dimVec_.push(0); + } + + return this; + } + /** + * Tests for a dimension vector set to all zeroes. + * + * @return true if exponents (elements) of this dimension's vector are all + * zero; false otherwise (including if the current vector is null). + * + */ + + + isZero() { + let allZero = this.dimVec_ !== null; + + if (this.dimVec_) { + for (let i = 0; allZero && i < UC.Ucum.dimLen_; i++) allZero = this.dimVec_[i] === 0; + } + + return allZero; + } + /** + * Tests for a Dimension object with no dimension vector (dimVec_ is null). + * + * @return true the dimension vector is null; false if it is not + * + */ + + + isNull() { + return this.dimVec_ === null; + } + /** + * Creates and returns a clone of this Dimension object + * + * @return the clone + */ + + + clone() { + let that = new Dimension(); + that.assignDim(this); + return that; + } + +} // end Dimension class + + +exports.Dimension = Dimension; + + +},{"./config.js":9,"is-integer":213}],11:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.packArray = packArray; +exports.unpackArray = unpackArray; + +/** + * This file provides functions to reduce the size of an array of objects of the same structure in JSON. + */ +const pushFn = Array.prototype.push; + +function isObject(value) { + return Object.prototype.toString.call(value) === '[object Object]'; +} +/** + * Makes human readable config used to pack/unpack array of objects of the same structure to store with packed data. + * @param {Object} refObj - reference item of array of objects of the same structure + * @returns {Array} + */ + + +function createConfig(refObj) { + return Object.keys(refObj).reduce((config, key) => { + if (isObject(refObj[key])) { + pushFn.apply(config, createConfig(refObj[key]).map(keyTail => [key, ...[].concat(keyTail)])); + } else { + config.push(key); + } + + return config; + }, []); +} +/** + * Prepares config created with createConfig function to use in packItem/unpackItem functions. + * @param {Array} config + * @returns {Array} + */ + + +function prepareConfig(config) { + return config.map(key => Array.isArray(key) ? key : [key]); +} +/** + * Converts an object to an array of values in the order of keys from configuration array. + * @param {Array} config - configuration array + * @param {Object} item - input object + * @returns {Array} + */ + + +function packItem(config, item) { + if (config.join() !== prepareConfig(createConfig(item)).join()) { + throw new Error('Object of unusual structure'); + } + + return config.map(keyArr => { + let place = item; + keyArr.forEach(key => { + place = place[key]; + + if (place === undefined) { + throw new Error('Object of unusual structure'); + } + }); + return place; + }); +} +/** + * Performs the reverse of packItem function. + * @param {Array} config - configuration array + * @param {Array} item - input object + * @returns {Object} + */ + + +function unpackItem(config, item) { + let result = {}; + config.forEach((keyArr, i) => { + let place = result; + + for (let i = 0; i < keyArr.length - 1; i++) { + place = place[keyArr[i]] = place[keyArr[i]] || {}; + } + + place[keyArr[keyArr.length - 1]] = item[i]; + }); + return result; +} +/** + * Reduces size of an array of objects of the same structure before serialize it to JSON + * @param {Array} arr + * @returns {Object} + */ + + +function packArray(arr) { + if (arr && arr.length) { + const config = createConfig(arr[0]), + _config = prepareConfig(config); + + if (config.length) { + return { + config: config, + data: arr.map(packItem.bind(null, _config)) + }; + } + } + + return { + config: [], + data: arr + }; +} +/** + * Restores an array of objects of the same structure after deserializing this object from JSON + * @param {Object} obj + * @returns {Array} + */ + + +function unpackArray(obj) { + const config = obj && obj.config; + + if (config) { + if (config.length && obj.data) { + const _config = prepareConfig(config); + + return obj.data.map(unpackItem.bind(null, _config)); + } else { + return obj.data; + } + } + + return obj; +} + + +},{}],12:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Prefix = void 0; + +/** + * Prefix objects are defined in this file. + */ + +/** + * This class implements the prefix object. Prefixes are used as multipliers + * for units, e.g., km for kilometers + * + * @author Lee Mericle, based on java version by Gunther Schadow + * + */ +var Ucum = require('./config.js'); + +class Prefix { + /** + * Creates a single prefix object. + * + * @param attrs a hash of the values to use in creating the prefix object. + * They should be: + * code_ - which is the case-sensitive code used for the prefix, + * e.g., k for kilo + * ciCode_ - which is the case-insensitive code used for the prefix, + * e.g., K for kilo + * name_ - which is the name of the prefix, e.g., kilo + * printSymbol_ - which is the print symbol for the prefix, e.g., k for kilo + * value_ - which is teh value to use in multiplying the magnitude of + * a unit, e.g., for a prefix of c the value will be .01. + * exp_ - which is the exponent used to get the value. For decimal based + * prefixes the base is 10 and the exp_ is applied to 10, e.g., for a + * prefix of c, the exponent will be -2. For prefixes that are not + * decimal based, this will be null (but must not be undefined). + * + * @throws an error if the not all required parameters are provided + */ + constructor(attrs) { + if (attrs['code_'] === undefined || attrs['code_'] === null || attrs['name_'] === undefined || attrs['name_'] === null || attrs['value_'] === undefined || attrs['value_'] === null || attrs['exp_'] === undefined) { + throw new Error('Prefix constructor called missing one or more parameters. ' + 'Prefix codes (cs or ci), name, value and exponent must all be specified ' + 'and all but the exponent must not be null.'); + } + /** + * The prefix code, e.g., k for kilo. This should be the case-sensitive + * code. Since there's no way to check to see if it's the case-sensitive + * one as opposed to the case-insensitive one (because although + * case-insensitive codes all seem to be uppercase, some case-sensitive + * codes are also all uppercase), we'll just have to believe that the + * right one was passed in. + */ + + + this.code_ = attrs['code_']; + /** + * The case-insensitive code, e.g., K for kilo + */ + + this.ciCode_ = attrs['ciCode_']; + /** + * The prefix name, e.g., kilo + */ + + this.name_ = attrs['name_']; + /** + * The printSymbol for the prefix, e.g., k for kilo + */ + + this.printSymbol_ = attrs['printSymbol_']; + /** + * The value to use in multiplying the magnitude of a unit + */ + + if (typeof attrs['value_'] === 'string') this.value_ = parseFloat(attrs['value_']);else this.value_ = attrs['value_']; + /** + * The exponent used to create the value from 10. For prefixes that are + * not based on 10, this will be null. + */ + + this.exp_ = attrs['exp_']; + } // end constructor + + /** + * Returns the value for the current prefix object + * @return the value for the prefix object with the specified code + * */ + + + getValue() { + return this.value_; + } + /** + * Returns the prefix code for the current prefix object + * @return the code for the current prefix object + */ + + + getCode() { + return this.code_; + } + /** + * Returns the case-insensitive code for the current prefix object + * @return the case_insensitive code for the current prefix object + */ + + + getCiCode() { + return this.ciCode_; + } + /** + * Returns the prefix name for the current prefix object + * @return the name for the current prefix object + */ + + + getName() { + return this.name_; + } + /** + * Returns the print symbol for the current prefix object + * @return the print symbol for the current prefix object + */ + + + getPrintSymbol() { + return this.printSymbol_; + } + /** + * Returns the exponent for the current prefix object + * @return the exponent for the current prefix object + */ + + + getExp() { + return this.exp_; + } + /** + * Provides way to tell if one prefix equals another. The second prefix + * must match all attribute values. + * + * @param prefix2 prefix object to check for a match + * @return true for a match; false if one or more attributes don't match + */ + + + equals(prefix2) { + return this.code_ === prefix2.code_ && this.ciCode_ === prefix2.ciCode_ && this.name_ === prefix2.name_ && this.printSymbol_ === prefix2.printSymbol_ && this.value_ === prefix2.value_ && this.exp_ === prefix2.exp_; + } + +} // end Prefix class + + +exports.Prefix = Prefix; + + +},{"./config.js":9}],13:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.PrefixTables = exports.PrefixTablesFactory = void 0; + +/** + * The tables of defined prefixes is defined in this file. + */ + +/** + * This class implements the table of multiplier prefixes. + * + * @author Lee Mericle, based on java version by Gunther Schadow + * + */ +class PrefixTablesFactory { + /** + * Constructor. This creates the empty PrefixTable hashes once. + * There is one hash whose key is the prefix code and one whose + * key is the prefix value. + * + * Implementation of this as a singleton is based on the UnitTables + * implementation. See that class for details. + */ + constructor() { + this.byCode_ = {}; + this.byValue_ = {}; + } + /** + * Provides the number of prefix objects in each table + * @returns count of the number of prefix objects in each table + */ + + + prefixCount() { + return Object.keys(this.byCode_).length; + } + /** + * This is used to get all prefix objects by value. Currently it is used + * to create a csv file with all prefixes and units. + * @returns csv string containing all prefix objects, ordered by value. + */ + + + allPrefixesByValue() { + let prefixBuff = ''; + let pList = Object.keys(this.byValue_); //pList.sort() ; + + let pLen = pList.length; + + for (let p = 0; p < pLen; p++) { + let pfx = this.getPrefixByValue(pList[p]); + prefixBuff += pfx.code_ + ',' + pfx.name_ + ',,' + pfx.value_ + '\r\n'; + } + + return prefixBuff; + } + /** + * This is used to get all prefix objects. Currently it is used + * to get the objects to write to the json ucum definitions file + * that is used to provide prefix and unit definition objects for + * conversions and validations. + * + * @returns an array containing all prefix objects, ordered by code. + */ + + + allPrefixesByCode() { + let prefixList = []; + let pList = Object.keys(this.byCode_); + pList.sort(); + let pLen = pList.length; + + for (let p = 0; p < pLen; p++) { + prefixList.push(this.getPrefixByCode(pList[p])); + } + + return prefixList; + } + /** + * Adds a prefix object to the tables + * + * @param prefixObj the object to be added to the tables + */ + + + add(prefixObj) { + this.byCode_[prefixObj.getCode()] = prefixObj; + this.byValue_[prefixObj.getValue()] = prefixObj; + } + /** + * Tests whether a prefix object is found for a specified code. This + * is used to determine whether or not a prefix object has been created + * for the code. + * + * @param code the code to be used to find the prefix object + * @return boolean indicating whether or not a prefix object was found + * for the specified code + */ + + + isDefined(code) { + return this.byCode_[code] !== null && this.byCode_[code] !== undefined; + } + /** + * Obtains a prefix object for a specified code. + * + * @param code the code to be used to find the prefix object + * @return the prefix object found, or null if nothing was found + */ + + + getPrefixByCode(code) { + return this.byCode_[code]; + } + /** + * Obtains a prefix object for a specified value. + * + * @param value the value to be used to find the prefix object + * @return the prefix object found, or null if nothing was found + */ + + + getPrefixByValue(value) { + return this.byValue_[value]; + } + +} // end PrefixTablesFactory class +// Create a singleton instance and (to preserve the existing API) an object that +// provides that instance via getInstance(). + + +exports.PrefixTablesFactory = PrefixTablesFactory; +var prefixTablesInstance = new PrefixTablesFactory(); +const PrefixTables = { + getInstance: function () { + return prefixTablesInstance; + } +}; +exports.PrefixTables = PrefixTables; + + +},{}],14:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +/* + * This class manages the special functions used by some units. + * + * @author Lee Mericle, based on java version by Gunther Schadow + * + */ +class UcumFunctions { + /** + * Constructor + * + * Creates the singleton object that contains the list of functions used + * to convert special units. + */ + constructor() { + // Create the hash containing the function pairs + this.funcs = {}; // Celsius - convert to Celsius from kelvin and from Celsius to kelvin + // where kelvin is the base unit for temperature + + this.funcs['cel'] = { + cnvTo: function (x) { + return x - 273.15; + }, + cnvFrom: function (x) { + return x + 273.15; + } + }; // Fahrenheit - convert to Fahrenheit from kelvin and from Fahrenheit to + // kelvin - which is the base unit for temperature + + this.funcs['degf'] = { + cnvTo: function (x) { + return x - 459.67; + }, + cnvFrom: function (x) { + return x + 459.67; + } + }; // Reaumur - convert between Reaumur and Kelvin. Because of the way the + // calling code in the Units class is set up (in the convertFrom method), + // what is given here as the convertTo function is actually the convert + // from method and vice versa. + //this.funcs['degre'] = {cnvTo : function(x){return x + 273.15;}, + // cnvFrom : function(x){return x - 273.15;}}; + + this.funcs['degre'] = { + cnvTo: function (x) { + return x - 273.15; + }, + cnvFrom: function (x) { + return x + 273.15; + } + }; // pH - convert to pH from moles per liter and from moles per liter to pH + // where a mole is an amount of a substance (a count of particles) + + this.funcs['ph'] = { + cnvTo: function (x) { + return -Math.log(x) / Math.LN10; + }, + cnvFrom: function (x) { + return Math.pow(10, -x); + } + }; // ln - natural logarithm (base e 2.71828) - apply (cnvTo) and invert (cnvFrom) + // and 2ln - two times the natural logarithm + + this.funcs['ln'] = { + cnvTo: function (x) { + return Math.log(x); + }, + cnvFrom: function (x) { + return Math.exp(x); + } + }; + this.funcs['2ln'] = { + cnvTo: function (x) { + return 2 * Math.log(x); + }, + cnvFrom: function (x) { + return Math.exp(x / 2); + } + }; // lg - the decadic logarithm (base 10) + + this.funcs['lg'] = { + cnvTo: function (x) { + return Math.log(x) / Math.LN10; + }, + cnvFrom: function (x) { + return Math.pow(10, x); + } + }; + this.funcs['10lg'] = { + cnvTo: function (x) { + return 10 * Math.log(x) / Math.LN10; + }, + cnvFrom: function (x) { + return Math.pow(10, x / 10); + } + }; + this.funcs['20lg'] = { + cnvTo: function (x) { + return 20 * Math.log(x) / Math.LN10; + }, + cnvFrom: function (x) { + return Math.pow(10, x / 20); + } + }; // The plain text ucum units file uses '2lg' + + this.funcs['2lg'] = { + cnvTo: function (x) { + return 2 * Math.log(x) / Math.LN10; + }, + cnvFrom: function (x) { + return Math.pow(10, x / 2); + } + }; // The xml essence ucum file uses lgTimes2 + + this.funcs['lgtimes2'] = this.funcs['2lg']; // ld - dual logarithm (base 2) + + this.funcs['ld'] = { + cnvTo: function (x) { + return Math.log(x) / Math.LN2; + }, + cnvFrom: function (x) { + return Math.pow(2, x); + } + }; // tan - tangent + + this.funcs['100tan'] = { + cnvTo: function (x) { + return Math.tan(x) * 100; + }, + cnvFrom: function (x) { + return Math.atan(x / 100); + } + }; // the xml essence ucum file uses both 100tan and tanTimes100 + + this.funcs['tanTimes100'] = this.funcs['100tan']; // sqrt - square root + + this.funcs['sqrt'] = { + cnvTo: function (x) { + return Math.sqrt(x); + }, + cnvFrom: function (x) { + return x * x; + } + }; // inv - inverse + + this.funcs['inv'] = { + cnvTo: function (x) { + return 1.0 / x; + }, + cnvFrom: function (x) { + return 1.0 / x; + } + }; // homeopathic potency functions + + this.funcs['hpX'] = { + cnvTo: function (x) { + return -this.funcs['lg'](x); + }, + cnvFrom: function (x) { + return Math.pow(10, -x); + } + }; + this.funcs['hpC'] = { + cnvTo: function (x) { + return -this.func['ln'](x) / this.funcs['ln'](100); + }, + cnvFrom: function (x) { + return Math.pow(100, -x); + } + }; + this.funcs['hpM'] = { + cnvTo: function (x) { + return -this.funcs['ln'](x) / this.funcs['ln'](1000); + }, + cnvFrom: function (x) { + return Math.pow(1000, -x); + } + }; + this.funcs['hpQ'] = { + cnvTo: function (x) { + return -this.funcs['ln'](x) / this.funcs['ln'](50000); + }, + cnvFrom: function (x) { + return Math.pow(50000, -x); + } + }; + } // end of constructor + + /** + * Returns the function with the name specified + * + * @param fname name of the function to be returned + * @return the function with the specified name + * @throws an error message if the function is not found + */ + + + forName(fname) { + fname = fname.toLowerCase(); + let f = this.funcs[fname]; + if (f === null) throw new Error(`Requested function ${fname} is not defined`); + return f; + } + /** + * Returns a flag indicating whether or not the function has been + * defined. + * + * @param fname name of the function in question + * @return true if it has been defined; false if not + */ + + + isDefined(fname) { + fname = fname.toLowerCase(); + return this.funcs[fname] !== null; + } + +} // end of UcumFunctions class + + +var _default = new UcumFunctions(); // one singleton instance + + +exports.default = _default; + + +},{}],15:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isNumericString = isNumericString; +exports.isIntegerUnit = isIntegerUnit; +exports.getSynonyms = getSynonyms; + +/** + * Internal utilities used by multiple UCUM classes. For example, + * isNumericString is used by both the UnitString and UcumLhcUtils + * classes. If it's in the UnitString class the UcumLhcUtils class + * needs to require the UnitString class. But the checkSynonyms + * class is used by the UnitString class - but was in the UcumLhcUtils + * class. Requiring the UcumLhcUtils class from the UnitString class + * made everything break (cyclical requires). + * + * So now they're here. + */ + +/** + * This module implements internal ucum utilities. + * + * @author Lee Mericle, based on java version by Gunther Schadow + * + */ +var UnitTables = require('./unitTables.js').UnitTables; +/** + * This function tests a string to see if it contains only numbers (digits, + * a period, leading - or +). This code was taken from a stackoverflow + * solution: + * https://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number/42356340#42356340 + * + * @params theString + * @returns true if the string contains only numbers; false otherwise + */ + + +function isNumericString(theString) { + let num = "" + theString; //coerce num to be a string + + return !isNaN(num) && !isNaN(parseFloat(num)); +} // end isNumericString + +/** + * Checks whether a string qualifies as an integer unit. Section 2.2.8 ("integer + * numbers", says, "A positive integer number may appear in place of a simple + * unit symbol. Only a pure string of decimal digits (‘0’–‘9’) is + * interpreted as a number." + * Note: This leaves open the question of whether "0" is a valid unit, since + * it is positive, but you can't measure anything in units of zero. + * @param str the string to check + */ + + +function isIntegerUnit(str) { + return /^\d+$/.test(str); +} +/** + * This method accepts a term and looks for units that include it as + * a synonym - or that include the term in its name. + * + * @param theSyn the term to search for. This is assumed to be + * a string and not undefined. The calling method should do any + * necessary checking before calling this. + * @returns a hash with up to three elements: + * 'status' contains the status of the request, which can be 'error', + * 'failed' or succeeded'; + * 'msg' which contains a message for an error or if no units were found; and + * 'units' which is an array that contains one array for each unit found: + * the unit's csCode_, the unit's name_, and the unit's guidance_ + * + */ + + +function getSynonyms(theSyn) { + let retObj = {}; + let utab = UnitTables.getInstance(); + let resp = {}; + resp = utab.getUnitBySynonym(theSyn); // If we didn't get any units, transfer the status and message + + if (!resp['units']) { + retObj['status'] = resp['status']; + retObj['msg'] = resp['msg']; + } else { + retObj['status'] = 'succeeded'; + let aLen = resp['units'].length; + retObj['units'] = []; + + for (let a = 0; a < aLen; a++) { + let theUnit = resp['units'][a]; + retObj['units'][a] = { + 'code': theUnit.csCode_, + 'name': theUnit.name_, + 'guidance': theUnit.guidance_ + }; + } // end do for all units returned + + } // end if we got a units list + + + return retObj; +} // end getSynonyms + + +},{"./unitTables.js":21}],16:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.ucumJsonDefs = exports.UcumJsonDefs = void 0; + +/** + * This class handles opening, reading and loading the JSON file of ucum + * definitions (prefixes, base units, and unit atoms). + * + * @author Lee Mericle + * + */ +var Pfx = require("./prefix.js"); + +var PfxT = require("./prefixTables.js"); + +var Un = require("./unit.js"); + +var Utab = require('./unitTables.js'); + +var unpackArray = require('./jsonArrayPack.js').unpackArray; + +class UcumJsonDefs { + /** + * This method loads the JSON prefix and unit objects into the prefix and + * unit tables. + * + * @returns nothing + */ + loadJsonDefs() { + // requiring the file will take care of opening it for use + const jsonDefs = require('../data/ucumDefs.min.json'); + + jsonDefs.prefixes = unpackArray(jsonDefs.prefixes); + jsonDefs.units = unpackArray(jsonDefs.units); + + if (Utab.UnitTables.getInstance().unitsCount() === 0) { + let pTab = PfxT.PrefixTables.getInstance(); + let prefixes = jsonDefs["prefixes"]; + let plen = prefixes.length; + + for (let p = 0; p < plen; p++) { + let newPref = new Pfx.Prefix(prefixes[p]); + pTab.add(newPref); + } + + let uTab = Utab.UnitTables.getInstance(); + let units = jsonDefs["units"]; + let ulen = units.length; + + for (let u = 0; u < ulen; u++) { + let newUnit = new Un.Unit(units[u]); + uTab.addUnit(newUnit); + } + } // end if the data has not already been loaded + + } // end loadJsonDefs + + +} // end UcumJsonDefs class + + +exports.UcumJsonDefs = UcumJsonDefs; +var ucumJsonDefs = new UcumJsonDefs(); +exports.ucumJsonDefs = ucumJsonDefs; + + +},{"../data/ucumDefs.min.json":8,"./jsonArrayPack.js":11,"./prefix.js":12,"./prefixTables.js":13,"./unit.js":19,"./unitTables.js":21}],17:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.UcumLhcUtils = void 0; + +var _ucumJsonDefs = require("./ucumJsonDefs.js"); + +var intUtils_ = _interopRequireWildcard(require("./ucumInternalUtils.js")); + +function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } + +/** + * This class provides a single point of access to the LHC UCUM utilities + * + * @author Lee Mericle + * + */ +var Ucum = require('./config.js').Ucum; + +var UnitTables = require('./unitTables.js').UnitTables; + +var UnitString = require('./unitString.js').UnitString; + +/** + * UCUM external utilities class + */ +class UcumLhcUtils { + /** + * Constructor. This loads the json prefix and unit definitions if + * they haven't been loaded already and creates itself as a singleton object. + * + */ + constructor() { + if (UnitTables.getInstance().unitsCount() === 0) { + // Load the prefix and unit objects + _ucumJsonDefs.ucumJsonDefs.loadJsonDefs(); + } // Get the UnitString parser that will be used with this instance + // of the LHC Utilities + + + this.uStrParser_ = UnitString.getInstance(); + } // end constructor + + /** + * This method calls the useHTMLInMessages method on the UnitString + * object. It should be called by web applications that use + * these utilities. + * + * @param use flag indicating whether or not to use the braces message; + * defaults to true + */ + + + useHTMLInMessages(use) { + if (use === undefined) use = true; + this.uStrParser_.useHTMLInMessages(use); + } + /** + * This method calls the useBraceMsgForEachString method on the UnitString + * object. It should be called by web applications where unit + * strings are validated individually (as opposed to validating a whole + * file of unit strings). + * + * @param use flag indicating whether or not to use the braces message; + * defaults to true + */ + + + useBraceMsgForEachString(use) { + if (use === undefined) use = true; + this.uStrParser_.useBraceMsgForEachString(use); + } + /** + * This method validates a unit string. It first checks to see if the + * string passed in is a unit code that is found in the unit codes table. + * If it is not found it parses the string to see if it resolves to a + * valid unit string. + * + * If a valid unit cannot be found, the string is tested for some common + * errors, such as missing brackets or a missing multiplication operator. + * If found, the error is reported in the messages array that is returned. + * + * If a valid unit cannot be found and an error cannot be discerned, this + * may return, if requested, a list of suggested units in the messages + * array that is returned. Suggestions are based on matching the expression + * with unit names and synonyms. + * + * @param uStr the string to be validated + * @param suggest a boolean to indicate whether or not suggestions are + * requested for a string that cannot be resolved to a valid unit; + * true indicates suggestions are wanted; false indicates they are not, + * and is the default if the parameter is not specified; + * @param valConv a string indicating if this validation request was initiated + * by a validation task ('validate') or a conversion task ('convert'), + * used only for the demo code, and the default is 'Validator' if the + * parameter is not specified; + * @returns an object with five properties: + * 'status' will be 'valid' (the uStr is a valid UCUM code), 'invalid' + * (the uStr is not a valid UCUM code, and substitutions or + * suggestions may or may not be returned, depending on what was + * requested and found); or 'error' (an input or programming error + * occurred); + * 'ucumCode' the valid ucum code, which may differ from what was passed + * in (e.g., if 'Gauss' is passed in, this will contain 'G') OR null if + * the string was flagged as invalid or an error occurred; + * 'msg' is an array of one or more messages, if the string is invalid or + * an error occurred, indicating the problem, or an explanation of a + * substitution such as the substitution of 'G' for 'Gauss', or + * an empty array if no messages were generated; + * 'unit' which is null if no unit is found, or a hash for a unit found: + * 'code' is the unit's ucum code (G in the above example; + * 'name' is the unit's name (Gauss in the above example); and + * 'guidance' is the unit's guidance/description data; and + * 'suggestions' if suggestions were requested and found, this is an array + * of one or more hash objects. Each hash contains three elements: + * 'msg' which is a message indicating what part of the uStr input + * parameter the suggestions are for; + * 'invalidUnit' which is the unit expression the suggestions are + * for; and + * 'units' which is an array of data for each suggested unit found. + * Each array will contain the unit code, the unit name and the + * unit guidance (if any). + * If no suggestions were requested and found, this property is not + * returned. + */ + + + validateUnitString(uStr, suggest, valConv) { + if (suggest === undefined) suggest = false; + if (valConv === undefined) valConv = 'validate'; + let resp = this.getSpecifiedUnit(uStr, valConv, suggest); + let theUnit = resp['unit']; + let retObj = {}; + + if (!theUnit) { + retObj = { + 'status': !resp['origString'] || resp['origString'] === null ? 'error' : 'invalid', + 'ucumCode': null + }; + } else { + retObj = { + 'status': resp['origString'] === uStr ? 'valid' : 'invalid', + 'ucumCode': resp['origString'], + 'unit': { + 'code': theUnit.csCode_, + 'name': theUnit.name_, + 'guidance': theUnit.guidance_ + } + }; + } + + if (resp['suggestions']) { + retObj['suggestions'] = resp['suggestions']; + } + + retObj['msg'] = resp['retMsg']; + return retObj; + } // end validateUnitString + + /** + * This method converts one unit to another + * + * @param fromUnitCode the unit code/expression/string of the unit to be converted + * @param fromVal the number of "from" units to be converted to "to" units + * @param toUnitCode the unit code/expression/string of the unit that the from + * field is to be converted to + * @param suggest a boolean to indicate whether or not suggestions are + * requested for a string that cannot be resolved to a valid unit; + * true indicates suggestions are wanted; false indicates they are not, + * and is the default if the parameter is not specified; + * @param molecularWeight the molecular weight of the substance in question + * when a conversion is being requested from mass to moles and vice versa. + * This is required when one of the units represents a value in moles. It is + * ignored if neither unit includes a measurement in moles. + * @returns a hash with six elements: + * 'status' that will be: 'succeeded' if the conversion was successfully + * calculated; 'failed' if the conversion could not be made, e.g., if + * the units are not commensurable; or 'error' if an error occurred; + * 'toVal' the numeric value indicating the conversion amount, or null + * if the conversion failed (e.g., if the units are not commensurable); + * 'msg' is an array message, if the string is invalid or an error occurred, + * indicating the problem, or an explanation of a substitution such as + * the substitution of 'G' for 'Gauss', or an empty array if no + * messages were generated; + * 'suggestions' if suggestions were requested and found, this is a hash + * that contains at most two elements: + * 'from' which, if the fromUnitCode input parameter or one or more of + * its components could not be found, is an array one or more hash + * objects. Each hash contains three elements: + * 'msg' which is a message indicating what unit expression the + * suggestions are for; + * 'invalidUnit' which is the unit expression the suggestions + * are for; and + * 'units' which is an array of data for each suggested unit found. + * Each array will contain the unit code, the unit name and the + * unit guidance (if any). + * If no suggestions were found for the fromUnitCode this element + * will not be included. + * 'to' which, if the "to" unit expression or one or more of its + * components could not be found, is an array one or more hash objects. Each hash + * contains three elements: + * 'msg' which is a message indicating what toUnitCode input + * parameter the suggestions are for; + * 'invalidUnit' which is the unit expression the suggestions + * are for; and + * 'units' which is an array of data for each suggested unit found. + * Each array will contain the unit code, the unit name and the + * unit guidance (if any). + * If no suggestions were found for the toUnitCode this element + * will not be included. + * No 'suggestions' element will be included in the returned hash + * object if none were found, whether or not they were requested. + * 'fromUnit' the unit object for the fromUnitCode passed in; returned + * in case it's needed for additional data from the object; and + * 'toUnit' the unit object for the toUnitCode passed in; returned + * in case it's needed for additional data from the object. + */ + + + convertUnitTo(fromUnitCode, fromVal, toUnitCode, suggest, molecularWeight) { + if (suggest === undefined) suggest = false; + if (molecularWeight === undefined) molecularWeight = null; + let returnObj = { + 'status': 'failed', + 'toVal': null, + 'msg': [] + }; + + if (fromUnitCode) { + fromUnitCode = fromUnitCode.trim(); + } + + if (!fromUnitCode || fromUnitCode == '') { + returnObj['status'] = 'error'; + returnObj['msg'].push('No "from" unit expression specified.'); + } + + if (fromVal === null || isNaN(fromVal) || typeof fromVal !== 'number' && !intUtils_.isNumericString(fromVal)) { + returnObj['status'] = 'error'; + returnObj['msg'].push('No "from" value, or an invalid "from" value, ' + 'was specified.'); + } + + if (toUnitCode) { + toUnitCode = toUnitCode.trim(); + } + + if (!toUnitCode || toUnitCode == '') { + returnObj['status'] = 'error'; + returnObj['msg'].push('No "to" unit expression specified.'); + } + + if (returnObj['status'] !== 'error') { + try { + let fromUnit = null; + let parseResp = this.getSpecifiedUnit(fromUnitCode, 'convert', suggest); + fromUnit = parseResp['unit']; + if (parseResp['retMsg']) returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']); + + if (parseResp['suggestions']) { + returnObj['suggestions'] = {}; + returnObj['suggestions']['from'] = parseResp['suggestions']; + } + + if (!fromUnit) { + returnObj['msg'].push(`Unable to find a unit for ${fromUnitCode}, ` + `so no conversion could be performed.`); + } + + let toUnit = null; + parseResp = this.getSpecifiedUnit(toUnitCode, 'convert', suggest); + toUnit = parseResp['unit']; + if (parseResp['retMsg']) returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']); + + if (parseResp['suggestions']) { + if (!returnObj['suggestions']) returnObj['suggestions'] = {}; + returnObj['suggestions']['to'] = parseResp['suggestions']; + } + + if (!toUnit) { + returnObj['msg'].push(`Unable to find a unit for ${toUnitCode}, ` + `so no conversion could be performed.`); + } + + if (fromUnit && toUnit) { + try { + // if no molecular weight was specified perform a normal conversion + if (!molecularWeight) { + returnObj['toVal'] = toUnit.convertFrom(fromVal, fromUnit); + } else { + if (fromUnit.moleExp_ !== 0 && toUnit.moleExp_ !== 0) { + throw new Error('A molecular weight was specified ' + 'but a mass <-> mole conversion cannot be executed for two ' + 'mole-based units. No conversion was attempted.'); + } + + if (fromUnit.moleExp_ === 0 && toUnit.moleExp_ === 0) { + throw new Error('A molecular weight was specified ' + 'but a mass <-> mole conversion cannot be executed when ' + 'neither unit is mole-based. No conversion was attempted.'); + } + + if (!fromUnit.isMoleMassCommensurable(toUnit)) { + throw new Error(`Sorry. ${fromUnitCode} cannot be ` + `converted to ${toUnitCode}.`); + } // if the "from" unit is a mole-based unit, assume a mole to mass + // request + + + if (fromUnit.moleExp_ !== 0) { + returnObj['toVal'] = fromUnit.convertMolToMass(fromVal, toUnit, molecularWeight); + } // else the "to" unit must be the mole-based unit, so assume a + // mass to mole request + else { + returnObj['toVal'] = fromUnit.convertMassToMol(fromVal, toUnit, molecularWeight); + } + } // end if a molecular weight was specified + // if an error hasn't been thrown - either from convertFrom or here, + // set the return object to show success + + + returnObj['status'] = 'succeeded'; + returnObj['fromUnit'] = fromUnit; + returnObj['toUnit'] = toUnit; + } catch (err) { + returnObj['status'] = 'failed'; + returnObj['msg'].push(err.message); + } + } // end if we have the from and to units + + } catch (err) { + if (err.message == Ucum.needMoleWeightMsg_) returnObj['status'] = 'failed';else returnObj['status'] = 'error'; + returnObj['msg'].push(err.message); + } + } + + return returnObj; + } // end convertUnitTo + + /** + * Converts the given unit string into its base units, their exponents, and + * a magnitude, and returns that data. + * @param fromUnit the unit string to be converted to base units information + * @param fromVal the number of "from" units to be converted + * @returns an object with the properties: + * 'msg': an array of one or more messages, if the string is invalid or + * an error occurred, indicating the problem, or a suggestion of a + * substitution such as the substitution of 'G' for 'Gauss', or + * an empty array if no messages were generated. If this is not empty, + * no other information will be returned. + * 'magnitude': the new value when fromVal units of fromUnits is expressed in the base units. + * 'fromUnitIsSpecial': whether the input unit fromUnit is a "special unit" + * as defined in UCUM. This means there is some function applied to convert + * between fromUnit and the base units, so the returned magnitude is likely not + * useful as a scale factor for other conversions (i.e., it only has validity + * and usefulness for the input values that produced it). + * 'unitToExp': a map of base units in uStr to their exponent + */ + + + convertToBaseUnits(fromUnit, fromVal) { + let inputUnitLookup = this.getSpecifiedUnit(fromUnit, 'validate'); + let retObj = {}; + let unit = inputUnitLookup.unit; + retObj.msg = inputUnitLookup.retMsg || []; + + if (!unit) { + if (inputUnitLookup.retMsg?.length == 0) retObj.msg.push('Could not find unit information for ' + fromUnit); + } else if (unit.isArbitrary_) { + retObj.msg.push('Arbitrary units cannot be converted to base units or other units.'); + } else if (retObj.msg.length == 0) { + let unitToExp = {}; + let dimVec = unit.dim_?.dimVec_; + let baseUnitString = '1'; + + if (dimVec) { + let dimVecIndexToBaseUnit = UnitTables.getInstance().dimVecIndexToBaseUnit_; + + for (let i = 0, len = dimVec.length; i < len; ++i) { + let exp = dimVec[i]; + + if (exp) { + unitToExp[dimVecIndexToBaseUnit[i]] = exp; + baseUnitString += '.' + dimVecIndexToBaseUnit[i] + exp; + } + } + } // The unit might have a conversion function, which has to be applied; we + // cannot just assume unit_.magnitude_ is the magnitude in base units. + + + let retUnitLookup = this.getSpecifiedUnit(baseUnitString, 'validate'); // There should not be any error in retUnitLookup, unless there is a bug. + + let retUnit = retUnitLookup.unit; + if (!retUnit && retUnitLookup.retMsg?.length == 0) retObj.msg.push('Unable construct base unit string; tried ' + baseUnitString);else { + try { + retObj.magnitude = retUnit.convertFrom(fromVal, unit); + } catch (e) { + retObj.msg.push(e.toString()); + } + + if (retObj.msg.length == 0) { + retObj.unitToExp = unitToExp; + retObj.fromUnitIsSpecial = unit.isSpecial_; + } + } + } + + return retObj; + } + /** + * This method accepts a term and looks for units that include it as + * a synonym - or that include the term in its name. + * + * @param theSyn the term to search for + * @returns a hash with up to three elements: + * 'status' contains the status of the request, which can be 'error', + * 'failed' or succeeded'; + * 'msg' which contains a message for an error or if no units were found; and + * 'units' which is an array that contains one hash for each unit found: + * 'code' is the unit's csCode_ + * 'name' is the unit's name_ + * 'guidance' is the unit's guidance_ + * + */ + + + checkSynonyms(theSyn) { + let retObj = {}; + + if (theSyn === undefined || theSyn === null) { + retObj['status'] = 'error'; + retObj['msg'] = 'No term specified for synonym search.'; + } else { + retObj = intUtils_.getSynonyms(theSyn); + } // end if a search synonym was supplied + + + return retObj; + } // end checkSynonyms + + /** + * This method parses a unit string to get (or try to get) the unit + * represented by the string. It returns an error message if no string was specified + * or if any errors were encountered trying to get the unit. + * + * @param uName the expression/string representing the unit + * @param valConv indicates what type of request this is for - a request to + * validate (pass in 'validate') or a request to convert (pass in 'convert') + * @param suggest a boolean to indicate whether or not suggestions are + * requested for a string that cannot be resolved to a valid unit; + * true indicates suggestions are wanted; false indicates they are not, + * and is the default if the parameter is not specified; + * @returns a hash containing: + * 'unit' the unit object (or null if there were problems creating the + * unit); + * 'origString' the possibly updated unit string passed in; + * 'retMsg' an array of user messages (informational, error or warning) if + * any were generated (IF any were generated, otherwise will be an + * empty array); and + * 'suggestions' is an array of 1 or more hash objects. Each hash + * contains three elements: + * 'msg' which is a message indicating what unit expression the + * suggestions are for; + * 'invalidUnit' which is the unit expression the suggestions are + * for; and + * 'units' which is an array of data for each suggested unit found. + * Each array will contain the unit code, the unit name and the + * unit guidance (if any). + * The return hash will not contain a suggestions array if a valid unit + * was found or if suggestions were not requested and found. + */ + + + getSpecifiedUnit(uName, valConv, suggest) { + if (suggest === undefined) suggest = false; + let retObj = {}; + retObj['retMsg'] = []; + + if (!uName) { + retObj['retMsg'].push('No unit string specified.'); + } else { + let utab = UnitTables.getInstance(); + uName = uName.trim(); // go ahead and just try using the name as the code. This may or may not + // work, but if it does, it cuts out a lot of parsing. + + let theUnit = utab.getUnitByCode(uName); // If we found it, set the returned unit string to what was passed in; + // otherwise try parsing as a unit string + + if (theUnit) { + retObj['unit'] = theUnit; + retObj['origString'] = uName; + } else { + try { + let resp = this.uStrParser_.parseString(uName, valConv, suggest); + retObj['unit'] = resp[0]; + retObj['origString'] = resp[1]; + if (resp[2]) retObj['retMsg'] = resp[2]; + retObj['suggestions'] = resp[3]; + } catch (err) { + console.log(`Unit requested for unit string ${uName}.` + 'request unsuccessful; error thrown = ' + err.message); + retObj['retMsg'].unshift(`${uName} is not a valid unit. ` + `${err.message}`); + } + } // end if the unit was not found as a unit name + + } // end if a unit expression was specified + + + return retObj; + } // end getSpecifiedUnit + + /** + * This method retrieves a list of units commensurable, i.e., that can be + * converted from and to, a specified unit. Returns an error if the "from" + * unit cannot be found. + * + * @param fromName the name/unit string of the "from" unit + * @returns an array containing two elements; + * first element is the list of commensurable units if any were found + * second element is an error message if the "from" unit is not found + */ + + + commensurablesList(fromName) { + let retMsg = []; + let commUnits = null; + let parseResp = this.getSpecifiedUnit(fromName, 'validate', false); + let fromUnit = parseResp['unit']; + if (parseResp['retMsg'].length > 0) retMsg = parseResp['retMsg']; + + if (!fromUnit) { + retMsg.push(`Could not find unit ${fromName}.`); + } else { + let dimVec = null; + let fromDim = fromUnit.getProperty('dim_'); + + if (!fromDim) { + retMsg.push('No commensurable units were found for ' + fromName); + } else { + try { + dimVec = fromDim.getProperty('dimVec_'); + } catch (err) { + retMsg.push(err.message); + if (err.message === "Dimension does not have requested property(dimVec_)") dimVec = null; + } + + if (dimVec) { + let utab = UnitTables.getInstance(); + commUnits = utab.getUnitsByDimension(dimVec); + } + } // end if the from unit has a dimension vector + + } // end if we found a "from" unit + + + return [commUnits, retMsg]; + } // end commensurablesList + + +} // end UcumLhcUtils class + +/** + * This function exists ONLY until the original UcumLhcUtils constructor + * is called for the first time. It's defined here in case getInstance + * is called before the constructor. This calls the constructor. + * + * The constructor redefines the getInstance function to return the + * singleton UcumLhcUtils object. This is based on the UnitTables singleton + * implementation; see more detail in the UnitTables constructor description. + * + * NO LONGER TRUE - not implemented as a singleton. This method retained to + * avoid problems with calls to it that exist throughout the code. + * + * @return the (formerly singleton) UcumLhcUtils object. + */ + + +exports.UcumLhcUtils = UcumLhcUtils; + +UcumLhcUtils.getInstance = function () { + return new UcumLhcUtils(); +}; + + +},{"./config.js":9,"./ucumInternalUtils.js":15,"./ucumJsonDefs.js":16,"./unitString.js":20,"./unitTables.js":21}],18:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.UnitTables = exports.UcumLhcUtils = exports.Ucum = void 0; + +/** + * This exports definitions for ucum classes that need references to them + * available to the demo code. The actual code will be in the ucumPkg + * library found in the dist directory. This file provides the hooks to + * those classes within the library. + */ +var Ucum = require("./config.js").Ucum; + +exports.Ucum = Ucum; + +var UcumLhcUtils = require("./ucumLhcUtils.js").UcumLhcUtils; + +exports.UcumLhcUtils = UcumLhcUtils; + +var UnitTables = require("./unitTables.js").UnitTables; + +exports.UnitTables = UnitTables; + + +},{"./config.js":9,"./ucumLhcUtils.js":17,"./unitTables.js":21}],19:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Unit = void 0; + +var _ucumFunctions = _interopRequireDefault(require("./ucumFunctions.js")); + +var intUtils_ = _interopRequireWildcard(require("./ucumInternalUtils.js")); + +function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * This class represents one unit of measure. It includes + * functions to cover constructor, accessor, and assignment tasks as + * well as operators to calculate multiplication, division and raising + * to a power. + * + * @author Lee Mericle, based on java version by Gunther Schadow + * + */ +var Ucum = require('./config.js').Ucum; + +var Dimension = require('./dimension.js').Dimension; + +var UnitTables; + +var isInteger = require("is-integer"); + +class Unit { + /** + * Constructor. + * + * @param attrs an optional parameter that may be: + * a string, which is parsed by the unit parser, which creates + * the unit from the parsed string; or + * a hash containing all or some values for the attributes of + * the unit, where the keys are the attribute names, without a + * trailing underscore, e.g., name instead of name_; or + * null, in which case an empty hash is created and used to + * set the values forthe attributes. + * If a hash (empty or not) is used, attributes for which no value + * is specified are assigned a default value. + * + */ + constructor(attrs = {}) { + // Process the attrs hash passed in, which may be empty. + // Create and assign values (from the attrs hash or defaults) to all + // attributes. From Class Declarations in Understanding ECMAScript, + // https://leanpub.com/understandinges6/read/#leanpub-auto-class-declarations, + // "Own properties, properties that occur on the instance rather than the + // prototype, can only be created inside of a class constructor or method. + // It's recommended to create all possible own properties inside of the + // constructor function so there's a single place that's responsible for + // all of them." + + /* + * Flag indicating whether or not this is a base unit + */ + this.isBase_ = attrs['isBase_'] || false; + /* + * The unit name, e.g., meter + */ + + this.name_ = attrs['name_'] || ''; + /* + * The unit's case-sensitive code, e.g., m + */ + + this.csCode_ = attrs['csCode_'] || ''; + /* + * The unit's case-insensitive code, e.g., M + */ + + this.ciCode_ = attrs['ciCode_'] || ''; + /* + * The unit's property, e.g., length + */ + + this.property_ = attrs['property_'] || ''; + /* + * The magnitude of the unit, e.g., 3600/3937 for a yard, + * where a yard - 3600/3973 * m(eter). The Dimension + * property specifies the meter - which is the unit on which + * a yard is based, and this magnitude specifies how to figure + * this unit based on the base unit. + */ + + this.magnitude_ = attrs['magnitude_'] || 1; + /* + * The Dimension object of the unit + */ + + if (attrs['dim_'] === undefined || attrs['dim_'] === null) { + this.dim_ = new Dimension(); + } // When the unit data stored in json format is reloaded, the dimension data + // is recognized as a a hash, not as a Dimension object. + else if (attrs['dim_']['dimVec_'] !== undefined) { + this.dim_ = new Dimension(attrs['dim_']['dimVec_']); + } else if (attrs['dim_'] instanceof Dimension) { + this.dim_ = attrs['dim_']; + } else if (attrs['dim_'] instanceof Array || isInteger(attrs['dim_'])) { + this.dim_ = new Dimension(attrs['dim_']); + } else { + this.dim_ = new Dimension(); + } + /* + * The print symbol of the unit, e.g., m + */ + + + this.printSymbol_ = attrs['printSymbol_'] || null; + /* + * The class of the unit, where given, e.g., dimless + */ + + this.class_ = attrs['class_'] || null; + /* + * A flag indicating whether or not the unit is metric + */ + + this.isMetric_ = attrs['isMetric_'] || false; + /* + * The "variable" - which I think is used only for base units + * The symbol for the variable as used in equations, e.g., s for distance + */ + + this.variable_ = attrs['variable_'] || null; // comes from 'dim' in XML + + /* + * The conversion function + */ + + this.cnv_ = attrs['cnv_'] || null; + /* + * The conversion prefix + */ + + this.cnvPfx_ = attrs['cnvPfx_'] || 1; + /* + * Flag indicating whether or not this is a "special" unit, i.e., is + * constructed using a function specific to the measurement, e.g., + * fahrenheit and celsius + */ + + this.isSpecial_ = attrs['isSpecial_'] || false; + /* + * Flag indicating whether or not this is an arbitrary unit + */ + + this.isArbitrary_ = attrs['isArbitrary_'] || false; + /* + * Integer indicating what level of exponent applies to a mole-based portion + * of the unit. So, for the unit "mol", this will be 1. For "mol2" this + * will be 2. For "1/mol" this will be -1. Any unit that does not include + * a mole will have a 0 in this field. This is used to determine + * commensurability for mole<->mass conversions. + */ + + this.moleExp_ = attrs['moleExp_'] || 0; + /* + * Added when added LOINC list of units + * synonyms are used by the autocompleter to enhance lookup capabilities + * while source says where the unit first shows up. Current sources are + * UCUM - which are units from the unitsofmeasure.org list and LOINC - + * which are units from the LOINC data. + */ + + this.synonyms_ = attrs['synonyms_'] || null; + this.source_ = attrs['source_'] || null; + this.loincProperty_ = attrs['loincProperty_'] || null; + this.category_ = attrs['category_'] || null; + this.guidance_ = attrs['guidance_'] || null; + /* + * Used to compute dimension; storing for now until I complete + * unit definition parsing + */ + + /* + * Case sensitive (cs) and case insensitive (ci) base unit strings, + * includes exponent and prefix if applicable - specified in + * nnn -- the unit part -- + * in the ucum-essence.xml file, and may be specified by a user + * when requesting conversion or validation of a unit string. The + * magnitude (base factor) is used with this to determine the new unit. + * For example, a Newton (unit code N) is created from the string + * kg.m/s2, and the value of 1 (base factor defined below). An hour + * (unit code h) is created from the unit min (minute) with a value + * of 60. + */ + + this.csUnitString_ = attrs['csUnitString_'] || null; + this.ciUnitString_ = attrs['ciUnitString_'] || null; + /* + * String and numeric versions of factor applied to unit specified in + * nnn -- the value part + */ + + this.baseFactorStr_ = attrs['baseFactorStr_'] || null; + this.baseFactor_ = attrs['baseFactor_'] || null; + /* + * Flag used to indicate units where the definition process failed + * when parsing units from the official units definitions file + * (currently using the ucum-essence.xml file). We keep these + * so that we can use them to at least validate them as valid + * units, but we don't try to convert them. This is temporary + * and only to account for instances where the code does not + * take into account various special cases in the xml file. + * + * This is NOT used when trying to validate a unit string + * submitted during a conversion or validation attempt. + */ + + this.defError_ = attrs['defError_'] || false; + } // end constructor + + /** + * Assign the unity (= dimensionless unit 1) to this unit. + * + * @return this unit + */ + + + assignUnity() { + this.name_ = ""; + this.magnitude_ = 1; + if (!this.dim_) this.dim_ = new Dimension(); + this.dim_.assignZero(); + this.cnv_ = null; + this.cnvPfx_ = 1; + return this; + } // end assignUnity + + /** + * This assigns one or more values, as provided in the hash passed in, + * to this unit. + * + * @param vals hash of values to be assigned to the attributes + * specified by the key(s), which should be the attribute + * name without the trailing underscore, e.g., name instead + * of name_. + * @return nothing + */ + + + assignVals(vals) { + for (let key in vals) { + let uKey = !key.charAt(key.length - 1) === '_' ? key + '_' : key; + if (this.hasOwnProperty(uKey)) this[uKey] = vals[key];else throw new Error(`Parameter error; ${key} is not a property of a Unit`); + } + } // end assignVals + + /** + * This creates a clone of this unit. + * + * @return the clone + */ + + + clone() { + let retUnit = new Unit(); + Object.getOwnPropertyNames(this).forEach(val => { + if (val === 'dim_') { + if (this['dim_']) retUnit['dim_'] = this['dim_'].clone();else retUnit['dim_'] = null; + } else retUnit[val] = this[val]; + }); + return retUnit; + } // end clone + + /** + * This assigns all properties of a unit passed to it to this unit. + * + * @param unit2 the unit whose properties are to be assigned to this one. + * @return nothing; this unit is updated + */ + + + assign(unit2) { + Object.getOwnPropertyNames(unit2).forEach(val => { + if (val === 'dim_') { + if (unit2['dim_']) this['dim_'] = unit2['dim_'].clone();else this['dim_'] = null; + } else { + this[val] = unit2[val]; + } + }); + } // end assign + + /** + * This determines whether or not object properties of the unit + * passed in are equal to the corresponding properties in this unit. + * The following properties are the only ones checked: + * magnitude_, dim_, cnv_ and cnvPfx_ + * + * @param unit2 the unit whose properties are to be checked. + * @return boolean indicating whether or not they match + */ + + + equals(unit2) { + return this.magnitude_ === unit2.magnitude_ && this.cnv_ === unit2.cnv_ && this.cnvPfx_ === unit2.cnvPfx_ && (this.dim_ === null && unit2.dim_ === null || this.dim_.equals(unit2.dim_)); + } // end equals + + /** + * This method compares every attribute of two objects to determine + * if they all match. + * + * @param unit2 the unit that is to be compared to this unit + * @return boolean indicating whether or not every attribute matches + */ + + + fullEquals(unit2) { + let thisAttr = Object.keys(this).sort(); + let u2Attr = Object.keys(unit2).sort(); + let keyLen = thisAttr.length; + let match = keyLen === u2Attr.length; // check each attribute. Dimension objects have to checked using + // the equals function of the Dimension class. + + for (let k = 0; k < keyLen && match; k++) { + if (thisAttr[k] === u2Attr[k]) { + if (thisAttr[k] === 'dim_') match = this.dim_.equals(unit2.dim_);else match = this[thisAttr[k]] === unit2[thisAttr[k]]; + } else match = false; + } // end do for each key and attribute + + + return match; + } // end of fullEquals + + /** + * This returns the value of the property named by the parameter + * passed in. + * + * @param propertyName name of the property to be returned, with + * or without the trailing underscore. + * @return the requested property, if found for this unit + * @throws an error if the property is not found for this unit + */ + + + getProperty(propertyName) { + let uProp = propertyName.charAt(propertyName.length - 1) === '_' ? propertyName : propertyName + '_'; + return this[uProp]; + } // end getProperty + + /** + * Takes a measurement consisting of a number of units and a unit and returns + * the equivalent number of this unit. So, 15 mL would translate + * to 1 tablespoon if this object is a tablespoon. + * + * Note that the number returned may not be what is normally expected. + * For example, converting 10 Celsius units to Fahrenheit would "normally" + * return a value of 50. But in this case you'll get back something like + * 49.99999999999994. + * + * If either unit is an arbitrary unit an exception is raised. + * + * @param num the magnitude for the unit to be translated (e.g. 15 for 15 mL) + * @param fromUnit the unit to be translated to one of this type (e.g. a mL unit) + * + * @return the number of converted units (e.g. 1 for 1 tablespoon) + * @throws an error if the dimension of the fromUnit differs from this unit's + * dimension + */ + + + convertFrom(num, fromUnit) { + let newNum = 0.0; + if (this.isArbitrary_) throw new Error(`Attempt to convert to arbitrary unit "${this.csCode_}"`); + if (fromUnit.isArbitrary_) throw new Error(`Attempt to convert arbitrary unit "${fromUnit.csCode_}"`); // reject request if both units have dimensions that are not equal + + if (fromUnit.dim_ && this.dim_ && !fromUnit.dim_.equals(this.dim_)) { + // check first to see if a mole<->mass conversion is appropriate + if (this.isMoleMassCommensurable(fromUnit)) { + throw new Error(Ucum.needMoleWeightMsg_); + } else { + throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`); + } + } // reject request if there is a "from" dimension but no "to" dimension + + + if (fromUnit.dim_ && (!this.dim_ || this.dim_.isNull())) { + throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`); + } // reject request if there is a "to" dimension but no "from" dimension + + + if (this.dim_ && (!fromUnit.dim_ || fromUnit.dim_.isNull())) { + throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`); + } + + let fromCnv = fromUnit.cnv_; + let fromMag = fromUnit.magnitude_; + let x; + + if (fromCnv != null) { + // turn num * fromUnit.magnitude into its ratio scale equivalent, + // e.g., convert Celsius to Kelvin + let fromFunc = _ucumFunctions.default.forName(fromCnv); + + x = fromFunc.cnvFrom(num * fromUnit.cnvPfx_) * fromMag; //x = fromFunc.cnvFrom(num * fromMag) * fromUnit.cnvPfx_; + } else { + x = num * fromMag; + } + + if (this.cnv_ != null) { + // turn mag * origUnit on ratio scale into a non-ratio unit, + // e.g. convert Kelvin to Fahrenheit + let toFunc = _ucumFunctions.default.forName(this.cnv_); + + newNum = toFunc.cnvTo(x / this.magnitude_) / this.cnvPfx_; + } else { + newNum = x / this.magnitude_; + } + + return newNum; + } // end convertFrom + + /** + * Takes a number and a target unit and returns the number for a measurement + * of this unit that corresponds to the number of the target unit passed in. + * So, 1 tablespoon (where this unit represents a tablespoon) would translate + * to 15 mL. + * + * See the note on convertFrom about return values. + * + * @param mag the magnitude for this unit (e.g. 1 for 1 tablespoon) + * @param toUnit the unit to which this unit is to be translated + * (e.g. an mL unit) + * + * @return the converted number value (e.g. 15 mL) + * @throws an error if the dimension of the toUnit differs from this unit's + * dimension + */ + + + convertTo(num, toUnit) { + return toUnit.convertFrom(num, this); + } // end convertTo + + /** + * Takes a given number of this unit returns the number of this unit + * if it is converted into a coherent unit. Does not change this unit. + * + * If this is a coherent unit already, just gives back the number + * passed in. + * + * @param num the number for the coherent version of this unit + * @return the number for the coherent version of this unit + */ + + + convertCoherent(num) { + // convert mag' * u' into canonical number * u on ratio scale + if (this.cnv_ !== null) num = this.cnv_.f_from(num / this.cnvPfx_) * this.magnitude_; + return num; + } // end convertCoherent + + /** + * Mutates this unit into a coherent unit and converts a given number of + * units to the appropriate value for this unit as a coherent unit + * + * @param num the number for this unit before conversion + * @return the number of this unit after conversion + * @throws an error if the dimensions differ + */ + + + mutateCoherent(num) { + // convert mu' * u' into canonical mu * u on ratio scale + num = this.convertCoherent(num); // mutate to coherent unit + + this.magnitude_ = 1; + this.cnv_ = null; + this.cnvPfx_ = 1; + this.name_ = ""; // build a name as a term of coherent base units + // This is probably ALL WRONG and a HORRIBLE MISTAKE + // but until we figure out what the heck the name being + // built here really is, it will have to stay. + + for (let i = 0, max = Dimension.getMax(); i < max; i++) { + let elem = this.dim_.getElementAt(i); + + let tabs = this._getUnitTables(); + + let uA = tabs.getUnitsByDimension(new Dimension(i)); + if (uA == null) throw new Error(`Can't find base unit for dimension ${i}`); + this.name_ = uA.name + elem; + } + + return num; + } // end mutateCoherent + + /** + * Calculates the number of units that would result from converting a unit + * expressed in mass/grams to a unit expressed in moles. The "this" unit is + * the unit expressed in some form of mass (g, mg, mmg, kg, whatever) and the + * target or "to" unit - the molUnit parameter - is a unit expressed in moles + * - mol, umol, mmol, etc. The unit expressions surrounding the moles and + * mass must be convertible. No validation of this requirement is performed. + * + * @param amt the quantity of this unit to be converted + * @param molUnit the target/to unit for which the converted # is wanted + * @param molecularWeight the molecular weight of the substance for which the + * conversion is being made + * @return the equivalent amount in molUnit + */ + + + convertMassToMol(amt, molUnit, molecularWeight) { + // The prefix values that have been applied to this unit, which is the mass + // (grams) unit, are reflected in the magnitude. So the number of moles + // represented by this unit equals the number of grams -- amount * magnitude + // divided by the molecular Weight + let molAmt = this.magnitude_ * amt / molecularWeight; // The molUnit's basic magnitude, before prefixes are applied, + // is avogadro's number, get that and divide it out of the current magnitude. + + let tabs = this._getUnitTables(); + + let avoNum = tabs.getUnitByCode('mol').magnitude_; + let molesFactor = molUnit.magnitude_ / avoNum; // return the molAmt divided by the molesFactor as the number of moles + // for the molUnit + + return molAmt / molesFactor; + } + /** + * Calculates the number of units that would result from converting a unit + * expressed in moles to a unit expressed in mass (grams). The "this" unit + * is the unit expressed in some form of moles, e.g., mol, umol, mmol, etc., + * and the target or "to" unit is a unit expressed in some form of mass, e.g., + * g, mg, mmg, kg, etc. Any unit expressions surrounding the moles and mass + * must be convertible. No validation of this requirement is performed. + * + * @param amt the quantity of this unit to be converted + * @param massUnit the target/to unit for which the converted # is wanted + * @param molecularWeight the molecular weight of the substance for which the + * conversion is being made + * @return the equivalent amount in massUnit + */ + + + convertMolToMass(amt, massUnit, molecularWeight) { + // A simple mole unit has a magnitude of avogadro's number. Get that + // number now (since not everyone agrees on what it is, and what is + // being used in this system might change). + let tabs = this._getUnitTables(); + + let avoNum = tabs.getUnitByCode('mol').magnitude_; // Determine what prefix values (mg or mg/dL, etc.) have been applied to + // this unit by dividing the simple mole unit magnitude out of the + // current mole unit magnitude. + + let molesFactor = this.magnitude_ / avoNum; // The number of grams (mass) is equal to the number of moles (amt) + // times the molecular weight. We also multiply that by the prefix values + // applied to the current unit (molesFactor) to get the grams for this + // particular unit. + + let massAmt = molesFactor * amt * molecularWeight; // Finally, we return the mass amount/grams for this particular unit + // divided by any effects of prefixes applied to the "to" unit, which + // is assumed to be some form of a gram unit + + return massAmt / massUnit.magnitude_; + } + /** + * Mutates this unit into a unit on a ratio scale and converts a specified + * number of units to an appropriate value for this converted unit + * + * @param num the number of this unit before it's converted + * @return the magnitude of this unit after it's converted + * @throw an error if the dimensions differ + */ + + + mutateRatio(num) { + if (this.cnv_ == null) return this.mutateCoherent(num);else return num; + } // end mutateRatio + + /** + * Multiplies this unit with a scalar. Special meaning for + * special units so that (0.1*B) is 1 dB. + * + * This function DOES NOT modify this unit. + * + * @param s the value by which this unit is to be multiplied + * @return a copy this unit multiplied by s + * */ + + + multiplyThis(s) { + let retUnit = this.clone(); + if (retUnit.cnv_ != null) retUnit.cnvPfx_ *= s;else retUnit.magnitude_ *= s; + let mulVal = s.toString(); + retUnit.name_ = this._concatStrs(mulVal, '*', this.name_, '[', ']'); + retUnit.csCode_ = this._concatStrs(mulVal, '.', this.csCode_, '(', ')'); + retUnit.ciCode_ = this._concatStrs(mulVal, '.', this.ciCode_, '(', ')'); + retUnit.printSymbol_ = this._concatStrs(mulVal, '.', this.printSymbol_, '(', ')'); + return retUnit; + } // end multiplyThis + + /** + * Multiplies this unit with another unit. If one of the + * units is a non-ratio unit the other must be dimensionless or + * else an exception is thrown. + * + * This function does NOT modify this unit + * @param unit2 the unit to be multiplied with this one + * @return this unit after it is multiplied + * @throws an error if one of the units is not on a ratio-scale + * and the other is not dimensionless. + */ + + + multiplyThese(unit2) { + var retUnit = this.clone(); + + if (retUnit.cnv_ != null) { + if (unit2.cnv_ == null && (!unit2.dim_ || unit2.dim_.isZero())) retUnit.cnvPfx_ *= unit2.magnitude_;else throw new Error(`Attempt to multiply non-ratio unit ${retUnit.name_} ` + 'failed.'); + } // end if this unit has a conversion function + else if (unit2.cnv_ != null) { + if (!retUnit.dim_ || retUnit.dim_.isZero()) { + retUnit.cnvPfx_ = unit2.cnvPfx_ * retUnit.magnitude_; + retUnit.magnitude_ = unit2.magnitude_; + retUnit.cnv_ = unit2.cnv_; + } else throw new Error(`Attempt to multiply non-ratio unit ${unit2.name_}`); + } // end if unit2 has a conversion function + // else neither unit has a conversion function + else { + retUnit.magnitude_ *= unit2.magnitude_; + } // end if unit2 does not have a conversion function + // If this.dim_ isn't there, clone the dimension in unit2 - if dimVec_ + // is a dimension in unit2.dim_; else just transfer it to this dimension + + + if (!retUnit.dim_ || retUnit.dim_ && !retUnit.dim_.dimVec_) { + if (unit2.dim_) retUnit.dim_ = unit2.dim_.clone();else retUnit.dim_ = unit2.dim_; + } // Else this.dim_ is there. If there is a dimension for unit2, + // add it to this one. + else if (unit2.dim_ && unit2.dim_ instanceof Dimension) { + retUnit.dim_.add(unit2.dim_); + } // Concatenate the unit info (name, code, etc) for all cases + // where the multiplication was performed (an error wasn't thrown) + + + retUnit.name_ = this._concatStrs(retUnit.name_, '*', unit2.name_, '[', ']'); + retUnit.csCode_ = this._concatStrs(retUnit.csCode_, '.', unit2.csCode_, '(', ')'); + if (retUnit.ciCode_ && unit2.ciCode_) retUnit.ciCode_ = this._concatStrs(retUnit.ciCode_, '.', unit2.ciCode_, '(', ')');else if (unit2.ciCode_) retUnit.ciCode_ = unit2.ciCode_; + retUnit.resetFieldsForDerivedUnit(); + if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '.', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.printSymbol_; // Update the mole exponent count by adding the count for unit2 to the + // count for this unit. + + retUnit.moleExp_ = retUnit.moleExp_ + unit2.moleExp_; // A unit that has the arbitrary attribute taints any unit created from it + // via an arithmetic operation. Taint accordingly + // if (!retUnit.isMole_) + // retUnit.isMole_ = unit2.isMole_ ; + + if (!retUnit.isArbitrary_) retUnit.isArbitrary_ = unit2.isArbitrary_; // Likewise for special units + + if (!retUnit.isSpecial_) retUnit.isSpecial_ = unit2.isSpecial_; + return retUnit; + } // end multiplyThese + + /** + * Clears fields like isBase_, synonyms_, etc. when a unit has been cloned + * from a known unit but it being used to construct a derived unit. + */ + + + resetFieldsForDerivedUnit() { + this.guidance_ = ''; + this.synonyms_ = null; + this.isBase_ = false; + } + /** + * Divides this unit by another unit. If this unit is not on a ratio + * scale an exception is raised. Mutating to a ratio scale unit + * is not possible for a unit, only for a measurement. + * + * This unit is NOT modified by this function. + * @param unit2 the unit by which to divide this one + * @return this unit after it is divided by unit2 + * @throws an error if either of the units is not on a ratio scale. + * */ + + + divide(unit2) { + var retUnit = this.clone(); + if (retUnit.cnv_ != null) throw new Error(`Attempt to divide non-ratio unit ${retUnit.name_}`); + if (unit2.cnv_ != null) throw new Error(`Attempt to divide by non-ratio unit ${unit2.name_}`); + if (retUnit.name_ && unit2.name_) retUnit.name_ = this._concatStrs(retUnit.name_, '/', unit2.name_, '[', ']');else if (unit2.name_) retUnit.name_ = unit2.invertString(unit2.name_); + retUnit.csCode_ = this._concatStrs(retUnit.csCode_, '/', unit2.csCode_, '(', ')'); + if (retUnit.ciCode_ && unit2.ciCode_) retUnit.ciCode_ = this._concatStrs(retUnit.ciCode_, '/', unit2.ciCode_, '(', ')');else if (unit2.ciCode_) retUnit.ciCode_ = unit2.invertString(unit2.ciCode_); + retUnit.resetFieldsForDerivedUnit(); + retUnit.magnitude_ /= unit2.magnitude_; + if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '/', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.invertString(unit2.printSymbol_); // Continue if unit2 has a dimension object. + // If this object has a dimension object, subtract unit2's dim_ object from + // this one. The sub method will take care of cases where the dimVec_ arrays + // are missing on one or both dim_ objects. + + if (unit2.dim_) { + if (retUnit.dim_) { + if (retUnit.dim_.isNull()) retUnit.dim_.assignZero(); + retUnit.dim_ = retUnit.dim_.sub(unit2.dim_); + } // end if this.dim_ exists + // Else if this dim_ object is missing, clone unit2's dim_ object + // and give the inverted clone to this unit. + else retUnit.dim_ = unit2.dim_.clone().minus(); + } // end if unit2 has a dimension object + // Update the mole exponent count by subtracting the count for unit2 from + // the // count for this unit. + + + retUnit.moleExp_ = retUnit.moleExp_ - unit2.moleExp_; // A unit that has the arbitrary attribute taints any unit created from + // it via an arithmetic operation. Taint accordingly + // if (!retUnit.isMole_) + // retUnit.isMole_ = unit2.isMole_ ; + + if (!retUnit.isArbitrary_) retUnit.isArbitrary_ = unit2.isArbitrary_; + return retUnit; + } // end divide + + /** + * Invert this unit with respect to multiplication. If this unit is not + * on a ratio scale an exception is thrown. Mutating to a ratio scale unit + * is not possible for a unit, only for a measurement (the magnitude and + * dimension). + * + * This unit is modified by this function. + * @return this unit after being inverted + * @throws and error if this unit is not on a ratio scale + */ + + + invert() { + if (this.cnv_ != null) throw new Error(`Attempt to invert a non-ratio unit - ${this.name_}`); + this.name_ = this.invertString(this.name_); + this.magnitude_ = 1 / this.magnitude_; + this.dim_.minus(); + return this; + } // end invert + + /** + * Inverts a string, where the string is assumed to be a code or a name + * of a division operation where the string is the divisor and the dividend + * is blank. + * + * @param the string to be inverted + * @return the inverted string + */ + + + invertString(theString) { + if (theString.length > 0) { + let stringRep = theString.replace('/', "!").replace('.', '/').replace("!", '.'); + + switch (stringRep.charAt(0)) { + case '.': + theString = stringRep.substr(1); + break; + + case '/': + theString = stringRep; + break; + + default: + theString = "/" + stringRep; + } + } + + return theString; + } // end invertString + + /** + * This function handles concatenation of two strings and an operator. + * It's called to build unit data, e.g., unit name, unit code, etc., from + * two different units, joined by the specified operator. + * + * @param str1 the first string to appear in the result + * @param operator the operator ('*', '.' or '/') to appear between the strings + * @param str2 the second string to appear in the result + * @param startChar the starting character to be used, when needed, to + * enclose a string + * @param endChar the ending character to be used, when needed, to enclose + * a string + * @returns the built string + */ + + + _concatStrs(str1, operator, str2, startChar, endChar) { + return this._buildOneString(str1, startChar, endChar) + operator + this._buildOneString(str2, startChar, endChar); + } + /** + * This function handles creation of one string to be included in a + * concatenated string. Basically it checks to see if the string + * needs to be enclosed either in parentheses or square brackets. + * + * The string is enclosed if it is not a number, is not already enclosed in a pair of + * parentheses or square brackets, and includes a period, and asterisk, + * a slash or a blank space. + * + * @param str the string + * @param startChar starting enclosing character + * @param endChar ending enclosing character + * @returns the string + */ + + + _buildOneString(str, startChar, endChar) { + let ret = ''; + + if (intUtils_.isNumericString(str)) { + ret = str; + } else { + if (str.charAt(0) === '(' && str.endsWith(')') || str.charAt(0) === '[' && str.endsWith(']')) { + ret = str; + } else if (/[./* ]/.test(str)) { + ret = startChar + str + endChar; + } else { + ret = str; + } + } + + return ret; + } + /** + * Raises the unit to a power. For example + * kg.m/s2 raised to the -2 power would be kg-2.m-2/s-4 + * + * If this unit is not on a ratio scale an error is thrown. Mutating + * to a ratio scale unit is not possible for a unit, only for a + * measurement (magnitude and dimension). + * + * This is based on the pow method in Gunter Schadow's java version, + * although it uses javascript capabilities to simplify the processing. + * + * This unit is modified by this function + * + * @param p the power to with this unit is to be raise + * @return this unit after it is raised + * @throws an error if this unit is not on a ratio scale. + */ + + + power(p) { + if (this.cnv_ != null) throw new Error(`Attempt to raise a non-ratio unit, ${this.name_}, ` + 'to a power.'); //this.name_ = UnitString.pow(this.name_, p); + // the above line is replaced with the code below, as the pow method + // never actually existing in the UnitString class. (Tried to use + // Schadow java code but this way ended up being a lot easier). + + let uStr = this.csCode_; + let uArray = uStr.match(/([./]|[^./]+)/g); + let arLen = uArray.length; + + for (let i = 0; i < arLen; i++) { + let un = uArray[i]; + + if (un !== '/' && un !== '.') { + let nun = parseInt(un); + if (isInteger(nun)) uArray[i] = Math.pow(nun, p).toString();else { + let uLen = un.length; + + for (let u = uLen - 1; u >= 0; u--) { + let uChar = parseInt(un[u]); + + if (!isInteger(uChar)) { + if (un[u] === '-' || un[u] === '+') { + u--; + } + + if (u < uLen - 1) { + let exp = parseInt(un.substr(u)); + exp = Math.pow(exp, p); + uArray[i] = un.substr(0, u) + exp.toString(); + u = -1; + } else { + uArray[i] += p.toString(); + u = -1; + } // end if there are/aren't some numbers at the end + + + u = -1; + } // end if this character is not a number + + } // end searching backwards for start of exponent + + } // end if this element is not a number + } // end if the current element is not an operator + + } // end do for each element of the units array + // reassemble the updated units array to a string + + + this.csCode_ = uArray.join(''); + this.magnitude_ = Math.pow(this.magnitude_, p); + + if (this.dim_) { + this.dim_.mul(p); + } + + return this; + } // end power + + /* + * This function tests this unit against the unit passed in to see if the + * two are mole to mass commensurable. It assumes that one of the units + * is a mole-based unit and the other is a mass-based unit. It also assumes + * that the mole-based unit has a single mole unit in the numerator and that + * the mass-based unit has a single mass unit in the numerator. It does NOT + * check to validate those assumptions. + * + * The check is made by setting the dimension vector element corresponding + * to the base mass unit (gram) in the mole unit, and then comparing the + * two dimension vectors. If they match, the units are commensurable. + * Otherwise they are not. + * + * @param unit2 the unit to be compared to this one + * @returns boolean indicating commensurability + */ + + + isMoleMassCommensurable(unit2) { + let tabs = this._getUnitTables(); + + let d = tabs.getMassDimensionIndex(); + let commensurable = false; + + if (this.moleExp_ === 1 && unit2.moleExp_ === 0) { + let testDim = this.dim_.clone(); + let curVal = testDim.getElementAt(d); + testDim.setElementAt(d, curVal + this.moleExp_); + commensurable = testDim.equals(unit2.dim_); + } else if (unit2.moleExp_ === 1 && this.moleExp_ === 0) { + let testDim = unit2.dim_.clone(); + let curVal = testDim.getElementAt(d); + testDim.setElementAt(d, curVal + unit2.moleExp_); + commensurable = testDim.equals(this.dim_); + } + + return commensurable; + } + /** + * This returns the UnitTables singleton object. Including the require + * statement included here causes a circular dependency condition that + * resulted in the UnitTables object not being defined for the Unit object. + * sigh. Thanks, Paul, for figuring this out. + * + * @private + */ + + + _getUnitTables() { + if (!UnitTables) UnitTables = require('./unitTables.js').UnitTables; + return UnitTables.getInstance(); + } + +} // end Unit class + + +exports.Unit = Unit; + + +},{"./config.js":9,"./dimension.js":10,"./ucumFunctions.js":14,"./ucumInternalUtils.js":15,"./unitTables.js":21,"is-integer":213}],20:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.UnitString = void 0; + +var intUtils_ = _interopRequireWildcard(require("./ucumInternalUtils.js")); + +function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * This class handles the parsing of a unit string into a unit object + */ +var Ucum = require('./config.js').Ucum; + +var Unit = require('./unit.js').Unit; + +var UnitTables = require('./unitTables.js').UnitTables; + +var PrefixTables = require('./prefixTables.js').PrefixTables; + +class UnitString { + /** + * Constructor + */ + constructor() { + // Get instances of the unit and prefix tables and the utilities + this.utabs_ = UnitTables.getInstance(); + this.pfxTabs_ = PrefixTables.getInstance(); // Set emphasis characters to defaults. These are used to emphasize + // certain characters or strings in user messages. They can be reset in + // the useHTMLInMessages method. + + this.openEmph_ = Ucum.openEmph_; + this.closeEmph_ = Ucum.closeEmph_; // Set the braces message to blank. This message is displayed for each + // validation request on the web page, but is included separately as + // a note on the validation spreadsheet. The useBraceMsgForEachString + // method should be used to set the message to be displayed for each + // unit string. + + this.bracesMsg_ = ''; // Set the flags used, with indices, as place holders in unit strings + // for parenthetical strings and strings within braces. + + this.parensFlag_ = "parens_placeholder"; // in lieu of Jehoshaphat + + this.pFlagLen_ = this.parensFlag_.length; + this.braceFlag_ = "braces_placeholder"; // in lieu of Nebuchadnezzar + + this.bFlagLen_ = this.braceFlag_.length; // Initialize the message start/end strings, which will be set when + // parseString is called. + + this.vcMsgStart_ = null; + this.vcMsgEnd_ = null; // Arrays used by multiple methods within this class to hold persistent + // data. Just gets too bulky to pass these guys around. + // Messages to be returned to the calling function + + this.retMsg_ = []; // Units for parenthetical unit strings + + this.parensUnits_ = []; // annotation text for annotations found in unit strings + + this.annotations_ = []; // suggestions for unit strings that for which no unit was found + + this.suggestions = []; + } // end constructor + // The start of an error message about an invalid annotation character. + + + /** + * Sets the emphasis strings to the HTML used in the webpage display - or + * blanks them out, depending on the use parameter. + * + * @param use flag indicating whether or not to use the html message format; + * defaults to true + */ + useHTMLInMessages(use) { + if (use === undefined || use) { + this.openEmph_ = Ucum.openEmphHTML_; + this.closeEmph_ = Ucum.closeEmphHTML_; + } else { + this.openEmph_ = Ucum.openEmph_; + this.closeEmph_ = Ucum.closeEmph_; + } + } // end useHTMLInMessages + + /** + * Sets the braces message to be displayed for each unit string validation + * requested, as appropriate. + * + * @param use flag indicating whether or not to use the braces message; + * defaults to true + */ + + + useBraceMsgForEachString(use) { + if (use === undefined || use) this.bracesMsg_ = Ucum.bracesMsg_;else this.bracesMsg_ = ''; + } + /** + * Parses a unit string, returns a unit, a possibly updated version of + * the string passed in, and messages and suggestions where appropriate. + * + * The string returned may be updated if the input string contained unit + * names, e.g., "pound". The unit code ([lb_av] for pound) is placed in + * the string returned, a the returned messages array includes a note + * explaining the substitution. + * + * @param uStr the string defining the unit + * @param valConv indicates what type of request this is for - a request to + * validate (pass in 'validate') or a request to convert (pass in 'convert'); + * optional, defaults to 'validate' + * @param suggest a boolean to indicate whether or not suggestions are + * requested for a string that cannot be resolved to a valid unit; + * true indicates suggestions are wanted; false indicates they are not, + * and is the default if the parameter is not specified; + * @returns an array containing: + * the unit object or null if a unit could not be created. In cases where + * a fix was found for a problem string, .e.g., 2.mg for 2mg, a unit will + * be returned but an error message will also be returned, describing + * the substitution; + * the possibly updated unit string passed in; + * an array of any user messages (informational, error or warning) + * generated (or an empty array); and + * a suggestions array of hash objects (1 or more). Each hash contains + * three elements: + * 'msg' which is a message indicating what unit expression the + * suggestions are for; + * 'invalidUnit' which is the unit expression the suggestions are + * for; and + * 'units' which is an array of data for each suggested unit found. + * Each array will contain the unit code, the unit name and the + * unit guidance (if any). + * The return array will not contain a suggestions array if a valid unit + * was found or if suggestions were not requested. + * @throws an error if nothing was specified. + */ + + + parseString(uStr, valConv, suggest) { + uStr = uStr.trim(); // Make sure we have something to work with + + if (uStr === '' || uStr === null) { + throw new Error('Please specify a unit expression to be validated.'); + } + + if (valConv === 'validate') { + this.vcMsgStart_ = Ucum.valMsgStart_; + this.vcMsgEnd_ = Ucum.valMsgEnd_; + } else { + this.vcMsgStart_ = Ucum.cnvMsgStart_; + this.vcMsgEnd_ = Ucum.cnvMsgEnd_; + } + + if (suggest === undefined || suggest === false) { + this.suggestions_ = null; + } else { + this.suggestions_ = []; + } + + this.retMsg_ = []; + this.parensUnits_ = []; + this.annotations_ = []; + let origString = uStr; + let retObj = []; // Extract any annotations, i.e., text enclosed in braces ({}) from the + // string before further processing. Store each one in this.annotations_ + // array and put a placeholder in the string for the annotation. Do + // this before other processing in case an annotation contains characters + // that will be interpreted as parenthetical markers or operators in + // subsequent processing. + + uStr = this._getAnnotations(uStr); + + if (this.retMsg_.length > 0) { + retObj[0] = null; + retObj[1] = null; + } else { + // Flag used to block further processing on an unrecoverable error + let endProcessing = this.retMsg_.length > 0; // First check for one of the "special" units. If it's one of those, put + // in a substitution phrase for it to avoid having it separated on its + // embedded operator. This will only happen, by the way, if it is + // preceded by a prefix or followed by an operator and another unit. + + let sUnit = null; + + for (sUnit in Ucum.specUnits_) { + while (uStr.indexOf(sUnit) !== -1) uStr = uStr.replace(sUnit, Ucum.specUnits_[sUnit]); + } // Check for spaces and throw an error if any are found. The spec + // explicitly forbids spaces except in annotations, which is why any + // annotations are extracted before this check is made. + + + if (uStr.indexOf(' ') > -1) { + throw new Error('Blank spaces are not allowed in unit expressions.'); + } // end if blanks were found in the string + // assign the array returned to retObj. It will contain 2 elements: + // the unit returned in position 0; and the origString (possibly + // modified) in position 1. The origString in position 1 will not + // be changed by subsequent processing. + + + retObj = this._parseTheString(uStr, origString); + let finalUnit = retObj[0]; // Do a final check to make sure that finalUnit is a unit and not + // just a number. Something like "8/{HCP}" will return a "unit" of 8 + // - which is not a unit. Hm - evidently it is. So just create a unit + // object for it. + + if (intUtils_.isIntegerUnit(finalUnit) || typeof finalUnit === 'number') { + finalUnit = new Unit({ + 'csCode_': origString, + 'ciCode_': origString, + 'magnitude_': finalUnit, + 'name_': origString + }); + retObj[0] = finalUnit; + } // end final check + + } // end if no annotation errors were found + + + retObj[2] = this.retMsg_; + if (this.suggestions_ && this.suggestions_.length > 0) retObj[3] = this.suggestions_; + return retObj; + } // end parseString + + /** + * Parses a unit string, returns a unit, a possibly updated version of + * the string passed in, and messages where appropriate. This should + * only be called from within this class (or by test code). + * + * The string returned may be updated if the input string contained unit + * names, e.g., "pound". The unit code ([lb_av] for pound) is placed in + * the string returned, a the returned messages array includes a note + * explaining the substitution. + * + * @param uStr the string defining the unit + * @param origString the original unit string passed in + * + * @returns + * an array containing: + * the unit object (or null if there were problems creating the unit); and + * the possibly updated unit string passed in. + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + * the this.parensUnits_ array is referenced and possibly populated by + * methods called within this one + * the this.annotations_ array is referenced by methods called within + * this one + * the this.suggestions_ array may be populated by methods called within + * this one + */ + + + _parseTheString(uStr, origString) { + // Unit to be returned + let finalUnit = null; // Flag used to block further processing on an unrecoverable error + + let endProcessing = this.retMsg_.length > 0; // Call _processParens to search for and process any/all parenthetical + // strings in uStr. Units created for parenthetical strings will be + // stored in the this.parensUnits_ array. + + let parensResp = this._processParens(uStr, origString); + + endProcessing = parensResp[2]; // The array used to hold the units and their operators. + + let uArray = []; // Continue if we didn't hit a problem + + if (!endProcessing) { + uStr = parensResp[0]; + origString = parensResp[1]; // Call _makeUnitsArray to convert the string to an array of unit + // descriptors with operators. + + let mkUArray = this._makeUnitsArray(uStr, origString); + + endProcessing = mkUArray[2]; + + if (!endProcessing) { + uArray = mkUArray[0]; + origString = mkUArray[1]; // Create a unit object out of each un element + + let uLen = uArray.length; + + for (let u1 = 0; u1 < uLen; u1++) { + //for (let u1 = 0; u1 < uLen && !endProcessing; u1++) { + let curCode = uArray[u1]['un']; // Determine the type of the "un" attribute of the current array element + // Check to see if it's a number. If so write the number version of + // the number back to the "un" attribute and move on + + if (intUtils_.isIntegerUnit(curCode)) { + uArray[u1]['un'] = Number(curCode); + } else { + // The current unit array element is a string. Check now to see + // if it is or contains a parenthesized unit from this.parensUnits_. + // If so, call _getParens to process the string and get the unit. + if (curCode.indexOf(this.parensFlag_) >= 0) { + let parenUnit = this._getParensUnit(curCode, origString); // if we couldn't process the string, set the end flag and bypass + // further processing. + + + if (!endProcessing) endProcessing = parenUnit[1]; // If we're good, put the unit in the uArray and replace the + // curCode, which contains the parentheses placeholders, etc., + // with the unit's code - including any substitutions. + + if (!endProcessing) { + uArray[u1]['un'] = parenUnit[0]; + } + } // end if the curCode contains a parenthesized unit + // Else it's not a parenthetical unit and not a number. Call + // _makeUnit to create a unit for it. + else { + let uRet = this._makeUnit(curCode, origString); // If we didn't get a unit, set the endProcessing flag. + + + if (uRet[0] === null) { + endProcessing = true; + } else { + uArray[u1]['un'] = uRet[0]; + origString = uRet[1]; + } + } // end if the curCode is not a parenthetical expression + + } // end if the "un" array is a not a number + + } // end do for each element in the units array + + } // end if _makeUnitsArray did not return an error + + } // end if _processParens did not find an error that causes a stop + // If we're still good, continue + + + if (!endProcessing) { + // Process the units (and numbers) to create one final unit object + if ((uArray[0] === null || uArray[0] === ' ' || uArray[0]['un'] === undefined || uArray[0]['un'] === null) && this.retMsg_.length === 0) { + // not sure what this might be, but this is a safeguard + this.retMsg_.push(`Unit string (${origString}) did not contain ` + `anything that could be used to create a unit, or else something ` + `that is not handled yet by this package. Sorry`); + endProcessing = true; + } + } + + if (!endProcessing) { + finalUnit = this._performUnitArithmetic(uArray, origString); + } + + return [finalUnit, origString]; + } // end _parseTheString + + /** + * Extracts all annotations from a unit string, replacing them with + * placeholders for later evaluation. The annotations are stored in the + * this.annotations_ array. This should only be called from within this + * class (or by test code). + * + * @param uString the unit string being parsed + * @returns the string after the annotations are replaced with placeholders + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + * the this.annotations_ array is populated by this method + */ + + + _getAnnotations(uString) { + let openBrace = uString.indexOf('{'); + + while (openBrace >= 0) { + let closeBrace = uString.indexOf('}'); + + if (closeBrace < 0) { + this.retMsg_.push('Missing closing brace for annotation starting at ' + this.openEmph_ + uString.substr(openBrace) + this.closeEmph_); + openBrace = -1; + } else { + let braceStr = uString.substring(openBrace, closeBrace + 1); // Check for valid characters in the annotation. + + if (!UnitString.VALID_ANNOTATION_REGEX.test(braceStr)) { + this.retMsg_.push(UnitString.INVALID_ANNOTATION_CHAR_MSG + this.openEmph_ + braceStr + this.closeEmph_); + openBrace = -1; // end search for annotations + } else { + let aIdx = this.annotations_.length.toString(); + uString = uString.replace(braceStr, this.braceFlag_ + aIdx + this.braceFlag_); + this.annotations_.push(braceStr); + openBrace = uString.indexOf('{'); + } + } + } // end do while we have an opening brace + // check for a stray/unmatched closing brace + + + if (this.retMsg_.length == 0) { + // if there were no other errors above + let closeBrace = uString.indexOf('}'); + if (closeBrace >= 0) this.retMsg_.push('Missing opening brace for closing brace found at ' + this.openEmph_ + uString.substring(0, closeBrace + 1) + this.closeEmph_); + } + + return uString; + } // end _getAnnotations + + /** + * Finds and processes any/all parenthesized unit strings. This should only + * be called from within this class (or by test code). + * + * Nested parenthesized strings are processed from the inside out. The + * parseString function is called from within this one for each parenthesized + * unit string, and the resulting unit object is stored in this.parensUnits_, + * to be processed after all strings are translated to units. + * + * A placeholder is placed in the unit string returned to indicate that the + * unit object should be obtained from the this.parensUnits_ array. The + * placeholder consists of the parenthesis flag (this.parensFlag_) followed + * by the index of the unit in this.parensUnits_ followed by this.parensFlag_. + * + * @param uString the unit string being parsed, where this will be the full + * string the first time this is called and parenthesized strings on any + * subsequent calls + * @param origString the original string first passed in to parseString + * @returns + * an array containing: + * the string after the parentheses are replaced; + * the original string; and + * a boolean flag indicating whether or not an error occurred that + * should stop processing. + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + * this this.parensUnits_ array will be populated with units found for + * parenthetical unit strings + */ + + + _processParens(uString, origString) { + // Unit strings array and index + let uStrArray = []; + let uStrAryPos = 0; + let stopProcessing = false; + let pu = this.parensUnits_.length; // Count of characters trimmed off the beginning of the unit string (uString) + // as units are removed from it; used for error messages to provide + // context. + + let trimmedCt = 0; // Break the unit string into pieces that consist of text outside of + // parenthetical strings and placeholders for the parenthetical units. + // This method is called recursively for parenthetical strings and the units + // returned are stored in the this.parensUnits_ array. + + while (uString !== "" && !stopProcessing) { + let openCt = 0; + let closeCt = 0; + let openPos = uString.indexOf('('); // If an opening parenthesis was not found, check for an unmatched + // close parenthesis. If one was found report the error and end + // processing. + + if (openPos < 0) { + let closePos = uString.indexOf(')'); + + if (closePos >= 0) { + let theMsg = `Missing open parenthesis for close ` + `parenthesis at ${uString.substring(0, closePos + trimmedCt)}` + `${this.openEmph_}${uString.substr(closePos, 1)}${this.closeEmph_}`; + + if (closePos < uString.length - 1) { + theMsg += `${uString.substr(closePos + 1)}`; + } + + this.retMsg_.push(theMsg); + uStrArray[uStrAryPos] = uString; + stopProcessing = true; + } // end if a close parenthesis was found + // If no parentheses were found in the current unit string, transfer + // it to the units array and blank out the string, which will end + // the search for parenthetical units. + else { + uStrArray[uStrAryPos] = uString; + uString = ""; + } // end if no close parenthesis was found + + } // end if no open parenthesis was found + // Otherwise an open parenthesis was found. Process the string that + // includes the parenthetical group + else { + openCt += 1; // Write the text before the parentheses (if any) to the unit strings array + + let uLen = uString.length; + + if (openPos > 0) { + uStrArray[uStrAryPos++] = uString.substr(0, openPos); + } // Find the matching closePos, i.e., the one that closes the + // parenthetical group that this one opens. Look also for + // another open parenthesis, in case this includes nested parenthetical + // strings. This continues until it finds the same number of close + // parentheses as open parentheses, or runs out of string to check. + // In the case of nested parentheses this will identify the outer set + // of parentheses. + + + let closePos = 0; + let c = openPos + 1; + + for (; c < uLen && openCt != closeCt; c++) { + if (uString[c] === '(') openCt += 1;else if (uString[c] === ')') closeCt += 1; + } // Put a placeholder for the group in the unit strings array and recursively + // call this method for the parenthetical group. Put the unit returned + // in this.parensUnits_. Set the unit string to whatever follows + // the position of the closing parenthesis for this group, to be + // processed by the next iteration of this loop. If there's nothing + // left uString is set to "". + + + if (openCt === closeCt) { + closePos = c; + uStrArray[uStrAryPos++] = this.parensFlag_ + pu.toString() + this.parensFlag_; + + let parseResp = this._parseTheString(uString.substring(openPos + 1, closePos - 1), origString); + + if (parseResp[0] === null) stopProcessing = true;else { + origString = parseResp[1]; + this.parensUnits_[pu++] = parseResp[0]; + uString = uString.substr(closePos); + trimmedCt = closePos; + } + } // end if the number of open and close parentheses matched + // If the number of open and close parentheses doesn't match, indicate + // an error. + else { + uStrArray.push(origString.substr(openPos)); + this.retMsg_.push(`Missing close parenthesis for open parenthesis at ` + `${origString.substring(0, openPos + trimmedCt)}` + `${this.openEmph_}${origString.substr(openPos, 1)}` + `${this.closeEmph_}${origString.substr(openPos + 1)}`); + stopProcessing = true; + } + } // end if an open parenthesis was found + + } // end do while the input string is not empty + + + if (stopProcessing) this.parensUnits_ = []; + return [uStrArray.join(''), origString, stopProcessing]; + } // end _processParens + + /** + * Breaks the unit string into an array of unit descriptors and operators. + * If a unit descriptor consists of a number preceding a unit code, with + * no multiplication operator, e.g., 2mg instead of 2.mg, it is handled + * as if it were a parenthetical expression. + * + * This should only be called from within this class (or by test code). + * + * @param uStr the unit string being parsed + * @param origString the original string passed to parseString + * @returns + * an array containing: + * the array representing the unit string; + * the original string passed in, possibly updated with corrections; and + * and a flag indicating whether or not processing can continue. + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + */ + + + _makeUnitsArray(uStr, origString) { + // Separate the string into pieces based on delimiters / (division) and . + // (multiplication). The idea is to get an array of units on which we + // can then perform any operations (prefixes, multiplication, division). + let uArray1 = uStr.match(/([./]|[^./]+)/g); + let endProcessing = false; + let uArray = []; + let startNumCheck = /(^[0-9]+)(\[?[a-zA-Z\_0-9a-zA-Z\_]+\]?$)/; // If the first element in the array is the division operator (/), the + // string started with '/'. Add a first element containing 1 to the + // array, which will cause the correct computation to be performed (inversion). + + if (uArray1[0] === "/") { + uArray1.unshift("1"); + } // If the first element in the array is the multiplication operator (.) + // return an error. + else if (uArray1[0] === '.') { + this.retMsg_.push(`${origString} is not a valid UCUM code. ` + `The multiplication operator at the beginning of the expression is ` + `not valid. A multiplication operator must appear only between ` + `two codes.`); + endProcessing = true; + } + + if (!endProcessing) { + // Check to see if there is a number preceding a unit code, e.g., 2mg + // If so, update the first element to remove the number (2mg -> mg) and + // add two elements to the beginning of the array - the number and the + // multiplication operator. + if (!intUtils_.isNumericString(uArray1[0])) { + let numRes = uArray1[0].match(startNumCheck); + + if (numRes && numRes.length === 3 && numRes[1] !== '' && numRes[2] !== '' && numRes[2].indexOf(this.braceFlag_) !== 0) { + let dispVal = numRes[2]; + + if (!endProcessing && numRes[2].indexOf(this.parensFlag_) !== -1) { + let parensback = this._getParensUnit(numRes[2], origString); + + numRes[2] = parensback[0]['csCode_']; + dispVal = `(${numRes[2]})`; + endProcessing = parensback[1]; + } + + if (!endProcessing) { + this.retMsg_.push(`${numRes[1]}${dispVal} is not a valid UCUM code.` + ` ${this.vcMsgStart_}${numRes[1]}.${dispVal}${this.vcMsgEnd_}`); + origString = origString.replace(`${numRes[1]}${dispVal}`, `${numRes[1]}.${dispVal}`); + uArray1[0] = numRes[2]; + uArray1.unshift(numRes[1], '.'); + } + } + } // end if the first element is not a number (only) + // Create an array of unit/operator objects. The unit is, for now, the + // string containing the unit code (e.g., Hz for hertz) including + // a possible prefix and exponent. The operator is the operator to be + // applied to that unit and the one preceding it. So, a.b would give + // us two objects. The first will have a unit of a, and a blank operator + // (because it's the first unit). The second would have a unit of b + // and the multiplication operator (.). + + + if (!endProcessing) { + let u1 = uArray1.length; + uArray = [{ + op: "", + un: uArray1[0] + }]; + + for (let n = 1; n < u1; n++) { + // check to make sure that we don't have two operators together, e.g., + // mg./K. If so, let the user know the problem. + let theOp = uArray1[n++]; // oh wait - check to make sure something is even there, that the + // user didn't end the expression with an operator. + + if (!uArray1[n]) { + this.retMsg_.push(`${origString} is not a valid UCUM code. ` + `It is terminated with the operator ${this.openEmph_}` + `${theOp}${this.closeEmph_}.`); + n = u1; + endProcessing = true; + } else if (Ucum.validOps_.indexOf(uArray1[n]) !== -1) { + this.retMsg_.push(`${origString} is not a valid UCUM code. ` + `A unit code is missing between${this.openEmph_}` + `${theOp}${this.closeEmph_}and${this.openEmph_}` + `${uArray1[n]}${this.closeEmph_}in${this.openEmph_}` + `${theOp}${uArray1[n]}${this.closeEmph_}.`); + n = u1; + endProcessing = true; + } else { + // Check to see if a number precedes a unit code. + // If so, send the element to _processParens, inserting the multiplication + // operator where it belongs. Treating it as parenthetical keeps it from + // being interpreted incorrectly because of operator parentheses. For + // example, if the whole string is mg/2kJ we don't want to rewrite it as + // mg/2.kJ - because mg/2 would be performed, followed by .kJ. Instead, + // handling 2kJ as a parenthesized unit will make sure mg is divided by + // 2.kJ. + if (!intUtils_.isNumericString(uArray1[n])) { + let numRes2 = uArray1[n].match(startNumCheck); + + if (numRes2 && numRes2.length === 3 && numRes2[1] !== '' && numRes2[2] !== '' && numRes2[2].indexOf(this.braceFlag_) !== 0) { + let invalidString = numRes2[0]; + + if (!endProcessing && numRes2[2].indexOf(this.parensFlag_) !== -1) { + let parensback = this._getParensUnit(numRes2[2], origString); + + numRes2[2] = parensback[0]['csCode_']; + invalidString = `(${numRes2[2]})`; + endProcessing = parensback[1]; + + if (!endProcessing) { + this.retMsg_.push(`${numRes2[1]}${invalidString} is not a ` + `valid UCUM code. ${this.vcMsgStart_}${numRes2[1]}.${invalidString}` + `${this.vcMsgEnd_}`); + let parensString = `(${numRes2[1]}.${invalidString})`; + origString = origString.replace(`${numRes2[1]}${invalidString}`, parensString); + + let nextParens = this._processParens(parensString, origString); + + endProcessing = nextParens[2]; + + if (!endProcessing) { + uArray.push({ + op: theOp, + un: nextParens[0] + }); + } //uArray.push({op: '.', un: numRes2[2]}); + + } + } // end if the string represents a parenthesized unit + else { + let parensStr = '(' + numRes2[1] + '.' + numRes2[2] + ')'; + + let parensResp = this._processParens(parensStr, origString); // if a "stop processing" flag was returned, set the n index to end + // the loop and set the endProcessing flag + + + if (parensResp[2]) { + n = u1; + endProcessing = true; + } else { + this.retMsg_.push(`${numRes2[0]} is not a ` + `valid UCUM code. ${this.vcMsgStart_}${numRes2[1]}.${numRes2[2]}` + `${this.vcMsgEnd_}`); + origString = origString.replace(numRes2[0], parensStr); + uArray.push({ + op: theOp, + un: parensResp[0] + }); + } // end if no error on the processParens call + + } // end if the string does not represent a parenthesized unit + + } // end if the string is a number followed by a string + else { + uArray.push({ + op: theOp, + un: uArray1[n] + }); + } + } else { + uArray.push({ + op: theOp, + un: uArray1[n] + }); + } + } // end if there isn't a missing operator or unit code + + } // end do for each element in uArray1 + + } // end if a processing error didn't occur in getParensUnit + + } // end if the string did not begin with a '.' with no following digit + + + return [uArray, origString, endProcessing]; + } // end _makeUnitsArray + + /** + * Takes a unit string containing parentheses flags and returns the unit they + * represent. Any text found before and/or after the parenthetical + * expression is checked to see if we can tell what the user meant and + * let them know what it should have been. For example, 2(mg), which + * would resolve to 2mg, should be 2.mg. + * + * This should only be called from within this class (or by test code). + * + * @param pStr the string being parsed + * @param origString the original unit string passed in; passed through + * to _getAnnonText if annotation flags are found in any text preceding + * or following the parenthetical unit + * @returns + * an array containing + * the unit object; and + * a flag indicating whether or not processing should be ended. + * True indicates that the string was invalid and no corrections + * (substitutions or suggestions) could be found; + * False indicates that it was either valid or substitutions/suggestions + * were made. + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + * this this.parensUnits_ array contains the units that are acquired by + * this method + * @throws an error if an invalid parensUnit index was found. This is + * a processing error. + */ + + + _getParensUnit(pStr, origString) { + let endProcessing = false; + let retAry = []; + let retUnit = null; + let befAnnoText = null; + let aftAnnoText = null; // Get the location of the flags. We're assuming there are only two + // because _processParens takes care of nesting. By the time we get + // here we should not be looking a nested parens. Also get any text + // before and after the parentheses. Once we get the unit we update + // the input string with the unit's csCode_, which will wipe out any + // before and after text + + let psIdx = pStr.indexOf(this.parensFlag_); + let befText = null; + + if (psIdx > 0) { + befText = pStr.substr(0, psIdx - 1); + } + + let peIdx = pStr.lastIndexOf(this.parensFlag_); + let aftText = null; + + if (peIdx + this.pFlagLen_ < pStr.length) { + aftText = pStr.substr(peIdx + this.pFlagLen_); + } // Get the text between the flags + + + let pNumText = pStr.substring(psIdx + this.pFlagLen_, peIdx); // Make sure the index is a number, and if it is, get the unit from the + // this.parensUnits_ array + + if (intUtils_.isNumericString(pNumText)) { + retUnit = this.parensUnits_[Number(pNumText)]; + + if (!intUtils_.isIntegerUnit(retUnit)) { + pStr = retUnit.csCode_; + } else { + pStr = retUnit; + } + } // If it's not a number, it's a programming error. Throw a fit. + else { + throw new Error(`Processing error - invalid parens number ${pNumText} ` + `found in ${pStr}.`); + } // If there's something in front of the starting parentheses flag, check to + // see if it's a number or an annotation. + + + if (befText) { + // If it's a number, assume that multiplication was assumed + if (intUtils_.isNumericString(befText)) { + let nMag = retUnit.getProperty('magnitude_'); + nMag *= Number(befText); + retUnit.assignVals({ + 'magnitude_': nMag + }); + pStr = `${befText}.${pStr}`; + this.retMsg_.push(`${befText}${pStr} is not a valid UCUM code.\n` + this.vcMsgStart_ + pStr + this.vcMsgEnd_); + } else { + if (befText.indexOf(this.braceFlag_) >= 0) { + let annoRet = this._getAnnoText(befText, origString); // if we found not only an annotation, but text before or after + // the annotation (remembering that this is all before the + // parentheses) throw an error - because we don't know what + // to do with it. Could it be missing an operator? + + + if (annoRet[1] || annoRet[2]) { + throw new Error(`Text found before the parentheses (` + `${befText}) included an annotation along with other text ` + `for parenthetical unit ${retUnit.csCode_}`); + } // Otherwise put the annotation after the unit string and note + // the misplacement. + + + pStr += annoRet[0]; + this.retMsg_.push(`The annotation ${annoRet[0]} before the unit ` + `code is invalid.\n` + this.vcMsgStart_ + pStr + this.vcMsgEnd_); + } // else the text before the parentheses is neither a number nor + // an annotation. If suggestions were NOT requested, record an + // error. + else if (!this.suggestions_) { + this.retMsg_.push(`${befText} preceding the unit code ${pStr} ` + `is invalid. Unable to make a substitution.`); + endProcessing = true; + } // otherwise try for suggestions + else { + let suggestStat = this._getSuggestions(befText); + + endProcessing = suggestStat !== 'succeeded'; + } // end if a brace was found or, if not, suggestions were not or + // were requested + + } // end if text preceding the parentheses was not a number + + } // end if there was text before the parentheses + // Process any text after the parentheses + + + if (aftText) { + // if it's an annotation, get it and add it to the pStr + if (aftText.indexOf(this.braceFlag_) >= 0) { + let annoRet = this._getAnnoText(aftText, origString); // if we found not only an annotation, but text before or after + // the annotation (remembering that this is all after the + // parentheses) throw an error - because we don't know what + // to do with it. Could it be missing an operator? + + + if (annoRet[1] || annoRet[2]) { + throw new Error(`Text found after the parentheses (` + `${aftText}) included an annotation along with other text ` + `for parenthetical unit ${retUnit.csCode_}`); + } // Otherwise put the annotation after the unit string - no message + // needed. + + + pStr += annoRet[0]; + } // Otherwise check to see if it's an exponent. If so, warn the + // user that it's not valid - but try it anyway + else { + if (intUtils_.isNumericString(aftText)) { + pStr += aftText; + retUnit = retUnit.power(Number(aftText)); + this.retMsg_.push(`An exponent (${aftText}) following a parenthesis ` + `is invalid as of revision 1.9 of the UCUM Specification.\n ` + this.vcMsgStart_ + pStr + this.vcMsgEnd_); + } // else the text after the parentheses is neither a number nor + // an annotation. If suggestions were NOT requested, record an + // error. + else if (!this.suggestions_) { + this.retMsg_.push(`Text ${aftText} following the unit code ${pStr} ` + `is invalid. Unable to make a substitution.`); + endProcessing = true; + } // otherwise try for suggestions + else { + let suggestStat = this._getSuggestions(befText); + + endProcessing = suggestStat !== 'succeeded'; + } // end if text following the parentheses not an exponent + + } // end if text following the parentheses is not an annotation + + } // end if there is text following the parentheses + + + if (!endProcessing) { + if (!retUnit) { + retUnit = new Unit({ + 'csCode_': pStr, + 'magnitude_': 1, + 'name_': pStr + }); + } else if (intUtils_.isIntegerUnit(retUnit)) { + retUnit = new Unit({ + 'csCode_': retUnit, + 'magnitude_': retUnit, + 'name_': retUnit + }); + } else { + retUnit.csCode_ = pStr; + } + } + + return [retUnit, endProcessing]; + } // end _getParensUnit + + /** + * Takes a unit string containing annotation flags and returns the + * annotation they represent. This also returns any text found before + * the annotation and any found after the annotation. + * + * This should only be called from within this class (or by test code). + * NEEDS FIX in next branch to handle string with multiple annotations. + * + * @param pStr the string being parsed + * @param origString the original string being parsed; used in error msg + * thrown for an invalid index to the annotations array + * @returns + * an array containing + * the annotation for the pStr; + * any text found before the annotation; and + * any text found after the annotation. + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + * the this.annotations_ array is used as the source for the annotations text + * @throws an error if for a processing error - an invalid annotation index. + */ + + + _getAnnoText(pStr, origString) { + // if the starting braces flag is not at index 0, get the starting + // text and the adjust the pStr to omit it. + let asIdx = pStr.indexOf(this.braceFlag_); + let startText = asIdx > 0 ? pStr.substring(0, asIdx) : null; + + if (asIdx !== 0) { + pStr = pStr.substr(asIdx); + } // Get the location of the end flag and, if text follows it, get the text + + + let aeIdx = pStr.indexOf(this.braceFlag_, 1); + let endText = aeIdx + this.bFlagLen_ < pStr.length ? pStr.substr(aeIdx + this.bFlagLen_) : null; // Get the index of the annotation in this.annotations_. + // Check it to make sure it's valid, and if not, throw an error + + let idx = pStr.substring(this.bFlagLen_, aeIdx); + let idxNum = Number(idx); + + if (!intUtils_.isNumericString(idx) || idxNum >= this.annotations_.length) { + throw new Error(`Processing Error - invalid annotation index ${idx} found ` + `in ${pStr} that was created from ${origString}`); + } // Replace the flags and annotation index with the annotation expression + + + pStr = this.annotations_[idxNum]; + return [pStr, startText, endText]; + } // end _getAnnoText + + /** + * Takes a unit string and looks for suggested units. This should be + * called for unit strings that cannot be resolved to unit codes. The + * string is searched for in the synonyms table found in the UnitTables + * class. That table includes all synonyms and unit names for the units + * in the unit data table. + * + * @param pStr the string being parsed + * @returns an object that contains an element named 'status', whose + * value indicates the status of the request: + * 'succeeded' indicates that synonyms were found; + * 'failed' indicates that no synonyms were found; or + * 'error' which indicates that an error occurred + * + * the this.retMsg_ array will be updated with a message indicating whether + * or not synonyms/suggestions were found + * the this.suggestions_ array will be updated with a hash (added to the + * array if it already contains others) that contains three elements: + * 'msg' which is a message indicating what unit expression the + * suggestions are for; + * 'invalidUnit' which is the unit expression the suggestions are for; and + * 'units' which is an array of data for each suggested unit found. + * Each array will contain the unit code, the unit name and the + * unit guidance (if any). + */ + + + _getSuggestions(pStr) { + let retObj = intUtils_.getSynonyms(pStr); + + if (retObj['status'] === 'succeeded') { + let suggSet = {}; + suggSet['msg'] = `${pStr} is not a valid UCUM code. We found possible ` + `units that might be what was meant:`; + suggSet['invalidUnit'] = pStr; + let synLen = retObj['units'].length; + suggSet['units'] = []; + + for (let s = 0; s < synLen; s++) { + let unit = retObj['units'][s]; + let unitArray = [unit['code'], unit['name'], unit['guidance']]; + suggSet['units'].push(unitArray); + } + + this.suggestions_.push(suggSet); + } else { + this.retMsg_.push(`${pStr} is not a valid UCUM code. No alternatives ` + `were found.`); + } + + return retObj['status']; + } // end getSuggestions + + /** + * Creates a unit object from a string defining one unit. The string + * should consist of a unit code for a unit already defined (base or + * otherwise). It may include a prefix and an exponent, e.g., cm2 + * (centimeter squared). This should only be called from within this + * class (or by test code). + * + * @params uCode the string defining the unit + * @param origString the original string to be parsed; used to provide + * context for messages + * @returns + * an array containing: + * a unit object, or null if there were problems creating the unit; and + * the origString passed in, which may be updated if a unit name was + * translated to a unit code. + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + * the this.suggestions_ array will be populated if no unit (with or without + * substitutions) could be found and suggestions were requested + */ + + + _makeUnit(uCode, origString) { + // First try the code just as is, without looking for annotations, + // prefixes, exponents, or elephants. + let retUnit = this.utabs_.getUnitByCode(uCode); + + if (retUnit) { + retUnit = retUnit.clone(); + } // If we found it, we're done. No need to parse for those elephants (or + // other stuff). + else if (uCode.indexOf(this.braceFlag_) >= 0) { + let getAnnoRet = this._getUnitWithAnnotation(uCode, origString); + + retUnit = getAnnoRet[0]; + + if (retUnit) { + origString = getAnnoRet[1]; + } // If a unit is not found, retUnit will be returned null and + // the this.retMsg_ array will contain a message describing the problem. + // If a unit is found, of course, all is good. So ... nothing left + // to see here, move along. + + } // end if the uCode includes an annotation + else { + // So we didn't find a unit for the full uCode or for one with + // annotations. Try looking for a unit that uses a carat (^) + // instead of an asterisk (*) + if (uCode.indexOf('^') > -1) { + let tryCode = uCode.replace('^', '*'); + retUnit = this.utabs_.getUnitByCode(tryCode); + + if (retUnit) { + retUnit = retUnit.clone(); + retUnit.csCode_ = retUnit.csCode_.replace('*', '^'); + retUnit.ciCode_ = retUnit.ciCode_.replace('*', '^'); + } + } // If that didn't work, check to see if it should have brackets + // around it (uCode = degF when it should be [degF] + + + if (!retUnit) { + let addBrackets = '[' + uCode + ']'; + retUnit = this.utabs_.getUnitByCode(addBrackets); + + if (retUnit) { + retUnit = retUnit.clone(); + origString = origString.replace(uCode, addBrackets); + this.retMsg_.push(`${uCode} is not a valid unit expression, but ` + `${addBrackets} is.\n` + this.vcMsgStart_ + `${addBrackets} (${retUnit.name_})${this.vcMsgEnd_}`); + } // end if we found the unit after adding brackets + + } // end trying to add brackets + // If we didn't find it, try it as a name + + + if (!retUnit) { + let retUnitAry = this.utabs_.getUnitByName(uCode); + + if (retUnitAry && retUnitAry.length > 0) { + retUnit = retUnitAry[0].clone(); + let mString = 'The UCUM code for ' + uCode + ' is ' + retUnit.csCode_ + '.\n' + this.vcMsgStart_ + retUnit.csCode_ + this.vcMsgEnd_; + let dupMsg = false; + + for (let r = 0; r < this.retMsg_.length && !dupMsg; r++) dupMsg = this.retMsg_[r] === mString; + + if (!dupMsg) this.retMsg_.push(mString); + let rStr = new RegExp('(^|[.\/({])(' + uCode + ')($|[.\/)}])'); + let res = origString.match(rStr); + origString = origString.replace(rStr, res[1] + retUnit.csCode_ + res[3]); + uCode = retUnit.csCode_; + } + } // If we still don't have a unit, try assuming a modifier (prefix and/or + // exponent) and look for a unit without the modifier + + + if (!retUnit) { + // Well, first see if it's one of the special units. If so, + // replace the placeholder text with the actual unit string, keeping + // whatever text (probably a prefix) goes with the unit string. + let sUnit = null; + + for (sUnit in Ucum.specUnits_) { + if (uCode.indexOf(Ucum.specUnits_[sUnit]) !== -1) uCode = uCode.replace(Ucum.specUnits_[sUnit], sUnit); + } + + retUnit = this.utabs_.getUnitByCode(uCode); + if (retUnit) retUnit = retUnit.clone(); + } + + if (!retUnit) { + let origCode = uCode; + let origUnit = null; + let exp = null; + let pfxCode = null; + let pfxObj = null; + let pfxVal = null; + let pfxExp = null; // Look first for an exponent. If we got one, separate it out and + // try to get the unit again + + let codeAndExp = this._isCodeWithExponent(uCode); + + if (codeAndExp) { + uCode = codeAndExp[0]; + exp = codeAndExp[1]; + origUnit = this.utabs_.getUnitByCode(uCode); + } // If we still don't have a unit, separate out the prefix, if any, + // and try without it. + + + if (!origUnit) { + // Try for a single character prefix first. + pfxCode = uCode.charAt(0); + pfxObj = this.pfxTabs_.getPrefixByCode(pfxCode); // if we got a prefix, get its info and remove it from the unit code + + if (pfxObj) { + pfxVal = pfxObj.getValue(); + pfxExp = pfxObj.getExp(); + let pCodeLen = pfxCode.length; + uCode = uCode.substr(pCodeLen); // try again for the unit + + origUnit = this.utabs_.getUnitByCode(uCode); // If we still don't have a unit, see if the prefix could be the + // two character "da" (deka) prefix. That's the only prefix with + // two characters, and without this check it's interpreted as "d" + // (deci) and the "a" is considered part of the unit code. + + if (!origUnit && pfxCode == 'd' && uCode.substr(0, 1) == 'a') { + pfxCode = 'da'; + pfxObj = this.pfxTabs_.getPrefixByCode(pfxCode); + pfxVal = pfxObj.getValue(); + uCode = uCode.substr(1); // try one more time for the unit + + origUnit = this.utabs_.getUnitByCode(uCode); + } // Reject the unit we found if it might have another prefix. + // Such things are in our tables through the LOINC source_ + // (ucum.csv) which has guidance and synonyms. I think it should be + // safe to exclude anything whose source is LOINC from having a + // prefix. + + + if (origUnit && origUnit.source_ == 'LOINC') origUnit = null; + } // end if we found a prefix + + } // end if we didn't get a unit after removing an exponent + // If we still haven't found anything, we're done looking. + // (We tried with the full unit string, with the unit string + // without the exponent, the unit string without a prefix, + // common errors, etc. That's all we can try). + + + if (!origUnit) { + retUnit = null; // BUT if the user asked for suggestions, at least look for them + + if (this.suggestions_) { + let suggestStat = this._getSuggestions(origCode); + } else { + this.retMsg_.push(`${origCode} is not a valid UCUM code.`); + } + } else { + // Otherwise we found a unit object. Clone it and then apply the + // prefix and exponent, if any, to it. And remove the guidance. + retUnit = origUnit.clone(); // If we are here, this is only part of the full unit string, so it is + // not a base unit, and the synonyms will mostly likely not be correct for the full + // string. + + retUnit.resetFieldsForDerivedUnit(); + let theDim = retUnit.getProperty('dim_'); + let theMag = retUnit.getProperty('magnitude_'); + let theName = retUnit.getProperty('name_'); + let theCiCode = retUnit.getProperty('ciCode_'); + let thePrintSymbol = retUnit.getProperty('printSymbol_'); // If there is an exponent for the unit, apply it to the dimension + // and magnitude now + + if (exp) { + exp = parseInt(exp); + let expMul = exp; + if (theDim) theDim = theDim.mul(exp); + theMag = Math.pow(theMag, exp); + retUnit.assignVals({ + 'magnitude_': theMag + }); // If there is also a prefix, apply the exponent to the prefix. + + if (pfxObj) { + // if the prefix base is 10 it will have an exponent. Multiply + // the current prefix exponent by the exponent for the unit + // we're working with. Then raise the prefix value to the level + // defined by the exponent. + if (pfxExp) { + expMul *= pfxObj.getExp(); + pfxVal = Math.pow(10, expMul); + } // If the prefix base is not 10, it won't have an exponent. + // At the moment I don't see any units using the prefixes + // that aren't base 10. But if we get one the prefix value + // will be applied to the magnitude (below) if the unit does + // not have a conversion function, and to the conversion prefix + // if it does. + + } // end if there's a prefix as well as the exponent + + } // end if there's an exponent + // Now apply the prefix, if there is one, to the conversion + // prefix or the magnitude + + + if (pfxObj) { + if (retUnit.cnv_) { + retUnit.assignVals({ + 'cnvPfx_': pfxVal + }); + } else { + theMag *= pfxVal; + retUnit.assignVals({ + 'magnitude_': theMag + }); + } + } // if we have a prefix and/or an exponent, add them to the unit + // attributes - name, csCode, ciCode and print symbol + + + let theCode = retUnit.csCode_; + + if (pfxObj) { + theName = pfxObj.getName() + theName; + theCode = pfxCode + theCode; + theCiCode = pfxObj.getCiCode() + theCiCode; + thePrintSymbol = pfxObj.getPrintSymbol() + thePrintSymbol; + retUnit.assignVals({ + 'name_': theName, + 'csCode_': theCode, + 'ciCode_': theCiCode, + 'printSymbol_': thePrintSymbol + }); + } + + if (exp) { + let expStr = exp.toString(); + retUnit.assignVals({ + 'name_': theName + '' + expStr + '', + 'csCode_': theCode + expStr, + 'ciCode_': theCiCode + expStr, + 'printSymbol_': thePrintSymbol + '' + expStr + '' + }); + } + } // end if an original unit was found (without prefix and/or exponent) + + } // end if we didn't get a unit for the full unit code (w/out modifiers) + + } // end if we didn't find the unit on the first try, before parsing + + + return [retUnit, origString]; + } // end _makeUnit + + /** + * This method handles unit creation when an annotation is included + * in the unit string. This basically isolates and retrieves the + * annotation and then calls _makeUnit to try to get a unit from + * any text that precedes or follows the annotation. + * + * @param uCode the string defining the unit + * @param origString the original full string submitted to parseString + * @returns the unit object found, or null if one could not be found + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + */ + + + _getUnitWithAnnotation(uCode, origString) { + let retUnit = null; // Get the annotation and anything that precedes or follows it. + + let annoRet = this._getAnnoText(uCode, origString); + + let annoText = annoRet[0]; + let befAnnoText = annoRet[1]; + let aftAnnoText = annoRet[2]; // Add the warning about annotations - just once. + + if (this.bracesMsg_ && this.retMsg_.indexOf(this.bracesMsg_) === -1) this.retMsg_.push(this.bracesMsg_); // If there's no text before or after the annotation, it's probably + // something that should be interpreted as a 1, e.g., {KCT'U}. + // HOWEVER, it could also be a case where someone used braces instead + // of brackets, e.g., {degF} instead of [degF]. Check for that before + // we assume it should be a 1. + + let msgLen = this.retMsg_.length; + + if (!befAnnoText && !aftAnnoText) { + let tryBrackets = '[' + annoText.substring(1, annoText.length - 1) + ']'; + + let mkUnitRet = this._makeUnit(tryBrackets, origString); // If we got back a unit, assign it to the returned unit, and add + // a message to advise the user that brackets should enclose the code + + + if (mkUnitRet[0]) { + retUnit = mkUnitRet[0]; + origString = origString.replace(annoText, tryBrackets); + this.retMsg_.push(`${annoText} is not a valid unit expression, but ` + `${tryBrackets} is.\n` + this.vcMsgStart_ + `${tryBrackets} (${retUnit.name_})${this.vcMsgEnd_}`); + } // Otherwise assume that this should be interpreted as a 1 + else { + // remove error message generated for trybrackets + if (this.retMsg_.length > msgLen) { + this.retMsg_.pop(); + } + + uCode = 1; + retUnit = 1; + } + } // end if it's only an annotation + else { + // if there's text before and no text after, assume the text before + // the annotation is the unit code (with an annotation following it). + // Call _makeUnit for the text before the annotation. + if (befAnnoText && !aftAnnoText) { + // make sure that what's before the annoText is not a number, e.g., + // /100{cells}. But f it is a number, just set the return unit to + // the number. + if (intUtils_.isIntegerUnit(befAnnoText)) { + retUnit = befAnnoText; + } // Otherwise try to find a unit + else { + let mkUnitRet = this._makeUnit(befAnnoText, origString); // if a unit was returned + + + if (mkUnitRet[0]) { + retUnit = mkUnitRet[0]; + retUnit.csCode_ += annoText; + origString = mkUnitRet[1]; + } // Otherwise add a not found message + else { + this.retMsg_.push(`Unable to find a unit for ${befAnnoText} that ` + `precedes the annotation ${annoText}.`); + } + } + } // else if there's only text after the annotation, try for a unit + // from the after text and assume the user put the annotation in + // the wrong place (and tell them) + else if (!befAnnoText && aftAnnoText) { + // Again, test for a number and if it is a number, set the return + // unit to the number. + if (intUtils_.isIntegerUnit(aftAnnoText)) { + retUnit = aftAnnoText + annoText; + this.retMsg_.push(`The annotation ${annoText} before the ``${aftAnnoText} is invalid.\n` + this.vcMsgStart_ + retUnit + this.vcMsgEnd_); + } else { + let mkUnitRet = this._makeUnit(aftAnnoText, origString); + + if (mkUnitRet[0]) { + retUnit = mkUnitRet[0]; + retUnit.csCode_ += annoText; + origString = retUnit.csCode_; + this.retMsg_.push(`The annotation ${annoText} before the unit ` + `code is invalid.\n` + this.vcMsgStart_ + retUnit.csCode_ + this.vcMsgEnd_); + } // Otherwise add a not found message + else { + this.retMsg_.push(`Unable to find a unit for ${befAnnoText} that ` + `follows the annotation ${annoText}.`); + } + } + } // else it's got text before AND after the annotation. Now what? + // For now this is an error. This may be a case of a missing + // operator but that is not handled yet. + else { + this.retMsg_.push(`Unable to find a unit for ${befAnnoText}${annoText}` + `${aftAnnoText}.\nWe are not sure how to interpret text both before ` + `and after the annotation. Sorry`); + } + } // else if there's text before/and or after the annotation + + + return [retUnit, origString]; + } // end _getUnitWithAnnotations + + /** + * Performs unit arithmetic for the units in the units array. That array + * contains units/numbers and the operators (division or multiplication) to + * be performed on each unit/unit or unit/number pair in the array. This + * should only be called from within this class (or by test code). + * + * @params uArray the array that contains the units, numbers and operators + * derived from the unit string passed in to parseString + * @param origString the original string to be parsed; used to provide + * context for messages + * + * @returns a single unit object that is the result of the unit arithmetic + * + * the this.retMsg_ array will be updated with any user messages + * (informational, error or warning) generated by this or called methods + */ + + + _performUnitArithmetic(uArray, origString) { + let finalUnit = uArray[0]['un']; + + if (intUtils_.isIntegerUnit(finalUnit)) { + finalUnit = new Unit({ + 'csCode_': finalUnit, + 'ciCode_': finalUnit, + 'magnitude_': Number(finalUnit), + 'name_': finalUnit + }); + } + + let uLen = uArray.length; + let endProcessing = false; // Perform the arithmetic for the units, starting with the first 2 units. + // We only need to do the arithmetic if we have more than one unit. + + for (let u2 = 1; u2 < uLen && !endProcessing; u2++) { + let nextUnit = uArray[u2]['un']; + + if (intUtils_.isIntegerUnit(nextUnit)) { + nextUnit = new Unit({ + 'csCode_': nextUnit, + 'ciCode_': nextUnit, + 'magnitude_': Number(nextUnit), + 'name_': nextUnit + }); + } + + if (nextUnit === null || typeof nextUnit !== 'number' && !nextUnit.getProperty) { + let msgString = `Unit string (${origString}) contains unrecognized ` + 'element'; + + if (nextUnit) { + msgString += ` (${this.openEmph_}${nextUnit.toString()}` + `${this.closeEmph_})`; + } + + msgString += '; could not parse full string. Sorry'; + this.retMsg_.push(msgString); + endProcessing = true; + } else { + try { + // Is the operation division? + let thisOp = uArray[u2]['op']; + let isDiv = thisOp === '/'; // Perform the operation. Both the finalUnit and nextUnit + // are unit objects. + + isDiv ? finalUnit = finalUnit.divide(nextUnit) : finalUnit = finalUnit.multiplyThese(nextUnit); + } catch (err) { + this.retMsg_.unshift(err.message); + endProcessing = true; + finalUnit = null; + } + } // end if we have another valid unit/number to process + + } // end do for each unit after the first one + + + return finalUnit; + } // end _performUnitArithmetic + + /** + * This tests a string to see if it starts with characters and ends with + * digits. This is used to test for an exponent on a UCUM code (or what + * we think might be a UCUM code). This is broken out to a separate + * function so that the regular expression can be verified to provide the + * results we expect, in case someone changes it. (Per Paul Lynch) + * See "Test _isCodeWithExponent method" in testUnitString.spec.js + * + * This particular regex has been tweaked several times. This one + * works with the following test strings: + * "m[H2O]-21 gives ["m[H2O]-21", "m[H2O]", "-21"] + * "m[H2O]+21 gives ["m[H2O]+21", "m[H2O]", "+21"] + * "m[H2O]21 gives ["m[H2O]-21", "m[H2O]", "21"] + * "s2" gives ["s2", "s, "2"] + * "kg" gives null + * "m[H2O]" gives null + * "m[H2O]23X" gives null + * + * @params uCode the code being tested + * @returns an array containing: (1) the code without the exponent (or + * trailing number); and (2) the exponent/trailing number. Returns null + * if there is no trailing number or something follows the trailing + * number, or if the first part is not characters. + */ + + + _isCodeWithExponent(uCode) { + let ret = []; + let res = uCode.match(/(^[^\-\+]+?)([\-\+\d]+)$/); // If we got a return with an exponent, separate the exponent from the + // unit and return both (as separate values) + + if (res && res[2] && res[2] !== "") { + ret.push(res[1]); + ret.push(res[2]); + } // end if we got an exponent + else { + ret = null; + } + + return ret; + } // end _isCodeWithExponent + + +} // end class UnitString + +/** + * This function exists ONLY until the original UnitString constructor + * is called for the first time. It's defined here in case getInstance + * is called before the constructor. This calls the constructor. + * + * The constructor redefines the getInstance function to return the + * singleton UnitString object. This is based on the UnitTables singleton + * implementation; see more detail in the UnitTables constructor description. + * + * @return the singleton UnitString object. + */ + + +exports.UnitString = UnitString; + +_defineProperty(UnitString, "INVALID_ANNOTATION_CHAR_MSG", 'An invalid character was found in the annotation '); + +// A regular expression for validating annotation strings. +_defineProperty(UnitString, "VALID_ANNOTATION_REGEX", /^\{[!-z|~]*\}$/); + +UnitString.getInstance = function () { + return new UnitString(); +}; +/* +// Perform the first request for the object, to set the getInstance method. +UnitString.getInstance(); + +*/ + + +},{"./config.js":9,"./prefixTables.js":13,"./ucumInternalUtils.js":15,"./unit.js":19,"./unitTables.js":21}],21:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.UnitTables = void 0; + +/** + * This class manages Hashtables that provide references to + * defined units. + * + * @author Lee Mericle, based on java version by Gunther Schadow + * + */ +var Ucum = require('./config.js').Ucum; + +class UnitTablesFactory { + /** + * Constructor. This creates the empty unit tables (hashes) once. After the + * tables are created, it redefines this constructor to throw an error + * stating that the constructor is no longer available and that the + * getInstance function must be used. Here's a description of the first + * and then all subsequent calls to this constructor. + * + * First call to constructor: + * 1. creates OBJECT1 + * 2. initializes attributes of OBJECT1 + * 3. stores reference to OBJECT1.prototype in holdthis local variable + * 4. redefines OBJECT1 as a function that throws an error + * 5. defines the getInstance function (which is also defined outside of + * the class definition - see below). + * + * All subsequent calls to constructor: + * 1. throw error message referring to getInstance + * 2. call getInstance, returns this - which is OBJECT1. + */ + constructor() { + /** + * Tracks units by name + * @type hash - key is the name; + * value is an array of references to the Unit objects + * with the name. More than one unit may have the same + * name, e.g., "second", which is shared by the base unit + * with the code = "s" and the unit with code = "'". + */ + this.unitNames_ = {}; + /** + * Tracks units by code using case-sensitive version. + * + * @type hash - key is the code; + * value is the reference to the Unit object. Codes must + * be unique. + */ + + this.unitCodes_ = {}; + /** + * Keeps track of the order in which units are defined. The order is + * important because unit definitions build on previous definitions. + * + * @type {Array} + */ + + this.codeOrder_ = []; + /** + * Tracks units by unit strings, e.g., cm-1 + * + * @type hash - key is the unit string + * value is an array of unit objects with that ciUnitString. + */ + + this.unitStrings_ = {}; + /** + * Tracks units by Dimension vector + * + * @type hash - key is the dimension vector (not the object, just the + * vector); + * value is an array of references to the Unit objects + * with that vector. More than one unit may have the same + * unit vector, and this can be used to provide a list + * of commensurable units. + */ + + this.unitDimensions_ = {}; + /** + * Maps synonyms to units. Not built until first requested. + * + * @type hash - key is the synonym + * value is an array of references to Unit objects that + * include that synonym. + */ + + this.unitSynonyms_ = {}; + /* + * Holds onto the index of the index of the dimension vector flag for + * the base mass unit (gram). This is set when the base unit (gram) is + * created, and is stored here so that it doesn't have to be found + * over and over again to try to determine whether or not a unit is + * mass-based (for mole<->mass conversions) + * + * @type integer + */ + + this.massDimIndex_ = 0; + /** + * Map of indices in the dimension vector to base unit symbols. + */ + + this.dimVecIndexToBaseUnit_ = {}; + } + /** + * Provides the number of unit objects written to the tables, using the + * codes table since codes must be unique. + * + * @returns count of the number of unit objects in the unitCodes_ table. + */ + + + unitsCount() { + return Object.keys(this.unitCodes_).length; + } + /** + * Adds a Unit object to the tables. + * + * @param theUnit the unit to be added + * @returns nothing + * @throws passes on an error if one is thrown by the called functions for + * a problem with the unit code or unit name + */ + + + addUnit(theUnit) { + let uName = theUnit['name_']; + + if (uName) { + this.addUnitName(theUnit); + } + + this.addUnitCode(theUnit); + this.addUnitString(theUnit); + + try { + if (theUnit['dim_'].getProperty('dimVec_')) this.addUnitDimension(theUnit); + } catch (err) {// do nothing - throws error if the property is null + // and that's OK here. + } + + if (theUnit.isBase_) { + const dimVec = theUnit.dim_.dimVec_; + let nonZeroIndex; + + for (let i = 0, len = dimVec.length; nonZeroIndex == undefined && i < len; ++i) { + if (dimVec[i] != 0) nonZeroIndex = i; + } + + this.dimVecIndexToBaseUnit_[nonZeroIndex] = theUnit.csCode_; + } + } // end addUnit + + /** + * Adds a Unit object to the unitNames_ table. More than one unit + * can have the same name, e.g., the two units with the name "second", + * where the code for one of them is 's' and the code for the other is + * "'". Because of this, an array of unit objects is stored for the + * name. In most cases it will be an array of one object, but this + * clarifies that there may be more than one. + * + * @param theUnit the unit to be added + * @returns nothing + * @throws an error if the unit has no name + */ + + + addUnitName(theUnit) { + let uName = theUnit['name_']; + + if (uName) { + if (this.unitNames_[uName]) this.unitNames_[uName].push(theUnit);else this.unitNames_[uName] = [theUnit]; + } else throw new Error('UnitTables.addUnitName called for a unit with no name. ' + `Unit code = ${theUnit['csCode_']}.`); + } // end addUnitName + + /** + * Adds a Unit object to the unitCodes_, unitUcCodes_, unitLcCodes_ and + * codeOrder_ tables. This also sets the mass dimension index when the + * base mass unit (gram) is read. + * + * @param theUnit the unit to be added + * @returns nothing + * @throws an error if the unitCodes_ table already contains a unit with + * the code + */ + + + addUnitCode(theUnit) { + let uCode = theUnit['csCode_']; + + if (uCode) { + if (this.unitCodes_[uCode]) throw new Error(`UnitTables.addUnitCode called, already contains entry for ` + `unit with code = ${uCode}`);else { + this.unitCodes_[uCode] = theUnit; + this.codeOrder_.push(uCode); + + if (uCode == 'g') { + let dimVec = theUnit.dim_.dimVec_; + let d = 0; + + for (; d < dimVec.length && dimVec[d] < 1; d++); + + this.massDimIndex_ = d; + } + } + } else throw new Error('UnitTables.addUnitCode called for unit that has ' + 'no code.'); + } // end addUnitCode + + /** + * Adds a unit object to the unitStrings_ table. More than one unit + * can have the same string, so an array of unit objects is stored + * for the string. The unit string is the string that creates a non-base + * unit, e.g., a Newton has a unit code of N, a name of Newton, and a + * unitString of kg.m/s2. + * + * If the unit has no string, nothing is stored and no error is reported. + * + * @param theUnit the unit to be added + * @returns nothing + */ + + + addUnitString(theUnit) { + let uString = null; + if (Ucum.caseSensitive_ == true) uString = theUnit['csUnitString_'];else uString = theUnit['ciUnitString_']; + + if (uString) { + let uEntry = { + mag: theUnit['baseFactorStr_'], + unit: theUnit + }; + if (this.unitStrings_[uString]) this.unitStrings_[uString].push(uEntry);else this.unitStrings_[uString] = [uEntry]; + } + } // end addUnitString + + /** + * Adds a Unit object to the unitDimensions_ table. More than one unit + * can have the same dimension (commensurable units have the same dimension). + * Because of this, an array of unit objects is stored for the + * dimension. + * + * @param theUnit the unit to be added + * @returns nothing + * @throws an error if the unit has no dimension + */ + + + addUnitDimension(theUnit) { + let uDim = theUnit['dim_'].getProperty('dimVec_'); + + if (uDim) { + if (this.unitDimensions_[uDim]) this.unitDimensions_[uDim].push(theUnit);else this.unitDimensions_[uDim] = [theUnit]; + } else throw new Error('UnitTables.addUnitDimension called for a unit with no dimension. ' + `Unit code = ${theUnit['csCode_']}.`); + } // end addUnitDimension + + /** + * Builds the unitSynonyms_ table. This is called the first time the + * getUnitsBySynonym method is called. The table/hash contains each word + * (once) from each synonym as well as each word from each unit name. + * + * Hash keys are the words. Hash values are an array of unit codes for + * each unit that has that word in its synonyms or name. + * + * @returns nothing + */ + + + buildUnitSynonyms() { + for (let code in this.unitCodes_) { + let theUnit = this.unitCodes_[code]; + let uSyns = theUnit.synonyms_; // If the current unit has synonyms, process each synonym (often multiples) + + if (uSyns) { + let synsAry = uSyns.split(';'); + + if (synsAry[0] !== '') { + let aLen = synsAry.length; + + for (let a = 0; a < aLen; a++) { + let theSyn = synsAry[a].trim(); // call addSynonymCodes to process each word in the + // synonym, e.g., "British fluid ounces" + + this.addSynonymCodes(code, theSyn); + } // end do for each synonym + + } // end if the current unit has a non-null synonym attribute + + } // end if the unit has any synonyms + // Now call addSynonymCodes to process each word in the unit's name + + + this.addSynonymCodes(code, theUnit.name_); + } // end do for each unit + + } // end buildUnitSynonyms + + /** + * Adds unit code entries to the synonyms table for a string containing + * one or more words to be considered as synonyms. + * + * @param theCode the unit code to be connected to the synonyms + * @param theSynonyms a string containing one or more words to be + * considered synonyms (and thus to be added to the unitSynonyms hash). + */ + + + addSynonymCodes(theCode, theSynonyms) { + let words = theSynonyms.split(' '); + let wLen = words.length; + + for (let w = 0; w < wLen; w++) { + let word = words[w]; // if there is already a synonyms entry for the word, + // get the array of unit codes currently assigned to + // the word and add the code for the current word to + // the synonyms array if it's not already there. + + if (this.unitSynonyms_[word]) { + let synCodes = this.unitSynonyms_[word]; + + if (synCodes.indexOf(theCode) === -1) { + this.unitSynonyms_[word].push(theCode); + } + } // else there are no synonyms entry for the word. Create a + // synonyms array for the word, setting it to contain the unit code. + else { + this.unitSynonyms_[word] = [theCode]; + } + } // end do for each word in the synonyms being processed + + } // end addSynonymCodes + + /** + * Returns a unit object with a case-sensitive code matching the + * uCode parameter, or null if no unit is found with that code. + * + * @param uCode the code of the unit to be returned + * @returns the unit object or null if it is not found + */ + + + getUnitByCode(uCode) { + let retUnit = null; + + if (uCode) { + retUnit = this.unitCodes_[uCode]; + } + + return retUnit; + } + /** + * Returns a array of unit objects based on the unit's name. Usually this + * will be an array of one, but there may be more, since unit names are + * not necessarily unique. + * + * @param uName the name of the unit to be returned. If more than one + * unit has the same name and you only want one specific unit, append the + * csCode of the unit you want to the end of the name, separated by the + * Ucum.codeSep_ value, e.g., inch - [in_i] vs. inch - [in_us]. + * @returns null if no unit was found for the specified name OR an array of + * unit objects with the specified name. Normally this will be an array + * of one object. + * @throws an error if no name is provided to search on + */ + + + getUnitByName(uName) { + if (uName === null || uName === undefined) { + throw new Error('Unable to find unit by name because no name was provided.'); + } + + let sepPos = uName.indexOf(Ucum.codeSep_); + let uCode = null; + + if (sepPos >= 1) { + uCode = uName.substr(sepPos + Ucum.codeSep_.length); + uName = uName.substr(0, sepPos); + } + + let retUnits = this.unitNames_[uName]; + + if (retUnits) { + let uLen = retUnits.length; + + if (uCode && uLen > 1) { + let i = 0; + + for (; retUnits[i].csCode_ !== uCode && i < uLen; i++); + + if (i < uLen) retUnits = [retUnits[i]];else { + retUnits = null; + } + } // end if we need to find both a name and a code + + } // end if we got an array of units + + + return retUnits; + } // end getUnitByName + + /** + * Returns an array of unit objects with the specified unit string. + * The array may contain one or more unit reference objects. + * Or none, if no units have a matching unit string (which is not + * considered an error) + * + * @param name the name of the unit to be returned + * @returns the array of unit references or null if none were found + */ + + + getUnitByString(uString) { + let retAry = null; + + if (uString) { + retAry = this.unitStrings_[uString]; + if (retAry === undefined) retAry = null; + } + + return retAry; + } + /** + * Returns a array of unit objects based on the unit's dimension vector. + * + * @param uName the dimension vector of the units to be returned. + * + * @returns null if no unit was found for the specified vector OR an array of + * one or more unit objects with the specified vector. + * @throws an error if no vector is provided to search on + * logs an error to the console if no unit is found + */ + + + getUnitsByDimension(uDim) { + let unitsArray = null; + + if (uDim === null || uDim === undefined) { + throw new Error('Unable to find unit by because no dimension ' + 'vector was provided.'); + } + + unitsArray = this.unitDimensions_[uDim]; + + if (unitsArray === undefined || unitsArray === null) { + console.log(`Unable to find unit with dimension = ${uDim}`); + } + + return unitsArray; + } // end getUnitsByDimension + + /** + * Returns a array of unit objects that include the specified synonym. + * + * @param uSyn the synonym of the units to be returned. + * + * @returns an object with two of the following three elements: + * 'status' will be error, failed or succeeded + * 'msg' will be included for returns with status = error or failed and + * will explain why the request did not return any units + * 'units' any array of unit objects with the specified synonym will be + * returned for requests with status = succeeded + */ + + + getUnitBySynonym(uSyn) { + let retObj = {}; + let unitsArray = []; + + try { + if (uSyn === null || uSyn === undefined) { + retObj['status'] = 'error'; + throw new Error('Unable to find unit by synonym because no synonym ' + 'was provided.'); + } // If this is the first request for a unit by synonym, build the hash map + + + if (Object.keys(this.unitSynonyms_).length === 0) { + this.buildUnitSynonyms(); + } + + let foundCodes = []; + foundCodes = this.unitSynonyms_[uSyn]; + + if (foundCodes) { + retObj['status'] = 'succeeded'; + let fLen = foundCodes.length; + + for (let f = 0; f < fLen; f++) { + unitsArray.push(this.unitCodes_[foundCodes[f]]); + } + + retObj['units'] = unitsArray; + } + + if (unitsArray.length === 0) { + retObj['status'] = 'failed'; + retObj['msg'] = `Unable to find any units with synonym = ${uSyn}`; + } + } catch (err) { + retObj['msg'] = err.message; + } + + return retObj; + } // end getUnitBySynonym + + /** + * Gets a list of all unit names in the Unit tables + * + * @returns an array of the unit names + */ + + + getAllUnitNames() { + return Object.keys(this.unitNames_); + } // end getAllUnitNames + + /** + * Gets a list of all unit names in the tables. Where more than one + * unit has the same name, the unit code, in parentheses, is appended + * to the end of the name. + * + * @returns {Array} + */ + + + getUnitNamesList() { + let nameList = []; + let codes = Object.keys(this.unitCodes_); + codes.sort(this.compareCodes); + let uLen = codes.length; + + for (let i = 0; i < uLen; i++) { + nameList[i] = codes[i] + Ucum.codeSep_ + this.unitCodes_[codes[i]].name_; + } // end do for each code + + + return nameList; + } + /* + * Returns the mass dimension index + * @returns this.massDimIndex_ + */ + + + getMassDimensionIndex() { + return this.massDimIndex_; + } + /** + * This provides a sort function for unit codes so that sorting ignores + * square brackets and case. + * + * @param a first value + * @param b second value + * @returns -1 if a is should fall before b; otherwise 1. + */ + + + compareCodes(a, b) { + a = a.replace(/[\[\]]/g, ''); + a = a.toLowerCase(); + b = b.replace(/[\[\]]/g, ''); + b = b.toLowerCase(); + return a < b ? -1 : 1; + } + /** + * Gets a list of all unit codes in the Unit tables + * + * @returns an array of the unit names + */ + + + getAllUnitCodes() { + return Object.keys(this.unitCodes_); + } // end getAllUnitNames + + /** + * This is used to get all unit objects. Currently it is used + * to get the objects to write to the json ucum definitions file + * that is used to provide prefix and unit definition objects for + * conversions and validations. + * + * @returns an array containing all unit objects, ordered by definition + * order + */ + + + allUnitsByDef() { + let unitsList = []; + let uLen = this.codeOrder_.length; + + for (let u = 0; u < uLen; u++) { + unitsList.push(this.getUnitByCode(this.codeOrder_[u])); + } + + return unitsList; + } // end allUnitsByDef + + /** + * This is used to get all unit objects, ordered by unit name. Currently it + * is used to create a csv list of all units. + * @param sep separator character (or string) to be used to separate each + * column in the output. Optional, defaults to '|' if not specified. + * (Used to use ; but the synonyms use that extensively). Don't use a + * comma or any other punctuation found in the output data. + * @returns a buffer containing all unit objects, ordered by name + * order + */ + + + allUnitsByName(cols, sep) { + if (sep === undefined || sep === null) sep = '|'; + let unitBuff = ''; + let unitsList = this.getAllUnitNames(); + let uLen = unitsList.length; + let cLen = cols.length; + + for (let i = 0; i < uLen; i++) { + let nameRecs = this.getUnitByName(unitsList[i]); + + for (let u = 0; u < nameRecs.length; u++) { + let rec = nameRecs[u]; + + for (let c = 0; c < cLen; c++) { + if (c > 0) unitBuff += sep; + + if (cols[c] === 'dim_') { + if (rec.dim_ !== null && rec.dim_ !== undefined && rec.dim_.dimVec_ instanceof Array) unitBuff += '[' + rec.dim_.dimVec_.join(',') + ']';else unitBuff += ''; + } else { + let cbuf = rec[cols[c]]; + if (typeof cbuf === 'string') unitBuff += cbuf.replace(/[\n\r]/g, ' ');else unitBuff += cbuf; + } + } // end do for each column requested + + + unitBuff += '\r\n'; + } // end do for each unit in the unit names array + + } + + return unitBuff; + } // end allUnitsByName + + /** + * This creates a list of all units in the tables. It uses the byCode + * table, and uses the codeOrder_ array to determine the order in which + * the units are listed. + * + * @param doLong boolean indicating how much to output. If true, all data + * from the unit objects is included. If false, only a few major values + * are included. + * @param sep separator character (or string) to be used to separate each + * column in the output. Optional, defaults to '|' if not specified. + * (Used to use ; but the synonyms use that extensively). + * @returns {string} buffer containing all the listings + */ + + + printUnits(doLong, sep) { + if (doLong === undefined) doLong = false; + if (sep === undefined) sep = '|'; + let codeList = ''; + let uLen = this.codeOrder_.length; + let unitString = 'csCode' + sep; + + if (doLong) { + unitString += 'ciCode' + sep; + } + + unitString += 'name' + sep; + if (doLong) unitString += 'isBase' + sep; + unitString += 'magnitude' + sep + 'dimension' + sep + 'from unit(s)' + sep + 'value' + sep + 'function' + sep; + if (doLong) unitString += 'property' + sep + 'printSymbol' + sep + 'synonyms' + sep + 'source' + sep + 'class' + sep + 'isMetric' + sep + 'variable' + sep + 'isSpecial' + sep + 'isAbitrary' + sep; + unitString += 'comment'; + codeList = unitString + '\n'; + + for (let u = 0; u < uLen; u++) { + let curUnit = this.getUnitByCode(this.codeOrder_[u]); + unitString = this.codeOrder_[u] + sep; + + if (doLong) { + unitString += curUnit.getProperty('ciCode_') + sep; + } + + unitString += curUnit.getProperty('name_') + sep; + + if (doLong) { + if (curUnit.getProperty('isBase_')) unitString += 'true' + sep;else unitString += 'false' + sep; + } + + unitString += curUnit.getProperty('magnitude_') + sep; + let curDim = curUnit.getProperty('dim_'); + + if (curDim) { + unitString += curDim.dimVec_ + sep; + } else { + unitString += 'null' + sep; + } + + if (curUnit.csUnitString_) unitString += curUnit.csUnitString_ + sep + curUnit.baseFactor_ + sep;else unitString += 'null' + sep + 'null' + sep; + if (curUnit.cnv_) unitString += curUnit.cnv_ + sep;else unitString += 'null' + sep; + + if (doLong) { + unitString += curUnit.getProperty('property_') + sep + curUnit.getProperty('printSymbol_') + sep + curUnit.getProperty('synonyms_') + sep + curUnit.getProperty('source_') + sep + curUnit.getProperty('class_') + sep + curUnit.getProperty('isMetric_') + sep + curUnit.getProperty('variable_') + sep + curUnit.getProperty('isSpecial_') + sep + curUnit.getProperty('isArbitrary_') + sep; + } + + if (curUnit.defError_) unitString += 'problem parsing this one, deferred to later.'; + codeList += unitString + '\n'; + } + + return codeList; + } + +} // end UnitTablesFactory +// Create a singleton instance and (to preserve the existing API) an object that +// provides that instance via getInstance(). + + +var unitTablesInstance = new UnitTablesFactory(); +const UnitTables = { + getInstance: function () { + return unitTablesInstance; + } +}; +exports.UnitTables = UnitTables; + + +},{"./config.js":9}],22:[function(require,module,exports){ +(function (global){(function (){ +'use strict'; + +var objectAssign = require('object-assign'); + +// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js +// original notice: + +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +function compare(a, b) { + if (a === b) { + return 0; + } + + var x = a.length; + var y = b.length; + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i]; + y = b[i]; + break; + } + } + + if (x < y) { + return -1; + } + if (y < x) { + return 1; + } + return 0; +} +function isBuffer(b) { + if (global.Buffer && typeof global.Buffer.isBuffer === 'function') { + return global.Buffer.isBuffer(b); + } + return !!(b != null && b._isBuffer); +} + +// based on node assert, original notice: +// NB: The URL to the CommonJS spec is kept just for tradition. +// node-assert has evolved a lot since then, both in API and behavior. + +// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 +// +// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! +// +// Originally from narwhal.js (http://narwhaljs.org) +// Copyright (c) 2009 Thomas Robinson <280north.com> +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the 'Software'), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +var util = require('util/'); +var hasOwn = Object.prototype.hasOwnProperty; +var pSlice = Array.prototype.slice; +var functionsHaveNames = (function () { + return function foo() {}.name === 'foo'; +}()); +function pToString (obj) { + return Object.prototype.toString.call(obj); +} +function isView(arrbuf) { + if (isBuffer(arrbuf)) { + return false; + } + if (typeof global.ArrayBuffer !== 'function') { + return false; + } + if (typeof ArrayBuffer.isView === 'function') { + return ArrayBuffer.isView(arrbuf); + } + if (!arrbuf) { + return false; + } + if (arrbuf instanceof DataView) { + return true; + } + if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) { + return true; + } + return false; +} +// 1. The assert module provides functions that throw +// AssertionError's when particular conditions are not met. The +// assert module must conform to the following interface. + +var assert = module.exports = ok; + +// 2. The AssertionError is defined in assert. +// new assert.AssertionError({ message: message, +// actual: actual, +// expected: expected }) + +var regex = /\s*function\s+([^\(\s]*)\s*/; +// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js +function getName(func) { + if (!util.isFunction(func)) { + return; + } + if (functionsHaveNames) { + return func.name; + } + var str = func.toString(); + var match = str.match(regex); + return match && match[1]; +} +assert.AssertionError = function AssertionError(options) { + this.name = 'AssertionError'; + this.actual = options.actual; + this.expected = options.expected; + this.operator = options.operator; + if (options.message) { + this.message = options.message; + this.generatedMessage = false; + } else { + this.message = getMessage(this); + this.generatedMessage = true; + } + var stackStartFunction = options.stackStartFunction || fail; + if (Error.captureStackTrace) { + Error.captureStackTrace(this, stackStartFunction); + } else { + // non v8 browsers so we can have a stacktrace + var err = new Error(); + if (err.stack) { + var out = err.stack; + + // try to strip useless frames + var fn_name = getName(stackStartFunction); + var idx = out.indexOf('\n' + fn_name); + if (idx >= 0) { + // once we have located the function frame + // we need to strip out everything before it (and its line) + var next_line = out.indexOf('\n', idx + 1); + out = out.substring(next_line + 1); + } + + this.stack = out; + } + } +}; + +// assert.AssertionError instanceof Error +util.inherits(assert.AssertionError, Error); + +function truncate(s, n) { + if (typeof s === 'string') { + return s.length < n ? s : s.slice(0, n); + } else { + return s; + } +} +function inspect(something) { + if (functionsHaveNames || !util.isFunction(something)) { + return util.inspect(something); + } + var rawname = getName(something); + var name = rawname ? ': ' + rawname : ''; + return '[Function' + name + ']'; +} +function getMessage(self) { + return truncate(inspect(self.actual), 128) + ' ' + + self.operator + ' ' + + truncate(inspect(self.expected), 128); +} + +// At present only the three keys mentioned above are used and +// understood by the spec. Implementations or sub modules can pass +// other keys to the AssertionError's constructor - they will be +// ignored. + +// 3. All of the following functions must throw an AssertionError +// when a corresponding condition is not met, with a message that +// may be undefined if not provided. All assertion methods provide +// both the actual and expected values to the assertion error for +// display purposes. + +function fail(actual, expected, message, operator, stackStartFunction) { + throw new assert.AssertionError({ + message: message, + actual: actual, + expected: expected, + operator: operator, + stackStartFunction: stackStartFunction + }); +} + +// EXTENSION! allows for well behaved errors defined elsewhere. +assert.fail = fail; + +// 4. Pure assertion tests whether a value is truthy, as determined +// by !!guard. +// assert.ok(guard, message_opt); +// This statement is equivalent to assert.equal(true, !!guard, +// message_opt);. To test strictly for the value true, use +// assert.strictEqual(true, guard, message_opt);. + +function ok(value, message) { + if (!value) fail(value, true, message, '==', assert.ok); +} +assert.ok = ok; + +// 5. The equality assertion tests shallow, coercive equality with +// ==. +// assert.equal(actual, expected, message_opt); + +assert.equal = function equal(actual, expected, message) { + if (actual != expected) fail(actual, expected, message, '==', assert.equal); +}; + +// 6. The non-equality assertion tests for whether two objects are not equal +// with != assert.notEqual(actual, expected, message_opt); + +assert.notEqual = function notEqual(actual, expected, message) { + if (actual == expected) { + fail(actual, expected, message, '!=', assert.notEqual); + } +}; + +// 7. The equivalence assertion tests a deep equality relation. +// assert.deepEqual(actual, expected, message_opt); + +assert.deepEqual = function deepEqual(actual, expected, message) { + if (!_deepEqual(actual, expected, false)) { + fail(actual, expected, message, 'deepEqual', assert.deepEqual); + } +}; + +assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { + if (!_deepEqual(actual, expected, true)) { + fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); + } +}; + +function _deepEqual(actual, expected, strict, memos) { + // 7.1. All identical values are equivalent, as determined by ===. + if (actual === expected) { + return true; + } else if (isBuffer(actual) && isBuffer(expected)) { + return compare(actual, expected) === 0; + + // 7.2. If the expected value is a Date object, the actual value is + // equivalent if it is also a Date object that refers to the same time. + } else if (util.isDate(actual) && util.isDate(expected)) { + return actual.getTime() === expected.getTime(); + + // 7.3 If the expected value is a RegExp object, the actual value is + // equivalent if it is also a RegExp object with the same source and + // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). + } else if (util.isRegExp(actual) && util.isRegExp(expected)) { + return actual.source === expected.source && + actual.global === expected.global && + actual.multiline === expected.multiline && + actual.lastIndex === expected.lastIndex && + actual.ignoreCase === expected.ignoreCase; + + // 7.4. Other pairs that do not both pass typeof value == 'object', + // equivalence is determined by ==. + } else if ((actual === null || typeof actual !== 'object') && + (expected === null || typeof expected !== 'object')) { + return strict ? actual === expected : actual == expected; + + // If both values are instances of typed arrays, wrap their underlying + // ArrayBuffers in a Buffer each to increase performance + // This optimization requires the arrays to have the same type as checked by + // Object.prototype.toString (aka pToString). Never perform binary + // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their + // bit patterns are not identical. + } else if (isView(actual) && isView(expected) && + pToString(actual) === pToString(expected) && + !(actual instanceof Float32Array || + actual instanceof Float64Array)) { + return compare(new Uint8Array(actual.buffer), + new Uint8Array(expected.buffer)) === 0; + + // 7.5 For all other Object pairs, including Array objects, equivalence is + // determined by having the same number of owned properties (as verified + // with Object.prototype.hasOwnProperty.call), the same set of keys + // (although not necessarily the same order), equivalent values for every + // corresponding key, and an identical 'prototype' property. Note: this + // accounts for both named and indexed properties on Arrays. + } else if (isBuffer(actual) !== isBuffer(expected)) { + return false; + } else { + memos = memos || {actual: [], expected: []}; + + var actualIndex = memos.actual.indexOf(actual); + if (actualIndex !== -1) { + if (actualIndex === memos.expected.indexOf(expected)) { + return true; + } + } + + memos.actual.push(actual); + memos.expected.push(expected); + + return objEquiv(actual, expected, strict, memos); + } +} + +function isArguments(object) { + return Object.prototype.toString.call(object) == '[object Arguments]'; +} + +function objEquiv(a, b, strict, actualVisitedObjects) { + if (a === null || a === undefined || b === null || b === undefined) + return false; + // if one is a primitive, the other must be same + if (util.isPrimitive(a) || util.isPrimitive(b)) + return a === b; + if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) + return false; + var aIsArgs = isArguments(a); + var bIsArgs = isArguments(b); + if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) + return false; + if (aIsArgs) { + a = pSlice.call(a); + b = pSlice.call(b); + return _deepEqual(a, b, strict); + } + var ka = objectKeys(a); + var kb = objectKeys(b); + var key, i; + // having the same number of owned properties (keys incorporates + // hasOwnProperty) + if (ka.length !== kb.length) + return false; + //the same set of keys (although not necessarily the same order), + ka.sort(); + kb.sort(); + //~~~cheap key test + for (i = ka.length - 1; i >= 0; i--) { + if (ka[i] !== kb[i]) + return false; + } + //equivalent values for every corresponding key, and + //~~~possibly expensive deep test + for (i = ka.length - 1; i >= 0; i--) { + key = ka[i]; + if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) + return false; + } + return true; +} + +// 8. The non-equivalence assertion tests for any deep inequality. +// assert.notDeepEqual(actual, expected, message_opt); + +assert.notDeepEqual = function notDeepEqual(actual, expected, message) { + if (_deepEqual(actual, expected, false)) { + fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); + } +}; + +assert.notDeepStrictEqual = notDeepStrictEqual; +function notDeepStrictEqual(actual, expected, message) { + if (_deepEqual(actual, expected, true)) { + fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); + } +} + + +// 9. The strict equality assertion tests strict equality, as determined by ===. +// assert.strictEqual(actual, expected, message_opt); + +assert.strictEqual = function strictEqual(actual, expected, message) { + if (actual !== expected) { + fail(actual, expected, message, '===', assert.strictEqual); + } +}; + +// 10. The strict non-equality assertion tests for strict inequality, as +// determined by !==. assert.notStrictEqual(actual, expected, message_opt); + +assert.notStrictEqual = function notStrictEqual(actual, expected, message) { + if (actual === expected) { + fail(actual, expected, message, '!==', assert.notStrictEqual); + } +}; + +function expectedException(actual, expected) { + if (!actual || !expected) { + return false; + } + + if (Object.prototype.toString.call(expected) == '[object RegExp]') { + return expected.test(actual); + } + + try { + if (actual instanceof expected) { + return true; + } + } catch (e) { + // Ignore. The instanceof check doesn't work for arrow functions. + } + + if (Error.isPrototypeOf(expected)) { + return false; + } + + return expected.call({}, actual) === true; +} + +function _tryBlock(block) { + var error; + try { + block(); + } catch (e) { + error = e; + } + return error; +} + +function _throws(shouldThrow, block, expected, message) { + var actual; + + if (typeof block !== 'function') { + throw new TypeError('"block" argument must be a function'); + } + + if (typeof expected === 'string') { + message = expected; + expected = null; + } + + actual = _tryBlock(block); + + message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + + (message ? ' ' + message : '.'); + + if (shouldThrow && !actual) { + fail(actual, expected, 'Missing expected exception' + message); + } + + var userProvidedMessage = typeof message === 'string'; + var isUnwantedException = !shouldThrow && util.isError(actual); + var isUnexpectedException = !shouldThrow && actual && !expected; + + if ((isUnwantedException && + userProvidedMessage && + expectedException(actual, expected)) || + isUnexpectedException) { + fail(actual, expected, 'Got unwanted exception' + message); + } + + if ((shouldThrow && actual && expected && + !expectedException(actual, expected)) || (!shouldThrow && actual)) { + throw actual; + } +} + +// 11. Expected to throw an error: +// assert.throws(block, Error_opt, message_opt); + +assert.throws = function(block, /*optional*/error, /*optional*/message) { + _throws(true, block, error, message); +}; + +// EXTENSION! This is annoying to write outside this module. +assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { + _throws(false, block, error, message); +}; + +assert.ifError = function(err) { if (err) throw err; }; + +// Expose a strict only variant of assert +function strict(value, message) { + if (!value) fail(value, true, message, '==', strict); +} +assert.strict = objectAssign(strict, assert, { + equal: assert.strictEqual, + deepEqual: assert.deepStrictEqual, + notEqual: assert.notStrictEqual, + notDeepEqual: assert.notDeepStrictEqual +}); +assert.strict.strict = assert.strict; + +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) { + if (hasOwn.call(obj, key)) keys.push(key); + } + return keys; +}; + +}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"object-assign":361,"util/":25}],23:[function(require,module,exports){ +if (typeof Object.create === 'function') { + // implementation from standard node.js 'util' module + module.exports = function inherits(ctor, superCtor) { + ctor.super_ = superCtor + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }); + }; +} else { + // old school shim for old browsers + module.exports = function inherits(ctor, superCtor) { + ctor.super_ = superCtor + var TempCtor = function () {} + TempCtor.prototype = superCtor.prototype + ctor.prototype = new TempCtor() + ctor.prototype.constructor = ctor + } +} + +},{}],24:[function(require,module,exports){ +module.exports = function isBuffer(arg) { + return arg && typeof arg === 'object' + && typeof arg.copy === 'function' + && typeof arg.fill === 'function' + && typeof arg.readUInt8 === 'function'; +} +},{}],25:[function(require,module,exports){ +(function (process,global){(function (){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var formatRegExp = /%[sdj%]/g; +exports.format = function(f) { + if (!isString(f)) { + var objects = []; + for (var i = 0; i < arguments.length; i++) { + objects.push(inspect(arguments[i])); + } + return objects.join(' '); + } + + var i = 1; + var args = arguments; + var len = args.length; + var str = String(f).replace(formatRegExp, function(x) { + if (x === '%%') return '%'; + if (i >= len) return x; + switch (x) { + case '%s': return String(args[i++]); + case '%d': return Number(args[i++]); + case '%j': + try { + return JSON.stringify(args[i++]); + } catch (_) { + return '[Circular]'; + } + default: + return x; + } + }); + for (var x = args[i]; i < len; x = args[++i]) { + if (isNull(x) || !isObject(x)) { + str += ' ' + x; + } else { + str += ' ' + inspect(x); + } + } + return str; +}; + + +// Mark that a method should not be used. +// Returns a modified function which warns once by default. +// If --no-deprecation is set, then it is a no-op. +exports.deprecate = function(fn, msg) { + // Allow for deprecating things in the process of starting up. + if (isUndefined(global.process)) { + return function() { + return exports.deprecate(fn, msg).apply(this, arguments); + }; + } + + if (process.noDeprecation === true) { + return fn; + } + + var warned = false; + function deprecated() { + if (!warned) { + if (process.throwDeprecation) { + throw new Error(msg); + } else if (process.traceDeprecation) { + console.trace(msg); + } else { + console.error(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; +}; + + +var debugs = {}; +var debugEnviron; +exports.debuglog = function(set) { + if (isUndefined(debugEnviron)) + debugEnviron = process.env.NODE_DEBUG || ''; + set = set.toUpperCase(); + if (!debugs[set]) { + if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { + var pid = process.pid; + debugs[set] = function() { + var msg = exports.format.apply(exports, arguments); + console.error('%s %d: %s', set, pid, msg); + }; + } else { + debugs[set] = function() {}; + } + } + return debugs[set]; +}; + + +/** + * Echos the value of a value. Trys to print the value out + * in the best way possible given the different types. + * + * @param {Object} obj The object to print out. + * @param {Object} opts Optional options object that alters the output. + */ +/* legacy: obj, showHidden, depth, colors*/ +function inspect(obj, opts) { + // default options + var ctx = { + seen: [], + stylize: stylizeNoColor + }; + // legacy... + if (arguments.length >= 3) ctx.depth = arguments[2]; + if (arguments.length >= 4) ctx.colors = arguments[3]; + if (isBoolean(opts)) { + // legacy... + ctx.showHidden = opts; + } else if (opts) { + // got an "options" object + exports._extend(ctx, opts); + } + // set default options + if (isUndefined(ctx.showHidden)) ctx.showHidden = false; + if (isUndefined(ctx.depth)) ctx.depth = 2; + if (isUndefined(ctx.colors)) ctx.colors = false; + if (isUndefined(ctx.customInspect)) ctx.customInspect = true; + if (ctx.colors) ctx.stylize = stylizeWithColor; + return formatValue(ctx, obj, ctx.depth); +} +exports.inspect = inspect; + + +// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics +inspect.colors = { + 'bold' : [1, 22], + 'italic' : [3, 23], + 'underline' : [4, 24], + 'inverse' : [7, 27], + 'white' : [37, 39], + 'grey' : [90, 39], + 'black' : [30, 39], + 'blue' : [34, 39], + 'cyan' : [36, 39], + 'green' : [32, 39], + 'magenta' : [35, 39], + 'red' : [31, 39], + 'yellow' : [33, 39] +}; + +// Don't use 'blue' not visible on cmd.exe +inspect.styles = { + 'special': 'cyan', + 'number': 'yellow', + 'boolean': 'yellow', + 'undefined': 'grey', + 'null': 'bold', + 'string': 'green', + 'date': 'magenta', + // "name": intentionally not styling + 'regexp': 'red' +}; + + +function stylizeWithColor(str, styleType) { + var style = inspect.styles[styleType]; + + if (style) { + return '\u001b[' + inspect.colors[style][0] + 'm' + str + + '\u001b[' + inspect.colors[style][1] + 'm'; + } else { + return str; + } +} + + +function stylizeNoColor(str, styleType) { + return str; +} + + +function arrayToHash(array) { + var hash = {}; + + array.forEach(function(val, idx) { + hash[val] = true; + }); + + return hash; +} + + +function formatValue(ctx, value, recurseTimes) { + // Provide a hook for user-specified inspect functions. + // Check that value is an object with an inspect function on it + if (ctx.customInspect && + value && + isFunction(value.inspect) && + // Filter out the util module, it's inspect function is special + value.inspect !== exports.inspect && + // Also filter out any prototype objects using the circular check. + !(value.constructor && value.constructor.prototype === value)) { + var ret = value.inspect(recurseTimes, ctx); + if (!isString(ret)) { + ret = formatValue(ctx, ret, recurseTimes); + } + return ret; + } + + // Primitive types cannot have properties + var primitive = formatPrimitive(ctx, value); + if (primitive) { + return primitive; + } + + // Look up the keys of the object. + var keys = Object.keys(value); + var visibleKeys = arrayToHash(keys); + + if (ctx.showHidden) { + keys = Object.getOwnPropertyNames(value); + } + + // IE doesn't make error fields non-enumerable + // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx + if (isError(value) + && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { + return formatError(value); + } + + // Some type of object without properties can be shortcutted. + if (keys.length === 0) { + if (isFunction(value)) { + var name = value.name ? ': ' + value.name : ''; + return ctx.stylize('[Function' + name + ']', 'special'); + } + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } + if (isDate(value)) { + return ctx.stylize(Date.prototype.toString.call(value), 'date'); + } + if (isError(value)) { + return formatError(value); + } + } + + var base = '', array = false, braces = ['{', '}']; + + // Make Array say that they are Array + if (isArray(value)) { + array = true; + braces = ['[', ']']; + } + + // Make functions say that they are functions + if (isFunction(value)) { + var n = value.name ? ': ' + value.name : ''; + base = ' [Function' + n + ']'; + } + + // Make RegExps say that they are RegExps + if (isRegExp(value)) { + base = ' ' + RegExp.prototype.toString.call(value); + } + + // Make dates with properties first say the date + if (isDate(value)) { + base = ' ' + Date.prototype.toUTCString.call(value); + } + + // Make error with message first say the error + if (isError(value)) { + base = ' ' + formatError(value); + } + + if (keys.length === 0 && (!array || value.length == 0)) { + return braces[0] + base + braces[1]; + } + + if (recurseTimes < 0) { + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } else { + return ctx.stylize('[Object]', 'special'); + } + } + + ctx.seen.push(value); + + var output; + if (array) { + output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else { + output = keys.map(function(key) { + return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); + }); + } + + ctx.seen.pop(); + + return reduceToSingleString(output, base, braces); +} + + +function formatPrimitive(ctx, value) { + if (isUndefined(value)) + return ctx.stylize('undefined', 'undefined'); + if (isString(value)) { + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + } + if (isNumber(value)) + return ctx.stylize('' + value, 'number'); + if (isBoolean(value)) + return ctx.stylize('' + value, 'boolean'); + // For some reason typeof null is "object", so special case here. + if (isNull(value)) + return ctx.stylize('null', 'null'); +} + + +function formatError(value) { + return '[' + Error.prototype.toString.call(value) + ']'; +} + + +function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { + var output = []; + for (var i = 0, l = value.length; i < l; ++i) { + if (hasOwnProperty(value, String(i))) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + String(i), true)); + } else { + output.push(''); + } + } + keys.forEach(function(key) { + if (!key.match(/^\d+$/)) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + key, true)); + } + }); + return output; +} + + +function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { + var name, str, desc; + desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; + if (desc.get) { + if (desc.set) { + str = ctx.stylize('[Getter/Setter]', 'special'); + } else { + str = ctx.stylize('[Getter]', 'special'); + } + } else { + if (desc.set) { + str = ctx.stylize('[Setter]', 'special'); + } + } + if (!hasOwnProperty(visibleKeys, key)) { + name = '[' + key + ']'; + } + if (!str) { + if (ctx.seen.indexOf(desc.value) < 0) { + if (isNull(recurseTimes)) { + str = formatValue(ctx, desc.value, null); + } else { + str = formatValue(ctx, desc.value, recurseTimes - 1); + } + if (str.indexOf('\n') > -1) { + if (array) { + str = str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n').substr(2); + } else { + str = '\n' + str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n'); + } + } + } else { + str = ctx.stylize('[Circular]', 'special'); + } + } + if (isUndefined(name)) { + if (array && key.match(/^\d+$/)) { + return str; + } + name = JSON.stringify('' + key); + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + name = name.substr(1, name.length - 2); + name = ctx.stylize(name, 'name'); + } else { + name = name.replace(/'/g, "\\'") + .replace(/\\"/g, '"') + .replace(/(^"|"$)/g, "'"); + name = ctx.stylize(name, 'string'); + } + } + + return name + ': ' + str; +} + + +function reduceToSingleString(output, base, braces) { + var numLinesEst = 0; + var length = output.reduce(function(prev, cur) { + numLinesEst++; + if (cur.indexOf('\n') >= 0) numLinesEst++; + return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; + }, 0); + + if (length > 60) { + return braces[0] + + (base === '' ? '' : base + '\n ') + + ' ' + + output.join(',\n ') + + ' ' + + braces[1]; + } + + return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; +} + + +// NOTE: These type checking functions intentionally don't use `instanceof` +// because it is fragile and can be easily faked with `Object.create()`. +function isArray(ar) { + return Array.isArray(ar); +} +exports.isArray = isArray; + +function isBoolean(arg) { + return typeof arg === 'boolean'; +} +exports.isBoolean = isBoolean; + +function isNull(arg) { + return arg === null; +} +exports.isNull = isNull; + +function isNullOrUndefined(arg) { + return arg == null; +} +exports.isNullOrUndefined = isNullOrUndefined; + +function isNumber(arg) { + return typeof arg === 'number'; +} +exports.isNumber = isNumber; + +function isString(arg) { + return typeof arg === 'string'; +} +exports.isString = isString; + +function isSymbol(arg) { + return typeof arg === 'symbol'; +} +exports.isSymbol = isSymbol; + +function isUndefined(arg) { + return arg === void 0; +} +exports.isUndefined = isUndefined; + +function isRegExp(re) { + return isObject(re) && objectToString(re) === '[object RegExp]'; +} +exports.isRegExp = isRegExp; + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} +exports.isObject = isObject; + +function isDate(d) { + return isObject(d) && objectToString(d) === '[object Date]'; +} +exports.isDate = isDate; + +function isError(e) { + return isObject(e) && + (objectToString(e) === '[object Error]' || e instanceof Error); +} +exports.isError = isError; + +function isFunction(arg) { + return typeof arg === 'function'; +} +exports.isFunction = isFunction; + +function isPrimitive(arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; +} +exports.isPrimitive = isPrimitive; + +exports.isBuffer = require('./support/isBuffer'); + +function objectToString(o) { + return Object.prototype.toString.call(o); +} + + +function pad(n) { + return n < 10 ? '0' + n.toString(10) : n.toString(10); +} + + +var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', + 'Oct', 'Nov', 'Dec']; + +// 26 Feb 16:19:34 +function timestamp() { + var d = new Date(); + var time = [pad(d.getHours()), + pad(d.getMinutes()), + pad(d.getSeconds())].join(':'); + return [d.getDate(), months[d.getMonth()], time].join(' '); +} + + +// log is just a thin wrapper to console.log that prepends a timestamp +exports.log = function() { + console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); +}; + + +/** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be rewritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor Constructor function to inherit prototype from. + */ +exports.inherits = require('inherits'); + +exports._extend = function(origin, add) { + // Don't do anything if add isn't an object + if (!add || !isObject(add)) return origin; + + var keys = Object.keys(add); + var i = keys.length; + while (i--) { + origin[keys[i]] = add[keys[i]]; + } + return origin; +}; + +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} + +}).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./support/isBuffer":24,"_process":362,"inherits":23}],26:[function(require,module,exports){ +(function (global){(function (){ +'use strict'; + +var possibleNames = [ + 'BigInt64Array', + 'BigUint64Array', + 'Float32Array', + 'Float64Array', + 'Int16Array', + 'Int32Array', + 'Int8Array', + 'Uint16Array', + 'Uint32Array', + 'Uint8Array', + 'Uint8ClampedArray' +]; + +var g = typeof globalThis === 'undefined' ? global : globalThis; + +module.exports = function availableTypedArrays() { + var out = []; + for (var i = 0; i < possibleNames.length; i++) { + if (typeof g[possibleNames[i]] === 'function') { + out[out.length] = possibleNames[i]; + } + } + return out; +}; + +}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{}],27:[function(require,module,exports){ +'use strict' + +exports.byteLength = byteLength +exports.toByteArray = toByteArray +exports.fromByteArray = fromByteArray + +var lookup = [] +var revLookup = [] +var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array + +var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' +for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i] + revLookup[code.charCodeAt(i)] = i +} + +// Support decoding URL-safe base64 strings, as Node.js does. +// See: https://en.wikipedia.org/wiki/Base64#URL_applications +revLookup['-'.charCodeAt(0)] = 62 +revLookup['_'.charCodeAt(0)] = 63 + +function getLens (b64) { + var len = b64.length + + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // Trim off extra bytes after placeholder bytes are found + // See: https://github.com/beatgammit/base64-js/issues/42 + var validLen = b64.indexOf('=') + if (validLen === -1) validLen = len + + var placeHoldersLen = validLen === len + ? 0 + : 4 - (validLen % 4) + + return [validLen, placeHoldersLen] +} + +// base64 is 4/3 + up to two characters of the original data +function byteLength (b64) { + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen +} + +function _byteLength (b64, validLen, placeHoldersLen) { + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen +} + +function toByteArray (b64) { + var tmp + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] + + var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) + + var curByte = 0 + + // if there are placeholders, only get up to the last complete 4 chars + var len = placeHoldersLen > 0 + ? validLen - 4 + : validLen + + var i + for (i = 0; i < len; i += 4) { + tmp = + (revLookup[b64.charCodeAt(i)] << 18) | + (revLookup[b64.charCodeAt(i + 1)] << 12) | + (revLookup[b64.charCodeAt(i + 2)] << 6) | + revLookup[b64.charCodeAt(i + 3)] + arr[curByte++] = (tmp >> 16) & 0xFF + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } + + if (placeHoldersLen === 2) { + tmp = + (revLookup[b64.charCodeAt(i)] << 2) | + (revLookup[b64.charCodeAt(i + 1)] >> 4) + arr[curByte++] = tmp & 0xFF + } + + if (placeHoldersLen === 1) { + tmp = + (revLookup[b64.charCodeAt(i)] << 10) | + (revLookup[b64.charCodeAt(i + 1)] << 4) | + (revLookup[b64.charCodeAt(i + 2)] >> 2) + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } + + return arr +} + +function tripletToBase64 (num) { + return lookup[num >> 18 & 0x3F] + + lookup[num >> 12 & 0x3F] + + lookup[num >> 6 & 0x3F] + + lookup[num & 0x3F] +} + +function encodeChunk (uint8, start, end) { + var tmp + var output = [] + for (var i = start; i < end; i += 3) { + tmp = + ((uint8[i] << 16) & 0xFF0000) + + ((uint8[i + 1] << 8) & 0xFF00) + + (uint8[i + 2] & 0xFF) + output.push(tripletToBase64(tmp)) + } + return output.join('') +} + +function fromByteArray (uint8) { + var tmp + var len = uint8.length + var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes + var parts = [] + var maxChunkLength = 16383 // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1] + parts.push( + lookup[tmp >> 2] + + lookup[(tmp << 4) & 0x3F] + + '==' + ) + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + uint8[len - 1] + parts.push( + lookup[tmp >> 10] + + lookup[(tmp >> 4) & 0x3F] + + lookup[(tmp << 2) & 0x3F] + + '=' + ) + } + + return parts.join('') +} + +},{}],28:[function(require,module,exports){ +(function (process,global,setImmediate){(function (){ +/* @preserve + * The MIT License (MIT) + * + * Copyright (c) 2013-2017 Petka Antonov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +/** + * bluebird build version 3.5.1 + * Features enabled: core, race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each +*/ +!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Promise=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof _dereq_=="function"&&_dereq_;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof _dereq_=="function"&&_dereq_;for(var o=0;o 0) { + var fn = queue.shift(); + if (typeof fn !== "function") { + fn._settlePromises(); + continue; + } + var receiver = queue.shift(); + var arg = queue.shift(); + fn.call(receiver, arg); + } +}; + +Async.prototype._drainQueues = function () { + this._drainQueue(this._normalQueue); + this._reset(); + this._haveDrainedQueues = true; + this._drainQueue(this._lateQueue); +}; + +Async.prototype._queueTick = function () { + if (!this._isTickUsed) { + this._isTickUsed = true; + this._schedule(this.drainQueues); + } +}; + +Async.prototype._reset = function () { + this._isTickUsed = false; +}; + +module.exports = Async; +module.exports.firstLineError = firstLineError; + +},{"./queue":26,"./schedule":29,"./util":36}],3:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) { +var calledBind = false; +var rejectThis = function(_, e) { + this._reject(e); +}; + +var targetRejected = function(e, context) { + context.promiseRejectionQueued = true; + context.bindingPromise._then(rejectThis, rejectThis, null, this, e); +}; + +var bindingResolved = function(thisArg, context) { + if (((this._bitField & 50397184) === 0)) { + this._resolveCallback(context.target); + } +}; + +var bindingRejected = function(e, context) { + if (!context.promiseRejectionQueued) this._reject(e); +}; + +Promise.prototype.bind = function (thisArg) { + if (!calledBind) { + calledBind = true; + Promise.prototype._propagateFrom = debug.propagateFromFunction(); + Promise.prototype._boundValue = debug.boundValueFunction(); + } + var maybePromise = tryConvertToPromise(thisArg); + var ret = new Promise(INTERNAL); + ret._propagateFrom(this, 1); + var target = this._target(); + ret._setBoundTo(maybePromise); + if (maybePromise instanceof Promise) { + var context = { + promiseRejectionQueued: false, + promise: ret, + target: target, + bindingPromise: maybePromise + }; + target._then(INTERNAL, targetRejected, undefined, ret, context); + maybePromise._then( + bindingResolved, bindingRejected, undefined, ret, context); + ret._setOnCancel(maybePromise); + } else { + ret._resolveCallback(target); + } + return ret; +}; + +Promise.prototype._setBoundTo = function (obj) { + if (obj !== undefined) { + this._bitField = this._bitField | 2097152; + this._boundTo = obj; + } else { + this._bitField = this._bitField & (~2097152); + } +}; + +Promise.prototype._isBound = function () { + return (this._bitField & 2097152) === 2097152; +}; + +Promise.bind = function (thisArg, value) { + return Promise.resolve(value).bind(thisArg); +}; +}; + +},{}],4:[function(_dereq_,module,exports){ +"use strict"; +var old; +if (typeof Promise !== "undefined") old = Promise; +function noConflict() { + try { if (Promise === bluebird) Promise = old; } + catch (e) {} + return bluebird; +} +var bluebird = _dereq_("./promise")(); +bluebird.noConflict = noConflict; +module.exports = bluebird; + +},{"./promise":22}],5:[function(_dereq_,module,exports){ +"use strict"; +var cr = Object.create; +if (cr) { + var callerCache = cr(null); + var getterCache = cr(null); + callerCache[" size"] = getterCache[" size"] = 0; +} + +module.exports = function(Promise) { +var util = _dereq_("./util"); +var canEvaluate = util.canEvaluate; +var isIdentifier = util.isIdentifier; + +var getMethodCaller; +var getGetter; +if (!true) { +var makeMethodCaller = function (methodName) { + return new Function("ensureMethod", " \n\ + return function(obj) { \n\ + 'use strict' \n\ + var len = this.length; \n\ + ensureMethod(obj, 'methodName'); \n\ + switch(len) { \n\ + case 1: return obj.methodName(this[0]); \n\ + case 2: return obj.methodName(this[0], this[1]); \n\ + case 3: return obj.methodName(this[0], this[1], this[2]); \n\ + case 0: return obj.methodName(); \n\ + default: \n\ + return obj.methodName.apply(obj, this); \n\ + } \n\ + }; \n\ + ".replace(/methodName/g, methodName))(ensureMethod); +}; + +var makeGetter = function (propertyName) { + return new Function("obj", " \n\ + 'use strict'; \n\ + return obj.propertyName; \n\ + ".replace("propertyName", propertyName)); +}; + +var getCompiled = function(name, compiler, cache) { + var ret = cache[name]; + if (typeof ret !== "function") { + if (!isIdentifier(name)) { + return null; + } + ret = compiler(name); + cache[name] = ret; + cache[" size"]++; + if (cache[" size"] > 512) { + var keys = Object.keys(cache); + for (var i = 0; i < 256; ++i) delete cache[keys[i]]; + cache[" size"] = keys.length - 256; + } + } + return ret; +}; + +getMethodCaller = function(name) { + return getCompiled(name, makeMethodCaller, callerCache); +}; + +getGetter = function(name) { + return getCompiled(name, makeGetter, getterCache); +}; +} + +function ensureMethod(obj, methodName) { + var fn; + if (obj != null) fn = obj[methodName]; + if (typeof fn !== "function") { + var message = "Object " + util.classString(obj) + " has no method '" + + util.toString(methodName) + "'"; + throw new Promise.TypeError(message); + } + return fn; +} + +function caller(obj) { + var methodName = this.pop(); + var fn = ensureMethod(obj, methodName); + return fn.apply(obj, this); +} +Promise.prototype.call = function (methodName) { + var args = [].slice.call(arguments, 1);; + if (!true) { + if (canEvaluate) { + var maybeCaller = getMethodCaller(methodName); + if (maybeCaller !== null) { + return this._then( + maybeCaller, undefined, undefined, args, undefined); + } + } + } + args.push(methodName); + return this._then(caller, undefined, undefined, args, undefined); +}; + +function namedGetter(obj) { + return obj[this]; +} +function indexedGetter(obj) { + var index = +this; + if (index < 0) index = Math.max(0, index + obj.length); + return obj[index]; +} +Promise.prototype.get = function (propertyName) { + var isIndex = (typeof propertyName === "number"); + var getter; + if (!isIndex) { + if (canEvaluate) { + var maybeGetter = getGetter(propertyName); + getter = maybeGetter !== null ? maybeGetter : namedGetter; + } else { + getter = namedGetter; + } + } else { + getter = indexedGetter; + } + return this._then(getter, undefined, undefined, propertyName, undefined); +}; +}; + +},{"./util":36}],6:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, PromiseArray, apiRejection, debug) { +var util = _dereq_("./util"); +var tryCatch = util.tryCatch; +var errorObj = util.errorObj; +var async = Promise._async; + +Promise.prototype["break"] = Promise.prototype.cancel = function() { + if (!debug.cancellation()) return this._warn("cancellation is disabled"); + + var promise = this; + var child = promise; + while (promise._isCancellable()) { + if (!promise._cancelBy(child)) { + if (child._isFollowing()) { + child._followee().cancel(); + } else { + child._cancelBranched(); + } + break; + } + + var parent = promise._cancellationParent; + if (parent == null || !parent._isCancellable()) { + if (promise._isFollowing()) { + promise._followee().cancel(); + } else { + promise._cancelBranched(); + } + break; + } else { + if (promise._isFollowing()) promise._followee().cancel(); + promise._setWillBeCancelled(); + child = promise; + promise = parent; + } + } +}; + +Promise.prototype._branchHasCancelled = function() { + this._branchesRemainingToCancel--; +}; + +Promise.prototype._enoughBranchesHaveCancelled = function() { + return this._branchesRemainingToCancel === undefined || + this._branchesRemainingToCancel <= 0; +}; + +Promise.prototype._cancelBy = function(canceller) { + if (canceller === this) { + this._branchesRemainingToCancel = 0; + this._invokeOnCancel(); + return true; + } else { + this._branchHasCancelled(); + if (this._enoughBranchesHaveCancelled()) { + this._invokeOnCancel(); + return true; + } + } + return false; +}; + +Promise.prototype._cancelBranched = function() { + if (this._enoughBranchesHaveCancelled()) { + this._cancel(); + } +}; + +Promise.prototype._cancel = function() { + if (!this._isCancellable()) return; + this._setCancelled(); + async.invoke(this._cancelPromises, this, undefined); +}; + +Promise.prototype._cancelPromises = function() { + if (this._length() > 0) this._settlePromises(); +}; + +Promise.prototype._unsetOnCancel = function() { + this._onCancelField = undefined; +}; + +Promise.prototype._isCancellable = function() { + return this.isPending() && !this._isCancelled(); +}; + +Promise.prototype.isCancellable = function() { + return this.isPending() && !this.isCancelled(); +}; + +Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) { + if (util.isArray(onCancelCallback)) { + for (var i = 0; i < onCancelCallback.length; ++i) { + this._doInvokeOnCancel(onCancelCallback[i], internalOnly); + } + } else if (onCancelCallback !== undefined) { + if (typeof onCancelCallback === "function") { + if (!internalOnly) { + var e = tryCatch(onCancelCallback).call(this._boundValue()); + if (e === errorObj) { + this._attachExtraTrace(e.e); + async.throwLater(e.e); + } + } + } else { + onCancelCallback._resultCancelled(this); + } + } +}; + +Promise.prototype._invokeOnCancel = function() { + var onCancelCallback = this._onCancel(); + this._unsetOnCancel(); + async.invoke(this._doInvokeOnCancel, this, onCancelCallback); +}; + +Promise.prototype._invokeInternalOnCancel = function() { + if (this._isCancellable()) { + this._doInvokeOnCancel(this._onCancel(), true); + this._unsetOnCancel(); + } +}; + +Promise.prototype._resultCancelled = function() { + this.cancel(); +}; + +}; + +},{"./util":36}],7:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(NEXT_FILTER) { +var util = _dereq_("./util"); +var getKeys = _dereq_("./es5").keys; +var tryCatch = util.tryCatch; +var errorObj = util.errorObj; + +function catchFilter(instances, cb, promise) { + return function(e) { + var boundTo = promise._boundValue(); + predicateLoop: for (var i = 0; i < instances.length; ++i) { + var item = instances[i]; + + if (item === Error || + (item != null && item.prototype instanceof Error)) { + if (e instanceof item) { + return tryCatch(cb).call(boundTo, e); + } + } else if (typeof item === "function") { + var matchesPredicate = tryCatch(item).call(boundTo, e); + if (matchesPredicate === errorObj) { + return matchesPredicate; + } else if (matchesPredicate) { + return tryCatch(cb).call(boundTo, e); + } + } else if (util.isObject(e)) { + var keys = getKeys(item); + for (var j = 0; j < keys.length; ++j) { + var key = keys[j]; + if (item[key] != e[key]) { + continue predicateLoop; + } + } + return tryCatch(cb).call(boundTo, e); + } + } + return NEXT_FILTER; + }; +} + +return catchFilter; +}; + +},{"./es5":13,"./util":36}],8:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise) { +var longStackTraces = false; +var contextStack = []; + +Promise.prototype._promiseCreated = function() {}; +Promise.prototype._pushContext = function() {}; +Promise.prototype._popContext = function() {return null;}; +Promise._peekContext = Promise.prototype._peekContext = function() {}; + +function Context() { + this._trace = new Context.CapturedTrace(peekContext()); +} +Context.prototype._pushContext = function () { + if (this._trace !== undefined) { + this._trace._promiseCreated = null; + contextStack.push(this._trace); + } +}; + +Context.prototype._popContext = function () { + if (this._trace !== undefined) { + var trace = contextStack.pop(); + var ret = trace._promiseCreated; + trace._promiseCreated = null; + return ret; + } + return null; +}; + +function createContext() { + if (longStackTraces) return new Context(); +} + +function peekContext() { + var lastIndex = contextStack.length - 1; + if (lastIndex >= 0) { + return contextStack[lastIndex]; + } + return undefined; +} +Context.CapturedTrace = null; +Context.create = createContext; +Context.deactivateLongStackTraces = function() {}; +Context.activateLongStackTraces = function() { + var Promise_pushContext = Promise.prototype._pushContext; + var Promise_popContext = Promise.prototype._popContext; + var Promise_PeekContext = Promise._peekContext; + var Promise_peekContext = Promise.prototype._peekContext; + var Promise_promiseCreated = Promise.prototype._promiseCreated; + Context.deactivateLongStackTraces = function() { + Promise.prototype._pushContext = Promise_pushContext; + Promise.prototype._popContext = Promise_popContext; + Promise._peekContext = Promise_PeekContext; + Promise.prototype._peekContext = Promise_peekContext; + Promise.prototype._promiseCreated = Promise_promiseCreated; + longStackTraces = false; + }; + longStackTraces = true; + Promise.prototype._pushContext = Context.prototype._pushContext; + Promise.prototype._popContext = Context.prototype._popContext; + Promise._peekContext = Promise.prototype._peekContext = peekContext; + Promise.prototype._promiseCreated = function() { + var ctx = this._peekContext(); + if (ctx && ctx._promiseCreated == null) ctx._promiseCreated = this; + }; +}; +return Context; +}; + +},{}],9:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, Context) { +var getDomain = Promise._getDomain; +var async = Promise._async; +var Warning = _dereq_("./errors").Warning; +var util = _dereq_("./util"); +var canAttachTrace = util.canAttachTrace; +var unhandledRejectionHandled; +var possiblyUnhandledRejection; +var bluebirdFramePattern = + /[\\\/]bluebird[\\\/]js[\\\/](release|debug|instrumented)/; +var nodeFramePattern = /\((?:timers\.js):\d+:\d+\)/; +var parseLinePattern = /[\/<\(](.+?):(\d+):(\d+)\)?\s*$/; +var stackFramePattern = null; +var formatStack = null; +var indentStackFrames = false; +var printWarning; +var debugging = !!(util.env("BLUEBIRD_DEBUG") != 0 && + (true || + util.env("BLUEBIRD_DEBUG") || + util.env("NODE_ENV") === "development")); + +var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 && + (debugging || util.env("BLUEBIRD_WARNINGS"))); + +var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 && + (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES"))); + +var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 && + (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN")); + +Promise.prototype.suppressUnhandledRejections = function() { + var target = this._target(); + target._bitField = ((target._bitField & (~1048576)) | + 524288); +}; + +Promise.prototype._ensurePossibleRejectionHandled = function () { + if ((this._bitField & 524288) !== 0) return; + this._setRejectionIsUnhandled(); + var self = this; + setTimeout(function() { + self._notifyUnhandledRejection(); + }, 1); +}; + +Promise.prototype._notifyUnhandledRejectionIsHandled = function () { + fireRejectionEvent("rejectionHandled", + unhandledRejectionHandled, undefined, this); +}; + +Promise.prototype._setReturnedNonUndefined = function() { + this._bitField = this._bitField | 268435456; +}; + +Promise.prototype._returnedNonUndefined = function() { + return (this._bitField & 268435456) !== 0; +}; + +Promise.prototype._notifyUnhandledRejection = function () { + if (this._isRejectionUnhandled()) { + var reason = this._settledValue(); + this._setUnhandledRejectionIsNotified(); + fireRejectionEvent("unhandledRejection", + possiblyUnhandledRejection, reason, this); + } +}; + +Promise.prototype._setUnhandledRejectionIsNotified = function () { + this._bitField = this._bitField | 262144; +}; + +Promise.prototype._unsetUnhandledRejectionIsNotified = function () { + this._bitField = this._bitField & (~262144); +}; + +Promise.prototype._isUnhandledRejectionNotified = function () { + return (this._bitField & 262144) > 0; +}; + +Promise.prototype._setRejectionIsUnhandled = function () { + this._bitField = this._bitField | 1048576; +}; + +Promise.prototype._unsetRejectionIsUnhandled = function () { + this._bitField = this._bitField & (~1048576); + if (this._isUnhandledRejectionNotified()) { + this._unsetUnhandledRejectionIsNotified(); + this._notifyUnhandledRejectionIsHandled(); + } +}; + +Promise.prototype._isRejectionUnhandled = function () { + return (this._bitField & 1048576) > 0; +}; + +Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) { + return warn(message, shouldUseOwnTrace, promise || this); +}; + +Promise.onPossiblyUnhandledRejection = function (fn) { + var domain = getDomain(); + possiblyUnhandledRejection = + typeof fn === "function" ? (domain === null ? + fn : util.domainBind(domain, fn)) + : undefined; +}; + +Promise.onUnhandledRejectionHandled = function (fn) { + var domain = getDomain(); + unhandledRejectionHandled = + typeof fn === "function" ? (domain === null ? + fn : util.domainBind(domain, fn)) + : undefined; +}; + +var disableLongStackTraces = function() {}; +Promise.longStackTraces = function () { + if (async.haveItemsQueued() && !config.longStackTraces) { + throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + if (!config.longStackTraces && longStackTracesIsSupported()) { + var Promise_captureStackTrace = Promise.prototype._captureStackTrace; + var Promise_attachExtraTrace = Promise.prototype._attachExtraTrace; + config.longStackTraces = true; + disableLongStackTraces = function() { + if (async.haveItemsQueued() && !config.longStackTraces) { + throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + Promise.prototype._captureStackTrace = Promise_captureStackTrace; + Promise.prototype._attachExtraTrace = Promise_attachExtraTrace; + Context.deactivateLongStackTraces(); + async.enableTrampoline(); + config.longStackTraces = false; + }; + Promise.prototype._captureStackTrace = longStackTracesCaptureStackTrace; + Promise.prototype._attachExtraTrace = longStackTracesAttachExtraTrace; + Context.activateLongStackTraces(); + async.disableTrampolineIfNecessary(); + } +}; + +Promise.hasLongStackTraces = function () { + return config.longStackTraces && longStackTracesIsSupported(); +}; + +var fireDomEvent = (function() { + try { + if (typeof CustomEvent === "function") { + var event = new CustomEvent("CustomEvent"); + util.global.dispatchEvent(event); + return function(name, event) { + var domEvent = new CustomEvent(name.toLowerCase(), { + detail: event, + cancelable: true + }); + return !util.global.dispatchEvent(domEvent); + }; + } else if (typeof Event === "function") { + var event = new Event("CustomEvent"); + util.global.dispatchEvent(event); + return function(name, event) { + var domEvent = new Event(name.toLowerCase(), { + cancelable: true + }); + domEvent.detail = event; + return !util.global.dispatchEvent(domEvent); + }; + } else { + var event = document.createEvent("CustomEvent"); + event.initCustomEvent("testingtheevent", false, true, {}); + util.global.dispatchEvent(event); + return function(name, event) { + var domEvent = document.createEvent("CustomEvent"); + domEvent.initCustomEvent(name.toLowerCase(), false, true, + event); + return !util.global.dispatchEvent(domEvent); + }; + } + } catch (e) {} + return function() { + return false; + }; +})(); + +var fireGlobalEvent = (function() { + if (util.isNode) { + return function() { + return process.emit.apply(process, arguments); + }; + } else { + if (!util.global) { + return function() { + return false; + }; + } + return function(name) { + var methodName = "on" + name.toLowerCase(); + var method = util.global[methodName]; + if (!method) return false; + method.apply(util.global, [].slice.call(arguments, 1)); + return true; + }; + } +})(); + +function generatePromiseLifecycleEventObject(name, promise) { + return {promise: promise}; +} + +var eventToObjectGenerator = { + promiseCreated: generatePromiseLifecycleEventObject, + promiseFulfilled: generatePromiseLifecycleEventObject, + promiseRejected: generatePromiseLifecycleEventObject, + promiseResolved: generatePromiseLifecycleEventObject, + promiseCancelled: generatePromiseLifecycleEventObject, + promiseChained: function(name, promise, child) { + return {promise: promise, child: child}; + }, + warning: function(name, warning) { + return {warning: warning}; + }, + unhandledRejection: function (name, reason, promise) { + return {reason: reason, promise: promise}; + }, + rejectionHandled: generatePromiseLifecycleEventObject +}; + +var activeFireEvent = function (name) { + var globalEventFired = false; + try { + globalEventFired = fireGlobalEvent.apply(null, arguments); + } catch (e) { + async.throwLater(e); + globalEventFired = true; + } + + var domEventFired = false; + try { + domEventFired = fireDomEvent(name, + eventToObjectGenerator[name].apply(null, arguments)); + } catch (e) { + async.throwLater(e); + domEventFired = true; + } + + return domEventFired || globalEventFired; +}; + +Promise.config = function(opts) { + opts = Object(opts); + if ("longStackTraces" in opts) { + if (opts.longStackTraces) { + Promise.longStackTraces(); + } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) { + disableLongStackTraces(); + } + } + if ("warnings" in opts) { + var warningsOption = opts.warnings; + config.warnings = !!warningsOption; + wForgottenReturn = config.warnings; + + if (util.isObject(warningsOption)) { + if ("wForgottenReturn" in warningsOption) { + wForgottenReturn = !!warningsOption.wForgottenReturn; + } + } + } + if ("cancellation" in opts && opts.cancellation && !config.cancellation) { + if (async.haveItemsQueued()) { + throw new Error( + "cannot enable cancellation after promises are in use"); + } + Promise.prototype._clearCancellationData = + cancellationClearCancellationData; + Promise.prototype._propagateFrom = cancellationPropagateFrom; + Promise.prototype._onCancel = cancellationOnCancel; + Promise.prototype._setOnCancel = cancellationSetOnCancel; + Promise.prototype._attachCancellationCallback = + cancellationAttachCancellationCallback; + Promise.prototype._execute = cancellationExecute; + propagateFromFunction = cancellationPropagateFrom; + config.cancellation = true; + } + if ("monitoring" in opts) { + if (opts.monitoring && !config.monitoring) { + config.monitoring = true; + Promise.prototype._fireEvent = activeFireEvent; + } else if (!opts.monitoring && config.monitoring) { + config.monitoring = false; + Promise.prototype._fireEvent = defaultFireEvent; + } + } + return Promise; +}; + +function defaultFireEvent() { return false; } + +Promise.prototype._fireEvent = defaultFireEvent; +Promise.prototype._execute = function(executor, resolve, reject) { + try { + executor(resolve, reject); + } catch (e) { + return e; + } +}; +Promise.prototype._onCancel = function () {}; +Promise.prototype._setOnCancel = function (handler) { ; }; +Promise.prototype._attachCancellationCallback = function(onCancel) { + ; +}; +Promise.prototype._captureStackTrace = function () {}; +Promise.prototype._attachExtraTrace = function () {}; +Promise.prototype._clearCancellationData = function() {}; +Promise.prototype._propagateFrom = function (parent, flags) { + ; + ; +}; + +function cancellationExecute(executor, resolve, reject) { + var promise = this; + try { + executor(resolve, reject, function(onCancel) { + if (typeof onCancel !== "function") { + throw new TypeError("onCancel must be a function, got: " + + util.toString(onCancel)); + } + promise._attachCancellationCallback(onCancel); + }); + } catch (e) { + return e; + } +} + +function cancellationAttachCancellationCallback(onCancel) { + if (!this._isCancellable()) return this; + + var previousOnCancel = this._onCancel(); + if (previousOnCancel !== undefined) { + if (util.isArray(previousOnCancel)) { + previousOnCancel.push(onCancel); + } else { + this._setOnCancel([previousOnCancel, onCancel]); + } + } else { + this._setOnCancel(onCancel); + } +} + +function cancellationOnCancel() { + return this._onCancelField; +} + +function cancellationSetOnCancel(onCancel) { + this._onCancelField = onCancel; +} + +function cancellationClearCancellationData() { + this._cancellationParent = undefined; + this._onCancelField = undefined; +} + +function cancellationPropagateFrom(parent, flags) { + if ((flags & 1) !== 0) { + this._cancellationParent = parent; + var branchesRemainingToCancel = parent._branchesRemainingToCancel; + if (branchesRemainingToCancel === undefined) { + branchesRemainingToCancel = 0; + } + parent._branchesRemainingToCancel = branchesRemainingToCancel + 1; + } + if ((flags & 2) !== 0 && parent._isBound()) { + this._setBoundTo(parent._boundTo); + } +} + +function bindingPropagateFrom(parent, flags) { + if ((flags & 2) !== 0 && parent._isBound()) { + this._setBoundTo(parent._boundTo); + } +} +var propagateFromFunction = bindingPropagateFrom; + +function boundValueFunction() { + var ret = this._boundTo; + if (ret !== undefined) { + if (ret instanceof Promise) { + if (ret.isFulfilled()) { + return ret.value(); + } else { + return undefined; + } + } + } + return ret; +} + +function longStackTracesCaptureStackTrace() { + this._trace = new CapturedTrace(this._peekContext()); +} + +function longStackTracesAttachExtraTrace(error, ignoreSelf) { + if (canAttachTrace(error)) { + var trace = this._trace; + if (trace !== undefined) { + if (ignoreSelf) trace = trace._parent; + } + if (trace !== undefined) { + trace.attachExtraTrace(error); + } else if (!error.__stackCleaned__) { + var parsed = parseStackAndMessage(error); + util.notEnumerableProp(error, "stack", + parsed.message + "\n" + parsed.stack.join("\n")); + util.notEnumerableProp(error, "__stackCleaned__", true); + } + } +} + +function checkForgottenReturns(returnValue, promiseCreated, name, promise, + parent) { + if (returnValue === undefined && promiseCreated !== null && + wForgottenReturn) { + if (parent !== undefined && parent._returnedNonUndefined()) return; + if ((promise._bitField & 65535) === 0) return; + + if (name) name = name + " "; + var handlerLine = ""; + var creatorLine = ""; + if (promiseCreated._trace) { + var traceLines = promiseCreated._trace.stack.split("\n"); + var stack = cleanStack(traceLines); + for (var i = stack.length - 1; i >= 0; --i) { + var line = stack[i]; + if (!nodeFramePattern.test(line)) { + var lineMatches = line.match(parseLinePattern); + if (lineMatches) { + handlerLine = "at " + lineMatches[1] + + ":" + lineMatches[2] + ":" + lineMatches[3] + " "; + } + break; + } + } + + if (stack.length > 0) { + var firstUserLine = stack[0]; + for (var i = 0; i < traceLines.length; ++i) { + + if (traceLines[i] === firstUserLine) { + if (i > 0) { + creatorLine = "\n" + traceLines[i - 1]; + } + break; + } + } + + } + } + var msg = "a promise was created in a " + name + + "handler " + handlerLine + "but was not returned from it, " + + "see http://goo.gl/rRqMUw" + + creatorLine; + promise._warn(msg, true, promiseCreated); + } +} + +function deprecated(name, replacement) { + var message = name + + " is deprecated and will be removed in a future version."; + if (replacement) message += " Use " + replacement + " instead."; + return warn(message); +} + +function warn(message, shouldUseOwnTrace, promise) { + if (!config.warnings) return; + var warning = new Warning(message); + var ctx; + if (shouldUseOwnTrace) { + promise._attachExtraTrace(warning); + } else if (config.longStackTraces && (ctx = Promise._peekContext())) { + ctx.attachExtraTrace(warning); + } else { + var parsed = parseStackAndMessage(warning); + warning.stack = parsed.message + "\n" + parsed.stack.join("\n"); + } + + if (!activeFireEvent("warning", warning)) { + formatAndLogError(warning, "", true); + } +} + +function reconstructStack(message, stacks) { + for (var i = 0; i < stacks.length - 1; ++i) { + stacks[i].push("From previous event:"); + stacks[i] = stacks[i].join("\n"); + } + if (i < stacks.length) { + stacks[i] = stacks[i].join("\n"); + } + return message + "\n" + stacks.join("\n"); +} + +function removeDuplicateOrEmptyJumps(stacks) { + for (var i = 0; i < stacks.length; ++i) { + if (stacks[i].length === 0 || + ((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) { + stacks.splice(i, 1); + i--; + } + } +} + +function removeCommonRoots(stacks) { + var current = stacks[0]; + for (var i = 1; i < stacks.length; ++i) { + var prev = stacks[i]; + var currentLastIndex = current.length - 1; + var currentLastLine = current[currentLastIndex]; + var commonRootMeetPoint = -1; + + for (var j = prev.length - 1; j >= 0; --j) { + if (prev[j] === currentLastLine) { + commonRootMeetPoint = j; + break; + } + } + + for (var j = commonRootMeetPoint; j >= 0; --j) { + var line = prev[j]; + if (current[currentLastIndex] === line) { + current.pop(); + currentLastIndex--; + } else { + break; + } + } + current = prev; + } +} + +function cleanStack(stack) { + var ret = []; + for (var i = 0; i < stack.length; ++i) { + var line = stack[i]; + var isTraceLine = " (No stack trace)" === line || + stackFramePattern.test(line); + var isInternalFrame = isTraceLine && shouldIgnore(line); + if (isTraceLine && !isInternalFrame) { + if (indentStackFrames && line.charAt(0) !== " ") { + line = " " + line; + } + ret.push(line); + } + } + return ret; +} + +function stackFramesAsArray(error) { + var stack = error.stack.replace(/\s+$/g, "").split("\n"); + for (var i = 0; i < stack.length; ++i) { + var line = stack[i]; + if (" (No stack trace)" === line || stackFramePattern.test(line)) { + break; + } + } + if (i > 0 && error.name != "SyntaxError") { + stack = stack.slice(i); + } + return stack; +} + +function parseStackAndMessage(error) { + var stack = error.stack; + var message = error.toString(); + stack = typeof stack === "string" && stack.length > 0 + ? stackFramesAsArray(error) : [" (No stack trace)"]; + return { + message: message, + stack: error.name == "SyntaxError" ? stack : cleanStack(stack) + }; +} + +function formatAndLogError(error, title, isSoft) { + if (typeof console !== "undefined") { + var message; + if (util.isObject(error)) { + var stack = error.stack; + message = title + formatStack(stack, error); + } else { + message = title + String(error); + } + if (typeof printWarning === "function") { + printWarning(message, isSoft); + } else if (typeof console.log === "function" || + typeof console.log === "object") { + console.log(message); + } + } +} + +function fireRejectionEvent(name, localHandler, reason, promise) { + var localEventFired = false; + try { + if (typeof localHandler === "function") { + localEventFired = true; + if (name === "rejectionHandled") { + localHandler(promise); + } else { + localHandler(reason, promise); + } + } + } catch (e) { + async.throwLater(e); + } + + if (name === "unhandledRejection") { + if (!activeFireEvent(name, reason, promise) && !localEventFired) { + formatAndLogError(reason, "Unhandled rejection "); + } + } else { + activeFireEvent(name, promise); + } +} + +function formatNonError(obj) { + var str; + if (typeof obj === "function") { + str = "[function " + + (obj.name || "anonymous") + + "]"; + } else { + str = obj && typeof obj.toString === "function" + ? obj.toString() : util.toString(obj); + var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/; + if (ruselessToString.test(str)) { + try { + var newStr = JSON.stringify(obj); + str = newStr; + } + catch(e) { + + } + } + if (str.length === 0) { + str = "(empty array)"; + } + } + return ("(<" + snip(str) + ">, no stack trace)"); +} + +function snip(str) { + var maxChars = 41; + if (str.length < maxChars) { + return str; + } + return str.substr(0, maxChars - 3) + "..."; +} + +function longStackTracesIsSupported() { + return typeof captureStackTrace === "function"; +} + +var shouldIgnore = function() { return false; }; +var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/; +function parseLineInfo(line) { + var matches = line.match(parseLineInfoRegex); + if (matches) { + return { + fileName: matches[1], + line: parseInt(matches[2], 10) + }; + } +} + +function setBounds(firstLineError, lastLineError) { + if (!longStackTracesIsSupported()) return; + var firstStackLines = firstLineError.stack.split("\n"); + var lastStackLines = lastLineError.stack.split("\n"); + var firstIndex = -1; + var lastIndex = -1; + var firstFileName; + var lastFileName; + for (var i = 0; i < firstStackLines.length; ++i) { + var result = parseLineInfo(firstStackLines[i]); + if (result) { + firstFileName = result.fileName; + firstIndex = result.line; + break; + } + } + for (var i = 0; i < lastStackLines.length; ++i) { + var result = parseLineInfo(lastStackLines[i]); + if (result) { + lastFileName = result.fileName; + lastIndex = result.line; + break; + } + } + if (firstIndex < 0 || lastIndex < 0 || !firstFileName || !lastFileName || + firstFileName !== lastFileName || firstIndex >= lastIndex) { + return; + } + + shouldIgnore = function(line) { + if (bluebirdFramePattern.test(line)) return true; + var info = parseLineInfo(line); + if (info) { + if (info.fileName === firstFileName && + (firstIndex <= info.line && info.line <= lastIndex)) { + return true; + } + } + return false; + }; +} + +function CapturedTrace(parent) { + this._parent = parent; + this._promisesCreated = 0; + var length = this._length = 1 + (parent === undefined ? 0 : parent._length); + captureStackTrace(this, CapturedTrace); + if (length > 32) this.uncycle(); +} +util.inherits(CapturedTrace, Error); +Context.CapturedTrace = CapturedTrace; + +CapturedTrace.prototype.uncycle = function() { + var length = this._length; + if (length < 2) return; + var nodes = []; + var stackToIndex = {}; + + for (var i = 0, node = this; node !== undefined; ++i) { + nodes.push(node); + node = node._parent; + } + length = this._length = i; + for (var i = length - 1; i >= 0; --i) { + var stack = nodes[i].stack; + if (stackToIndex[stack] === undefined) { + stackToIndex[stack] = i; + } + } + for (var i = 0; i < length; ++i) { + var currentStack = nodes[i].stack; + var index = stackToIndex[currentStack]; + if (index !== undefined && index !== i) { + if (index > 0) { + nodes[index - 1]._parent = undefined; + nodes[index - 1]._length = 1; + } + nodes[i]._parent = undefined; + nodes[i]._length = 1; + var cycleEdgeNode = i > 0 ? nodes[i - 1] : this; + + if (index < length - 1) { + cycleEdgeNode._parent = nodes[index + 1]; + cycleEdgeNode._parent.uncycle(); + cycleEdgeNode._length = + cycleEdgeNode._parent._length + 1; + } else { + cycleEdgeNode._parent = undefined; + cycleEdgeNode._length = 1; + } + var currentChildLength = cycleEdgeNode._length + 1; + for (var j = i - 2; j >= 0; --j) { + nodes[j]._length = currentChildLength; + currentChildLength++; + } + return; + } + } +}; + +CapturedTrace.prototype.attachExtraTrace = function(error) { + if (error.__stackCleaned__) return; + this.uncycle(); + var parsed = parseStackAndMessage(error); + var message = parsed.message; + var stacks = [parsed.stack]; + + var trace = this; + while (trace !== undefined) { + stacks.push(cleanStack(trace.stack.split("\n"))); + trace = trace._parent; + } + removeCommonRoots(stacks); + removeDuplicateOrEmptyJumps(stacks); + util.notEnumerableProp(error, "stack", reconstructStack(message, stacks)); + util.notEnumerableProp(error, "__stackCleaned__", true); +}; + +var captureStackTrace = (function stackDetection() { + var v8stackFramePattern = /^\s*at\s*/; + var v8stackFormatter = function(stack, error) { + if (typeof stack === "string") return stack; + + if (error.name !== undefined && + error.message !== undefined) { + return error.toString(); + } + return formatNonError(error); + }; + + if (typeof Error.stackTraceLimit === "number" && + typeof Error.captureStackTrace === "function") { + Error.stackTraceLimit += 6; + stackFramePattern = v8stackFramePattern; + formatStack = v8stackFormatter; + var captureStackTrace = Error.captureStackTrace; + + shouldIgnore = function(line) { + return bluebirdFramePattern.test(line); + }; + return function(receiver, ignoreUntil) { + Error.stackTraceLimit += 6; + captureStackTrace(receiver, ignoreUntil); + Error.stackTraceLimit -= 6; + }; + } + var err = new Error(); + + if (typeof err.stack === "string" && + err.stack.split("\n")[0].indexOf("stackDetection@") >= 0) { + stackFramePattern = /@/; + formatStack = v8stackFormatter; + indentStackFrames = true; + return function captureStackTrace(o) { + o.stack = new Error().stack; + }; + } + + var hasStackAfterThrow; + try { throw new Error(); } + catch(e) { + hasStackAfterThrow = ("stack" in e); + } + if (!("stack" in err) && hasStackAfterThrow && + typeof Error.stackTraceLimit === "number") { + stackFramePattern = v8stackFramePattern; + formatStack = v8stackFormatter; + return function captureStackTrace(o) { + Error.stackTraceLimit += 6; + try { throw new Error(); } + catch(e) { o.stack = e.stack; } + Error.stackTraceLimit -= 6; + }; + } + + formatStack = function(stack, error) { + if (typeof stack === "string") return stack; + + if ((typeof error === "object" || + typeof error === "function") && + error.name !== undefined && + error.message !== undefined) { + return error.toString(); + } + return formatNonError(error); + }; + + return null; + +})([]); + +if (typeof console !== "undefined" && typeof console.warn !== "undefined") { + printWarning = function (message) { + console.warn(message); + }; + if (util.isNode && process.stderr.isTTY) { + printWarning = function(message, isSoft) { + var color = isSoft ? "\u001b[33m" : "\u001b[31m"; + console.warn(color + message + "\u001b[0m\n"); + }; + } else if (!util.isNode && typeof (new Error().stack) === "string") { + printWarning = function(message, isSoft) { + console.warn("%c" + message, + isSoft ? "color: darkorange" : "color: red"); + }; + } +} + +var config = { + warnings: warnings, + longStackTraces: false, + cancellation: false, + monitoring: false +}; + +if (longStackTraces) Promise.longStackTraces(); + +return { + longStackTraces: function() { + return config.longStackTraces; + }, + warnings: function() { + return config.warnings; + }, + cancellation: function() { + return config.cancellation; + }, + monitoring: function() { + return config.monitoring; + }, + propagateFromFunction: function() { + return propagateFromFunction; + }, + boundValueFunction: function() { + return boundValueFunction; + }, + checkForgottenReturns: checkForgottenReturns, + setBounds: setBounds, + warn: warn, + deprecated: deprecated, + CapturedTrace: CapturedTrace, + fireDomEvent: fireDomEvent, + fireGlobalEvent: fireGlobalEvent +}; +}; + +},{"./errors":12,"./util":36}],10:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise) { +function returner() { + return this.value; +} +function thrower() { + throw this.reason; +} + +Promise.prototype["return"] = +Promise.prototype.thenReturn = function (value) { + if (value instanceof Promise) value.suppressUnhandledRejections(); + return this._then( + returner, undefined, undefined, {value: value}, undefined); +}; + +Promise.prototype["throw"] = +Promise.prototype.thenThrow = function (reason) { + return this._then( + thrower, undefined, undefined, {reason: reason}, undefined); +}; + +Promise.prototype.catchThrow = function (reason) { + if (arguments.length <= 1) { + return this._then( + undefined, thrower, undefined, {reason: reason}, undefined); + } else { + var _reason = arguments[1]; + var handler = function() {throw _reason;}; + return this.caught(reason, handler); + } +}; + +Promise.prototype.catchReturn = function (value) { + if (arguments.length <= 1) { + if (value instanceof Promise) value.suppressUnhandledRejections(); + return this._then( + undefined, returner, undefined, {value: value}, undefined); + } else { + var _value = arguments[1]; + if (_value instanceof Promise) _value.suppressUnhandledRejections(); + var handler = function() {return _value;}; + return this.caught(value, handler); + } +}; +}; + +},{}],11:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL) { +var PromiseReduce = Promise.reduce; +var PromiseAll = Promise.all; + +function promiseAllThis() { + return PromiseAll(this); +} + +function PromiseMapSeries(promises, fn) { + return PromiseReduce(promises, fn, INTERNAL, INTERNAL); +} + +Promise.prototype.each = function (fn) { + return PromiseReduce(this, fn, INTERNAL, 0) + ._then(promiseAllThis, undefined, undefined, this, undefined); +}; + +Promise.prototype.mapSeries = function (fn) { + return PromiseReduce(this, fn, INTERNAL, INTERNAL); +}; + +Promise.each = function (promises, fn) { + return PromiseReduce(promises, fn, INTERNAL, 0) + ._then(promiseAllThis, undefined, undefined, promises, undefined); +}; + +Promise.mapSeries = PromiseMapSeries; +}; + + +},{}],12:[function(_dereq_,module,exports){ +"use strict"; +var es5 = _dereq_("./es5"); +var Objectfreeze = es5.freeze; +var util = _dereq_("./util"); +var inherits = util.inherits; +var notEnumerableProp = util.notEnumerableProp; + +function subError(nameProperty, defaultMessage) { + function SubError(message) { + if (!(this instanceof SubError)) return new SubError(message); + notEnumerableProp(this, "message", + typeof message === "string" ? message : defaultMessage); + notEnumerableProp(this, "name", nameProperty); + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } else { + Error.call(this); + } + } + inherits(SubError, Error); + return SubError; +} + +var _TypeError, _RangeError; +var Warning = subError("Warning", "warning"); +var CancellationError = subError("CancellationError", "cancellation error"); +var TimeoutError = subError("TimeoutError", "timeout error"); +var AggregateError = subError("AggregateError", "aggregate error"); +try { + _TypeError = TypeError; + _RangeError = RangeError; +} catch(e) { + _TypeError = subError("TypeError", "type error"); + _RangeError = subError("RangeError", "range error"); +} + +var methods = ("join pop push shift unshift slice filter forEach some " + + "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" "); + +for (var i = 0; i < methods.length; ++i) { + if (typeof Array.prototype[methods[i]] === "function") { + AggregateError.prototype[methods[i]] = Array.prototype[methods[i]]; + } +} + +es5.defineProperty(AggregateError.prototype, "length", { + value: 0, + configurable: false, + writable: true, + enumerable: true +}); +AggregateError.prototype["isOperational"] = true; +var level = 0; +AggregateError.prototype.toString = function() { + var indent = Array(level * 4 + 1).join(" "); + var ret = "\n" + indent + "AggregateError of:" + "\n"; + level++; + indent = Array(level * 4 + 1).join(" "); + for (var i = 0; i < this.length; ++i) { + var str = this[i] === this ? "[Circular AggregateError]" : this[i] + ""; + var lines = str.split("\n"); + for (var j = 0; j < lines.length; ++j) { + lines[j] = indent + lines[j]; + } + str = lines.join("\n"); + ret += str + "\n"; + } + level--; + return ret; +}; + +function OperationalError(message) { + if (!(this instanceof OperationalError)) + return new OperationalError(message); + notEnumerableProp(this, "name", "OperationalError"); + notEnumerableProp(this, "message", message); + this.cause = message; + this["isOperational"] = true; + + if (message instanceof Error) { + notEnumerableProp(this, "message", message.message); + notEnumerableProp(this, "stack", message.stack); + } else if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } + +} +inherits(OperationalError, Error); + +var errorTypes = Error["__BluebirdErrorTypes__"]; +if (!errorTypes) { + errorTypes = Objectfreeze({ + CancellationError: CancellationError, + TimeoutError: TimeoutError, + OperationalError: OperationalError, + RejectionError: OperationalError, + AggregateError: AggregateError + }); + es5.defineProperty(Error, "__BluebirdErrorTypes__", { + value: errorTypes, + writable: false, + enumerable: false, + configurable: false + }); +} + +module.exports = { + Error: Error, + TypeError: _TypeError, + RangeError: _RangeError, + CancellationError: errorTypes.CancellationError, + OperationalError: errorTypes.OperationalError, + TimeoutError: errorTypes.TimeoutError, + AggregateError: errorTypes.AggregateError, + Warning: Warning +}; + +},{"./es5":13,"./util":36}],13:[function(_dereq_,module,exports){ +var isES5 = (function(){ + "use strict"; + return this === undefined; +})(); + +if (isES5) { + module.exports = { + freeze: Object.freeze, + defineProperty: Object.defineProperty, + getDescriptor: Object.getOwnPropertyDescriptor, + keys: Object.keys, + names: Object.getOwnPropertyNames, + getPrototypeOf: Object.getPrototypeOf, + isArray: Array.isArray, + isES5: isES5, + propertyIsWritable: function(obj, prop) { + var descriptor = Object.getOwnPropertyDescriptor(obj, prop); + return !!(!descriptor || descriptor.writable || descriptor.set); + } + }; +} else { + var has = {}.hasOwnProperty; + var str = {}.toString; + var proto = {}.constructor.prototype; + + var ObjectKeys = function (o) { + var ret = []; + for (var key in o) { + if (has.call(o, key)) { + ret.push(key); + } + } + return ret; + }; + + var ObjectGetDescriptor = function(o, key) { + return {value: o[key]}; + }; + + var ObjectDefineProperty = function (o, key, desc) { + o[key] = desc.value; + return o; + }; + + var ObjectFreeze = function (obj) { + return obj; + }; + + var ObjectGetPrototypeOf = function (obj) { + try { + return Object(obj).constructor.prototype; + } + catch (e) { + return proto; + } + }; + + var ArrayIsArray = function (obj) { + try { + return str.call(obj) === "[object Array]"; + } + catch(e) { + return false; + } + }; + + module.exports = { + isArray: ArrayIsArray, + keys: ObjectKeys, + names: ObjectKeys, + defineProperty: ObjectDefineProperty, + getDescriptor: ObjectGetDescriptor, + freeze: ObjectFreeze, + getPrototypeOf: ObjectGetPrototypeOf, + isES5: isES5, + propertyIsWritable: function() { + return true; + } + }; +} + +},{}],14:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL) { +var PromiseMap = Promise.map; + +Promise.prototype.filter = function (fn, options) { + return PromiseMap(this, fn, options, INTERNAL); +}; + +Promise.filter = function (promises, fn, options) { + return PromiseMap(promises, fn, options, INTERNAL); +}; +}; + +},{}],15:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, tryConvertToPromise, NEXT_FILTER) { +var util = _dereq_("./util"); +var CancellationError = Promise.CancellationError; +var errorObj = util.errorObj; +var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER); + +function PassThroughHandlerContext(promise, type, handler) { + this.promise = promise; + this.type = type; + this.handler = handler; + this.called = false; + this.cancelPromise = null; +} + +PassThroughHandlerContext.prototype.isFinallyHandler = function() { + return this.type === 0; +}; + +function FinallyHandlerCancelReaction(finallyHandler) { + this.finallyHandler = finallyHandler; +} + +FinallyHandlerCancelReaction.prototype._resultCancelled = function() { + checkCancel(this.finallyHandler); +}; + +function checkCancel(ctx, reason) { + if (ctx.cancelPromise != null) { + if (arguments.length > 1) { + ctx.cancelPromise._reject(reason); + } else { + ctx.cancelPromise._cancel(); + } + ctx.cancelPromise = null; + return true; + } + return false; +} + +function succeed() { + return finallyHandler.call(this, this.promise._target()._settledValue()); +} +function fail(reason) { + if (checkCancel(this, reason)) return; + errorObj.e = reason; + return errorObj; +} +function finallyHandler(reasonOrValue) { + var promise = this.promise; + var handler = this.handler; + + if (!this.called) { + this.called = true; + var ret = this.isFinallyHandler() + ? handler.call(promise._boundValue()) + : handler.call(promise._boundValue(), reasonOrValue); + if (ret === NEXT_FILTER) { + return ret; + } else if (ret !== undefined) { + promise._setReturnedNonUndefined(); + var maybePromise = tryConvertToPromise(ret, promise); + if (maybePromise instanceof Promise) { + if (this.cancelPromise != null) { + if (maybePromise._isCancelled()) { + var reason = + new CancellationError("late cancellation observer"); + promise._attachExtraTrace(reason); + errorObj.e = reason; + return errorObj; + } else if (maybePromise.isPending()) { + maybePromise._attachCancellationCallback( + new FinallyHandlerCancelReaction(this)); + } + } + return maybePromise._then( + succeed, fail, undefined, this, undefined); + } + } + } + + if (promise.isRejected()) { + checkCancel(this); + errorObj.e = reasonOrValue; + return errorObj; + } else { + checkCancel(this); + return reasonOrValue; + } +} + +Promise.prototype._passThrough = function(handler, type, success, fail) { + if (typeof handler !== "function") return this.then(); + return this._then(success, + fail, + undefined, + new PassThroughHandlerContext(this, type, handler), + undefined); +}; + +Promise.prototype.lastly = +Promise.prototype["finally"] = function (handler) { + return this._passThrough(handler, + 0, + finallyHandler, + finallyHandler); +}; + + +Promise.prototype.tap = function (handler) { + return this._passThrough(handler, 1, finallyHandler); +}; + +Promise.prototype.tapCatch = function (handlerOrPredicate) { + var len = arguments.length; + if(len === 1) { + return this._passThrough(handlerOrPredicate, + 1, + undefined, + finallyHandler); + } else { + var catchInstances = new Array(len - 1), + j = 0, i; + for (i = 0; i < len - 1; ++i) { + var item = arguments[i]; + if (util.isObject(item)) { + catchInstances[j++] = item; + } else { + return Promise.reject(new TypeError( + "tapCatch statement predicate: " + + "expecting an object but got " + util.classString(item) + )); + } + } + catchInstances.length = j; + var handler = arguments[i]; + return this._passThrough(catchFilter(catchInstances, handler, this), + 1, + undefined, + finallyHandler); + } + +}; + +return PassThroughHandlerContext; +}; + +},{"./catch_filter":7,"./util":36}],16:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, + apiRejection, + INTERNAL, + tryConvertToPromise, + Proxyable, + debug) { +var errors = _dereq_("./errors"); +var TypeError = errors.TypeError; +var util = _dereq_("./util"); +var errorObj = util.errorObj; +var tryCatch = util.tryCatch; +var yieldHandlers = []; + +function promiseFromYieldHandler(value, yieldHandlers, traceParent) { + for (var i = 0; i < yieldHandlers.length; ++i) { + traceParent._pushContext(); + var result = tryCatch(yieldHandlers[i])(value); + traceParent._popContext(); + if (result === errorObj) { + traceParent._pushContext(); + var ret = Promise.reject(errorObj.e); + traceParent._popContext(); + return ret; + } + var maybePromise = tryConvertToPromise(result, traceParent); + if (maybePromise instanceof Promise) return maybePromise; + } + return null; +} + +function PromiseSpawn(generatorFunction, receiver, yieldHandler, stack) { + if (debug.cancellation()) { + var internal = new Promise(INTERNAL); + var _finallyPromise = this._finallyPromise = new Promise(INTERNAL); + this._promise = internal.lastly(function() { + return _finallyPromise; + }); + internal._captureStackTrace(); + internal._setOnCancel(this); + } else { + var promise = this._promise = new Promise(INTERNAL); + promise._captureStackTrace(); + } + this._stack = stack; + this._generatorFunction = generatorFunction; + this._receiver = receiver; + this._generator = undefined; + this._yieldHandlers = typeof yieldHandler === "function" + ? [yieldHandler].concat(yieldHandlers) + : yieldHandlers; + this._yieldedPromise = null; + this._cancellationPhase = false; +} +util.inherits(PromiseSpawn, Proxyable); + +PromiseSpawn.prototype._isResolved = function() { + return this._promise === null; +}; + +PromiseSpawn.prototype._cleanup = function() { + this._promise = this._generator = null; + if (debug.cancellation() && this._finallyPromise !== null) { + this._finallyPromise._fulfill(); + this._finallyPromise = null; + } +}; + +PromiseSpawn.prototype._promiseCancelled = function() { + if (this._isResolved()) return; + var implementsReturn = typeof this._generator["return"] !== "undefined"; + + var result; + if (!implementsReturn) { + var reason = new Promise.CancellationError( + "generator .return() sentinel"); + Promise.coroutine.returnSentinel = reason; + this._promise._attachExtraTrace(reason); + this._promise._pushContext(); + result = tryCatch(this._generator["throw"]).call(this._generator, + reason); + this._promise._popContext(); + } else { + this._promise._pushContext(); + result = tryCatch(this._generator["return"]).call(this._generator, + undefined); + this._promise._popContext(); + } + this._cancellationPhase = true; + this._yieldedPromise = null; + this._continue(result); +}; + +PromiseSpawn.prototype._promiseFulfilled = function(value) { + this._yieldedPromise = null; + this._promise._pushContext(); + var result = tryCatch(this._generator.next).call(this._generator, value); + this._promise._popContext(); + this._continue(result); +}; + +PromiseSpawn.prototype._promiseRejected = function(reason) { + this._yieldedPromise = null; + this._promise._attachExtraTrace(reason); + this._promise._pushContext(); + var result = tryCatch(this._generator["throw"]) + .call(this._generator, reason); + this._promise._popContext(); + this._continue(result); +}; + +PromiseSpawn.prototype._resultCancelled = function() { + if (this._yieldedPromise instanceof Promise) { + var promise = this._yieldedPromise; + this._yieldedPromise = null; + promise.cancel(); + } +}; + +PromiseSpawn.prototype.promise = function () { + return this._promise; +}; + +PromiseSpawn.prototype._run = function () { + this._generator = this._generatorFunction.call(this._receiver); + this._receiver = + this._generatorFunction = undefined; + this._promiseFulfilled(undefined); +}; + +PromiseSpawn.prototype._continue = function (result) { + var promise = this._promise; + if (result === errorObj) { + this._cleanup(); + if (this._cancellationPhase) { + return promise.cancel(); + } else { + return promise._rejectCallback(result.e, false); + } + } + + var value = result.value; + if (result.done === true) { + this._cleanup(); + if (this._cancellationPhase) { + return promise.cancel(); + } else { + return promise._resolveCallback(value); + } + } else { + var maybePromise = tryConvertToPromise(value, this._promise); + if (!(maybePromise instanceof Promise)) { + maybePromise = + promiseFromYieldHandler(maybePromise, + this._yieldHandlers, + this._promise); + if (maybePromise === null) { + this._promiseRejected( + new TypeError( + "A value %s was yielded that could not be treated as a promise\u000a\u000a See http://goo.gl/MqrFmX\u000a\u000a".replace("%s", String(value)) + + "From coroutine:\u000a" + + this._stack.split("\n").slice(1, -7).join("\n") + ) + ); + return; + } + } + maybePromise = maybePromise._target(); + var bitField = maybePromise._bitField; + ; + if (((bitField & 50397184) === 0)) { + this._yieldedPromise = maybePromise; + maybePromise._proxy(this, null); + } else if (((bitField & 33554432) !== 0)) { + Promise._async.invoke( + this._promiseFulfilled, this, maybePromise._value() + ); + } else if (((bitField & 16777216) !== 0)) { + Promise._async.invoke( + this._promiseRejected, this, maybePromise._reason() + ); + } else { + this._promiseCancelled(); + } + } +}; + +Promise.coroutine = function (generatorFunction, options) { + if (typeof generatorFunction !== "function") { + throw new TypeError("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + var yieldHandler = Object(options).yieldHandler; + var PromiseSpawn$ = PromiseSpawn; + var stack = new Error().stack; + return function () { + var generator = generatorFunction.apply(this, arguments); + var spawn = new PromiseSpawn$(undefined, undefined, yieldHandler, + stack); + var ret = spawn.promise(); + spawn._generator = generator; + spawn._promiseFulfilled(undefined); + return ret; + }; +}; + +Promise.coroutine.addYieldHandler = function(fn) { + if (typeof fn !== "function") { + throw new TypeError("expecting a function but got " + util.classString(fn)); + } + yieldHandlers.push(fn); +}; + +Promise.spawn = function (generatorFunction) { + debug.deprecated("Promise.spawn()", "Promise.coroutine()"); + if (typeof generatorFunction !== "function") { + return apiRejection("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + var spawn = new PromiseSpawn(generatorFunction, this); + var ret = spawn.promise(); + spawn._run(Promise.spawn); + return ret; +}; +}; + +},{"./errors":12,"./util":36}],17:[function(_dereq_,module,exports){ +"use strict"; +module.exports = +function(Promise, PromiseArray, tryConvertToPromise, INTERNAL, async, + getDomain) { +var util = _dereq_("./util"); +var canEvaluate = util.canEvaluate; +var tryCatch = util.tryCatch; +var errorObj = util.errorObj; +var reject; + +if (!true) { +if (canEvaluate) { + var thenCallback = function(i) { + return new Function("value", "holder", " \n\ + 'use strict'; \n\ + holder.pIndex = value; \n\ + holder.checkFulfillment(this); \n\ + ".replace(/Index/g, i)); + }; + + var promiseSetter = function(i) { + return new Function("promise", "holder", " \n\ + 'use strict'; \n\ + holder.pIndex = promise; \n\ + ".replace(/Index/g, i)); + }; + + var generateHolderClass = function(total) { + var props = new Array(total); + for (var i = 0; i < props.length; ++i) { + props[i] = "this.p" + (i+1); + } + var assignment = props.join(" = ") + " = null;"; + var cancellationCode= "var promise;\n" + props.map(function(prop) { + return " \n\ + promise = " + prop + "; \n\ + if (promise instanceof Promise) { \n\ + promise.cancel(); \n\ + } \n\ + "; + }).join("\n"); + var passedArguments = props.join(", "); + var name = "Holder$" + total; + + + var code = "return function(tryCatch, errorObj, Promise, async) { \n\ + 'use strict'; \n\ + function [TheName](fn) { \n\ + [TheProperties] \n\ + this.fn = fn; \n\ + this.asyncNeeded = true; \n\ + this.now = 0; \n\ + } \n\ + \n\ + [TheName].prototype._callFunction = function(promise) { \n\ + promise._pushContext(); \n\ + var ret = tryCatch(this.fn)([ThePassedArguments]); \n\ + promise._popContext(); \n\ + if (ret === errorObj) { \n\ + promise._rejectCallback(ret.e, false); \n\ + } else { \n\ + promise._resolveCallback(ret); \n\ + } \n\ + }; \n\ + \n\ + [TheName].prototype.checkFulfillment = function(promise) { \n\ + var now = ++this.now; \n\ + if (now === [TheTotal]) { \n\ + if (this.asyncNeeded) { \n\ + async.invoke(this._callFunction, this, promise); \n\ + } else { \n\ + this._callFunction(promise); \n\ + } \n\ + \n\ + } \n\ + }; \n\ + \n\ + [TheName].prototype._resultCancelled = function() { \n\ + [CancellationCode] \n\ + }; \n\ + \n\ + return [TheName]; \n\ + }(tryCatch, errorObj, Promise, async); \n\ + "; + + code = code.replace(/\[TheName\]/g, name) + .replace(/\[TheTotal\]/g, total) + .replace(/\[ThePassedArguments\]/g, passedArguments) + .replace(/\[TheProperties\]/g, assignment) + .replace(/\[CancellationCode\]/g, cancellationCode); + + return new Function("tryCatch", "errorObj", "Promise", "async", code) + (tryCatch, errorObj, Promise, async); + }; + + var holderClasses = []; + var thenCallbacks = []; + var promiseSetters = []; + + for (var i = 0; i < 8; ++i) { + holderClasses.push(generateHolderClass(i + 1)); + thenCallbacks.push(thenCallback(i + 1)); + promiseSetters.push(promiseSetter(i + 1)); + } + + reject = function (reason) { + this._reject(reason); + }; +}} + +Promise.join = function () { + var last = arguments.length - 1; + var fn; + if (last > 0 && typeof arguments[last] === "function") { + fn = arguments[last]; + if (!true) { + if (last <= 8 && canEvaluate) { + var ret = new Promise(INTERNAL); + ret._captureStackTrace(); + var HolderClass = holderClasses[last - 1]; + var holder = new HolderClass(fn); + var callbacks = thenCallbacks; + + for (var i = 0; i < last; ++i) { + var maybePromise = tryConvertToPromise(arguments[i], ret); + if (maybePromise instanceof Promise) { + maybePromise = maybePromise._target(); + var bitField = maybePromise._bitField; + ; + if (((bitField & 50397184) === 0)) { + maybePromise._then(callbacks[i], reject, + undefined, ret, holder); + promiseSetters[i](maybePromise, holder); + holder.asyncNeeded = false; + } else if (((bitField & 33554432) !== 0)) { + callbacks[i].call(ret, + maybePromise._value(), holder); + } else if (((bitField & 16777216) !== 0)) { + ret._reject(maybePromise._reason()); + } else { + ret._cancel(); + } + } else { + callbacks[i].call(ret, maybePromise, holder); + } + } + + if (!ret._isFateSealed()) { + if (holder.asyncNeeded) { + var domain = getDomain(); + if (domain !== null) { + holder.fn = util.domainBind(domain, holder.fn); + } + } + ret._setAsyncGuaranteed(); + ret._setOnCancel(holder); + } + return ret; + } + } + } + var args = [].slice.call(arguments);; + if (fn) args.pop(); + var ret = new PromiseArray(args).promise(); + return fn !== undefined ? ret.spread(fn) : ret; +}; + +}; + +},{"./util":36}],18:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, + PromiseArray, + apiRejection, + tryConvertToPromise, + INTERNAL, + debug) { +var getDomain = Promise._getDomain; +var util = _dereq_("./util"); +var tryCatch = util.tryCatch; +var errorObj = util.errorObj; +var async = Promise._async; + +function MappingPromiseArray(promises, fn, limit, _filter) { + this.constructor$(promises); + this._promise._captureStackTrace(); + var domain = getDomain(); + this._callback = domain === null ? fn : util.domainBind(domain, fn); + this._preservedValues = _filter === INTERNAL + ? new Array(this.length()) + : null; + this._limit = limit; + this._inFlight = 0; + this._queue = []; + async.invoke(this._asyncInit, this, undefined); +} +util.inherits(MappingPromiseArray, PromiseArray); + +MappingPromiseArray.prototype._asyncInit = function() { + this._init$(undefined, -2); +}; + +MappingPromiseArray.prototype._init = function () {}; + +MappingPromiseArray.prototype._promiseFulfilled = function (value, index) { + var values = this._values; + var length = this.length(); + var preservedValues = this._preservedValues; + var limit = this._limit; + + if (index < 0) { + index = (index * -1) - 1; + values[index] = value; + if (limit >= 1) { + this._inFlight--; + this._drainQueue(); + if (this._isResolved()) return true; + } + } else { + if (limit >= 1 && this._inFlight >= limit) { + values[index] = value; + this._queue.push(index); + return false; + } + if (preservedValues !== null) preservedValues[index] = value; + + var promise = this._promise; + var callback = this._callback; + var receiver = promise._boundValue(); + promise._pushContext(); + var ret = tryCatch(callback).call(receiver, value, index, length); + var promiseCreated = promise._popContext(); + debug.checkForgottenReturns( + ret, + promiseCreated, + preservedValues !== null ? "Promise.filter" : "Promise.map", + promise + ); + if (ret === errorObj) { + this._reject(ret.e); + return true; + } + + var maybePromise = tryConvertToPromise(ret, this._promise); + if (maybePromise instanceof Promise) { + maybePromise = maybePromise._target(); + var bitField = maybePromise._bitField; + ; + if (((bitField & 50397184) === 0)) { + if (limit >= 1) this._inFlight++; + values[index] = maybePromise; + maybePromise._proxy(this, (index + 1) * -1); + return false; + } else if (((bitField & 33554432) !== 0)) { + ret = maybePromise._value(); + } else if (((bitField & 16777216) !== 0)) { + this._reject(maybePromise._reason()); + return true; + } else { + this._cancel(); + return true; + } + } + values[index] = ret; + } + var totalResolved = ++this._totalResolved; + if (totalResolved >= length) { + if (preservedValues !== null) { + this._filter(values, preservedValues); + } else { + this._resolve(values); + } + return true; + } + return false; +}; + +MappingPromiseArray.prototype._drainQueue = function () { + var queue = this._queue; + var limit = this._limit; + var values = this._values; + while (queue.length > 0 && this._inFlight < limit) { + if (this._isResolved()) return; + var index = queue.pop(); + this._promiseFulfilled(values[index], index); + } +}; + +MappingPromiseArray.prototype._filter = function (booleans, values) { + var len = values.length; + var ret = new Array(len); + var j = 0; + for (var i = 0; i < len; ++i) { + if (booleans[i]) ret[j++] = values[i]; + } + ret.length = j; + this._resolve(ret); +}; + +MappingPromiseArray.prototype.preservedValues = function () { + return this._preservedValues; +}; + +function map(promises, fn, options, _filter) { + if (typeof fn !== "function") { + return apiRejection("expecting a function but got " + util.classString(fn)); + } + + var limit = 0; + if (options !== undefined) { + if (typeof options === "object" && options !== null) { + if (typeof options.concurrency !== "number") { + return Promise.reject( + new TypeError("'concurrency' must be a number but it is " + + util.classString(options.concurrency))); + } + limit = options.concurrency; + } else { + return Promise.reject(new TypeError( + "options argument must be an object but it is " + + util.classString(options))); + } + } + limit = typeof limit === "number" && + isFinite(limit) && limit >= 1 ? limit : 0; + return new MappingPromiseArray(promises, fn, limit, _filter).promise(); +} + +Promise.prototype.map = function (fn, options) { + return map(this, fn, options, null); +}; + +Promise.map = function (promises, fn, options, _filter) { + return map(promises, fn, options, _filter); +}; + + +}; + +},{"./util":36}],19:[function(_dereq_,module,exports){ +"use strict"; +module.exports = +function(Promise, INTERNAL, tryConvertToPromise, apiRejection, debug) { +var util = _dereq_("./util"); +var tryCatch = util.tryCatch; + +Promise.method = function (fn) { + if (typeof fn !== "function") { + throw new Promise.TypeError("expecting a function but got " + util.classString(fn)); + } + return function () { + var ret = new Promise(INTERNAL); + ret._captureStackTrace(); + ret._pushContext(); + var value = tryCatch(fn).apply(this, arguments); + var promiseCreated = ret._popContext(); + debug.checkForgottenReturns( + value, promiseCreated, "Promise.method", ret); + ret._resolveFromSyncValue(value); + return ret; + }; +}; + +Promise.attempt = Promise["try"] = function (fn) { + if (typeof fn !== "function") { + return apiRejection("expecting a function but got " + util.classString(fn)); + } + var ret = new Promise(INTERNAL); + ret._captureStackTrace(); + ret._pushContext(); + var value; + if (arguments.length > 1) { + debug.deprecated("calling Promise.try with more than 1 argument"); + var arg = arguments[1]; + var ctx = arguments[2]; + value = util.isArray(arg) ? tryCatch(fn).apply(ctx, arg) + : tryCatch(fn).call(ctx, arg); + } else { + value = tryCatch(fn)(); + } + var promiseCreated = ret._popContext(); + debug.checkForgottenReturns( + value, promiseCreated, "Promise.try", ret); + ret._resolveFromSyncValue(value); + return ret; +}; + +Promise.prototype._resolveFromSyncValue = function (value) { + if (value === util.errorObj) { + this._rejectCallback(value.e, false); + } else { + this._resolveCallback(value, true); + } +}; +}; + +},{"./util":36}],20:[function(_dereq_,module,exports){ +"use strict"; +var util = _dereq_("./util"); +var maybeWrapAsError = util.maybeWrapAsError; +var errors = _dereq_("./errors"); +var OperationalError = errors.OperationalError; +var es5 = _dereq_("./es5"); + +function isUntypedError(obj) { + return obj instanceof Error && + es5.getPrototypeOf(obj) === Error.prototype; +} + +var rErrorKey = /^(?:name|message|stack|cause)$/; +function wrapAsOperationalError(obj) { + var ret; + if (isUntypedError(obj)) { + ret = new OperationalError(obj); + ret.name = obj.name; + ret.message = obj.message; + ret.stack = obj.stack; + var keys = es5.keys(obj); + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + if (!rErrorKey.test(key)) { + ret[key] = obj[key]; + } + } + return ret; + } + util.markAsOriginatingFromRejection(obj); + return obj; +} + +function nodebackForPromise(promise, multiArgs) { + return function(err, value) { + if (promise === null) return; + if (err) { + var wrapped = wrapAsOperationalError(maybeWrapAsError(err)); + promise._attachExtraTrace(wrapped); + promise._reject(wrapped); + } else if (!multiArgs) { + promise._fulfill(value); + } else { + var args = [].slice.call(arguments, 1);; + promise._fulfill(args); + } + promise = null; + }; +} + +module.exports = nodebackForPromise; + +},{"./errors":12,"./es5":13,"./util":36}],21:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise) { +var util = _dereq_("./util"); +var async = Promise._async; +var tryCatch = util.tryCatch; +var errorObj = util.errorObj; + +function spreadAdapter(val, nodeback) { + var promise = this; + if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback); + var ret = + tryCatch(nodeback).apply(promise._boundValue(), [null].concat(val)); + if (ret === errorObj) { + async.throwLater(ret.e); + } +} + +function successAdapter(val, nodeback) { + var promise = this; + var receiver = promise._boundValue(); + var ret = val === undefined + ? tryCatch(nodeback).call(receiver, null) + : tryCatch(nodeback).call(receiver, null, val); + if (ret === errorObj) { + async.throwLater(ret.e); + } +} +function errorAdapter(reason, nodeback) { + var promise = this; + if (!reason) { + var newReason = new Error(reason + ""); + newReason.cause = reason; + reason = newReason; + } + var ret = tryCatch(nodeback).call(promise._boundValue(), reason); + if (ret === errorObj) { + async.throwLater(ret.e); + } +} + +Promise.prototype.asCallback = Promise.prototype.nodeify = function (nodeback, + options) { + if (typeof nodeback == "function") { + var adapter = successAdapter; + if (options !== undefined && Object(options).spread) { + adapter = spreadAdapter; + } + this._then( + adapter, + errorAdapter, + undefined, + this, + nodeback + ); + } + return this; +}; +}; + +},{"./util":36}],22:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function() { +var makeSelfResolutionError = function () { + return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/MqrFmX\u000a"); +}; +var reflectHandler = function() { + return new Promise.PromiseInspection(this._target()); +}; +var apiRejection = function(msg) { + return Promise.reject(new TypeError(msg)); +}; +function Proxyable() {} +var UNDEFINED_BINDING = {}; +var util = _dereq_("./util"); + +var getDomain; +if (util.isNode) { + getDomain = function() { + var ret = process.domain; + if (ret === undefined) ret = null; + return ret; + }; +} else { + getDomain = function() { + return null; + }; +} +util.notEnumerableProp(Promise, "_getDomain", getDomain); + +var es5 = _dereq_("./es5"); +var Async = _dereq_("./async"); +var async = new Async(); +es5.defineProperty(Promise, "_async", {value: async}); +var errors = _dereq_("./errors"); +var TypeError = Promise.TypeError = errors.TypeError; +Promise.RangeError = errors.RangeError; +var CancellationError = Promise.CancellationError = errors.CancellationError; +Promise.TimeoutError = errors.TimeoutError; +Promise.OperationalError = errors.OperationalError; +Promise.RejectionError = errors.OperationalError; +Promise.AggregateError = errors.AggregateError; +var INTERNAL = function(){}; +var APPLY = {}; +var NEXT_FILTER = {}; +var tryConvertToPromise = _dereq_("./thenables")(Promise, INTERNAL); +var PromiseArray = + _dereq_("./promise_array")(Promise, INTERNAL, + tryConvertToPromise, apiRejection, Proxyable); +var Context = _dereq_("./context")(Promise); + /*jshint unused:false*/ +var createContext = Context.create; +var debug = _dereq_("./debuggability")(Promise, Context); +var CapturedTrace = debug.CapturedTrace; +var PassThroughHandlerContext = + _dereq_("./finally")(Promise, tryConvertToPromise, NEXT_FILTER); +var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER); +var nodebackForPromise = _dereq_("./nodeback"); +var errorObj = util.errorObj; +var tryCatch = util.tryCatch; +function check(self, executor) { + if (self == null || self.constructor !== Promise) { + throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + if (typeof executor !== "function") { + throw new TypeError("expecting a function but got " + util.classString(executor)); + } + +} + +function Promise(executor) { + if (executor !== INTERNAL) { + check(this, executor); + } + this._bitField = 0; + this._fulfillmentHandler0 = undefined; + this._rejectionHandler0 = undefined; + this._promise0 = undefined; + this._receiver0 = undefined; + this._resolveFromExecutor(executor); + this._promiseCreated(); + this._fireEvent("promiseCreated", this); +} + +Promise.prototype.toString = function () { + return "[object Promise]"; +}; + +Promise.prototype.caught = Promise.prototype["catch"] = function (fn) { + var len = arguments.length; + if (len > 1) { + var catchInstances = new Array(len - 1), + j = 0, i; + for (i = 0; i < len - 1; ++i) { + var item = arguments[i]; + if (util.isObject(item)) { + catchInstances[j++] = item; + } else { + return apiRejection("Catch statement predicate: " + + "expecting an object but got " + util.classString(item)); + } + } + catchInstances.length = j; + fn = arguments[i]; + return this.then(undefined, catchFilter(catchInstances, fn, this)); + } + return this.then(undefined, fn); +}; + +Promise.prototype.reflect = function () { + return this._then(reflectHandler, + reflectHandler, undefined, this, undefined); +}; + +Promise.prototype.then = function (didFulfill, didReject) { + if (debug.warnings() && arguments.length > 0 && + typeof didFulfill !== "function" && + typeof didReject !== "function") { + var msg = ".then() only accepts functions but was passed: " + + util.classString(didFulfill); + if (arguments.length > 1) { + msg += ", " + util.classString(didReject); + } + this._warn(msg); + } + return this._then(didFulfill, didReject, undefined, undefined, undefined); +}; + +Promise.prototype.done = function (didFulfill, didReject) { + var promise = + this._then(didFulfill, didReject, undefined, undefined, undefined); + promise._setIsFinal(); +}; + +Promise.prototype.spread = function (fn) { + if (typeof fn !== "function") { + return apiRejection("expecting a function but got " + util.classString(fn)); + } + return this.all()._then(fn, undefined, undefined, APPLY, undefined); +}; + +Promise.prototype.toJSON = function () { + var ret = { + isFulfilled: false, + isRejected: false, + fulfillmentValue: undefined, + rejectionReason: undefined + }; + if (this.isFulfilled()) { + ret.fulfillmentValue = this.value(); + ret.isFulfilled = true; + } else if (this.isRejected()) { + ret.rejectionReason = this.reason(); + ret.isRejected = true; + } + return ret; +}; + +Promise.prototype.all = function () { + if (arguments.length > 0) { + this._warn(".all() was passed arguments but it does not take any"); + } + return new PromiseArray(this).promise(); +}; + +Promise.prototype.error = function (fn) { + return this.caught(util.originatesFromRejection, fn); +}; + +Promise.getNewLibraryCopy = module.exports; + +Promise.is = function (val) { + return val instanceof Promise; +}; + +Promise.fromNode = Promise.fromCallback = function(fn) { + var ret = new Promise(INTERNAL); + ret._captureStackTrace(); + var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs + : false; + var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs)); + if (result === errorObj) { + ret._rejectCallback(result.e, true); + } + if (!ret._isFateSealed()) ret._setAsyncGuaranteed(); + return ret; +}; + +Promise.all = function (promises) { + return new PromiseArray(promises).promise(); +}; + +Promise.cast = function (obj) { + var ret = tryConvertToPromise(obj); + if (!(ret instanceof Promise)) { + ret = new Promise(INTERNAL); + ret._captureStackTrace(); + ret._setFulfilled(); + ret._rejectionHandler0 = obj; + } + return ret; +}; + +Promise.resolve = Promise.fulfilled = Promise.cast; + +Promise.reject = Promise.rejected = function (reason) { + var ret = new Promise(INTERNAL); + ret._captureStackTrace(); + ret._rejectCallback(reason, true); + return ret; +}; + +Promise.setScheduler = function(fn) { + if (typeof fn !== "function") { + throw new TypeError("expecting a function but got " + util.classString(fn)); + } + return async.setScheduler(fn); +}; + +Promise.prototype._then = function ( + didFulfill, + didReject, + _, receiver, + internalData +) { + var haveInternalData = internalData !== undefined; + var promise = haveInternalData ? internalData : new Promise(INTERNAL); + var target = this._target(); + var bitField = target._bitField; + + if (!haveInternalData) { + promise._propagateFrom(this, 3); + promise._captureStackTrace(); + if (receiver === undefined && + ((this._bitField & 2097152) !== 0)) { + if (!((bitField & 50397184) === 0)) { + receiver = this._boundValue(); + } else { + receiver = target === this ? undefined : this._boundTo; + } + } + this._fireEvent("promiseChained", this, promise); + } + + var domain = getDomain(); + if (!((bitField & 50397184) === 0)) { + var handler, value, settler = target._settlePromiseCtx; + if (((bitField & 33554432) !== 0)) { + value = target._rejectionHandler0; + handler = didFulfill; + } else if (((bitField & 16777216) !== 0)) { + value = target._fulfillmentHandler0; + handler = didReject; + target._unsetRejectionIsUnhandled(); + } else { + settler = target._settlePromiseLateCancellationObserver; + value = new CancellationError("late cancellation observer"); + target._attachExtraTrace(value); + handler = didReject; + } + + async.invoke(settler, target, { + handler: domain === null ? handler + : (typeof handler === "function" && + util.domainBind(domain, handler)), + promise: promise, + receiver: receiver, + value: value + }); + } else { + target._addCallbacks(didFulfill, didReject, promise, receiver, domain); + } + + return promise; +}; + +Promise.prototype._length = function () { + return this._bitField & 65535; +}; + +Promise.prototype._isFateSealed = function () { + return (this._bitField & 117506048) !== 0; +}; + +Promise.prototype._isFollowing = function () { + return (this._bitField & 67108864) === 67108864; +}; + +Promise.prototype._setLength = function (len) { + this._bitField = (this._bitField & -65536) | + (len & 65535); +}; + +Promise.prototype._setFulfilled = function () { + this._bitField = this._bitField | 33554432; + this._fireEvent("promiseFulfilled", this); +}; + +Promise.prototype._setRejected = function () { + this._bitField = this._bitField | 16777216; + this._fireEvent("promiseRejected", this); +}; + +Promise.prototype._setFollowing = function () { + this._bitField = this._bitField | 67108864; + this._fireEvent("promiseResolved", this); +}; + +Promise.prototype._setIsFinal = function () { + this._bitField = this._bitField | 4194304; +}; + +Promise.prototype._isFinal = function () { + return (this._bitField & 4194304) > 0; +}; + +Promise.prototype._unsetCancelled = function() { + this._bitField = this._bitField & (~65536); +}; + +Promise.prototype._setCancelled = function() { + this._bitField = this._bitField | 65536; + this._fireEvent("promiseCancelled", this); +}; + +Promise.prototype._setWillBeCancelled = function() { + this._bitField = this._bitField | 8388608; +}; + +Promise.prototype._setAsyncGuaranteed = function() { + if (async.hasCustomScheduler()) return; + this._bitField = this._bitField | 134217728; +}; + +Promise.prototype._receiverAt = function (index) { + var ret = index === 0 ? this._receiver0 : this[ + index * 4 - 4 + 3]; + if (ret === UNDEFINED_BINDING) { + return undefined; + } else if (ret === undefined && this._isBound()) { + return this._boundValue(); + } + return ret; +}; + +Promise.prototype._promiseAt = function (index) { + return this[ + index * 4 - 4 + 2]; +}; + +Promise.prototype._fulfillmentHandlerAt = function (index) { + return this[ + index * 4 - 4 + 0]; +}; + +Promise.prototype._rejectionHandlerAt = function (index) { + return this[ + index * 4 - 4 + 1]; +}; + +Promise.prototype._boundValue = function() {}; + +Promise.prototype._migrateCallback0 = function (follower) { + var bitField = follower._bitField; + var fulfill = follower._fulfillmentHandler0; + var reject = follower._rejectionHandler0; + var promise = follower._promise0; + var receiver = follower._receiverAt(0); + if (receiver === undefined) receiver = UNDEFINED_BINDING; + this._addCallbacks(fulfill, reject, promise, receiver, null); +}; + +Promise.prototype._migrateCallbackAt = function (follower, index) { + var fulfill = follower._fulfillmentHandlerAt(index); + var reject = follower._rejectionHandlerAt(index); + var promise = follower._promiseAt(index); + var receiver = follower._receiverAt(index); + if (receiver === undefined) receiver = UNDEFINED_BINDING; + this._addCallbacks(fulfill, reject, promise, receiver, null); +}; + +Promise.prototype._addCallbacks = function ( + fulfill, + reject, + promise, + receiver, + domain +) { + var index = this._length(); + + if (index >= 65535 - 4) { + index = 0; + this._setLength(0); + } + + if (index === 0) { + this._promise0 = promise; + this._receiver0 = receiver; + if (typeof fulfill === "function") { + this._fulfillmentHandler0 = + domain === null ? fulfill : util.domainBind(domain, fulfill); + } + if (typeof reject === "function") { + this._rejectionHandler0 = + domain === null ? reject : util.domainBind(domain, reject); + } + } else { + var base = index * 4 - 4; + this[base + 2] = promise; + this[base + 3] = receiver; + if (typeof fulfill === "function") { + this[base + 0] = + domain === null ? fulfill : util.domainBind(domain, fulfill); + } + if (typeof reject === "function") { + this[base + 1] = + domain === null ? reject : util.domainBind(domain, reject); + } + } + this._setLength(index + 1); + return index; +}; + +Promise.prototype._proxy = function (proxyable, arg) { + this._addCallbacks(undefined, undefined, arg, proxyable, null); +}; + +Promise.prototype._resolveCallback = function(value, shouldBind) { + if (((this._bitField & 117506048) !== 0)) return; + if (value === this) + return this._rejectCallback(makeSelfResolutionError(), false); + var maybePromise = tryConvertToPromise(value, this); + if (!(maybePromise instanceof Promise)) return this._fulfill(value); + + if (shouldBind) this._propagateFrom(maybePromise, 2); + + var promise = maybePromise._target(); + + if (promise === this) { + this._reject(makeSelfResolutionError()); + return; + } + + var bitField = promise._bitField; + if (((bitField & 50397184) === 0)) { + var len = this._length(); + if (len > 0) promise._migrateCallback0(this); + for (var i = 1; i < len; ++i) { + promise._migrateCallbackAt(this, i); + } + this._setFollowing(); + this._setLength(0); + this._setFollowee(promise); + } else if (((bitField & 33554432) !== 0)) { + this._fulfill(promise._value()); + } else if (((bitField & 16777216) !== 0)) { + this._reject(promise._reason()); + } else { + var reason = new CancellationError("late cancellation observer"); + promise._attachExtraTrace(reason); + this._reject(reason); + } +}; + +Promise.prototype._rejectCallback = +function(reason, synchronous, ignoreNonErrorWarnings) { + var trace = util.ensureErrorObject(reason); + var hasStack = trace === reason; + if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) { + var message = "a promise was rejected with a non-error: " + + util.classString(reason); + this._warn(message, true); + } + this._attachExtraTrace(trace, synchronous ? hasStack : false); + this._reject(reason); +}; + +Promise.prototype._resolveFromExecutor = function (executor) { + if (executor === INTERNAL) return; + var promise = this; + this._captureStackTrace(); + this._pushContext(); + var synchronous = true; + var r = this._execute(executor, function(value) { + promise._resolveCallback(value); + }, function (reason) { + promise._rejectCallback(reason, synchronous); + }); + synchronous = false; + this._popContext(); + + if (r !== undefined) { + promise._rejectCallback(r, true); + } +}; + +Promise.prototype._settlePromiseFromHandler = function ( + handler, receiver, value, promise +) { + var bitField = promise._bitField; + if (((bitField & 65536) !== 0)) return; + promise._pushContext(); + var x; + if (receiver === APPLY) { + if (!value || typeof value.length !== "number") { + x = errorObj; + x.e = new TypeError("cannot .spread() a non-array: " + + util.classString(value)); + } else { + x = tryCatch(handler).apply(this._boundValue(), value); + } + } else { + x = tryCatch(handler).call(receiver, value); + } + var promiseCreated = promise._popContext(); + bitField = promise._bitField; + if (((bitField & 65536) !== 0)) return; + + if (x === NEXT_FILTER) { + promise._reject(value); + } else if (x === errorObj) { + promise._rejectCallback(x.e, false); + } else { + debug.checkForgottenReturns(x, promiseCreated, "", promise, this); + promise._resolveCallback(x); + } +}; + +Promise.prototype._target = function() { + var ret = this; + while (ret._isFollowing()) ret = ret._followee(); + return ret; +}; + +Promise.prototype._followee = function() { + return this._rejectionHandler0; +}; + +Promise.prototype._setFollowee = function(promise) { + this._rejectionHandler0 = promise; +}; + +Promise.prototype._settlePromise = function(promise, handler, receiver, value) { + var isPromise = promise instanceof Promise; + var bitField = this._bitField; + var asyncGuaranteed = ((bitField & 134217728) !== 0); + if (((bitField & 65536) !== 0)) { + if (isPromise) promise._invokeInternalOnCancel(); + + if (receiver instanceof PassThroughHandlerContext && + receiver.isFinallyHandler()) { + receiver.cancelPromise = promise; + if (tryCatch(handler).call(receiver, value) === errorObj) { + promise._reject(errorObj.e); + } + } else if (handler === reflectHandler) { + promise._fulfill(reflectHandler.call(receiver)); + } else if (receiver instanceof Proxyable) { + receiver._promiseCancelled(promise); + } else if (isPromise || promise instanceof PromiseArray) { + promise._cancel(); + } else { + receiver.cancel(); + } + } else if (typeof handler === "function") { + if (!isPromise) { + handler.call(receiver, value, promise); + } else { + if (asyncGuaranteed) promise._setAsyncGuaranteed(); + this._settlePromiseFromHandler(handler, receiver, value, promise); + } + } else if (receiver instanceof Proxyable) { + if (!receiver._isResolved()) { + if (((bitField & 33554432) !== 0)) { + receiver._promiseFulfilled(value, promise); + } else { + receiver._promiseRejected(value, promise); + } + } + } else if (isPromise) { + if (asyncGuaranteed) promise._setAsyncGuaranteed(); + if (((bitField & 33554432) !== 0)) { + promise._fulfill(value); + } else { + promise._reject(value); + } + } +}; + +Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) { + var handler = ctx.handler; + var promise = ctx.promise; + var receiver = ctx.receiver; + var value = ctx.value; + if (typeof handler === "function") { + if (!(promise instanceof Promise)) { + handler.call(receiver, value, promise); + } else { + this._settlePromiseFromHandler(handler, receiver, value, promise); + } + } else if (promise instanceof Promise) { + promise._reject(value); + } +}; + +Promise.prototype._settlePromiseCtx = function(ctx) { + this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value); +}; + +Promise.prototype._settlePromise0 = function(handler, value, bitField) { + var promise = this._promise0; + var receiver = this._receiverAt(0); + this._promise0 = undefined; + this._receiver0 = undefined; + this._settlePromise(promise, handler, receiver, value); +}; + +Promise.prototype._clearCallbackDataAtIndex = function(index) { + var base = index * 4 - 4; + this[base + 2] = + this[base + 3] = + this[base + 0] = + this[base + 1] = undefined; +}; + +Promise.prototype._fulfill = function (value) { + var bitField = this._bitField; + if (((bitField & 117506048) >>> 16)) return; + if (value === this) { + var err = makeSelfResolutionError(); + this._attachExtraTrace(err); + return this._reject(err); + } + this._setFulfilled(); + this._rejectionHandler0 = value; + + if ((bitField & 65535) > 0) { + if (((bitField & 134217728) !== 0)) { + this._settlePromises(); + } else { + async.settlePromises(this); + } + } +}; + +Promise.prototype._reject = function (reason) { + var bitField = this._bitField; + if (((bitField & 117506048) >>> 16)) return; + this._setRejected(); + this._fulfillmentHandler0 = reason; + + if (this._isFinal()) { + return async.fatalError(reason, util.isNode); + } + + if ((bitField & 65535) > 0) { + async.settlePromises(this); + } else { + this._ensurePossibleRejectionHandled(); + } +}; + +Promise.prototype._fulfillPromises = function (len, value) { + for (var i = 1; i < len; i++) { + var handler = this._fulfillmentHandlerAt(i); + var promise = this._promiseAt(i); + var receiver = this._receiverAt(i); + this._clearCallbackDataAtIndex(i); + this._settlePromise(promise, handler, receiver, value); + } +}; + +Promise.prototype._rejectPromises = function (len, reason) { + for (var i = 1; i < len; i++) { + var handler = this._rejectionHandlerAt(i); + var promise = this._promiseAt(i); + var receiver = this._receiverAt(i); + this._clearCallbackDataAtIndex(i); + this._settlePromise(promise, handler, receiver, reason); + } +}; + +Promise.prototype._settlePromises = function () { + var bitField = this._bitField; + var len = (bitField & 65535); + + if (len > 0) { + if (((bitField & 16842752) !== 0)) { + var reason = this._fulfillmentHandler0; + this._settlePromise0(this._rejectionHandler0, reason, bitField); + this._rejectPromises(len, reason); + } else { + var value = this._rejectionHandler0; + this._settlePromise0(this._fulfillmentHandler0, value, bitField); + this._fulfillPromises(len, value); + } + this._setLength(0); + } + this._clearCancellationData(); +}; + +Promise.prototype._settledValue = function() { + var bitField = this._bitField; + if (((bitField & 33554432) !== 0)) { + return this._rejectionHandler0; + } else if (((bitField & 16777216) !== 0)) { + return this._fulfillmentHandler0; + } +}; + +function deferResolve(v) {this.promise._resolveCallback(v);} +function deferReject(v) {this.promise._rejectCallback(v, false);} + +Promise.defer = Promise.pending = function() { + debug.deprecated("Promise.defer", "new Promise"); + var promise = new Promise(INTERNAL); + return { + promise: promise, + resolve: deferResolve, + reject: deferReject + }; +}; + +util.notEnumerableProp(Promise, + "_makeSelfResolutionError", + makeSelfResolutionError); + +_dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection, + debug); +_dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug); +_dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug); +_dereq_("./direct_resolve")(Promise); +_dereq_("./synchronous_inspection")(Promise); +_dereq_("./join")( + Promise, PromiseArray, tryConvertToPromise, INTERNAL, async, getDomain); +Promise.Promise = Promise; +Promise.version = "3.5.1"; +_dereq_('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug); +_dereq_('./call_get.js')(Promise); +_dereq_('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext, INTERNAL, debug); +_dereq_('./timers.js')(Promise, INTERNAL, debug); +_dereq_('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise, Proxyable, debug); +_dereq_('./nodeify.js')(Promise); +_dereq_('./promisify.js')(Promise, INTERNAL); +_dereq_('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection); +_dereq_('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection); +_dereq_('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug); +_dereq_('./settle.js')(Promise, PromiseArray, debug); +_dereq_('./some.js')(Promise, PromiseArray, apiRejection); +_dereq_('./filter.js')(Promise, INTERNAL); +_dereq_('./each.js')(Promise, INTERNAL); +_dereq_('./any.js')(Promise); + + util.toFastProperties(Promise); + util.toFastProperties(Promise.prototype); + function fillTypes(value) { + var p = new Promise(INTERNAL); + p._fulfillmentHandler0 = value; + p._rejectionHandler0 = value; + p._promise0 = value; + p._receiver0 = value; + } + // Complete slack tracking, opt out of field-type tracking and + // stabilize map + fillTypes({a: 1}); + fillTypes({b: 2}); + fillTypes({c: 3}); + fillTypes(1); + fillTypes(function(){}); + fillTypes(undefined); + fillTypes(false); + fillTypes(new Promise(INTERNAL)); + debug.setBounds(Async.firstLineError, util.lastLineError); + return Promise; + +}; + +},{"./any.js":1,"./async":2,"./bind":3,"./call_get.js":5,"./cancel":6,"./catch_filter":7,"./context":8,"./debuggability":9,"./direct_resolve":10,"./each.js":11,"./errors":12,"./es5":13,"./filter.js":14,"./finally":15,"./generators.js":16,"./join":17,"./map.js":18,"./method":19,"./nodeback":20,"./nodeify.js":21,"./promise_array":23,"./promisify.js":24,"./props.js":25,"./race.js":27,"./reduce.js":28,"./settle.js":30,"./some.js":31,"./synchronous_inspection":32,"./thenables":33,"./timers.js":34,"./using.js":35,"./util":36}],23:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL, tryConvertToPromise, + apiRejection, Proxyable) { +var util = _dereq_("./util"); +var isArray = util.isArray; + +function toResolutionValue(val) { + switch(val) { + case -2: return []; + case -3: return {}; + case -6: return new Map(); + } +} + +function PromiseArray(values) { + var promise = this._promise = new Promise(INTERNAL); + if (values instanceof Promise) { + promise._propagateFrom(values, 3); + } + promise._setOnCancel(this); + this._values = values; + this._length = 0; + this._totalResolved = 0; + this._init(undefined, -2); +} +util.inherits(PromiseArray, Proxyable); + +PromiseArray.prototype.length = function () { + return this._length; +}; + +PromiseArray.prototype.promise = function () { + return this._promise; +}; + +PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) { + var values = tryConvertToPromise(this._values, this._promise); + if (values instanceof Promise) { + values = values._target(); + var bitField = values._bitField; + ; + this._values = values; + + if (((bitField & 50397184) === 0)) { + this._promise._setAsyncGuaranteed(); + return values._then( + init, + this._reject, + undefined, + this, + resolveValueIfEmpty + ); + } else if (((bitField & 33554432) !== 0)) { + values = values._value(); + } else if (((bitField & 16777216) !== 0)) { + return this._reject(values._reason()); + } else { + return this._cancel(); + } + } + values = util.asArray(values); + if (values === null) { + var err = apiRejection( + "expecting an array or an iterable object but got " + util.classString(values)).reason(); + this._promise._rejectCallback(err, false); + return; + } + + if (values.length === 0) { + if (resolveValueIfEmpty === -5) { + this._resolveEmptyArray(); + } + else { + this._resolve(toResolutionValue(resolveValueIfEmpty)); + } + return; + } + this._iterate(values); +}; + +PromiseArray.prototype._iterate = function(values) { + var len = this.getActualLength(values.length); + this._length = len; + this._values = this.shouldCopyValues() ? new Array(len) : this._values; + var result = this._promise; + var isResolved = false; + var bitField = null; + for (var i = 0; i < len; ++i) { + var maybePromise = tryConvertToPromise(values[i], result); + + if (maybePromise instanceof Promise) { + maybePromise = maybePromise._target(); + bitField = maybePromise._bitField; + } else { + bitField = null; + } + + if (isResolved) { + if (bitField !== null) { + maybePromise.suppressUnhandledRejections(); + } + } else if (bitField !== null) { + if (((bitField & 50397184) === 0)) { + maybePromise._proxy(this, i); + this._values[i] = maybePromise; + } else if (((bitField & 33554432) !== 0)) { + isResolved = this._promiseFulfilled(maybePromise._value(), i); + } else if (((bitField & 16777216) !== 0)) { + isResolved = this._promiseRejected(maybePromise._reason(), i); + } else { + isResolved = this._promiseCancelled(i); + } + } else { + isResolved = this._promiseFulfilled(maybePromise, i); + } + } + if (!isResolved) result._setAsyncGuaranteed(); +}; + +PromiseArray.prototype._isResolved = function () { + return this._values === null; +}; + +PromiseArray.prototype._resolve = function (value) { + this._values = null; + this._promise._fulfill(value); +}; + +PromiseArray.prototype._cancel = function() { + if (this._isResolved() || !this._promise._isCancellable()) return; + this._values = null; + this._promise._cancel(); +}; + +PromiseArray.prototype._reject = function (reason) { + this._values = null; + this._promise._rejectCallback(reason, false); +}; + +PromiseArray.prototype._promiseFulfilled = function (value, index) { + this._values[index] = value; + var totalResolved = ++this._totalResolved; + if (totalResolved >= this._length) { + this._resolve(this._values); + return true; + } + return false; +}; + +PromiseArray.prototype._promiseCancelled = function() { + this._cancel(); + return true; +}; + +PromiseArray.prototype._promiseRejected = function (reason) { + this._totalResolved++; + this._reject(reason); + return true; +}; + +PromiseArray.prototype._resultCancelled = function() { + if (this._isResolved()) return; + var values = this._values; + this._cancel(); + if (values instanceof Promise) { + values.cancel(); + } else { + for (var i = 0; i < values.length; ++i) { + if (values[i] instanceof Promise) { + values[i].cancel(); + } + } + } +}; + +PromiseArray.prototype.shouldCopyValues = function () { + return true; +}; + +PromiseArray.prototype.getActualLength = function (len) { + return len; +}; + +return PromiseArray; +}; + +},{"./util":36}],24:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL) { +var THIS = {}; +var util = _dereq_("./util"); +var nodebackForPromise = _dereq_("./nodeback"); +var withAppended = util.withAppended; +var maybeWrapAsError = util.maybeWrapAsError; +var canEvaluate = util.canEvaluate; +var TypeError = _dereq_("./errors").TypeError; +var defaultSuffix = "Async"; +var defaultPromisified = {__isPromisified__: true}; +var noCopyProps = [ + "arity", "length", + "name", + "arguments", + "caller", + "callee", + "prototype", + "__isPromisified__" +]; +var noCopyPropsPattern = new RegExp("^(?:" + noCopyProps.join("|") + ")$"); + +var defaultFilter = function(name) { + return util.isIdentifier(name) && + name.charAt(0) !== "_" && + name !== "constructor"; +}; + +function propsFilter(key) { + return !noCopyPropsPattern.test(key); +} + +function isPromisified(fn) { + try { + return fn.__isPromisified__ === true; + } + catch (e) { + return false; + } +} + +function hasPromisified(obj, key, suffix) { + var val = util.getDataPropertyOrDefault(obj, key + suffix, + defaultPromisified); + return val ? isPromisified(val) : false; +} +function checkValid(ret, suffix, suffixRegexp) { + for (var i = 0; i < ret.length; i += 2) { + var key = ret[i]; + if (suffixRegexp.test(key)) { + var keyWithoutAsyncSuffix = key.replace(suffixRegexp, ""); + for (var j = 0; j < ret.length; j += 2) { + if (ret[j] === keyWithoutAsyncSuffix) { + throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/MqrFmX\u000a" + .replace("%s", suffix)); + } + } + } + } +} + +function promisifiableMethods(obj, suffix, suffixRegexp, filter) { + var keys = util.inheritedDataKeys(obj); + var ret = []; + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + var value = obj[key]; + var passesDefaultFilter = filter === defaultFilter + ? true : defaultFilter(key, value, obj); + if (typeof value === "function" && + !isPromisified(value) && + !hasPromisified(obj, key, suffix) && + filter(key, value, obj, passesDefaultFilter)) { + ret.push(key, value); + } + } + checkValid(ret, suffix, suffixRegexp); + return ret; +} + +var escapeIdentRegex = function(str) { + return str.replace(/([$])/, "\\$"); +}; + +var makeNodePromisifiedEval; +if (!true) { +var switchCaseArgumentOrder = function(likelyArgumentCount) { + var ret = [likelyArgumentCount]; + var min = Math.max(0, likelyArgumentCount - 1 - 3); + for(var i = likelyArgumentCount - 1; i >= min; --i) { + ret.push(i); + } + for(var i = likelyArgumentCount + 1; i <= 3; ++i) { + ret.push(i); + } + return ret; +}; + +var argumentSequence = function(argumentCount) { + return util.filledRange(argumentCount, "_arg", ""); +}; + +var parameterDeclaration = function(parameterCount) { + return util.filledRange( + Math.max(parameterCount, 3), "_arg", ""); +}; + +var parameterCount = function(fn) { + if (typeof fn.length === "number") { + return Math.max(Math.min(fn.length, 1023 + 1), 0); + } + return 0; +}; + +makeNodePromisifiedEval = +function(callback, receiver, originalName, fn, _, multiArgs) { + var newParameterCount = Math.max(0, parameterCount(fn) - 1); + var argumentOrder = switchCaseArgumentOrder(newParameterCount); + var shouldProxyThis = typeof callback === "string" || receiver === THIS; + + function generateCallForArgumentCount(count) { + var args = argumentSequence(count).join(", "); + var comma = count > 0 ? ", " : ""; + var ret; + if (shouldProxyThis) { + ret = "ret = callback.call(this, {{args}}, nodeback); break;\n"; + } else { + ret = receiver === undefined + ? "ret = callback({{args}}, nodeback); break;\n" + : "ret = callback.call(receiver, {{args}}, nodeback); break;\n"; + } + return ret.replace("{{args}}", args).replace(", ", comma); + } + + function generateArgumentSwitchCase() { + var ret = ""; + for (var i = 0; i < argumentOrder.length; ++i) { + ret += "case " + argumentOrder[i] +":" + + generateCallForArgumentCount(argumentOrder[i]); + } + + ret += " \n\ + default: \n\ + var args = new Array(len + 1); \n\ + var i = 0; \n\ + for (var i = 0; i < len; ++i) { \n\ + args[i] = arguments[i]; \n\ + } \n\ + args[i] = nodeback; \n\ + [CodeForCall] \n\ + break; \n\ + ".replace("[CodeForCall]", (shouldProxyThis + ? "ret = callback.apply(this, args);\n" + : "ret = callback.apply(receiver, args);\n")); + return ret; + } + + var getFunctionCode = typeof callback === "string" + ? ("this != null ? this['"+callback+"'] : fn") + : "fn"; + var body = "'use strict'; \n\ + var ret = function (Parameters) { \n\ + 'use strict'; \n\ + var len = arguments.length; \n\ + var promise = new Promise(INTERNAL); \n\ + promise._captureStackTrace(); \n\ + var nodeback = nodebackForPromise(promise, " + multiArgs + "); \n\ + var ret; \n\ + var callback = tryCatch([GetFunctionCode]); \n\ + switch(len) { \n\ + [CodeForSwitchCase] \n\ + } \n\ + if (ret === errorObj) { \n\ + promise._rejectCallback(maybeWrapAsError(ret.e), true, true);\n\ + } \n\ + if (!promise._isFateSealed()) promise._setAsyncGuaranteed(); \n\ + return promise; \n\ + }; \n\ + notEnumerableProp(ret, '__isPromisified__', true); \n\ + return ret; \n\ + ".replace("[CodeForSwitchCase]", generateArgumentSwitchCase()) + .replace("[GetFunctionCode]", getFunctionCode); + body = body.replace("Parameters", parameterDeclaration(newParameterCount)); + return new Function("Promise", + "fn", + "receiver", + "withAppended", + "maybeWrapAsError", + "nodebackForPromise", + "tryCatch", + "errorObj", + "notEnumerableProp", + "INTERNAL", + body)( + Promise, + fn, + receiver, + withAppended, + maybeWrapAsError, + nodebackForPromise, + util.tryCatch, + util.errorObj, + util.notEnumerableProp, + INTERNAL); +}; +} + +function makeNodePromisifiedClosure(callback, receiver, _, fn, __, multiArgs) { + var defaultThis = (function() {return this;})(); + var method = callback; + if (typeof method === "string") { + callback = fn; + } + function promisified() { + var _receiver = receiver; + if (receiver === THIS) _receiver = this; + var promise = new Promise(INTERNAL); + promise._captureStackTrace(); + var cb = typeof method === "string" && this !== defaultThis + ? this[method] : callback; + var fn = nodebackForPromise(promise, multiArgs); + try { + cb.apply(_receiver, withAppended(arguments, fn)); + } catch(e) { + promise._rejectCallback(maybeWrapAsError(e), true, true); + } + if (!promise._isFateSealed()) promise._setAsyncGuaranteed(); + return promise; + } + util.notEnumerableProp(promisified, "__isPromisified__", true); + return promisified; +} + +var makeNodePromisified = canEvaluate + ? makeNodePromisifiedEval + : makeNodePromisifiedClosure; + +function promisifyAll(obj, suffix, filter, promisifier, multiArgs) { + var suffixRegexp = new RegExp(escapeIdentRegex(suffix) + "$"); + var methods = + promisifiableMethods(obj, suffix, suffixRegexp, filter); + + for (var i = 0, len = methods.length; i < len; i+= 2) { + var key = methods[i]; + var fn = methods[i+1]; + var promisifiedKey = key + suffix; + if (promisifier === makeNodePromisified) { + obj[promisifiedKey] = + makeNodePromisified(key, THIS, key, fn, suffix, multiArgs); + } else { + var promisified = promisifier(fn, function() { + return makeNodePromisified(key, THIS, key, + fn, suffix, multiArgs); + }); + util.notEnumerableProp(promisified, "__isPromisified__", true); + obj[promisifiedKey] = promisified; + } + } + util.toFastProperties(obj); + return obj; +} + +function promisify(callback, receiver, multiArgs) { + return makeNodePromisified(callback, receiver, undefined, + callback, null, multiArgs); +} + +Promise.promisify = function (fn, options) { + if (typeof fn !== "function") { + throw new TypeError("expecting a function but got " + util.classString(fn)); + } + if (isPromisified(fn)) { + return fn; + } + options = Object(options); + var receiver = options.context === undefined ? THIS : options.context; + var multiArgs = !!options.multiArgs; + var ret = promisify(fn, receiver, multiArgs); + util.copyDescriptors(fn, ret, propsFilter); + return ret; +}; + +Promise.promisifyAll = function (target, options) { + if (typeof target !== "function" && typeof target !== "object") { + throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + options = Object(options); + var multiArgs = !!options.multiArgs; + var suffix = options.suffix; + if (typeof suffix !== "string") suffix = defaultSuffix; + var filter = options.filter; + if (typeof filter !== "function") filter = defaultFilter; + var promisifier = options.promisifier; + if (typeof promisifier !== "function") promisifier = makeNodePromisified; + + if (!util.isIdentifier(suffix)) { + throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + + var keys = util.inheritedDataKeys(target); + for (var i = 0; i < keys.length; ++i) { + var value = target[keys[i]]; + if (keys[i] !== "constructor" && + util.isClass(value)) { + promisifyAll(value.prototype, suffix, filter, promisifier, + multiArgs); + promisifyAll(value, suffix, filter, promisifier, multiArgs); + } + } + + return promisifyAll(target, suffix, filter, promisifier, multiArgs); +}; +}; + + +},{"./errors":12,"./nodeback":20,"./util":36}],25:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function( + Promise, PromiseArray, tryConvertToPromise, apiRejection) { +var util = _dereq_("./util"); +var isObject = util.isObject; +var es5 = _dereq_("./es5"); +var Es6Map; +if (typeof Map === "function") Es6Map = Map; + +var mapToEntries = (function() { + var index = 0; + var size = 0; + + function extractEntry(value, key) { + this[index] = value; + this[index + size] = key; + index++; + } + + return function mapToEntries(map) { + size = map.size; + index = 0; + var ret = new Array(map.size * 2); + map.forEach(extractEntry, ret); + return ret; + }; +})(); + +var entriesToMap = function(entries) { + var ret = new Es6Map(); + var length = entries.length / 2 | 0; + for (var i = 0; i < length; ++i) { + var key = entries[length + i]; + var value = entries[i]; + ret.set(key, value); + } + return ret; +}; + +function PropertiesPromiseArray(obj) { + var isMap = false; + var entries; + if (Es6Map !== undefined && obj instanceof Es6Map) { + entries = mapToEntries(obj); + isMap = true; + } else { + var keys = es5.keys(obj); + var len = keys.length; + entries = new Array(len * 2); + for (var i = 0; i < len; ++i) { + var key = keys[i]; + entries[i] = obj[key]; + entries[i + len] = key; + } + } + this.constructor$(entries); + this._isMap = isMap; + this._init$(undefined, isMap ? -6 : -3); +} +util.inherits(PropertiesPromiseArray, PromiseArray); + +PropertiesPromiseArray.prototype._init = function () {}; + +PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) { + this._values[index] = value; + var totalResolved = ++this._totalResolved; + if (totalResolved >= this._length) { + var val; + if (this._isMap) { + val = entriesToMap(this._values); + } else { + val = {}; + var keyOffset = this.length(); + for (var i = 0, len = this.length(); i < len; ++i) { + val[this._values[i + keyOffset]] = this._values[i]; + } + } + this._resolve(val); + return true; + } + return false; +}; + +PropertiesPromiseArray.prototype.shouldCopyValues = function () { + return false; +}; + +PropertiesPromiseArray.prototype.getActualLength = function (len) { + return len >> 1; +}; + +function props(promises) { + var ret; + var castValue = tryConvertToPromise(promises); + + if (!isObject(castValue)) { + return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } else if (castValue instanceof Promise) { + ret = castValue._then( + Promise.props, undefined, undefined, undefined, undefined); + } else { + ret = new PropertiesPromiseArray(castValue).promise(); + } + + if (castValue instanceof Promise) { + ret._propagateFrom(castValue, 2); + } + return ret; +} + +Promise.prototype.props = function () { + return props(this); +}; + +Promise.props = function (promises) { + return props(promises); +}; +}; + +},{"./es5":13,"./util":36}],26:[function(_dereq_,module,exports){ +"use strict"; +function arrayMove(src, srcIndex, dst, dstIndex, len) { + for (var j = 0; j < len; ++j) { + dst[j + dstIndex] = src[j + srcIndex]; + src[j + srcIndex] = void 0; + } +} + +function Queue(capacity) { + this._capacity = capacity; + this._length = 0; + this._front = 0; +} + +Queue.prototype._willBeOverCapacity = function (size) { + return this._capacity < size; +}; + +Queue.prototype._pushOne = function (arg) { + var length = this.length(); + this._checkCapacity(length + 1); + var i = (this._front + length) & (this._capacity - 1); + this[i] = arg; + this._length = length + 1; +}; + +Queue.prototype.push = function (fn, receiver, arg) { + var length = this.length() + 3; + if (this._willBeOverCapacity(length)) { + this._pushOne(fn); + this._pushOne(receiver); + this._pushOne(arg); + return; + } + var j = this._front + length - 3; + this._checkCapacity(length); + var wrapMask = this._capacity - 1; + this[(j + 0) & wrapMask] = fn; + this[(j + 1) & wrapMask] = receiver; + this[(j + 2) & wrapMask] = arg; + this._length = length; +}; + +Queue.prototype.shift = function () { + var front = this._front, + ret = this[front]; + + this[front] = undefined; + this._front = (front + 1) & (this._capacity - 1); + this._length--; + return ret; +}; + +Queue.prototype.length = function () { + return this._length; +}; + +Queue.prototype._checkCapacity = function (size) { + if (this._capacity < size) { + this._resizeTo(this._capacity << 1); + } +}; + +Queue.prototype._resizeTo = function (capacity) { + var oldCapacity = this._capacity; + this._capacity = capacity; + var front = this._front; + var length = this._length; + var moveItemsCount = (front + length) & (oldCapacity - 1); + arrayMove(this, 0, this, oldCapacity, moveItemsCount); +}; + +module.exports = Queue; + +},{}],27:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function( + Promise, INTERNAL, tryConvertToPromise, apiRejection) { +var util = _dereq_("./util"); + +var raceLater = function (promise) { + return promise.then(function(array) { + return race(array, promise); + }); +}; + +function race(promises, parent) { + var maybePromise = tryConvertToPromise(promises); + + if (maybePromise instanceof Promise) { + return raceLater(maybePromise); + } else { + promises = util.asArray(promises); + if (promises === null) + return apiRejection("expecting an array or an iterable object but got " + util.classString(promises)); + } + + var ret = new Promise(INTERNAL); + if (parent !== undefined) { + ret._propagateFrom(parent, 3); + } + var fulfill = ret._fulfill; + var reject = ret._reject; + for (var i = 0, len = promises.length; i < len; ++i) { + var val = promises[i]; + + if (val === undefined && !(i in promises)) { + continue; + } + + Promise.cast(val)._then(fulfill, reject, undefined, ret, null); + } + return ret; +} + +Promise.race = function (promises) { + return race(promises, undefined); +}; + +Promise.prototype.race = function () { + return race(this, undefined); +}; + +}; + +},{"./util":36}],28:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, + PromiseArray, + apiRejection, + tryConvertToPromise, + INTERNAL, + debug) { +var getDomain = Promise._getDomain; +var util = _dereq_("./util"); +var tryCatch = util.tryCatch; + +function ReductionPromiseArray(promises, fn, initialValue, _each) { + this.constructor$(promises); + var domain = getDomain(); + this._fn = domain === null ? fn : util.domainBind(domain, fn); + if (initialValue !== undefined) { + initialValue = Promise.resolve(initialValue); + initialValue._attachCancellationCallback(this); + } + this._initialValue = initialValue; + this._currentCancellable = null; + if(_each === INTERNAL) { + this._eachValues = Array(this._length); + } else if (_each === 0) { + this._eachValues = null; + } else { + this._eachValues = undefined; + } + this._promise._captureStackTrace(); + this._init$(undefined, -5); +} +util.inherits(ReductionPromiseArray, PromiseArray); + +ReductionPromiseArray.prototype._gotAccum = function(accum) { + if (this._eachValues !== undefined && + this._eachValues !== null && + accum !== INTERNAL) { + this._eachValues.push(accum); + } +}; + +ReductionPromiseArray.prototype._eachComplete = function(value) { + if (this._eachValues !== null) { + this._eachValues.push(value); + } + return this._eachValues; +}; + +ReductionPromiseArray.prototype._init = function() {}; + +ReductionPromiseArray.prototype._resolveEmptyArray = function() { + this._resolve(this._eachValues !== undefined ? this._eachValues + : this._initialValue); +}; + +ReductionPromiseArray.prototype.shouldCopyValues = function () { + return false; +}; + +ReductionPromiseArray.prototype._resolve = function(value) { + this._promise._resolveCallback(value); + this._values = null; +}; + +ReductionPromiseArray.prototype._resultCancelled = function(sender) { + if (sender === this._initialValue) return this._cancel(); + if (this._isResolved()) return; + this._resultCancelled$(); + if (this._currentCancellable instanceof Promise) { + this._currentCancellable.cancel(); + } + if (this._initialValue instanceof Promise) { + this._initialValue.cancel(); + } +}; + +ReductionPromiseArray.prototype._iterate = function (values) { + this._values = values; + var value; + var i; + var length = values.length; + if (this._initialValue !== undefined) { + value = this._initialValue; + i = 0; + } else { + value = Promise.resolve(values[0]); + i = 1; + } + + this._currentCancellable = value; + + if (!value.isRejected()) { + for (; i < length; ++i) { + var ctx = { + accum: null, + value: values[i], + index: i, + length: length, + array: this + }; + value = value._then(gotAccum, undefined, undefined, ctx, undefined); + } + } + + if (this._eachValues !== undefined) { + value = value + ._then(this._eachComplete, undefined, undefined, this, undefined); + } + value._then(completed, completed, undefined, value, this); +}; + +Promise.prototype.reduce = function (fn, initialValue) { + return reduce(this, fn, initialValue, null); +}; + +Promise.reduce = function (promises, fn, initialValue, _each) { + return reduce(promises, fn, initialValue, _each); +}; + +function completed(valueOrReason, array) { + if (this.isFulfilled()) { + array._resolve(valueOrReason); + } else { + array._reject(valueOrReason); + } +} + +function reduce(promises, fn, initialValue, _each) { + if (typeof fn !== "function") { + return apiRejection("expecting a function but got " + util.classString(fn)); + } + var array = new ReductionPromiseArray(promises, fn, initialValue, _each); + return array.promise(); +} + +function gotAccum(accum) { + this.accum = accum; + this.array._gotAccum(accum); + var value = tryConvertToPromise(this.value, this.array._promise); + if (value instanceof Promise) { + this.array._currentCancellable = value; + return value._then(gotValue, undefined, undefined, this, undefined); + } else { + return gotValue.call(this, value); + } +} + +function gotValue(value) { + var array = this.array; + var promise = array._promise; + var fn = tryCatch(array._fn); + promise._pushContext(); + var ret; + if (array._eachValues !== undefined) { + ret = fn.call(promise._boundValue(), value, this.index, this.length); + } else { + ret = fn.call(promise._boundValue(), + this.accum, value, this.index, this.length); + } + if (ret instanceof Promise) { + array._currentCancellable = ret; + } + var promiseCreated = promise._popContext(); + debug.checkForgottenReturns( + ret, + promiseCreated, + array._eachValues !== undefined ? "Promise.each" : "Promise.reduce", + promise + ); + return ret; +} +}; + +},{"./util":36}],29:[function(_dereq_,module,exports){ +"use strict"; +var util = _dereq_("./util"); +var schedule; +var noAsyncScheduler = function() { + throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a"); +}; +var NativePromise = util.getNativePromise(); +if (util.isNode && typeof MutationObserver === "undefined") { + var GlobalSetImmediate = global.setImmediate; + var ProcessNextTick = process.nextTick; + schedule = util.isRecentNode + ? function(fn) { GlobalSetImmediate.call(global, fn); } + : function(fn) { ProcessNextTick.call(process, fn); }; +} else if (typeof NativePromise === "function" && + typeof NativePromise.resolve === "function") { + var nativePromise = NativePromise.resolve(); + schedule = function(fn) { + nativePromise.then(fn); + }; +} else if ((typeof MutationObserver !== "undefined") && + !(typeof window !== "undefined" && + window.navigator && + (window.navigator.standalone || window.cordova))) { + schedule = (function() { + var div = document.createElement("div"); + var opts = {attributes: true}; + var toggleScheduled = false; + var div2 = document.createElement("div"); + var o2 = new MutationObserver(function() { + div.classList.toggle("foo"); + toggleScheduled = false; + }); + o2.observe(div2, opts); + + var scheduleToggle = function() { + if (toggleScheduled) return; + toggleScheduled = true; + div2.classList.toggle("foo"); + }; + + return function schedule(fn) { + var o = new MutationObserver(function() { + o.disconnect(); + fn(); + }); + o.observe(div, opts); + scheduleToggle(); + }; + })(); +} else if (typeof setImmediate !== "undefined") { + schedule = function (fn) { + setImmediate(fn); + }; +} else if (typeof setTimeout !== "undefined") { + schedule = function (fn) { + setTimeout(fn, 0); + }; +} else { + schedule = noAsyncScheduler; +} +module.exports = schedule; + +},{"./util":36}],30:[function(_dereq_,module,exports){ +"use strict"; +module.exports = + function(Promise, PromiseArray, debug) { +var PromiseInspection = Promise.PromiseInspection; +var util = _dereq_("./util"); + +function SettledPromiseArray(values) { + this.constructor$(values); +} +util.inherits(SettledPromiseArray, PromiseArray); + +SettledPromiseArray.prototype._promiseResolved = function (index, inspection) { + this._values[index] = inspection; + var totalResolved = ++this._totalResolved; + if (totalResolved >= this._length) { + this._resolve(this._values); + return true; + } + return false; +}; + +SettledPromiseArray.prototype._promiseFulfilled = function (value, index) { + var ret = new PromiseInspection(); + ret._bitField = 33554432; + ret._settledValueField = value; + return this._promiseResolved(index, ret); +}; +SettledPromiseArray.prototype._promiseRejected = function (reason, index) { + var ret = new PromiseInspection(); + ret._bitField = 16777216; + ret._settledValueField = reason; + return this._promiseResolved(index, ret); +}; + +Promise.settle = function (promises) { + debug.deprecated(".settle()", ".reflect()"); + return new SettledPromiseArray(promises).promise(); +}; + +Promise.prototype.settle = function () { + return Promise.settle(this); +}; +}; + +},{"./util":36}],31:[function(_dereq_,module,exports){ +"use strict"; +module.exports = +function(Promise, PromiseArray, apiRejection) { +var util = _dereq_("./util"); +var RangeError = _dereq_("./errors").RangeError; +var AggregateError = _dereq_("./errors").AggregateError; +var isArray = util.isArray; +var CANCELLATION = {}; + + +function SomePromiseArray(values) { + this.constructor$(values); + this._howMany = 0; + this._unwrap = false; + this._initialized = false; +} +util.inherits(SomePromiseArray, PromiseArray); + +SomePromiseArray.prototype._init = function () { + if (!this._initialized) { + return; + } + if (this._howMany === 0) { + this._resolve([]); + return; + } + this._init$(undefined, -5); + var isArrayResolved = isArray(this._values); + if (!this._isResolved() && + isArrayResolved && + this._howMany > this._canPossiblyFulfill()) { + this._reject(this._getRangeError(this.length())); + } +}; + +SomePromiseArray.prototype.init = function () { + this._initialized = true; + this._init(); +}; + +SomePromiseArray.prototype.setUnwrap = function () { + this._unwrap = true; +}; + +SomePromiseArray.prototype.howMany = function () { + return this._howMany; +}; + +SomePromiseArray.prototype.setHowMany = function (count) { + this._howMany = count; +}; + +SomePromiseArray.prototype._promiseFulfilled = function (value) { + this._addFulfilled(value); + if (this._fulfilled() === this.howMany()) { + this._values.length = this.howMany(); + if (this.howMany() === 1 && this._unwrap) { + this._resolve(this._values[0]); + } else { + this._resolve(this._values); + } + return true; + } + return false; + +}; +SomePromiseArray.prototype._promiseRejected = function (reason) { + this._addRejected(reason); + return this._checkOutcome(); +}; + +SomePromiseArray.prototype._promiseCancelled = function () { + if (this._values instanceof Promise || this._values == null) { + return this._cancel(); + } + this._addRejected(CANCELLATION); + return this._checkOutcome(); +}; + +SomePromiseArray.prototype._checkOutcome = function() { + if (this.howMany() > this._canPossiblyFulfill()) { + var e = new AggregateError(); + for (var i = this.length(); i < this._values.length; ++i) { + if (this._values[i] !== CANCELLATION) { + e.push(this._values[i]); + } + } + if (e.length > 0) { + this._reject(e); + } else { + this._cancel(); + } + return true; + } + return false; +}; + +SomePromiseArray.prototype._fulfilled = function () { + return this._totalResolved; +}; + +SomePromiseArray.prototype._rejected = function () { + return this._values.length - this.length(); +}; + +SomePromiseArray.prototype._addRejected = function (reason) { + this._values.push(reason); +}; + +SomePromiseArray.prototype._addFulfilled = function (value) { + this._values[this._totalResolved++] = value; +}; + +SomePromiseArray.prototype._canPossiblyFulfill = function () { + return this.length() - this._rejected(); +}; + +SomePromiseArray.prototype._getRangeError = function (count) { + var message = "Input array must contain at least " + + this._howMany + " items but contains only " + count + " items"; + return new RangeError(message); +}; + +SomePromiseArray.prototype._resolveEmptyArray = function () { + this._reject(this._getRangeError(0)); +}; + +function some(promises, howMany) { + if ((howMany | 0) !== howMany || howMany < 0) { + return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + var ret = new SomePromiseArray(promises); + var promise = ret.promise(); + ret.setHowMany(howMany); + ret.init(); + return promise; +} + +Promise.some = function (promises, howMany) { + return some(promises, howMany); +}; + +Promise.prototype.some = function (howMany) { + return some(this, howMany); +}; + +Promise._SomePromiseArray = SomePromiseArray; +}; + +},{"./errors":12,"./util":36}],32:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise) { +function PromiseInspection(promise) { + if (promise !== undefined) { + promise = promise._target(); + this._bitField = promise._bitField; + this._settledValueField = promise._isFateSealed() + ? promise._settledValue() : undefined; + } + else { + this._bitField = 0; + this._settledValueField = undefined; + } +} + +PromiseInspection.prototype._settledValue = function() { + return this._settledValueField; +}; + +var value = PromiseInspection.prototype.value = function () { + if (!this.isFulfilled()) { + throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + return this._settledValue(); +}; + +var reason = PromiseInspection.prototype.error = +PromiseInspection.prototype.reason = function () { + if (!this.isRejected()) { + throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a"); + } + return this._settledValue(); +}; + +var isFulfilled = PromiseInspection.prototype.isFulfilled = function() { + return (this._bitField & 33554432) !== 0; +}; + +var isRejected = PromiseInspection.prototype.isRejected = function () { + return (this._bitField & 16777216) !== 0; +}; + +var isPending = PromiseInspection.prototype.isPending = function () { + return (this._bitField & 50397184) === 0; +}; + +var isResolved = PromiseInspection.prototype.isResolved = function () { + return (this._bitField & 50331648) !== 0; +}; + +PromiseInspection.prototype.isCancelled = function() { + return (this._bitField & 8454144) !== 0; +}; + +Promise.prototype.__isCancelled = function() { + return (this._bitField & 65536) === 65536; +}; + +Promise.prototype._isCancelled = function() { + return this._target().__isCancelled(); +}; + +Promise.prototype.isCancelled = function() { + return (this._target()._bitField & 8454144) !== 0; +}; + +Promise.prototype.isPending = function() { + return isPending.call(this._target()); +}; + +Promise.prototype.isRejected = function() { + return isRejected.call(this._target()); +}; + +Promise.prototype.isFulfilled = function() { + return isFulfilled.call(this._target()); +}; + +Promise.prototype.isResolved = function() { + return isResolved.call(this._target()); +}; + +Promise.prototype.value = function() { + return value.call(this._target()); +}; + +Promise.prototype.reason = function() { + var target = this._target(); + target._unsetRejectionIsUnhandled(); + return reason.call(target); +}; + +Promise.prototype._value = function() { + return this._settledValue(); +}; + +Promise.prototype._reason = function() { + this._unsetRejectionIsUnhandled(); + return this._settledValue(); +}; + +Promise.PromiseInspection = PromiseInspection; +}; + +},{}],33:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL) { +var util = _dereq_("./util"); +var errorObj = util.errorObj; +var isObject = util.isObject; + +function tryConvertToPromise(obj, context) { + if (isObject(obj)) { + if (obj instanceof Promise) return obj; + var then = getThen(obj); + if (then === errorObj) { + if (context) context._pushContext(); + var ret = Promise.reject(then.e); + if (context) context._popContext(); + return ret; + } else if (typeof then === "function") { + if (isAnyBluebirdPromise(obj)) { + var ret = new Promise(INTERNAL); + obj._then( + ret._fulfill, + ret._reject, + undefined, + ret, + null + ); + return ret; + } + return doThenable(obj, then, context); + } + } + return obj; +} + +function doGetThen(obj) { + return obj.then; +} + +function getThen(obj) { + try { + return doGetThen(obj); + } catch (e) { + errorObj.e = e; + return errorObj; + } +} + +var hasProp = {}.hasOwnProperty; +function isAnyBluebirdPromise(obj) { + try { + return hasProp.call(obj, "_promise0"); + } catch (e) { + return false; + } +} + +function doThenable(x, then, context) { + var promise = new Promise(INTERNAL); + var ret = promise; + if (context) context._pushContext(); + promise._captureStackTrace(); + if (context) context._popContext(); + var synchronous = true; + var result = util.tryCatch(then).call(x, resolve, reject); + synchronous = false; + + if (promise && result === errorObj) { + promise._rejectCallback(result.e, true, true); + promise = null; + } + + function resolve(value) { + if (!promise) return; + promise._resolveCallback(value); + promise = null; + } + + function reject(reason) { + if (!promise) return; + promise._rejectCallback(reason, synchronous, true); + promise = null; + } + return ret; +} + +return tryConvertToPromise; +}; + +},{"./util":36}],34:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function(Promise, INTERNAL, debug) { +var util = _dereq_("./util"); +var TimeoutError = Promise.TimeoutError; + +function HandleWrapper(handle) { + this.handle = handle; +} + +HandleWrapper.prototype._resultCancelled = function() { + clearTimeout(this.handle); +}; + +var afterValue = function(value) { return delay(+this).thenReturn(value); }; +var delay = Promise.delay = function (ms, value) { + var ret; + var handle; + if (value !== undefined) { + ret = Promise.resolve(value) + ._then(afterValue, null, null, ms, undefined); + if (debug.cancellation() && value instanceof Promise) { + ret._setOnCancel(value); + } + } else { + ret = new Promise(INTERNAL); + handle = setTimeout(function() { ret._fulfill(); }, +ms); + if (debug.cancellation()) { + ret._setOnCancel(new HandleWrapper(handle)); + } + ret._captureStackTrace(); + } + ret._setAsyncGuaranteed(); + return ret; +}; + +Promise.prototype.delay = function (ms) { + return delay(ms, this); +}; + +var afterTimeout = function (promise, message, parent) { + var err; + if (typeof message !== "string") { + if (message instanceof Error) { + err = message; + } else { + err = new TimeoutError("operation timed out"); + } + } else { + err = new TimeoutError(message); + } + util.markAsOriginatingFromRejection(err); + promise._attachExtraTrace(err); + promise._reject(err); + + if (parent != null) { + parent.cancel(); + } +}; + +function successClear(value) { + clearTimeout(this.handle); + return value; +} + +function failureClear(reason) { + clearTimeout(this.handle); + throw reason; +} + +Promise.prototype.timeout = function (ms, message) { + ms = +ms; + var ret, parent; + + var handleWrapper = new HandleWrapper(setTimeout(function timeoutTimeout() { + if (ret.isPending()) { + afterTimeout(ret, message, parent); + } + }, ms)); + + if (debug.cancellation()) { + parent = this.then(); + ret = parent._then(successClear, failureClear, + undefined, handleWrapper, undefined); + ret._setOnCancel(handleWrapper); + } else { + ret = this._then(successClear, failureClear, + undefined, handleWrapper, undefined); + } + + return ret; +}; + +}; + +},{"./util":36}],35:[function(_dereq_,module,exports){ +"use strict"; +module.exports = function (Promise, apiRejection, tryConvertToPromise, + createContext, INTERNAL, debug) { + var util = _dereq_("./util"); + var TypeError = _dereq_("./errors").TypeError; + var inherits = _dereq_("./util").inherits; + var errorObj = util.errorObj; + var tryCatch = util.tryCatch; + var NULL = {}; + + function thrower(e) { + setTimeout(function(){throw e;}, 0); + } + + function castPreservingDisposable(thenable) { + var maybePromise = tryConvertToPromise(thenable); + if (maybePromise !== thenable && + typeof thenable._isDisposable === "function" && + typeof thenable._getDisposer === "function" && + thenable._isDisposable()) { + maybePromise._setDisposable(thenable._getDisposer()); + } + return maybePromise; + } + function dispose(resources, inspection) { + var i = 0; + var len = resources.length; + var ret = new Promise(INTERNAL); + function iterator() { + if (i >= len) return ret._fulfill(); + var maybePromise = castPreservingDisposable(resources[i++]); + if (maybePromise instanceof Promise && + maybePromise._isDisposable()) { + try { + maybePromise = tryConvertToPromise( + maybePromise._getDisposer().tryDispose(inspection), + resources.promise); + } catch (e) { + return thrower(e); + } + if (maybePromise instanceof Promise) { + return maybePromise._then(iterator, thrower, + null, null, null); + } + } + iterator(); + } + iterator(); + return ret; + } + + function Disposer(data, promise, context) { + this._data = data; + this._promise = promise; + this._context = context; + } + + Disposer.prototype.data = function () { + return this._data; + }; + + Disposer.prototype.promise = function () { + return this._promise; + }; + + Disposer.prototype.resource = function () { + if (this.promise().isFulfilled()) { + return this.promise().value(); + } + return NULL; + }; + + Disposer.prototype.tryDispose = function(inspection) { + var resource = this.resource(); + var context = this._context; + if (context !== undefined) context._pushContext(); + var ret = resource !== NULL + ? this.doDispose(resource, inspection) : null; + if (context !== undefined) context._popContext(); + this._promise._unsetDisposable(); + this._data = null; + return ret; + }; + + Disposer.isDisposer = function (d) { + return (d != null && + typeof d.resource === "function" && + typeof d.tryDispose === "function"); + }; + + function FunctionDisposer(fn, promise, context) { + this.constructor$(fn, promise, context); + } + inherits(FunctionDisposer, Disposer); + + FunctionDisposer.prototype.doDispose = function (resource, inspection) { + var fn = this.data(); + return fn.call(resource, resource, inspection); + }; + + function maybeUnwrapDisposer(value) { + if (Disposer.isDisposer(value)) { + this.resources[this.index]._setDisposable(value); + return value.promise(); + } + return value; + } + + function ResourceList(length) { + this.length = length; + this.promise = null; + this[length-1] = null; + } + + ResourceList.prototype._resultCancelled = function() { + var len = this.length; + for (var i = 0; i < len; ++i) { + var item = this[i]; + if (item instanceof Promise) { + item.cancel(); + } + } + }; + + Promise.using = function () { + var len = arguments.length; + if (len < 2) return apiRejection( + "you must pass at least 2 arguments to Promise.using"); + var fn = arguments[len - 1]; + if (typeof fn !== "function") { + return apiRejection("expecting a function but got " + util.classString(fn)); + } + var input; + var spreadArgs = true; + if (len === 2 && Array.isArray(arguments[0])) { + input = arguments[0]; + len = input.length; + spreadArgs = false; + } else { + input = arguments; + len--; + } + var resources = new ResourceList(len); + for (var i = 0; i < len; ++i) { + var resource = input[i]; + if (Disposer.isDisposer(resource)) { + var disposer = resource; + resource = resource.promise(); + resource._setDisposable(disposer); + } else { + var maybePromise = tryConvertToPromise(resource); + if (maybePromise instanceof Promise) { + resource = + maybePromise._then(maybeUnwrapDisposer, null, null, { + resources: resources, + index: i + }, undefined); + } + } + resources[i] = resource; + } + + var reflectedResources = new Array(resources.length); + for (var i = 0; i < reflectedResources.length; ++i) { + reflectedResources[i] = Promise.resolve(resources[i]).reflect(); + } + + var resultPromise = Promise.all(reflectedResources) + .then(function(inspections) { + for (var i = 0; i < inspections.length; ++i) { + var inspection = inspections[i]; + if (inspection.isRejected()) { + errorObj.e = inspection.error(); + return errorObj; + } else if (!inspection.isFulfilled()) { + resultPromise.cancel(); + return; + } + inspections[i] = inspection.value(); + } + promise._pushContext(); + + fn = tryCatch(fn); + var ret = spreadArgs + ? fn.apply(undefined, inspections) : fn(inspections); + var promiseCreated = promise._popContext(); + debug.checkForgottenReturns( + ret, promiseCreated, "Promise.using", promise); + return ret; + }); + + var promise = resultPromise.lastly(function() { + var inspection = new Promise.PromiseInspection(resultPromise); + return dispose(resources, inspection); + }); + resources.promise = promise; + promise._setOnCancel(resources); + return promise; + }; + + Promise.prototype._setDisposable = function (disposer) { + this._bitField = this._bitField | 131072; + this._disposer = disposer; + }; + + Promise.prototype._isDisposable = function () { + return (this._bitField & 131072) > 0; + }; + + Promise.prototype._getDisposer = function () { + return this._disposer; + }; + + Promise.prototype._unsetDisposable = function () { + this._bitField = this._bitField & (~131072); + this._disposer = undefined; + }; + + Promise.prototype.disposer = function (fn) { + if (typeof fn === "function") { + return new FunctionDisposer(fn, this, createContext()); + } + throw new TypeError(); + }; + +}; + +},{"./errors":12,"./util":36}],36:[function(_dereq_,module,exports){ +"use strict"; +var es5 = _dereq_("./es5"); +var canEvaluate = typeof navigator == "undefined"; + +var errorObj = {e: {}}; +var tryCatchTarget; +var globalObject = typeof self !== "undefined" ? self : + typeof window !== "undefined" ? window : + typeof global !== "undefined" ? global : + this !== undefined ? this : null; + +function tryCatcher() { + try { + var target = tryCatchTarget; + tryCatchTarget = null; + return target.apply(this, arguments); + } catch (e) { + errorObj.e = e; + return errorObj; + } +} +function tryCatch(fn) { + tryCatchTarget = fn; + return tryCatcher; +} + +var inherits = function(Child, Parent) { + var hasProp = {}.hasOwnProperty; + + function T() { + this.constructor = Child; + this.constructor$ = Parent; + for (var propertyName in Parent.prototype) { + if (hasProp.call(Parent.prototype, propertyName) && + propertyName.charAt(propertyName.length-1) !== "$" + ) { + this[propertyName + "$"] = Parent.prototype[propertyName]; + } + } + } + T.prototype = Parent.prototype; + Child.prototype = new T(); + return Child.prototype; +}; + + +function isPrimitive(val) { + return val == null || val === true || val === false || + typeof val === "string" || typeof val === "number"; + +} + +function isObject(value) { + return typeof value === "function" || + typeof value === "object" && value !== null; +} + +function maybeWrapAsError(maybeError) { + if (!isPrimitive(maybeError)) return maybeError; + + return new Error(safeToString(maybeError)); +} + +function withAppended(target, appendee) { + var len = target.length; + var ret = new Array(len + 1); + var i; + for (i = 0; i < len; ++i) { + ret[i] = target[i]; + } + ret[i] = appendee; + return ret; +} + +function getDataPropertyOrDefault(obj, key, defaultValue) { + if (es5.isES5) { + var desc = Object.getOwnPropertyDescriptor(obj, key); + + if (desc != null) { + return desc.get == null && desc.set == null + ? desc.value + : defaultValue; + } + } else { + return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined; + } +} + +function notEnumerableProp(obj, name, value) { + if (isPrimitive(obj)) return obj; + var descriptor = { + value: value, + configurable: true, + enumerable: false, + writable: true + }; + es5.defineProperty(obj, name, descriptor); + return obj; +} + +function thrower(r) { + throw r; +} + +var inheritedDataKeys = (function() { + var excludedPrototypes = [ + Array.prototype, + Object.prototype, + Function.prototype + ]; + + var isExcludedProto = function(val) { + for (var i = 0; i < excludedPrototypes.length; ++i) { + if (excludedPrototypes[i] === val) { + return true; + } + } + return false; + }; + + if (es5.isES5) { + var getKeys = Object.getOwnPropertyNames; + return function(obj) { + var ret = []; + var visitedKeys = Object.create(null); + while (obj != null && !isExcludedProto(obj)) { + var keys; + try { + keys = getKeys(obj); + } catch (e) { + return ret; + } + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + if (visitedKeys[key]) continue; + visitedKeys[key] = true; + var desc = Object.getOwnPropertyDescriptor(obj, key); + if (desc != null && desc.get == null && desc.set == null) { + ret.push(key); + } + } + obj = es5.getPrototypeOf(obj); + } + return ret; + }; + } else { + var hasProp = {}.hasOwnProperty; + return function(obj) { + if (isExcludedProto(obj)) return []; + var ret = []; + + /*jshint forin:false */ + enumeration: for (var key in obj) { + if (hasProp.call(obj, key)) { + ret.push(key); + } else { + for (var i = 0; i < excludedPrototypes.length; ++i) { + if (hasProp.call(excludedPrototypes[i], key)) { + continue enumeration; + } + } + ret.push(key); + } + } + return ret; + }; + } + +})(); + +var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/; +function isClass(fn) { + try { + if (typeof fn === "function") { + var keys = es5.names(fn.prototype); + + var hasMethods = es5.isES5 && keys.length > 1; + var hasMethodsOtherThanConstructor = keys.length > 0 && + !(keys.length === 1 && keys[0] === "constructor"); + var hasThisAssignmentAndStaticMethods = + thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0; + + if (hasMethods || hasMethodsOtherThanConstructor || + hasThisAssignmentAndStaticMethods) { + return true; + } + } + return false; + } catch (e) { + return false; + } +} + +function toFastProperties(obj) { + /*jshint -W027,-W055,-W031*/ + function FakeConstructor() {} + FakeConstructor.prototype = obj; + var l = 8; + while (l--) new FakeConstructor(); + return obj; + eval(obj); +} + +var rident = /^[a-z$_][a-z$_0-9]*$/i; +function isIdentifier(str) { + return rident.test(str); +} + +function filledRange(count, prefix, suffix) { + var ret = new Array(count); + for(var i = 0; i < count; ++i) { + ret[i] = prefix + i + suffix; + } + return ret; +} + +function safeToString(obj) { + try { + return obj + ""; + } catch (e) { + return "[no string representation]"; + } +} + +function isError(obj) { + return obj instanceof Error || + (obj !== null && + typeof obj === "object" && + typeof obj.message === "string" && + typeof obj.name === "string"); +} + +function markAsOriginatingFromRejection(e) { + try { + notEnumerableProp(e, "isOperational", true); + } + catch(ignore) {} +} + +function originatesFromRejection(e) { + if (e == null) return false; + return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) || + e["isOperational"] === true); +} + +function canAttachTrace(obj) { + return isError(obj) && es5.propertyIsWritable(obj, "stack"); +} + +var ensureErrorObject = (function() { + if (!("stack" in new Error())) { + return function(value) { + if (canAttachTrace(value)) return value; + try {throw new Error(safeToString(value));} + catch(err) {return err;} + }; + } else { + return function(value) { + if (canAttachTrace(value)) return value; + return new Error(safeToString(value)); + }; + } +})(); + +function classString(obj) { + return {}.toString.call(obj); +} + +function copyDescriptors(from, to, filter) { + var keys = es5.names(from); + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + if (filter(key)) { + try { + es5.defineProperty(to, key, es5.getDescriptor(from, key)); + } catch (ignore) {} + } + } +} + +var asArray = function(v) { + if (es5.isArray(v)) { + return v; + } + return null; +}; + +if (typeof Symbol !== "undefined" && Symbol.iterator) { + var ArrayFrom = typeof Array.from === "function" ? function(v) { + return Array.from(v); + } : function(v) { + var ret = []; + var it = v[Symbol.iterator](); + var itResult; + while (!((itResult = it.next()).done)) { + ret.push(itResult.value); + } + return ret; + }; + + asArray = function(v) { + if (es5.isArray(v)) { + return v; + } else if (v != null && typeof v[Symbol.iterator] === "function") { + return ArrayFrom(v); + } + return null; + }; +} + +var isNode = typeof process !== "undefined" && + classString(process).toLowerCase() === "[object process]"; + +var hasEnvVariables = typeof process !== "undefined" && + typeof process.env !== "undefined"; + +function env(key) { + return hasEnvVariables ? process.env[key] : undefined; +} + +function getNativePromise() { + if (typeof Promise === "function") { + try { + var promise = new Promise(function(){}); + if ({}.toString.call(promise) === "[object Promise]") { + return Promise; + } + } catch (e) {} + } +} + +function domainBind(self, cb) { + return self.bind(cb); +} + +var ret = { + isClass: isClass, + isIdentifier: isIdentifier, + inheritedDataKeys: inheritedDataKeys, + getDataPropertyOrDefault: getDataPropertyOrDefault, + thrower: thrower, + isArray: es5.isArray, + asArray: asArray, + notEnumerableProp: notEnumerableProp, + isPrimitive: isPrimitive, + isObject: isObject, + isError: isError, + canEvaluate: canEvaluate, + errorObj: errorObj, + tryCatch: tryCatch, + inherits: inherits, + withAppended: withAppended, + maybeWrapAsError: maybeWrapAsError, + toFastProperties: toFastProperties, + filledRange: filledRange, + toString: safeToString, + canAttachTrace: canAttachTrace, + ensureErrorObject: ensureErrorObject, + originatesFromRejection: originatesFromRejection, + markAsOriginatingFromRejection: markAsOriginatingFromRejection, + classString: classString, + copyDescriptors: copyDescriptors, + hasDevTools: typeof chrome !== "undefined" && chrome && + typeof chrome.loadTimes === "function", + isNode: isNode, + hasEnvVariables: hasEnvVariables, + env: env, + global: globalObject, + getNativePromise: getNativePromise, + domainBind: domainBind +}; +ret.isRecentNode = ret.isNode && (function() { + var version = process.versions.node.split(".").map(Number); + return (version[0] === 0 && version[1] > 10) || (version[0] > 0); +})(); + +if (ret.isNode) ret.toFastProperties(process); + +try {throw new Error(); } catch (e) {ret.lastLineError = e;} +module.exports = ret; + +},{"./es5":13}]},{},[4])(4) +}); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; } +}).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("timers").setImmediate) +},{"_process":362,"timers":367}],29:[function(require,module,exports){ +(function (global){(function (){ +/** + * Module dependencies. + * @ignore + */ + +// Test if we're in Node via presence of "global" not absence of "window" +// to support hybrid environments like Electron +if (typeof global !== 'undefined') { + var Buffer = require('buffer').Buffer; // TODO just use global Buffer +} + +var utils = require('./parser/utils'); + +/** + * A class representation of the BSON Binary type. + * + * Sub types + * - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type. + * - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type. + * - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type. + * - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type. + * - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type. + * - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type. + * + * @class + * @param {Buffer} buffer a buffer object containing the binary data. + * @param {Number} [subType] the option binary type. + * @return {Binary} + */ +function Binary(buffer, subType) { + if (!(this instanceof Binary)) return new Binary(buffer, subType); + + if ( + buffer != null && + !(typeof buffer === 'string') && + !Buffer.isBuffer(buffer) && + !(buffer instanceof Uint8Array) && + !Array.isArray(buffer) + ) { + throw new Error('only String, Buffer, Uint8Array or Array accepted'); + } + + this._bsontype = 'Binary'; + + if (buffer instanceof Number) { + this.sub_type = buffer; + this.position = 0; + } else { + this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType; + this.position = 0; + } + + if (buffer != null && !(buffer instanceof Number)) { + // Only accept Buffer, Uint8Array or Arrays + if (typeof buffer === 'string') { + // Different ways of writing the length of the string for the different types + if (typeof Buffer !== 'undefined') { + this.buffer = utils.toBuffer(buffer); + } else if ( + typeof Uint8Array !== 'undefined' || + Object.prototype.toString.call(buffer) === '[object Array]' + ) { + this.buffer = writeStringToArray(buffer); + } else { + throw new Error('only String, Buffer, Uint8Array or Array accepted'); + } + } else { + this.buffer = buffer; + } + this.position = buffer.length; + } else { + if (typeof Buffer !== 'undefined') { + this.buffer = utils.allocBuffer(Binary.BUFFER_SIZE); + } else if (typeof Uint8Array !== 'undefined') { + this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE)); + } else { + this.buffer = new Array(Binary.BUFFER_SIZE); + } + // Set position to start of buffer + this.position = 0; + } +} + +/** + * Updates this binary with byte_value. + * + * @method + * @param {string} byte_value a single byte we wish to write. + */ +Binary.prototype.put = function put(byte_value) { + // If it's a string and a has more than one character throw an error + if (byte_value['length'] != null && typeof byte_value !== 'number' && byte_value.length !== 1) + throw new Error('only accepts single character String, Uint8Array or Array'); + if ((typeof byte_value !== 'number' && byte_value < 0) || byte_value > 255) + throw new Error('only accepts number in a valid unsigned byte range 0-255'); + + // Decode the byte value once + var decoded_byte = null; + if (typeof byte_value === 'string') { + decoded_byte = byte_value.charCodeAt(0); + } else if (byte_value['length'] != null) { + decoded_byte = byte_value[0]; + } else { + decoded_byte = byte_value; + } + + if (this.buffer.length > this.position) { + this.buffer[this.position++] = decoded_byte; + } else { + if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) { + // Create additional overflow buffer + var buffer = utils.allocBuffer(Binary.BUFFER_SIZE + this.buffer.length); + // Combine the two buffers together + this.buffer.copy(buffer, 0, 0, this.buffer.length); + this.buffer = buffer; + this.buffer[this.position++] = decoded_byte; + } else { + buffer = null; + // Create a new buffer (typed or normal array) + if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') { + buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length)); + } else { + buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length); + } + + // We need to copy all the content to the new array + for (var i = 0; i < this.buffer.length; i++) { + buffer[i] = this.buffer[i]; + } + + // Reassign the buffer + this.buffer = buffer; + // Write the byte + this.buffer[this.position++] = decoded_byte; + } + } +}; + +/** + * Writes a buffer or string to the binary. + * + * @method + * @param {(Buffer|string)} string a string or buffer to be written to the Binary BSON object. + * @param {number} offset specify the binary of where to write the content. + * @return {null} + */ +Binary.prototype.write = function write(string, offset) { + offset = typeof offset === 'number' ? offset : this.position; + + // If the buffer is to small let's extend the buffer + if (this.buffer.length < offset + string.length) { + var buffer = null; + // If we are in node.js + if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) { + buffer = utils.allocBuffer(this.buffer.length + string.length); + this.buffer.copy(buffer, 0, 0, this.buffer.length); + } else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') { + // Create a new buffer + buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length)); + // Copy the content + for (var i = 0; i < this.position; i++) { + buffer[i] = this.buffer[i]; + } + } + + // Assign the new buffer + this.buffer = buffer; + } + + if (typeof Buffer !== 'undefined' && Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) { + string.copy(this.buffer, offset, 0, string.length); + this.position = offset + string.length > this.position ? offset + string.length : this.position; + // offset = string.length + } else if ( + typeof Buffer !== 'undefined' && + typeof string === 'string' && + Buffer.isBuffer(this.buffer) + ) { + this.buffer.write(string, offset, 'binary'); + this.position = offset + string.length > this.position ? offset + string.length : this.position; + // offset = string.length; + } else if ( + Object.prototype.toString.call(string) === '[object Uint8Array]' || + (Object.prototype.toString.call(string) === '[object Array]' && typeof string !== 'string') + ) { + for (i = 0; i < string.length; i++) { + this.buffer[offset++] = string[i]; + } + + this.position = offset > this.position ? offset : this.position; + } else if (typeof string === 'string') { + for (i = 0; i < string.length; i++) { + this.buffer[offset++] = string.charCodeAt(i); + } + + this.position = offset > this.position ? offset : this.position; + } +}; + +/** + * Reads **length** bytes starting at **position**. + * + * @method + * @param {number} position read from the given position in the Binary. + * @param {number} length the number of bytes to read. + * @return {Buffer} + */ +Binary.prototype.read = function read(position, length) { + length = length && length > 0 ? length : this.position; + + // Let's return the data based on the type we have + if (this.buffer['slice']) { + return this.buffer.slice(position, position + length); + } else { + // Create a buffer to keep the result + var buffer = + typeof Uint8Array !== 'undefined' + ? new Uint8Array(new ArrayBuffer(length)) + : new Array(length); + for (var i = 0; i < length; i++) { + buffer[i] = this.buffer[position++]; + } + } + // Return the buffer + return buffer; +}; + +/** + * Returns the value of this binary as a string. + * + * @method + * @return {string} + */ +Binary.prototype.value = function value(asRaw) { + asRaw = asRaw == null ? false : asRaw; + + // Optimize to serialize for the situation where the data == size of buffer + if ( + asRaw && + typeof Buffer !== 'undefined' && + Buffer.isBuffer(this.buffer) && + this.buffer.length === this.position + ) + return this.buffer; + + // If it's a node.js buffer object + if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) { + return asRaw + ? this.buffer.slice(0, this.position) + : this.buffer.toString('binary', 0, this.position); + } else { + if (asRaw) { + // we support the slice command use it + if (this.buffer['slice'] != null) { + return this.buffer.slice(0, this.position); + } else { + // Create a new buffer to copy content to + var newBuffer = + Object.prototype.toString.call(this.buffer) === '[object Uint8Array]' + ? new Uint8Array(new ArrayBuffer(this.position)) + : new Array(this.position); + // Copy content + for (var i = 0; i < this.position; i++) { + newBuffer[i] = this.buffer[i]; + } + // Return the buffer + return newBuffer; + } + } else { + return convertArraytoUtf8BinaryString(this.buffer, 0, this.position); + } + } +}; + +/** + * Length. + * + * @method + * @return {number} the length of the binary. + */ +Binary.prototype.length = function length() { + return this.position; +}; + +/** + * @ignore + */ +Binary.prototype.toJSON = function() { + return this.buffer != null ? this.buffer.toString('base64') : ''; +}; + +/** + * @ignore + */ +Binary.prototype.toString = function(format) { + return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : ''; +}; + +/** + * Binary default subtype + * @ignore + */ +var BSON_BINARY_SUBTYPE_DEFAULT = 0; + +/** + * @ignore + */ +var writeStringToArray = function(data) { + // Create a buffer + var buffer = + typeof Uint8Array !== 'undefined' + ? new Uint8Array(new ArrayBuffer(data.length)) + : new Array(data.length); + // Write the content to the buffer + for (var i = 0; i < data.length; i++) { + buffer[i] = data.charCodeAt(i); + } + // Write the string to the buffer + return buffer; +}; + +/** + * Convert Array ot Uint8Array to Binary String + * + * @ignore + */ +var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) { + var result = ''; + for (var i = startIndex; i < endIndex; i++) { + result = result + String.fromCharCode(byteArray[i]); + } + return result; +}; + +Binary.BUFFER_SIZE = 256; + +/** + * Default BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_DEFAULT = 0; +/** + * Function BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_FUNCTION = 1; +/** + * Byte Array BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_BYTE_ARRAY = 2; +/** + * OLD UUID BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_UUID_OLD = 3; +/** + * UUID BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_UUID = 4; +/** + * MD5 BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_MD5 = 5; +/** + * User BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_USER_DEFINED = 128; + +/** + * Expose. + */ +module.exports = Binary; +module.exports.Binary = Binary; + +}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./parser/utils":45,"buffer":49}],30:[function(require,module,exports){ +'use strict'; + +var Map = require('./map'), + Long = require('./long'), + Double = require('./double'), + Timestamp = require('./timestamp'), + ObjectID = require('./objectid'), + BSONRegExp = require('./regexp'), + Symbol = require('./symbol'), + Int32 = require('./int_32'), + Code = require('./code'), + Decimal128 = require('./decimal128'), + MinKey = require('./min_key'), + MaxKey = require('./max_key'), + DBRef = require('./db_ref'), + Binary = require('./binary'); + +// Parts of the parser +var deserialize = require('./parser/deserializer'), + serializer = require('./parser/serializer'), + calculateObjectSize = require('./parser/calculate_size'), + utils = require('./parser/utils'); + +/** + * @ignore + * @api private + */ +// Default Max Size +var MAXSIZE = 1024 * 1024 * 17; + +// Current Internal Temporary Serialization Buffer +var buffer = utils.allocBuffer(MAXSIZE); + +var BSON = function() {}; + +/** + * Serialize a Javascript object. + * + * @param {Object} object the Javascript object to serialize. + * @param {Boolean} [options.checkKeys] the serializer will check if keys are valid. + * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**. + * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**. + * @param {Number} [options.minInternalBufferSize=1024*1024*17] minimum size of the internal temporary serialization buffer **(default:1024*1024*17)**. + * @return {Buffer} returns the Buffer object containing the serialized object. + * @api public + */ +BSON.prototype.serialize = function serialize(object, options) { + options = options || {}; + // Unpack the options + var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false; + var serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + var ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + var minInternalBufferSize = + typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE; + + // Resize the internal serialization buffer if needed + if (buffer.length < minInternalBufferSize) { + buffer = utils.allocBuffer(minInternalBufferSize); + } + + // Attempt to serialize + var serializationIndex = serializer( + buffer, + object, + checkKeys, + 0, + 0, + serializeFunctions, + ignoreUndefined, + [] + ); + // Create the final buffer + var finishedBuffer = utils.allocBuffer(serializationIndex); + // Copy into the finished buffer + buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length); + // Return the buffer + return finishedBuffer; +}; + +/** + * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization. + * + * @param {Object} object the Javascript object to serialize. + * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object. + * @param {Boolean} [options.checkKeys] the serializer will check if keys are valid. + * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**. + * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**. + * @param {Number} [options.index] the index in the buffer where we wish to start serializing into. + * @return {Number} returns the index pointing to the last written byte in the buffer. + * @api public + */ +BSON.prototype.serializeWithBufferAndIndex = function(object, finalBuffer, options) { + options = options || {}; + // Unpack the options + var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false; + var serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + var ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + var startIndex = typeof options.index === 'number' ? options.index : 0; + + // Attempt to serialize + var serializationIndex = serializer( + finalBuffer, + object, + checkKeys, + startIndex || 0, + 0, + serializeFunctions, + ignoreUndefined + ); + + // Return the index + return serializationIndex - 1; +}; + +/** + * Deserialize data as BSON. + * + * @param {Buffer} buffer the buffer containing the serialized set of BSON documents. + * @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized. + * @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse. + * @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function. + * @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits + * @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a node.js Buffer instance. + * @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types. + * @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer. + * @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances. + * @return {Object} returns the deserialized Javascript Object. + * @api public + */ +BSON.prototype.deserialize = function(buffer, options) { + return deserialize(buffer, options); +}; + +/** + * Calculate the bson size for a passed in Javascript object. + * + * @param {Object} object the Javascript object to calculate the BSON byte size for. + * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**. + * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**. + * @return {Number} returns the number of bytes the BSON object will take up. + * @api public + */ +BSON.prototype.calculateObjectSize = function(object, options) { + options = options || {}; + + var serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + var ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + + return calculateObjectSize(object, serializeFunctions, ignoreUndefined); +}; + +/** + * Deserialize stream data as BSON documents. + * + * @param {Buffer} data the buffer containing the serialized set of BSON documents. + * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start. + * @param {Number} numberOfDocuments number of documents to deserialize. + * @param {Array} documents an array where to store the deserialized documents. + * @param {Number} docStartIndex the index in the documents array from where to start inserting documents. + * @param {Object} [options] additional options used for the deserialization. + * @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized. + * @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse. + * @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function. + * @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits + * @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a node.js Buffer instance. + * @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types. + * @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer. + * @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances. + * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents. + * @api public + */ +BSON.prototype.deserializeStream = function( + data, + startIndex, + numberOfDocuments, + documents, + docStartIndex, + options +) { + options = options != null ? options : {}; + var index = startIndex; + // Loop over all documents + for (var i = 0; i < numberOfDocuments; i++) { + // Find size of the document + var size = + data[index] | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24); + // Update options with index + options['index'] = index; + // Parse the document at this point + documents[docStartIndex + i] = this.deserialize(data, options); + // Adjust index by the document size + index = index + size; + } + + // Return object containing end index of parsing and list of documents + return index; +}; + +/** + * @ignore + * @api private + */ +// BSON MAX VALUES +BSON.BSON_INT32_MAX = 0x7fffffff; +BSON.BSON_INT32_MIN = -0x80000000; + +BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1; +BSON.BSON_INT64_MIN = -Math.pow(2, 63); + +// JS MAX PRECISE VALUES +BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double. +BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double. + +// Internal long versions +// var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double. +// var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double. + +/** + * Number BSON Type + * + * @classconstant BSON_DATA_NUMBER + **/ +BSON.BSON_DATA_NUMBER = 1; +/** + * String BSON Type + * + * @classconstant BSON_DATA_STRING + **/ +BSON.BSON_DATA_STRING = 2; +/** + * Object BSON Type + * + * @classconstant BSON_DATA_OBJECT + **/ +BSON.BSON_DATA_OBJECT = 3; +/** + * Array BSON Type + * + * @classconstant BSON_DATA_ARRAY + **/ +BSON.BSON_DATA_ARRAY = 4; +/** + * Binary BSON Type + * + * @classconstant BSON_DATA_BINARY + **/ +BSON.BSON_DATA_BINARY = 5; +/** + * ObjectID BSON Type + * + * @classconstant BSON_DATA_OID + **/ +BSON.BSON_DATA_OID = 7; +/** + * Boolean BSON Type + * + * @classconstant BSON_DATA_BOOLEAN + **/ +BSON.BSON_DATA_BOOLEAN = 8; +/** + * Date BSON Type + * + * @classconstant BSON_DATA_DATE + **/ +BSON.BSON_DATA_DATE = 9; +/** + * null BSON Type + * + * @classconstant BSON_DATA_NULL + **/ +BSON.BSON_DATA_NULL = 10; +/** + * RegExp BSON Type + * + * @classconstant BSON_DATA_REGEXP + **/ +BSON.BSON_DATA_REGEXP = 11; +/** + * Code BSON Type + * + * @classconstant BSON_DATA_CODE + **/ +BSON.BSON_DATA_CODE = 13; +/** + * Symbol BSON Type + * + * @classconstant BSON_DATA_SYMBOL + **/ +BSON.BSON_DATA_SYMBOL = 14; +/** + * Code with Scope BSON Type + * + * @classconstant BSON_DATA_CODE_W_SCOPE + **/ +BSON.BSON_DATA_CODE_W_SCOPE = 15; +/** + * 32 bit Integer BSON Type + * + * @classconstant BSON_DATA_INT + **/ +BSON.BSON_DATA_INT = 16; +/** + * Timestamp BSON Type + * + * @classconstant BSON_DATA_TIMESTAMP + **/ +BSON.BSON_DATA_TIMESTAMP = 17; +/** + * Long BSON Type + * + * @classconstant BSON_DATA_LONG + **/ +BSON.BSON_DATA_LONG = 18; +/** + * MinKey BSON Type + * + * @classconstant BSON_DATA_MIN_KEY + **/ +BSON.BSON_DATA_MIN_KEY = 0xff; +/** + * MaxKey BSON Type + * + * @classconstant BSON_DATA_MAX_KEY + **/ +BSON.BSON_DATA_MAX_KEY = 0x7f; + +/** + * Binary Default Type + * + * @classconstant BSON_BINARY_SUBTYPE_DEFAULT + **/ +BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; +/** + * Binary Function Type + * + * @classconstant BSON_BINARY_SUBTYPE_FUNCTION + **/ +BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1; +/** + * Binary Byte Array Type + * + * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY + **/ +BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +/** + * Binary UUID Type + * + * @classconstant BSON_BINARY_SUBTYPE_UUID + **/ +BSON.BSON_BINARY_SUBTYPE_UUID = 3; +/** + * Binary MD5 Type + * + * @classconstant BSON_BINARY_SUBTYPE_MD5 + **/ +BSON.BSON_BINARY_SUBTYPE_MD5 = 4; +/** + * Binary User Defined Type + * + * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED + **/ +BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +// Return BSON +module.exports = BSON; +module.exports.Code = Code; +module.exports.Map = Map; +module.exports.Symbol = Symbol; +module.exports.BSON = BSON; +module.exports.DBRef = DBRef; +module.exports.Binary = Binary; +module.exports.ObjectID = ObjectID; +module.exports.Long = Long; +module.exports.Timestamp = Timestamp; +module.exports.Double = Double; +module.exports.Int32 = Int32; +module.exports.MinKey = MinKey; +module.exports.MaxKey = MaxKey; +module.exports.BSONRegExp = BSONRegExp; +module.exports.Decimal128 = Decimal128; + +},{"./binary":29,"./code":31,"./db_ref":32,"./decimal128":33,"./double":34,"./int_32":36,"./long":37,"./map":38,"./max_key":39,"./min_key":40,"./objectid":41,"./parser/calculate_size":42,"./parser/deserializer":43,"./parser/serializer":44,"./parser/utils":45,"./regexp":46,"./symbol":47,"./timestamp":48}],31:[function(require,module,exports){ +/** + * A class representation of the BSON Code type. + * + * @class + * @param {(string|function)} code a string or function. + * @param {Object} [scope] an optional scope for the function. + * @return {Code} + */ +var Code = function Code(code, scope) { + if (!(this instanceof Code)) return new Code(code, scope); + this._bsontype = 'Code'; + this.code = code; + this.scope = scope; +}; + +/** + * @ignore + */ +Code.prototype.toJSON = function() { + return { scope: this.scope, code: this.code }; +}; + +module.exports = Code; +module.exports.Code = Code; + +},{}],32:[function(require,module,exports){ +/** + * A class representation of the BSON DBRef type. + * + * @class + * @param {string} namespace the collection name. + * @param {ObjectID} oid the reference ObjectID. + * @param {string} [db] optional db name, if omitted the reference is local to the current db. + * @return {DBRef} + */ +function DBRef(namespace, oid, db) { + if (!(this instanceof DBRef)) return new DBRef(namespace, oid, db); + + this._bsontype = 'DBRef'; + this.namespace = namespace; + this.oid = oid; + this.db = db; +} + +/** + * @ignore + * @api private + */ +DBRef.prototype.toJSON = function() { + return { + $ref: this.namespace, + $id: this.oid, + $db: this.db == null ? '' : this.db + }; +}; + +module.exports = DBRef; +module.exports.DBRef = DBRef; + +},{}],33:[function(require,module,exports){ +'use strict'; + +var Long = require('./long'); + +var PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/; +var PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i; +var PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i; + +var EXPONENT_MAX = 6111; +var EXPONENT_MIN = -6176; +var EXPONENT_BIAS = 6176; +var MAX_DIGITS = 34; + +// Nan value bits as 32 bit values (due to lack of longs) +var NAN_BUFFER = [ + 0x7c, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00 +].reverse(); +// Infinity value bits 32 bit values (due to lack of longs) +var INF_NEGATIVE_BUFFER = [ + 0xf8, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00 +].reverse(); +var INF_POSITIVE_BUFFER = [ + 0x78, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00 +].reverse(); + +var EXPONENT_REGEX = /^([-+])?(\d+)?$/; + +var utils = require('./parser/utils'); + +// Detect if the value is a digit +var isDigit = function(value) { + return !isNaN(parseInt(value, 10)); +}; + +// Divide two uint128 values +var divideu128 = function(value) { + var DIVISOR = Long.fromNumber(1000 * 1000 * 1000); + var _rem = Long.fromNumber(0); + var i = 0; + + if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) { + return { quotient: value, rem: _rem }; + } + + for (i = 0; i <= 3; i++) { + // Adjust remainder to match value of next dividend + _rem = _rem.shiftLeft(32); + // Add the divided to _rem + _rem = _rem.add(new Long(value.parts[i], 0)); + value.parts[i] = _rem.div(DIVISOR).low_; + _rem = _rem.modulo(DIVISOR); + } + + return { quotient: value, rem: _rem }; +}; + +// Multiply two Long values and return the 128 bit value +var multiply64x2 = function(left, right) { + if (!left && !right) { + return { high: Long.fromNumber(0), low: Long.fromNumber(0) }; + } + + var leftHigh = left.shiftRightUnsigned(32); + var leftLow = new Long(left.getLowBits(), 0); + var rightHigh = right.shiftRightUnsigned(32); + var rightLow = new Long(right.getLowBits(), 0); + + var productHigh = leftHigh.multiply(rightHigh); + var productMid = leftHigh.multiply(rightLow); + var productMid2 = leftLow.multiply(rightHigh); + var productLow = leftLow.multiply(rightLow); + + productHigh = productHigh.add(productMid.shiftRightUnsigned(32)); + productMid = new Long(productMid.getLowBits(), 0) + .add(productMid2) + .add(productLow.shiftRightUnsigned(32)); + + productHigh = productHigh.add(productMid.shiftRightUnsigned(32)); + productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0)); + + // Return the 128 bit result + return { high: productHigh, low: productLow }; +}; + +var lessThan = function(left, right) { + // Make values unsigned + var uhleft = left.high_ >>> 0; + var uhright = right.high_ >>> 0; + + // Compare high bits first + if (uhleft < uhright) { + return true; + } else if (uhleft === uhright) { + var ulleft = left.low_ >>> 0; + var ulright = right.low_ >>> 0; + if (ulleft < ulright) return true; + } + + return false; +}; + +// var longtoHex = function(value) { +// var buffer = utils.allocBuffer(8); +// var index = 0; +// // Encode the low 64 bits of the decimal +// // Encode low bits +// buffer[index++] = value.low_ & 0xff; +// buffer[index++] = (value.low_ >> 8) & 0xff; +// buffer[index++] = (value.low_ >> 16) & 0xff; +// buffer[index++] = (value.low_ >> 24) & 0xff; +// // Encode high bits +// buffer[index++] = value.high_ & 0xff; +// buffer[index++] = (value.high_ >> 8) & 0xff; +// buffer[index++] = (value.high_ >> 16) & 0xff; +// buffer[index++] = (value.high_ >> 24) & 0xff; +// return buffer.reverse().toString('hex'); +// }; + +// var int32toHex = function(value) { +// var buffer = utils.allocBuffer(4); +// var index = 0; +// // Encode the low 64 bits of the decimal +// // Encode low bits +// buffer[index++] = value & 0xff; +// buffer[index++] = (value >> 8) & 0xff; +// buffer[index++] = (value >> 16) & 0xff; +// buffer[index++] = (value >> 24) & 0xff; +// return buffer.reverse().toString('hex'); +// }; + +/** + * A class representation of the BSON Decimal128 type. + * + * @class + * @param {Buffer} bytes a buffer containing the raw Decimal128 bytes. + * @return {Double} + */ +var Decimal128 = function(bytes) { + this._bsontype = 'Decimal128'; + this.bytes = bytes; +}; + +/** + * Create a Decimal128 instance from a string representation + * + * @method + * @param {string} string a numeric string representation. + * @return {Decimal128} returns a Decimal128 instance. + */ +Decimal128.fromString = function(string) { + // Parse state tracking + var isNegative = false; + var sawRadix = false; + var foundNonZero = false; + + // Total number of significant digits (no leading or trailing zero) + var significantDigits = 0; + // Total number of significand digits read + var nDigitsRead = 0; + // Total number of digits (no leading zeros) + var nDigits = 0; + // The number of the digits after radix + var radixPosition = 0; + // The index of the first non-zero in *str* + var firstNonZero = 0; + + // Digits Array + var digits = [0]; + // The number of digits in digits + var nDigitsStored = 0; + // Insertion pointer for digits + var digitsInsert = 0; + // The index of the first non-zero digit + var firstDigit = 0; + // The index of the last digit + var lastDigit = 0; + + // Exponent + var exponent = 0; + // loop index over array + var i = 0; + // The high 17 digits of the significand + var significandHigh = [0, 0]; + // The low 17 digits of the significand + var significandLow = [0, 0]; + // The biased exponent + var biasedExponent = 0; + + // Read index + var index = 0; + + // Trim the string + string = string.trim(); + + // Naively prevent against REDOS attacks. + // TODO: implementing a custom parsing for this, or refactoring the regex would yield + // further gains. + if (string.length >= 7000) { + throw new Error('' + string + ' not a valid Decimal128 string'); + } + + // Results + var stringMatch = string.match(PARSE_STRING_REGEXP); + var infMatch = string.match(PARSE_INF_REGEXP); + var nanMatch = string.match(PARSE_NAN_REGEXP); + + // Validate the string + if ((!stringMatch && !infMatch && !nanMatch) || string.length === 0) { + throw new Error('' + string + ' not a valid Decimal128 string'); + } + + // Check if we have an illegal exponent format + if (stringMatch && stringMatch[4] && stringMatch[2] === undefined) { + throw new Error('' + string + ' not a valid Decimal128 string'); + } + + // Get the negative or positive sign + if (string[index] === '+' || string[index] === '-') { + isNegative = string[index++] === '-'; + } + + // Check if user passed Infinity or NaN + if (!isDigit(string[index]) && string[index] !== '.') { + if (string[index] === 'i' || string[index] === 'I') { + return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); + } else if (string[index] === 'N') { + return new Decimal128(utils.toBuffer(NAN_BUFFER)); + } + } + + // Read all the digits + while (isDigit(string[index]) || string[index] === '.') { + if (string[index] === '.') { + if (sawRadix) { + return new Decimal128(utils.toBuffer(NAN_BUFFER)); + } + + sawRadix = true; + index = index + 1; + continue; + } + + if (nDigitsStored < 34) { + if (string[index] !== '0' || foundNonZero) { + if (!foundNonZero) { + firstNonZero = nDigitsRead; + } + + foundNonZero = true; + + // Only store 34 digits + digits[digitsInsert++] = parseInt(string[index], 10); + nDigitsStored = nDigitsStored + 1; + } + } + + if (foundNonZero) { + nDigits = nDigits + 1; + } + + if (sawRadix) { + radixPosition = radixPosition + 1; + } + + nDigitsRead = nDigitsRead + 1; + index = index + 1; + } + + if (sawRadix && !nDigitsRead) { + throw new Error('' + string + ' not a valid Decimal128 string'); + } + + // Read exponent if exists + if (string[index] === 'e' || string[index] === 'E') { + // Read exponent digits + var match = string.substr(++index).match(EXPONENT_REGEX); + + // No digits read + if (!match || !match[2]) { + return new Decimal128(utils.toBuffer(NAN_BUFFER)); + } + + // Get exponent + exponent = parseInt(match[0], 10); + + // Adjust the index + index = index + match[0].length; + } + + // Return not a number + if (string[index]) { + return new Decimal128(utils.toBuffer(NAN_BUFFER)); + } + + // Done reading input + // Find first non-zero digit in digits + firstDigit = 0; + + if (!nDigitsStored) { + firstDigit = 0; + lastDigit = 0; + digits[0] = 0; + nDigits = 1; + nDigitsStored = 1; + significantDigits = 0; + } else { + lastDigit = nDigitsStored - 1; + significantDigits = nDigits; + + if (exponent !== 0 && significantDigits !== 1) { + while (string[firstNonZero + significantDigits - 1] === '0') { + significantDigits = significantDigits - 1; + } + } + } + + // Normalization of exponent + // Correct exponent based on radix position, and shift significand as needed + // to represent user input + + // Overflow prevention + if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) { + exponent = EXPONENT_MIN; + } else { + exponent = exponent - radixPosition; + } + + // Attempt to normalize the exponent + while (exponent > EXPONENT_MAX) { + // Shift exponent to significand and decrease + lastDigit = lastDigit + 1; + + if (lastDigit - firstDigit > MAX_DIGITS) { + // Check if we have a zero then just hard clamp, otherwise fail + var digitsString = digits.join(''); + if (digitsString.match(/^0+$/)) { + exponent = EXPONENT_MAX; + break; + } else { + return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); + } + } + + exponent = exponent - 1; + } + + while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) { + // Shift last digit + if (lastDigit === 0) { + exponent = EXPONENT_MIN; + significantDigits = 0; + break; + } + + if (nDigitsStored < nDigits) { + // adjust to match digits not stored + nDigits = nDigits - 1; + } else { + // adjust to round + lastDigit = lastDigit - 1; + } + + if (exponent < EXPONENT_MAX) { + exponent = exponent + 1; + } else { + // Check if we have a zero then just hard clamp, otherwise fail + digitsString = digits.join(''); + if (digitsString.match(/^0+$/)) { + exponent = EXPONENT_MAX; + break; + } else { + return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); + } + } + } + + // Round + // We've normalized the exponent, but might still need to round. + if (lastDigit - firstDigit + 1 < significantDigits && string[significantDigits] !== '0') { + var endOfString = nDigitsRead; + + // If we have seen a radix point, 'string' is 1 longer than we have + // documented with ndigits_read, so inc the position of the first nonzero + // digit and the position that digits are read to. + if (sawRadix && exponent === EXPONENT_MIN) { + firstNonZero = firstNonZero + 1; + endOfString = endOfString + 1; + } + + var roundDigit = parseInt(string[firstNonZero + lastDigit + 1], 10); + var roundBit = 0; + + if (roundDigit >= 5) { + roundBit = 1; + + if (roundDigit === 5) { + roundBit = digits[lastDigit] % 2 === 1; + + for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) { + if (parseInt(string[i], 10)) { + roundBit = 1; + break; + } + } + } + } + + if (roundBit) { + var dIdx = lastDigit; + + for (; dIdx >= 0; dIdx--) { + if (++digits[dIdx] > 9) { + digits[dIdx] = 0; + + // overflowed most significant digit + if (dIdx === 0) { + if (exponent < EXPONENT_MAX) { + exponent = exponent + 1; + digits[dIdx] = 1; + } else { + return new Decimal128( + utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER) + ); + } + } + } else { + break; + } + } + } + } + + // Encode significand + // The high 17 digits of the significand + significandHigh = Long.fromNumber(0); + // The low 17 digits of the significand + significandLow = Long.fromNumber(0); + + // read a zero + if (significantDigits === 0) { + significandHigh = Long.fromNumber(0); + significandLow = Long.fromNumber(0); + } else if (lastDigit - firstDigit < 17) { + dIdx = firstDigit; + significandLow = Long.fromNumber(digits[dIdx++]); + significandHigh = new Long(0, 0); + + for (; dIdx <= lastDigit; dIdx++) { + significandLow = significandLow.multiply(Long.fromNumber(10)); + significandLow = significandLow.add(Long.fromNumber(digits[dIdx])); + } + } else { + dIdx = firstDigit; + significandHigh = Long.fromNumber(digits[dIdx++]); + + for (; dIdx <= lastDigit - 17; dIdx++) { + significandHigh = significandHigh.multiply(Long.fromNumber(10)); + significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx])); + } + + significandLow = Long.fromNumber(digits[dIdx++]); + + for (; dIdx <= lastDigit; dIdx++) { + significandLow = significandLow.multiply(Long.fromNumber(10)); + significandLow = significandLow.add(Long.fromNumber(digits[dIdx])); + } + } + + var significand = multiply64x2(significandHigh, Long.fromString('100000000000000000')); + + significand.low = significand.low.add(significandLow); + + if (lessThan(significand.low, significandLow)) { + significand.high = significand.high.add(Long.fromNumber(1)); + } + + // Biased exponent + biasedExponent = exponent + EXPONENT_BIAS; + var dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) }; + + // Encode combination, exponent, and significand. + if ( + significand.high + .shiftRightUnsigned(49) + .and(Long.fromNumber(1)) + .equals(Long.fromNumber) + ) { + // Encode '11' into bits 1 to 3 + dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61)); + dec.high = dec.high.or( + Long.fromNumber(biasedExponent).and(Long.fromNumber(0x3fff).shiftLeft(47)) + ); + dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x7fffffffffff))); + } else { + dec.high = dec.high.or(Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49)); + dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x1ffffffffffff))); + } + + dec.low = significand.low; + + // Encode sign + if (isNegative) { + dec.high = dec.high.or(Long.fromString('9223372036854775808')); + } + + // Encode into a buffer + var buffer = utils.allocBuffer(16); + index = 0; + + // Encode the low 64 bits of the decimal + // Encode low bits + buffer[index++] = dec.low.low_ & 0xff; + buffer[index++] = (dec.low.low_ >> 8) & 0xff; + buffer[index++] = (dec.low.low_ >> 16) & 0xff; + buffer[index++] = (dec.low.low_ >> 24) & 0xff; + // Encode high bits + buffer[index++] = dec.low.high_ & 0xff; + buffer[index++] = (dec.low.high_ >> 8) & 0xff; + buffer[index++] = (dec.low.high_ >> 16) & 0xff; + buffer[index++] = (dec.low.high_ >> 24) & 0xff; + + // Encode the high 64 bits of the decimal + // Encode low bits + buffer[index++] = dec.high.low_ & 0xff; + buffer[index++] = (dec.high.low_ >> 8) & 0xff; + buffer[index++] = (dec.high.low_ >> 16) & 0xff; + buffer[index++] = (dec.high.low_ >> 24) & 0xff; + // Encode high bits + buffer[index++] = dec.high.high_ & 0xff; + buffer[index++] = (dec.high.high_ >> 8) & 0xff; + buffer[index++] = (dec.high.high_ >> 16) & 0xff; + buffer[index++] = (dec.high.high_ >> 24) & 0xff; + + // Return the new Decimal128 + return new Decimal128(buffer); +}; + +// Extract least significant 5 bits +var COMBINATION_MASK = 0x1f; +// Extract least significant 14 bits +var EXPONENT_MASK = 0x3fff; +// Value of combination field for Inf +var COMBINATION_INFINITY = 30; +// Value of combination field for NaN +var COMBINATION_NAN = 31; +// Value of combination field for NaN +// var COMBINATION_SNAN = 32; +// decimal128 exponent bias +EXPONENT_BIAS = 6176; + +/** + * Create a string representation of the raw Decimal128 value + * + * @method + * @return {string} returns a Decimal128 string representation. + */ +Decimal128.prototype.toString = function() { + // Note: bits in this routine are referred to starting at 0, + // from the sign bit, towards the coefficient. + + // bits 0 - 31 + var high; + // bits 32 - 63 + var midh; + // bits 64 - 95 + var midl; + // bits 96 - 127 + var low; + // bits 1 - 5 + var combination; + // decoded biased exponent (14 bits) + var biased_exponent; + // the number of significand digits + var significand_digits = 0; + // the base-10 digits in the significand + var significand = new Array(36); + for (var i = 0; i < significand.length; i++) significand[i] = 0; + // read pointer into significand + var index = 0; + + // unbiased exponent + var exponent; + // the exponent if scientific notation is used + var scientific_exponent; + + // true if the number is zero + var is_zero = false; + + // the most signifcant significand bits (50-46) + var significand_msb; + // temporary storage for significand decoding + var significand128 = { parts: new Array(4) }; + // indexing variables + i; + var j, k; + + // Output string + var string = []; + + // Unpack index + index = 0; + + // Buffer reference + var buffer = this.bytes; + + // Unpack the low 64bits into a long + low = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + midl = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + + // Unpack the high 64bits into a long + midh = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + high = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + + // Unpack index + index = 0; + + // Create the state of the decimal + var dec = { + low: new Long(low, midl), + high: new Long(midh, high) + }; + + if (dec.high.lessThan(Long.ZERO)) { + string.push('-'); + } + + // Decode combination field and exponent + combination = (high >> 26) & COMBINATION_MASK; + + if (combination >> 3 === 3) { + // Check for 'special' values + if (combination === COMBINATION_INFINITY) { + return string.join('') + 'Infinity'; + } else if (combination === COMBINATION_NAN) { + return 'NaN'; + } else { + biased_exponent = (high >> 15) & EXPONENT_MASK; + significand_msb = 0x08 + ((high >> 14) & 0x01); + } + } else { + significand_msb = (high >> 14) & 0x07; + biased_exponent = (high >> 17) & EXPONENT_MASK; + } + + exponent = biased_exponent - EXPONENT_BIAS; + + // Create string of significand digits + + // Convert the 114-bit binary number represented by + // (significand_high, significand_low) to at most 34 decimal + // digits through modulo and division. + significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14); + significand128.parts[1] = midh; + significand128.parts[2] = midl; + significand128.parts[3] = low; + + if ( + significand128.parts[0] === 0 && + significand128.parts[1] === 0 && + significand128.parts[2] === 0 && + significand128.parts[3] === 0 + ) { + is_zero = true; + } else { + for (k = 3; k >= 0; k--) { + var least_digits = 0; + // Peform the divide + var result = divideu128(significand128); + significand128 = result.quotient; + least_digits = result.rem.low_; + + // We now have the 9 least significant digits (in base 2). + // Convert and output to string. + if (!least_digits) continue; + + for (j = 8; j >= 0; j--) { + // significand[k * 9 + j] = Math.round(least_digits % 10); + significand[k * 9 + j] = least_digits % 10; + // least_digits = Math.round(least_digits / 10); + least_digits = Math.floor(least_digits / 10); + } + } + } + + // Output format options: + // Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd + // Regular - ddd.ddd + + if (is_zero) { + significand_digits = 1; + significand[index] = 0; + } else { + significand_digits = 36; + i = 0; + + while (!significand[index]) { + i++; + significand_digits = significand_digits - 1; + index = index + 1; + } + } + + scientific_exponent = significand_digits - 1 + exponent; + + // The scientific exponent checks are dictated by the string conversion + // specification and are somewhat arbitrary cutoffs. + // + // We must check exponent > 0, because if this is the case, the number + // has trailing zeros. However, we *cannot* output these trailing zeros, + // because doing so would change the precision of the value, and would + // change stored data if the string converted number is round tripped. + + if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) { + // Scientific format + string.push(significand[index++]); + significand_digits = significand_digits - 1; + + if (significand_digits) { + string.push('.'); + } + + for (i = 0; i < significand_digits; i++) { + string.push(significand[index++]); + } + + // Exponent + string.push('E'); + if (scientific_exponent > 0) { + string.push('+' + scientific_exponent); + } else { + string.push(scientific_exponent); + } + } else { + // Regular format with no decimal place + if (exponent >= 0) { + for (i = 0; i < significand_digits; i++) { + string.push(significand[index++]); + } + } else { + var radix_position = significand_digits + exponent; + + // non-zero digits before radix + if (radix_position > 0) { + for (i = 0; i < radix_position; i++) { + string.push(significand[index++]); + } + } else { + string.push('0'); + } + + string.push('.'); + // add leading zeros after radix + while (radix_position++ < 0) { + string.push('0'); + } + + for (i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) { + string.push(significand[index++]); + } + } + } + + return string.join(''); +}; + +Decimal128.prototype.toJSON = function() { + return { $numberDecimal: this.toString() }; +}; + +module.exports = Decimal128; +module.exports.Decimal128 = Decimal128; + +},{"./long":37,"./parser/utils":45}],34:[function(require,module,exports){ +/** + * A class representation of the BSON Double type. + * + * @class + * @param {number} value the number we want to represent as a double. + * @return {Double} + */ +function Double(value) { + if (!(this instanceof Double)) return new Double(value); + + this._bsontype = 'Double'; + this.value = value; +} + +/** + * Access the number value. + * + * @method + * @return {number} returns the wrapped double number. + */ +Double.prototype.valueOf = function() { + return this.value; +}; + +/** + * @ignore + */ +Double.prototype.toJSON = function() { + return this.value; +}; + +module.exports = Double; +module.exports.Double = Double; + +},{}],35:[function(require,module,exports){ +// Copyright (c) 2008, Fair Oaks Labs, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// +// Modifications to writeIEEE754 to support negative zeroes made by Brian White + +var readIEEE754 = function(buffer, offset, endian, mLen, nBytes) { + var e, + m, + bBE = endian === 'big', + eLen = nBytes * 8 - mLen - 1, + eMax = (1 << eLen) - 1, + eBias = eMax >> 1, + nBits = -7, + i = bBE ? 0 : nBytes - 1, + d = bBE ? 1 : -1, + s = buffer[offset + i]; + + i += d; + + e = s & ((1 << -nBits) - 1); + s >>= -nBits; + nBits += eLen; + for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); + + m = e & ((1 << -nBits) - 1); + e >>= -nBits; + nBits += mLen; + for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); + + if (e === 0) { + e = 1 - eBias; + } else if (e === eMax) { + return m ? NaN : (s ? -1 : 1) * Infinity; + } else { + m = m + Math.pow(2, mLen); + e = e - eBias; + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen); +}; + +var writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) { + var e, + m, + c, + bBE = endian === 'big', + eLen = nBytes * 8 - mLen - 1, + eMax = (1 << eLen) - 1, + eBias = eMax >> 1, + rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0, + i = bBE ? nBytes - 1 : 0, + d = bBE ? -1 : 1, + s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; + + value = Math.abs(value); + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0; + e = eMax; + } else { + e = Math.floor(Math.log(value) / Math.LN2); + if (value * (c = Math.pow(2, -e)) < 1) { + e--; + c *= 2; + } + if (e + eBias >= 1) { + value += rt / c; + } else { + value += rt * Math.pow(2, 1 - eBias); + } + if (value * c >= 2) { + e++; + c /= 2; + } + + if (e + eBias >= eMax) { + m = 0; + e = eMax; + } else if (e + eBias >= 1) { + m = (value * c - 1) * Math.pow(2, mLen); + e = e + eBias; + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); + e = 0; + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); + + e = (e << mLen) | m; + eLen += mLen; + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); + + buffer[offset + i - d] |= s * 128; +}; + +exports.readIEEE754 = readIEEE754; +exports.writeIEEE754 = writeIEEE754; + +},{}],36:[function(require,module,exports){ +/** + * A class representation of a BSON Int32 type. + * + * @class + * @param {number} value the number we want to represent as an int32. + * @return {Int32} + */ +var Int32 = function(value) { + if (!(this instanceof Int32)) return new Int32(value); + + this._bsontype = 'Int32'; + this.value = value; +}; + +/** + * Access the number value. + * + * @method + * @return {number} returns the wrapped int32 number. + */ +Int32.prototype.valueOf = function() { + return this.value; +}; + +/** + * @ignore + */ +Int32.prototype.toJSON = function() { + return this.value; +}; + +module.exports = Int32; +module.exports.Int32 = Int32; + +},{}],37:[function(require,module,exports){ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright 2009 Google Inc. All Rights Reserved + +/** + * Defines a Long class for representing a 64-bit two's-complement + * integer value, which faithfully simulates the behavior of a Java "Long". This + * implementation is derived from LongLib in GWT. + * + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Longs. + * + * The internal representation of a Long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @class + * @param {number} low the low (signed) 32 bits of the Long. + * @param {number} high the high (signed) 32 bits of the Long. + * @return {Long} + */ +function Long(low, high) { + if (!(this instanceof Long)) return new Long(low, high); + + this._bsontype = 'Long'; + /** + * @type {number} + * @ignore + */ + this.low_ = low | 0; // force into 32 signed bits. + + /** + * @type {number} + * @ignore + */ + this.high_ = high | 0; // force into 32 signed bits. +} + +/** + * Return the int value. + * + * @method + * @return {number} the value, assuming it is a 32-bit integer. + */ +Long.prototype.toInt = function() { + return this.low_; +}; + +/** + * Return the Number value. + * + * @method + * @return {number} the closest floating-point representation to this value. + */ +Long.prototype.toNumber = function() { + return this.high_ * Long.TWO_PWR_32_DBL_ + this.getLowBitsUnsigned(); +}; + +/** Converts the Long to a BigInt (arbitrary precision). */ +Long.prototype.toBigInt = function () { + return BigInt(this.toString()); +} + +/** + * Return the JSON value. + * + * @method + * @return {string} the JSON representation. + */ +Long.prototype.toJSON = function() { + return this.toString(); +}; + +/** + * Return the String value. + * + * @method + * @param {number} [opt_radix] the radix in which the text should be written. + * @return {string} the textual representation of this value. + */ +Long.prototype.toString = function(opt_radix) { + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (this.isZero()) { + return '0'; + } + + if (this.isNegative()) { + if (this.equals(Long.MIN_VALUE)) { + // We need to change the Long value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixLong = Long.fromNumber(radix); + var div = this.div(radixLong); + var rem = div.multiply(radixLong).subtract(this); + return div.toString(radix) + rem.toInt().toString(radix); + } else { + return '-' + this.negate().toString(radix); + } + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Long.fromNumber(Math.pow(radix, 6)); + + rem = this; + var result = ''; + + while (!rem.isZero()) { + var remDiv = rem.div(radixToPower); + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt(); + var digits = intval.toString(radix); + + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } +}; + +/** + * Return the high 32-bits value. + * + * @method + * @return {number} the high 32-bits as a signed value. + */ +Long.prototype.getHighBits = function() { + return this.high_; +}; + +/** + * Return the low 32-bits value. + * + * @method + * @return {number} the low 32-bits as a signed value. + */ +Long.prototype.getLowBits = function() { + return this.low_; +}; + +/** + * Return the low unsigned 32-bits value. + * + * @method + * @return {number} the low 32-bits as an unsigned value. + */ +Long.prototype.getLowBitsUnsigned = function() { + return this.low_ >= 0 ? this.low_ : Long.TWO_PWR_32_DBL_ + this.low_; +}; + +/** + * Returns the number of bits needed to represent the absolute value of this Long. + * + * @method + * @return {number} Returns the number of bits needed to represent the absolute value of this Long. + */ +Long.prototype.getNumBitsAbs = function() { + if (this.isNegative()) { + if (this.equals(Long.MIN_VALUE)) { + return 64; + } else { + return this.negate().getNumBitsAbs(); + } + } else { + var val = this.high_ !== 0 ? this.high_ : this.low_; + for (var bit = 31; bit > 0; bit--) { + if ((val & (1 << bit)) !== 0) { + break; + } + } + return this.high_ !== 0 ? bit + 33 : bit + 1; + } +}; + +/** + * Return whether this value is zero. + * + * @method + * @return {boolean} whether this value is zero. + */ +Long.prototype.isZero = function() { + return this.high_ === 0 && this.low_ === 0; +}; + +/** + * Return whether this value is negative. + * + * @method + * @return {boolean} whether this value is negative. + */ +Long.prototype.isNegative = function() { + return this.high_ < 0; +}; + +/** + * Return whether this value is odd. + * + * @method + * @return {boolean} whether this value is odd. + */ +Long.prototype.isOdd = function() { + return (this.low_ & 1) === 1; +}; + +/** + * Return whether this Long equals the other + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} whether this Long equals the other + */ +Long.prototype.equals = function(other) { + return this.high_ === other.high_ && this.low_ === other.low_; +}; + +/** + * Return whether this Long does not equal the other. + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} whether this Long does not equal the other. + */ +Long.prototype.notEquals = function(other) { + return this.high_ !== other.high_ || this.low_ !== other.low_; +}; + +/** + * Return whether this Long is less than the other. + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} whether this Long is less than the other. + */ +Long.prototype.lessThan = function(other) { + return this.compare(other) < 0; +}; + +/** + * Return whether this Long is less than or equal to the other. + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} whether this Long is less than or equal to the other. + */ +Long.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; +}; + +/** + * Return whether this Long is greater than the other. + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} whether this Long is greater than the other. + */ +Long.prototype.greaterThan = function(other) { + return this.compare(other) > 0; +}; + +/** + * Return whether this Long is greater than or equal to the other. + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} whether this Long is greater than or equal to the other. + */ +Long.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; +}; + +/** + * Compares this Long with the given one. + * + * @method + * @param {Long} other Long to compare against. + * @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater. + */ +Long.prototype.compare = function(other) { + if (this.equals(other)) { + return 0; + } + + var thisNeg = this.isNegative(); + var otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + + // at this point, the signs are the same, so subtraction will not overflow + if (this.subtract(other).isNegative()) { + return -1; + } else { + return 1; + } +}; + +/** + * The negation of this value. + * + * @method + * @return {Long} the negation of this value. + */ +Long.prototype.negate = function() { + if (this.equals(Long.MIN_VALUE)) { + return Long.MIN_VALUE; + } else { + return this.not().add(Long.ONE); + } +}; + +/** + * Returns the sum of this and the given Long. + * + * @method + * @param {Long} other Long to add to this one. + * @return {Long} the sum of this and the given Long. + */ +Long.prototype.add = function(other) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xffff; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xffff; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xffff; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xffff; + + var c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xffff; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xffff; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xffff; + c48 += a48 + b48; + c48 &= 0xffff; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns the difference of this and the given Long. + * + * @method + * @param {Long} other Long to subtract from this. + * @return {Long} the difference of this and the given Long. + */ +Long.prototype.subtract = function(other) { + return this.add(other.negate()); +}; + +/** + * Returns the product of this and the given Long. + * + * @method + * @param {Long} other Long to multiply with this. + * @return {Long} the product of this and the other. + */ +Long.prototype.multiply = function(other) { + if (this.isZero()) { + return Long.ZERO; + } else if (other.isZero()) { + return Long.ZERO; + } + + if (this.equals(Long.MIN_VALUE)) { + return other.isOdd() ? Long.MIN_VALUE : Long.ZERO; + } else if (other.equals(Long.MIN_VALUE)) { + return this.isOdd() ? Long.MIN_VALUE : Long.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate() + .multiply(other) + .negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + + // If both Longs are small, use float multiplication + if (this.lessThan(Long.TWO_PWR_24_) && other.lessThan(Long.TWO_PWR_24_)) { + return Long.fromNumber(this.toNumber() * other.toNumber()); + } + + // Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xffff; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xffff; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xffff; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xffff; + + var c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xffff; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xffff; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xffff; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xffff; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xffff; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xffff; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xffff; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns this Long divided by the given one. + * + * @method + * @param {Long} other Long by which to divide. + * @return {Long} this Long divided by the given one. + */ +Long.prototype.div = function(other) { + if (other.isZero()) { + throw Error('division by zero'); + } else if (this.isZero()) { + return Long.ZERO; + } + + if (this.equals(Long.MIN_VALUE)) { + if (other.equals(Long.ONE) || other.equals(Long.NEG_ONE)) { + return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + } else if (other.equals(Long.MIN_VALUE)) { + return Long.ONE; + } else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shiftRight(1); + var approx = halfThis.div(other).shiftLeft(1); + if (approx.equals(Long.ZERO)) { + return other.isNegative() ? Long.ONE : Long.NEG_ONE; + } else { + var rem = this.subtract(other.multiply(approx)); + var result = approx.add(rem.div(other)); + return result; + } + } + } else if (other.equals(Long.MIN_VALUE)) { + return Long.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().div(other.negate()); + } else { + return this.negate() + .div(other) + .negate(); + } + } else if (other.isNegative()) { + return this.div(other.negate()).negate(); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + var res = Long.ZERO; + rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48); + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + var approxRes = Long.fromNumber(approx); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = Long.fromNumber(approx); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = Long.ONE; + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; +}; + +/** + * Returns this Long modulo the given one. + * + * @method + * @param {Long} other Long by which to mod. + * @return {Long} this Long modulo the given one. + */ +Long.prototype.modulo = function(other) { + return this.subtract(this.div(other).multiply(other)); +}; + +/** + * The bitwise-NOT of this value. + * + * @method + * @return {Long} the bitwise-NOT of this value. + */ +Long.prototype.not = function() { + return Long.fromBits(~this.low_, ~this.high_); +}; + +/** + * Returns the bitwise-AND of this Long and the given one. + * + * @method + * @param {Long} other the Long with which to AND. + * @return {Long} the bitwise-AND of this and the other. + */ +Long.prototype.and = function(other) { + return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_); +}; + +/** + * Returns the bitwise-OR of this Long and the given one. + * + * @method + * @param {Long} other the Long with which to OR. + * @return {Long} the bitwise-OR of this and the other. + */ +Long.prototype.or = function(other) { + return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_); +}; + +/** + * Returns the bitwise-XOR of this Long and the given one. + * + * @method + * @param {Long} other the Long with which to XOR. + * @return {Long} the bitwise-XOR of this and the other. + */ +Long.prototype.xor = function(other) { + return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_); +}; + +/** + * Returns this Long with bits shifted to the left by the given amount. + * + * @method + * @param {number} numBits the number of bits by which to shift. + * @return {Long} this shifted to the left by the given amount. + */ +Long.prototype.shiftLeft = function(numBits) { + numBits &= 63; + if (numBits === 0) { + return this; + } else { + var low = this.low_; + if (numBits < 32) { + var high = this.high_; + return Long.fromBits(low << numBits, (high << numBits) | (low >>> (32 - numBits))); + } else { + return Long.fromBits(0, low << (numBits - 32)); + } + } +}; + +/** + * Returns this Long with bits shifted to the right by the given amount. + * + * @method + * @param {number} numBits the number of bits by which to shift. + * @return {Long} this shifted to the right by the given amount. + */ +Long.prototype.shiftRight = function(numBits) { + numBits &= 63; + if (numBits === 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >> numBits); + } else { + return Long.fromBits(high >> (numBits - 32), high >= 0 ? 0 : -1); + } + } +}; + +/** + * Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit. + * + * @method + * @param {number} numBits the number of bits by which to shift. + * @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits. + */ +Long.prototype.shiftRightUnsigned = function(numBits) { + numBits &= 63; + if (numBits === 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits); + } else if (numBits === 32) { + return Long.fromBits(high, 0); + } else { + return Long.fromBits(high >>> (numBits - 32), 0); + } + } +}; + +/** + * Returns a Long representing the given (32-bit) integer value. + * + * @method + * @param {number} value the 32-bit integer in question. + * @return {Long} the corresponding Long value. + */ +Long.fromInt = function(value) { + if (-128 <= value && value < 128) { + var cachedObj = Long.INT_CACHE_[value]; + if (cachedObj) { + return cachedObj; + } + } + + var obj = new Long(value | 0, value < 0 ? -1 : 0); + if (-128 <= value && value < 128) { + Long.INT_CACHE_[value] = obj; + } + return obj; +}; + +/** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * + * @method + * @param {number} value the number in question. + * @return {Long} the corresponding Long value. + */ +Long.fromNumber = function(value) { + if (isNaN(value) || !isFinite(value)) { + return Long.ZERO; + } else if (value <= -Long.TWO_PWR_63_DBL_) { + return Long.MIN_VALUE; + } else if (value + 1 >= Long.TWO_PWR_63_DBL_) { + return Long.MAX_VALUE; + } else if (value < 0) { + return Long.fromNumber(-value).negate(); + } else { + return new Long((value % Long.TWO_PWR_32_DBL_) | 0, (value / Long.TWO_PWR_32_DBL_) | 0); + } +}; + +/** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @param {bigint} value - The number in question + * @returns {Long} The corresponding Long value + */ +Long.fromBigInt = function(value) { + return Long.fromString(value.toString(10), 10); +} + +/** + * Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits. + * + * @method + * @param {number} lowBits the low 32-bits. + * @param {number} highBits the high 32-bits. + * @return {Long} the corresponding Long value. + */ +Long.fromBits = function(lowBits, highBits) { + return new Long(lowBits, highBits); +}; + +/** + * Returns a Long representation of the given string, written using the given radix. + * + * @method + * @param {string} str the textual representation of the Long. + * @param {number} opt_radix the radix in which the text is written. + * @return {Long} the corresponding Long value. + */ +Long.fromString = function(str, opt_radix) { + if (str.length === 0) { + throw Error('number format error: empty string'); + } + + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (str.charAt(0) === '-') { + return Long.fromString(str.substring(1), radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw Error('number format error: interior "-" character: ' + str); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Long.fromNumber(Math.pow(radix, 8)); + + var result = Long.ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i); + var value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = Long.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(Long.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(Long.fromNumber(value)); + } + } + return result; +}; + +// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the +// from* methods on which they depend. + +/** + * A cache of the Long representations of small integer values. + * @type {Object} + * @ignore + */ +Long.INT_CACHE_ = {}; + +// NOTE: the compiler should inline these constant values below and then remove +// these variables, so there should be no runtime penalty for these. + +/** + * Number used repeated below in calculations. This must appear before the + * first call to any from* function below. + * @type {number} + * @ignore + */ +Long.TWO_PWR_16_DBL_ = 1 << 16; + +/** + * @type {number} + * @ignore + */ +Long.TWO_PWR_24_DBL_ = 1 << 24; + +/** + * @type {number} + * @ignore + */ +Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @ignore + */ +Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2; + +/** + * @type {number} + * @ignore + */ +Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @ignore + */ +Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_; + +/** + * @type {number} + * @ignore + */ +Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2; + +/** @type {Long} */ +Long.ZERO = Long.fromInt(0); + +/** @type {Long} */ +Long.ONE = Long.fromInt(1); + +/** @type {Long} */ +Long.NEG_ONE = Long.fromInt(-1); + +/** @type {Long} */ +Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0); + +/** @type {Long} */ +Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0); + +/** + * @type {Long} + * @ignore + */ +Long.TWO_PWR_24_ = Long.fromInt(1 << 24); + +/** + * Expose. + */ +module.exports = Long; +module.exports.Long = Long; + +},{}],38:[function(require,module,exports){ +(function (global){(function (){ +'use strict'; + +// We have an ES6 Map available, return the native instance +if (typeof global.Map !== 'undefined') { + module.exports = global.Map; + module.exports.Map = global.Map; +} else { + // We will return a polyfill + var Map = function(array) { + this._keys = []; + this._values = {}; + + for (var i = 0; i < array.length; i++) { + if (array[i] == null) continue; // skip null and undefined + var entry = array[i]; + var key = entry[0]; + var value = entry[1]; + // Add the key to the list of keys in order + this._keys.push(key); + // Add the key and value to the values dictionary with a point + // to the location in the ordered keys list + this._values[key] = { v: value, i: this._keys.length - 1 }; + } + }; + + Map.prototype.clear = function() { + this._keys = []; + this._values = {}; + }; + + Map.prototype.delete = function(key) { + var value = this._values[key]; + if (value == null) return false; + // Delete entry + delete this._values[key]; + // Remove the key from the ordered keys list + this._keys.splice(value.i, 1); + return true; + }; + + Map.prototype.entries = function() { + var self = this; + var index = 0; + + return { + next: function() { + var key = self._keys[index++]; + return { + value: key !== undefined ? [key, self._values[key].v] : undefined, + done: key !== undefined ? false : true + }; + } + }; + }; + + Map.prototype.forEach = function(callback, self) { + self = self || this; + + for (var i = 0; i < this._keys.length; i++) { + var key = this._keys[i]; + // Call the forEach callback + callback.call(self, this._values[key].v, key, self); + } + }; + + Map.prototype.get = function(key) { + return this._values[key] ? this._values[key].v : undefined; + }; + + Map.prototype.has = function(key) { + return this._values[key] != null; + }; + + Map.prototype.keys = function() { + var self = this; + var index = 0; + + return { + next: function() { + var key = self._keys[index++]; + return { + value: key !== undefined ? key : undefined, + done: key !== undefined ? false : true + }; + } + }; + }; + + Map.prototype.set = function(key, value) { + if (this._values[key]) { + this._values[key].v = value; + return this; + } + + // Add the key to the list of keys in order + this._keys.push(key); + // Add the key and value to the values dictionary with a point + // to the location in the ordered keys list + this._values[key] = { v: value, i: this._keys.length - 1 }; + return this; + }; + + Map.prototype.values = function() { + var self = this; + var index = 0; + + return { + next: function() { + var key = self._keys[index++]; + return { + value: key !== undefined ? self._values[key].v : undefined, + done: key !== undefined ? false : true + }; + } + }; + }; + + // Last ismaster + Object.defineProperty(Map.prototype, 'size', { + enumerable: true, + get: function() { + return this._keys.length; + } + }); + + module.exports = Map; + module.exports.Map = Map; +} + +}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{}],39:[function(require,module,exports){ +/** + * A class representation of the BSON MaxKey type. + * + * @class + * @return {MaxKey} A MaxKey instance + */ +function MaxKey() { + if (!(this instanceof MaxKey)) return new MaxKey(); + + this._bsontype = 'MaxKey'; +} + +module.exports = MaxKey; +module.exports.MaxKey = MaxKey; + +},{}],40:[function(require,module,exports){ +/** + * A class representation of the BSON MinKey type. + * + * @class + * @return {MinKey} A MinKey instance + */ +function MinKey() { + if (!(this instanceof MinKey)) return new MinKey(); + + this._bsontype = 'MinKey'; +} + +module.exports = MinKey; +module.exports.MinKey = MinKey; + +},{}],41:[function(require,module,exports){ +(function (process,Buffer){(function (){ +// Custom inspect property name / symbol. +var inspect = 'inspect'; + +var utils = require('./parser/utils'); + +/** + * Machine id. + * + * Create a random 3-byte value (i.e. unique for this + * process). Other drivers use a md5 of the machine id here, but + * that would mean an asyc call to gethostname, so we don't bother. + * @ignore + */ +var MACHINE_ID = parseInt(Math.random() * 0xffffff, 10); + +// Regular expression that checks for hex value +var checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$'); + +// Check if buffer exists +try { + if (Buffer && Buffer.from) { + var hasBufferType = true; + inspect = require('util').inspect.custom || 'inspect'; + } +} catch (err) { + hasBufferType = false; +} + +/** +* Create a new ObjectID instance +* +* @class +* @param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number. +* @property {number} generationTime The generation time of this ObjectId instance +* @return {ObjectID} instance of ObjectID. +*/ +var ObjectID = function ObjectID(id) { + // Duck-typing to support ObjectId from different npm packages + if (id instanceof ObjectID) return id; + if (!(this instanceof ObjectID)) return new ObjectID(id); + + this._bsontype = 'ObjectID'; + + // The most common usecase (blank id, new objectId instance) + if (id == null || typeof id === 'number') { + // Generate a new id + this.id = this.generate(id); + // If we are caching the hex string + if (ObjectID.cacheHexString) this.__id = this.toString('hex'); + // Return the object + return; + } + + // Check if the passed in id is valid + var valid = ObjectID.isValid(id); + + // Throw an error if it's not a valid setup + if (!valid && id != null) { + throw new Error( + 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' + ); + } else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) { + return new ObjectID(utils.toBuffer(id, 'hex')); + } else if (valid && typeof id === 'string' && id.length === 24) { + return ObjectID.createFromHexString(id); + } else if (id != null && id.length === 12) { + // assume 12 byte string + this.id = id; + } else if (id != null && typeof id.toHexString === 'function') { + // Duck-typing to support ObjectId from different npm packages + return id; + } else { + throw new Error( + 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' + ); + } + + if (ObjectID.cacheHexString) this.__id = this.toString('hex'); +}; + +// Allow usage of ObjectId as well as ObjectID +// var ObjectId = ObjectID; + +// Precomputed hex table enables speedy hex string conversion +var hexTable = []; +for (var i = 0; i < 256; i++) { + hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16); +} + +/** +* Return the ObjectID id as a 24 byte hex string representation +* +* @method +* @return {string} return the 24 byte hex string representation. +*/ +ObjectID.prototype.toHexString = function() { + if (ObjectID.cacheHexString && this.__id) return this.__id; + + var hexString = ''; + if (!this.id || !this.id.length) { + throw new Error( + 'invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [' + + JSON.stringify(this.id) + + ']' + ); + } + + if (this.id instanceof _Buffer) { + hexString = convertToHex(this.id); + if (ObjectID.cacheHexString) this.__id = hexString; + return hexString; + } + + for (var i = 0; i < this.id.length; i++) { + hexString += hexTable[this.id.charCodeAt(i)]; + } + + if (ObjectID.cacheHexString) this.__id = hexString; + return hexString; +}; + +/** +* Update the ObjectID index used in generating new ObjectID's on the driver +* +* @method +* @return {number} returns next index value. +* @ignore +*/ +ObjectID.prototype.get_inc = function() { + return (ObjectID.index = (ObjectID.index + 1) % 0xffffff); +}; + +/** +* Update the ObjectID index used in generating new ObjectID's on the driver +* +* @method +* @return {number} returns next index value. +* @ignore +*/ +ObjectID.prototype.getInc = function() { + return this.get_inc(); +}; + +/** +* Generate a 12 byte id buffer used in ObjectID's +* +* @method +* @param {number} [time] optional parameter allowing to pass in a second based timestamp. +* @return {Buffer} return the 12 byte id buffer string. +*/ +ObjectID.prototype.generate = function(time) { + if ('number' !== typeof time) { + time = ~~(Date.now() / 1000); + } + + // Use pid + var pid = + (typeof process === 'undefined' || process.pid === 1 + ? Math.floor(Math.random() * 100000) + : process.pid) % 0xffff; + var inc = this.get_inc(); + // Buffer used + var buffer = utils.allocBuffer(12); + // Encode time + buffer[3] = time & 0xff; + buffer[2] = (time >> 8) & 0xff; + buffer[1] = (time >> 16) & 0xff; + buffer[0] = (time >> 24) & 0xff; + // Encode machine + buffer[6] = MACHINE_ID & 0xff; + buffer[5] = (MACHINE_ID >> 8) & 0xff; + buffer[4] = (MACHINE_ID >> 16) & 0xff; + // Encode pid + buffer[8] = pid & 0xff; + buffer[7] = (pid >> 8) & 0xff; + // Encode index + buffer[11] = inc & 0xff; + buffer[10] = (inc >> 8) & 0xff; + buffer[9] = (inc >> 16) & 0xff; + // Return the buffer + return buffer; +}; + +/** +* Converts the id into a 24 byte hex string for printing +* +* @param {String} format The Buffer toString format parameter. +* @return {String} return the 24 byte hex string representation. +* @ignore +*/ +ObjectID.prototype.toString = function(format) { + // Is the id a buffer then use the buffer toString method to return the format + if (this.id && this.id.copy) { + return this.id.toString(typeof format === 'string' ? format : 'hex'); + } + + // if(this.buffer ) + return this.toHexString(); +}; + +/** +* Converts to a string representation of this Id. +* +* @return {String} return the 24 byte hex string representation. +* @ignore +*/ +ObjectID.prototype[inspect] = ObjectID.prototype.toString; + +/** +* Converts to its JSON representation. +* +* @return {String} return the 24 byte hex string representation. +* @ignore +*/ +ObjectID.prototype.toJSON = function() { + return this.toHexString(); +}; + +/** +* Compares the equality of this ObjectID with `otherID`. +* +* @method +* @param {object} otherID ObjectID instance to compare against. +* @return {boolean} the result of comparing two ObjectID's +*/ +ObjectID.prototype.equals = function equals(otherId) { + // var id; + + if (otherId instanceof ObjectID) { + return this.toString() === otherId.toString(); + } else if ( + typeof otherId === 'string' && + ObjectID.isValid(otherId) && + otherId.length === 12 && + this.id instanceof _Buffer + ) { + return otherId === this.id.toString('binary'); + } else if (typeof otherId === 'string' && ObjectID.isValid(otherId) && otherId.length === 24) { + return otherId.toLowerCase() === this.toHexString(); + } else if (typeof otherId === 'string' && ObjectID.isValid(otherId) && otherId.length === 12) { + return otherId === this.id; + } else if (otherId != null && (otherId instanceof ObjectID || otherId.toHexString)) { + return otherId.toHexString() === this.toHexString(); + } else { + return false; + } +}; + +/** +* Returns the generation date (accurate up to the second) that this ID was generated. +* +* @method +* @return {date} the generation date +*/ +ObjectID.prototype.getTimestamp = function() { + var timestamp = new Date(); + var time = this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24); + timestamp.setTime(Math.floor(time) * 1000); + return timestamp; +}; + +/** +* @ignore +*/ +ObjectID.index = ~~(Math.random() * 0xffffff); + +/** +* @ignore +*/ +ObjectID.createPk = function createPk() { + return new ObjectID(); +}; + +/** +* Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID. +* +* @method +* @param {number} time an integer number representing a number of seconds. +* @return {ObjectID} return the created ObjectID +*/ +ObjectID.createFromTime = function createFromTime(time) { + var buffer = utils.toBuffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + // Encode time into first 4 bytes + buffer[3] = time & 0xff; + buffer[2] = (time >> 8) & 0xff; + buffer[1] = (time >> 16) & 0xff; + buffer[0] = (time >> 24) & 0xff; + // Return the new objectId + return new ObjectID(buffer); +}; + +// Lookup tables +//var encodeLookup = '0123456789abcdef'.split(''); +var decodeLookup = []; +i = 0; +while (i < 10) decodeLookup[0x30 + i] = i++; +while (i < 16) decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++; + +var _Buffer = Buffer; +var convertToHex = function(bytes) { + return bytes.toString('hex'); +}; + +/** +* Creates an ObjectID from a hex string representation of an ObjectID. +* +* @method +* @param {string} hexString create a ObjectID from a passed in 24 byte hexstring. +* @return {ObjectID} return the created ObjectID +*/ +ObjectID.createFromHexString = function createFromHexString(string) { + // Throw an error if it's not a valid setup + if (typeof string === 'undefined' || (string != null && string.length !== 24)) { + throw new Error( + 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' + ); + } + + // Use Buffer.from method if available + if (hasBufferType) return new ObjectID(utils.toBuffer(string, 'hex')); + + // Calculate lengths + var array = new _Buffer(12); + var n = 0; + var i = 0; + + while (i < 24) { + array[n++] = (decodeLookup[string.charCodeAt(i++)] << 4) | decodeLookup[string.charCodeAt(i++)]; + } + + return new ObjectID(array); +}; + +/** +* Checks if a value is a valid bson ObjectId +* +* @method +* @return {boolean} return true if the value is a valid bson ObjectId, return false otherwise. +*/ +ObjectID.isValid = function isValid(id) { + if (id == null) return false; + + if (typeof id === 'number') { + return true; + } + + if (typeof id === 'string') { + return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id)); + } + + if (id instanceof ObjectID) { + return true; + } + + if (id instanceof _Buffer) { + return true; + } + + // Duck-Typing detection of ObjectId like objects + if ( + typeof id.toHexString === 'function' && + (id.id instanceof _Buffer || typeof id.id === 'string') + ) { + return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id)); + } + + return false; +}; + +/** +* @ignore +*/ +Object.defineProperty(ObjectID.prototype, 'generationTime', { + enumerable: true, + get: function() { + return this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24); + }, + set: function(value) { + // Encode time into first 4 bytes + this.id[3] = value & 0xff; + this.id[2] = (value >> 8) & 0xff; + this.id[1] = (value >> 16) & 0xff; + this.id[0] = (value >> 24) & 0xff; + } +}); + +/** + * Expose. + */ +module.exports = ObjectID; +module.exports.ObjectID = ObjectID; +module.exports.ObjectId = ObjectID; + +}).call(this)}).call(this,require('_process'),require("buffer").Buffer) +},{"./parser/utils":45,"_process":362,"buffer":49,"util":370}],42:[function(require,module,exports){ +(function (Buffer){(function (){ +'use strict'; + +var Long = require('../long').Long, + Double = require('../double').Double, + Timestamp = require('../timestamp').Timestamp, + ObjectID = require('../objectid').ObjectID, + Symbol = require('../symbol').Symbol, + BSONRegExp = require('../regexp').BSONRegExp, + Code = require('../code').Code, + Decimal128 = require('../decimal128'), + MinKey = require('../min_key').MinKey, + MaxKey = require('../max_key').MaxKey, + DBRef = require('../db_ref').DBRef, + Binary = require('../binary').Binary; + +var normalizedFunctionString = require('./utils').normalizedFunctionString; + +// To ensure that 0.4 of node works correctly +var isDate = function isDate(d) { + return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]'; +}; + +var calculateObjectSize = function calculateObjectSize( + object, + serializeFunctions, + ignoreUndefined +) { + var totalLength = 4 + 1; + + if (Array.isArray(object)) { + for (var i = 0; i < object.length; i++) { + totalLength += calculateElement( + i.toString(), + object[i], + serializeFunctions, + true, + ignoreUndefined + ); + } + } else { + // If we have toBSON defined, override the current object + if (object.toBSON) { + object = object.toBSON(); + } + + // Calculate size + for (var key in object) { + totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined); + } + } + + return totalLength; +}; + +/** + * @ignore + * @api private + */ +function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefined) { + // If we have toBSON defined, override the current object + if (value && value.toBSON) { + value = value.toBSON(); + } + + switch (typeof value) { + case 'string': + return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1; + case 'number': + if (Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { + if (value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { + // 32 bit + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1); + } else { + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); + } + } else { + // 64 bit + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); + } + case 'undefined': + if (isArray || !ignoreUndefined) + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1; + return 0; + case 'boolean': + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1); + case 'object': + if ( + value == null || + value instanceof MinKey || + value instanceof MaxKey || + value['_bsontype'] === 'MinKey' || + value['_bsontype'] === 'MaxKey' + ) { + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1; + } else if (value instanceof ObjectID || value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') { + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1); + } else if (value instanceof Date || isDate(value)) { + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); + } else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.length + ); + } else if ( + value instanceof Long || + value instanceof Double || + value instanceof Timestamp || + value['_bsontype'] === 'Long' || + value['_bsontype'] === 'Double' || + value['_bsontype'] === 'Timestamp' + ) { + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); + } else if (value instanceof Decimal128 || value['_bsontype'] === 'Decimal128') { + return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1); + } else if (value instanceof Code || value['_bsontype'] === 'Code') { + // Calculate size depending on the availability of a scope + if (value.scope != null && Object.keys(value.scope).length > 0) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + 4 + + 4 + + Buffer.byteLength(value.code.toString(), 'utf8') + + 1 + + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined) + ); + } else { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + 4 + + Buffer.byteLength(value.code.toString(), 'utf8') + + 1 + ); + } + } else if (value instanceof Binary || value['_bsontype'] === 'Binary') { + // Check what kind of subtype we have + if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + (value.position + 1 + 4 + 1 + 4) + ); + } else { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1) + ); + } + } else if (value instanceof Symbol || value['_bsontype'] === 'Symbol') { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + Buffer.byteLength(value.value, 'utf8') + + 4 + + 1 + + 1 + ); + } else if (value instanceof DBRef || value['_bsontype'] === 'DBRef') { + // Set up correct object for serialization + var ordered_values = { + $ref: value.namespace, + $id: value.oid + }; + + // Add db reference if it exists + if (null != value.db) { + ordered_values['$db'] = value.db; + } + + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined) + ); + } else if ( + value instanceof RegExp || + Object.prototype.toString.call(value) === '[object RegExp]' + ) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + Buffer.byteLength(value.source, 'utf8') + + 1 + + (value.global ? 1 : 0) + + (value.ignoreCase ? 1 : 0) + + (value.multiline ? 1 : 0) + + 1 + ); + } else if (value instanceof BSONRegExp || value['_bsontype'] === 'BSONRegExp') { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + Buffer.byteLength(value.pattern, 'utf8') + + 1 + + Buffer.byteLength(value.options, 'utf8') + + 1 + ); + } else { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + calculateObjectSize(value, serializeFunctions, ignoreUndefined) + + 1 + ); + } + case 'function': + // WTF for 0.4.X where typeof /someregexp/ === 'function' + if ( + value instanceof RegExp || + Object.prototype.toString.call(value) === '[object RegExp]' || + String.call(value) === '[object RegExp]' + ) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + Buffer.byteLength(value.source, 'utf8') + + 1 + + (value.global ? 1 : 0) + + (value.ignoreCase ? 1 : 0) + + (value.multiline ? 1 : 0) + + 1 + ); + } else { + if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + 4 + + 4 + + Buffer.byteLength(normalizedFunctionString(value), 'utf8') + + 1 + + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined) + ); + } else if (serializeFunctions) { + return ( + (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + + 1 + + 4 + + Buffer.byteLength(normalizedFunctionString(value), 'utf8') + + 1 + ); + } + } + } + + return 0; +} + +var BSON = {}; + +// BSON MAX VALUES +BSON.BSON_INT32_MAX = 0x7fffffff; +BSON.BSON_INT32_MIN = -0x80000000; + +// JS MAX PRECISE VALUES +BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double. +BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double. + +module.exports = calculateObjectSize; + +}).call(this)}).call(this,require("buffer").Buffer) +},{"../binary":29,"../code":31,"../db_ref":32,"../decimal128":33,"../double":34,"../long":37,"../max_key":39,"../min_key":40,"../objectid":41,"../regexp":46,"../symbol":47,"../timestamp":48,"./utils":45,"buffer":49}],43:[function(require,module,exports){ +'use strict'; + +var Long = require('../long').Long, + Double = require('../double').Double, + Timestamp = require('../timestamp').Timestamp, + ObjectID = require('../objectid').ObjectID, + Symbol = require('../symbol').Symbol, + Code = require('../code').Code, + MinKey = require('../min_key').MinKey, + MaxKey = require('../max_key').MaxKey, + Decimal128 = require('../decimal128'), + Int32 = require('../int_32'), + DBRef = require('../db_ref').DBRef, + BSONRegExp = require('../regexp').BSONRegExp, + Binary = require('../binary').Binary; + +var utils = require('./utils'); + +var deserialize = function(buffer, options, isArray) { + options = options == null ? {} : options; + var index = options && options.index ? options.index : 0; + // Read the document size + var size = + buffer[index] | + (buffer[index + 1] << 8) | + (buffer[index + 2] << 16) | + (buffer[index + 3] << 24); + + // Ensure buffer is valid size + if (size < 5 || buffer.length < size || size + index > buffer.length) { + throw new Error('corrupt bson message'); + } + + // Illegal end value + if (buffer[index + size - 1] !== 0) { + throw new Error("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00"); + } + + // Start deserializtion + return deserializeObject(buffer, index, options, isArray); +}; + +var deserializeObject = function(buffer, index, options, isArray) { + var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions']; + var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions']; + var cacheFunctionsCrc32 = + options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32']; + + if (!cacheFunctionsCrc32) var crc32 = null; + + var fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw']; + + // Return raw bson buffer instead of parsing it + var raw = options['raw'] == null ? false : options['raw']; + + // Return BSONRegExp objects instead of native regular expressions + var bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false; + + // Controls the promotion of values vs wrapper classes + var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers']; + var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs']; + var promoteValues = options['promoteValues'] == null ? true : options['promoteValues']; + + // Set the start index + var startIndex = index; + + // Validate that we have at least 4 bytes of buffer + if (buffer.length < 5) throw new Error('corrupt bson message < 5 bytes long'); + + // Read the document size + var size = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + + // Ensure buffer is valid size + if (size < 5 || size > buffer.length) throw new Error('corrupt bson message'); + + // Create holding object + var object = isArray ? [] : {}; + // Used for arrays to skip having to perform utf8 decoding + var arrayIndex = 0; + + var done = false; + + // While we have more left data left keep parsing + // while (buffer[index + 1] !== 0) { + while (!done) { + // Read the type + var elementType = buffer[index++]; + // If we get a zero it's the last byte, exit + if (elementType === 0) break; + + // Get the start search index + var i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); + var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i); + + index = i + 1; + + if (elementType === BSON.BSON_DATA_STRING) { + var stringSize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) + throw new Error('bad string length in bson'); + object[name] = buffer.toString('utf8', index, index + stringSize - 1); + index = index + stringSize; + } else if (elementType === BSON.BSON_DATA_OID) { + var oid = utils.allocBuffer(12); + buffer.copy(oid, 0, index, index + 12); + object[name] = new ObjectID(oid); + index = index + 12; + } else if (elementType === BSON.BSON_DATA_INT && promoteValues === false) { + object[name] = new Int32( + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24) + ); + } else if (elementType === BSON.BSON_DATA_INT) { + object[name] = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + } else if (elementType === BSON.BSON_DATA_NUMBER && promoteValues === false) { + object[name] = new Double(buffer.readDoubleLE(index)); + index = index + 8; + } else if (elementType === BSON.BSON_DATA_NUMBER) { + object[name] = buffer.readDoubleLE(index); + index = index + 8; + } else if (elementType === BSON.BSON_DATA_DATE) { + var lowBits = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + var highBits = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + object[name] = new Date(new Long(lowBits, highBits).toNumber()); + } else if (elementType === BSON.BSON_DATA_BOOLEAN) { + if (buffer[index] !== 0 && buffer[index] !== 1) throw new Error('illegal boolean type value'); + object[name] = buffer[index++] === 1; + } else if (elementType === BSON.BSON_DATA_OBJECT) { + var _index = index; + var objectSize = + buffer[index] | + (buffer[index + 1] << 8) | + (buffer[index + 2] << 16) | + (buffer[index + 3] << 24); + if (objectSize <= 0 || objectSize > buffer.length - index) + throw new Error('bad embedded document length in bson'); + + // We have a raw value + if (raw) { + object[name] = buffer.slice(index, index + objectSize); + } else { + object[name] = deserializeObject(buffer, _index, options, false); + } + + index = index + objectSize; + } else if (elementType === BSON.BSON_DATA_ARRAY) { + _index = index; + objectSize = + buffer[index] | + (buffer[index + 1] << 8) | + (buffer[index + 2] << 16) | + (buffer[index + 3] << 24); + var arrayOptions = options; + + // Stop index + var stopIndex = index + objectSize; + + // All elements of array to be returned as raw bson + if (fieldsAsRaw && fieldsAsRaw[name]) { + arrayOptions = {}; + for (var n in options) arrayOptions[n] = options[n]; + arrayOptions['raw'] = true; + } + + object[name] = deserializeObject(buffer, _index, arrayOptions, true); + index = index + objectSize; + + if (buffer[index - 1] !== 0) throw new Error('invalid array terminator byte'); + if (index !== stopIndex) throw new Error('corrupted array bson'); + } else if (elementType === BSON.BSON_DATA_UNDEFINED) { + object[name] = undefined; + } else if (elementType === BSON.BSON_DATA_NULL) { + object[name] = null; + } else if (elementType === BSON.BSON_DATA_LONG) { + // Unpack the low and high bits + lowBits = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + highBits = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + var long = new Long(lowBits, highBits); + // Promote the long if possible + if (promoteLongs && promoteValues === true) { + object[name] = + long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) + ? long.toNumber() + : long; + } else { + object[name] = long; + } + } else if (elementType === BSON.BSON_DATA_DECIMAL128) { + // Buffer to contain the decimal bytes + var bytes = utils.allocBuffer(16); + // Copy the next 16 bytes into the bytes buffer + buffer.copy(bytes, 0, index, index + 16); + // Update index + index = index + 16; + // Assign the new Decimal128 value + var decimal128 = new Decimal128(bytes); + // If we have an alternative mapper use that + object[name] = decimal128.toObject ? decimal128.toObject() : decimal128; + } else if (elementType === BSON.BSON_DATA_BINARY) { + var binarySize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + var totalBinarySize = binarySize; + var subType = buffer[index++]; + + // Did we have a negative binary size, throw + if (binarySize < 0) throw new Error('Negative binary type element size found'); + + // Is the length longer than the document + if (binarySize > buffer.length) throw new Error('Binary type size larger than document size'); + + // Decode as raw Buffer object if options specifies it + if (buffer['slice'] != null) { + // If we have subtype 2 skip the 4 bytes for the size + if (subType === Binary.SUBTYPE_BYTE_ARRAY) { + binarySize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + if (binarySize < 0) + throw new Error('Negative binary type element size found for subtype 0x02'); + if (binarySize > totalBinarySize - 4) + throw new Error('Binary type with subtype 0x02 contains to long binary size'); + if (binarySize < totalBinarySize - 4) + throw new Error('Binary type with subtype 0x02 contains to short binary size'); + } + + if (promoteBuffers && promoteValues) { + object[name] = buffer.slice(index, index + binarySize); + } else { + object[name] = new Binary(buffer.slice(index, index + binarySize), subType); + } + } else { + var _buffer = + typeof Uint8Array !== 'undefined' + ? new Uint8Array(new ArrayBuffer(binarySize)) + : new Array(binarySize); + // If we have subtype 2 skip the 4 bytes for the size + if (subType === Binary.SUBTYPE_BYTE_ARRAY) { + binarySize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + if (binarySize < 0) + throw new Error('Negative binary type element size found for subtype 0x02'); + if (binarySize > totalBinarySize - 4) + throw new Error('Binary type with subtype 0x02 contains to long binary size'); + if (binarySize < totalBinarySize - 4) + throw new Error('Binary type with subtype 0x02 contains to short binary size'); + } + + // Copy the data + for (i = 0; i < binarySize; i++) { + _buffer[i] = buffer[index + i]; + } + + if (promoteBuffers && promoteValues) { + object[name] = _buffer; + } else { + object[name] = new Binary(_buffer, subType); + } + } + + // Update the index + index = index + binarySize; + } else if (elementType === BSON.BSON_DATA_REGEXP && bsonRegExp === false) { + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); + // Return the C string + var source = buffer.toString('utf8', index, i); + // Create the regexp + index = i + 1; + + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); + // Return the C string + var regExpOptions = buffer.toString('utf8', index, i); + index = i + 1; + + // For each option add the corresponding one for javascript + var optionsArray = new Array(regExpOptions.length); + + // Parse options + for (i = 0; i < regExpOptions.length; i++) { + switch (regExpOptions[i]) { + case 'm': + optionsArray[i] = 'm'; + break; + case 's': + optionsArray[i] = 'g'; + break; + case 'i': + optionsArray[i] = 'i'; + break; + } + } + + object[name] = new RegExp(source, optionsArray.join('')); + } else if (elementType === BSON.BSON_DATA_REGEXP && bsonRegExp === true) { + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); + // Return the C string + source = buffer.toString('utf8', index, i); + index = i + 1; + + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); + // Return the C string + regExpOptions = buffer.toString('utf8', index, i); + index = i + 1; + + // Set the object + object[name] = new BSONRegExp(source, regExpOptions); + } else if (elementType === BSON.BSON_DATA_SYMBOL) { + stringSize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) + throw new Error('bad string length in bson'); + object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1)); + index = index + stringSize; + } else if (elementType === BSON.BSON_DATA_TIMESTAMP) { + lowBits = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + highBits = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + object[name] = new Timestamp(lowBits, highBits); + } else if (elementType === BSON.BSON_DATA_MIN_KEY) { + object[name] = new MinKey(); + } else if (elementType === BSON.BSON_DATA_MAX_KEY) { + object[name] = new MaxKey(); + } else if (elementType === BSON.BSON_DATA_CODE) { + stringSize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) + throw new Error('bad string length in bson'); + var functionString = buffer.toString('utf8', index, index + stringSize - 1); + + // If we are evaluating the functions + if (evalFunctions) { + // If we have cache enabled let's look for the md5 of the function in the cache + if (cacheFunctions) { + var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString; + // Got to do this to avoid V8 deoptimizing the call due to finding eval + object[name] = isolateEvalWithHash(functionCache, hash, functionString, object); + } else { + object[name] = isolateEval(functionString); + } + } else { + object[name] = new Code(functionString); + } + + // Update parse index position + index = index + stringSize; + } else if (elementType === BSON.BSON_DATA_CODE_W_SCOPE) { + var totalSize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + + // Element cannot be shorter than totalSize + stringSize + documentSize + terminator + if (totalSize < 4 + 4 + 4 + 1) { + throw new Error('code_w_scope total size shorter minimum expected length'); + } + + // Get the code string size + stringSize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + // Check if we have a valid string + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) + throw new Error('bad string length in bson'); + + // Javascript function + functionString = buffer.toString('utf8', index, index + stringSize - 1); + // Update parse index position + index = index + stringSize; + // Parse the element + _index = index; + // Decode the size of the object document + objectSize = + buffer[index] | + (buffer[index + 1] << 8) | + (buffer[index + 2] << 16) | + (buffer[index + 3] << 24); + // Decode the scope object + var scopeObject = deserializeObject(buffer, _index, options, false); + // Adjust the index + index = index + objectSize; + + // Check if field length is to short + if (totalSize < 4 + 4 + objectSize + stringSize) { + throw new Error('code_w_scope total size is to short, truncating scope'); + } + + // Check if totalSize field is to long + if (totalSize > 4 + 4 + objectSize + stringSize) { + throw new Error('code_w_scope total size is to long, clips outer document'); + } + + // If we are evaluating the functions + if (evalFunctions) { + // If we have cache enabled let's look for the md5 of the function in the cache + if (cacheFunctions) { + hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString; + // Got to do this to avoid V8 deoptimizing the call due to finding eval + object[name] = isolateEvalWithHash(functionCache, hash, functionString, object); + } else { + object[name] = isolateEval(functionString); + } + + object[name].scope = scopeObject; + } else { + object[name] = new Code(functionString, scopeObject); + } + } else if (elementType === BSON.BSON_DATA_DBPOINTER) { + // Get the code string size + stringSize = + buffer[index++] | + (buffer[index++] << 8) | + (buffer[index++] << 16) | + (buffer[index++] << 24); + // Check if we have a valid string + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) + throw new Error('bad string length in bson'); + // Namespace + var namespace = buffer.toString('utf8', index, index + stringSize - 1); + // Update parse index position + index = index + stringSize; + + // Read the oid + var oidBuffer = utils.allocBuffer(12); + buffer.copy(oidBuffer, 0, index, index + 12); + oid = new ObjectID(oidBuffer); + + // Update the index + index = index + 12; + + // Split the namespace + var parts = namespace.split('.'); + var db = parts.shift(); + var collection = parts.join('.'); + // Upgrade to DBRef type + object[name] = new DBRef(collection, oid, db); + } else { + throw new Error( + 'Detected unknown BSON type ' + + elementType.toString(16) + + ' for fieldname "' + + name + + '", are you using the latest BSON parser' + ); + } + } + + // Check if the deserialization was against a valid array/object + if (size !== index - startIndex) { + if (isArray) throw new Error('corrupt array bson'); + throw new Error('corrupt object bson'); + } + + // Check if we have a db ref object + if (object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']); + return object; +}; + +/** + * Ensure eval is isolated. + * + * @ignore + * @api private + */ +var isolateEvalWithHash = function(functionCache, hash, functionString, object) { + // Contains the value we are going to set + var value = null; + + // Check for cache hit, eval if missing and return cached function + if (functionCache[hash] == null) { + eval('value = ' + functionString); + functionCache[hash] = value; + } + // Set the object + return functionCache[hash].bind(object); +}; + +/** + * Ensure eval is isolated. + * + * @ignore + * @api private + */ +var isolateEval = function(functionString) { + // Contains the value we are going to set + var value = null; + // Eval the function + eval('value = ' + functionString); + return value; +}; + +var BSON = {}; + +/** + * Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5 + * + * @ignore + * @api private + */ +var functionCache = (BSON.functionCache = {}); + +/** + * Number BSON Type + * + * @classconstant BSON_DATA_NUMBER + **/ +BSON.BSON_DATA_NUMBER = 1; +/** + * String BSON Type + * + * @classconstant BSON_DATA_STRING + **/ +BSON.BSON_DATA_STRING = 2; +/** + * Object BSON Type + * + * @classconstant BSON_DATA_OBJECT + **/ +BSON.BSON_DATA_OBJECT = 3; +/** + * Array BSON Type + * + * @classconstant BSON_DATA_ARRAY + **/ +BSON.BSON_DATA_ARRAY = 4; +/** + * Binary BSON Type + * + * @classconstant BSON_DATA_BINARY + **/ +BSON.BSON_DATA_BINARY = 5; +/** + * Binary BSON Type + * + * @classconstant BSON_DATA_UNDEFINED + **/ +BSON.BSON_DATA_UNDEFINED = 6; +/** + * ObjectID BSON Type + * + * @classconstant BSON_DATA_OID + **/ +BSON.BSON_DATA_OID = 7; +/** + * Boolean BSON Type + * + * @classconstant BSON_DATA_BOOLEAN + **/ +BSON.BSON_DATA_BOOLEAN = 8; +/** + * Date BSON Type + * + * @classconstant BSON_DATA_DATE + **/ +BSON.BSON_DATA_DATE = 9; +/** + * null BSON Type + * + * @classconstant BSON_DATA_NULL + **/ +BSON.BSON_DATA_NULL = 10; +/** + * RegExp BSON Type + * + * @classconstant BSON_DATA_REGEXP + **/ +BSON.BSON_DATA_REGEXP = 11; +/** + * Code BSON Type + * + * @classconstant BSON_DATA_DBPOINTER + **/ +BSON.BSON_DATA_DBPOINTER = 12; +/** + * Code BSON Type + * + * @classconstant BSON_DATA_CODE + **/ +BSON.BSON_DATA_CODE = 13; +/** + * Symbol BSON Type + * + * @classconstant BSON_DATA_SYMBOL + **/ +BSON.BSON_DATA_SYMBOL = 14; +/** + * Code with Scope BSON Type + * + * @classconstant BSON_DATA_CODE_W_SCOPE + **/ +BSON.BSON_DATA_CODE_W_SCOPE = 15; +/** + * 32 bit Integer BSON Type + * + * @classconstant BSON_DATA_INT + **/ +BSON.BSON_DATA_INT = 16; +/** + * Timestamp BSON Type + * + * @classconstant BSON_DATA_TIMESTAMP + **/ +BSON.BSON_DATA_TIMESTAMP = 17; +/** + * Long BSON Type + * + * @classconstant BSON_DATA_LONG + **/ +BSON.BSON_DATA_LONG = 18; +/** + * Long BSON Type + * + * @classconstant BSON_DATA_DECIMAL128 + **/ +BSON.BSON_DATA_DECIMAL128 = 19; +/** + * MinKey BSON Type + * + * @classconstant BSON_DATA_MIN_KEY + **/ +BSON.BSON_DATA_MIN_KEY = 0xff; +/** + * MaxKey BSON Type + * + * @classconstant BSON_DATA_MAX_KEY + **/ +BSON.BSON_DATA_MAX_KEY = 0x7f; + +/** + * Binary Default Type + * + * @classconstant BSON_BINARY_SUBTYPE_DEFAULT + **/ +BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; +/** + * Binary Function Type + * + * @classconstant BSON_BINARY_SUBTYPE_FUNCTION + **/ +BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1; +/** + * Binary Byte Array Type + * + * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY + **/ +BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +/** + * Binary UUID Type + * + * @classconstant BSON_BINARY_SUBTYPE_UUID + **/ +BSON.BSON_BINARY_SUBTYPE_UUID = 3; +/** + * Binary MD5 Type + * + * @classconstant BSON_BINARY_SUBTYPE_MD5 + **/ +BSON.BSON_BINARY_SUBTYPE_MD5 = 4; +/** + * Binary User Defined Type + * + * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED + **/ +BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +// BSON MAX VALUES +BSON.BSON_INT32_MAX = 0x7fffffff; +BSON.BSON_INT32_MIN = -0x80000000; + +BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1; +BSON.BSON_INT64_MIN = -Math.pow(2, 63); + +// JS MAX PRECISE VALUES +BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double. +BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double. + +// Internal long versions +var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double. +var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double. + +module.exports = deserialize; + +},{"../binary":29,"../code":31,"../db_ref":32,"../decimal128":33,"../double":34,"../int_32":36,"../long":37,"../max_key":39,"../min_key":40,"../objectid":41,"../regexp":46,"../symbol":47,"../timestamp":48,"./utils":45}],44:[function(require,module,exports){ +(function (Buffer){(function (){ +'use strict'; + +var writeIEEE754 = require('../float_parser').writeIEEE754, + Long = require('../long').Long, + Map = require('../map'), + Binary = require('../binary').Binary; + +var normalizedFunctionString = require('./utils').normalizedFunctionString; + +// try { +// var _Buffer = Uint8Array; +// } catch (e) { +// _Buffer = Buffer; +// } + +var regexp = /\x00/; // eslint-disable-line no-control-regex +var ignoreKeys = ['$db', '$ref', '$id', '$clusterTime']; + +// To ensure that 0.4 of node works correctly +var isDate = function isDate(d) { + return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]'; +}; + +var isRegExp = function isRegExp(d) { + return Object.prototype.toString.call(d) === '[object RegExp]'; +}; + +var serializeString = function(buffer, key, value, index, isArray) { + // Encode String type + buffer[index++] = BSON.BSON_DATA_STRING; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write the string + var size = buffer.write(value, index + 4, 'utf8'); + // Write the size of the string to buffer + buffer[index + 3] = ((size + 1) >> 24) & 0xff; + buffer[index + 2] = ((size + 1) >> 16) & 0xff; + buffer[index + 1] = ((size + 1) >> 8) & 0xff; + buffer[index] = (size + 1) & 0xff; + // Update index + index = index + 4 + size; + // Write zero + buffer[index++] = 0; + return index; +}; + +var serializeNumber = function(buffer, key, value, index, isArray) { + // We have an integer value + if (Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { + // If the value fits in 32 bits encode as int, if it fits in a double + // encode it as a double, otherwise long + if (value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { + // Set int type 32 bits or less + buffer[index++] = BSON.BSON_DATA_INT; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the int value + buffer[index++] = value & 0xff; + buffer[index++] = (value >> 8) & 0xff; + buffer[index++] = (value >> 16) & 0xff; + buffer[index++] = (value >> 24) & 0xff; + } else if (value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { + // Encode as double + buffer[index++] = BSON.BSON_DATA_NUMBER; + // Number of written bytes + numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write float + writeIEEE754(buffer, value, index, 'little', 52, 8); + // Ajust index + index = index + 8; + } else { + // Set long type + buffer[index++] = BSON.BSON_DATA_LONG; + // Number of written bytes + numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + var longVal = Long.fromNumber(value); + var lowBits = longVal.getLowBits(); + var highBits = longVal.getHighBits(); + // Encode low bits + buffer[index++] = lowBits & 0xff; + buffer[index++] = (lowBits >> 8) & 0xff; + buffer[index++] = (lowBits >> 16) & 0xff; + buffer[index++] = (lowBits >> 24) & 0xff; + // Encode high bits + buffer[index++] = highBits & 0xff; + buffer[index++] = (highBits >> 8) & 0xff; + buffer[index++] = (highBits >> 16) & 0xff; + buffer[index++] = (highBits >> 24) & 0xff; + } + } else { + // Encode as double + buffer[index++] = BSON.BSON_DATA_NUMBER; + // Number of written bytes + numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write float + writeIEEE754(buffer, value, index, 'little', 52, 8); + // Ajust index + index = index + 8; + } + + return index; +}; + +var serializeNull = function(buffer, key, value, index, isArray) { + // Set long type + buffer[index++] = BSON.BSON_DATA_NULL; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + return index; +}; + +var serializeBoolean = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_BOOLEAN; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Encode the boolean value + buffer[index++] = value ? 1 : 0; + return index; +}; + +var serializeDate = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_DATE; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Write the date + var dateInMilis = Long.fromNumber(value.getTime()); + var lowBits = dateInMilis.getLowBits(); + var highBits = dateInMilis.getHighBits(); + // Encode low bits + buffer[index++] = lowBits & 0xff; + buffer[index++] = (lowBits >> 8) & 0xff; + buffer[index++] = (lowBits >> 16) & 0xff; + buffer[index++] = (lowBits >> 24) & 0xff; + // Encode high bits + buffer[index++] = highBits & 0xff; + buffer[index++] = (highBits >> 8) & 0xff; + buffer[index++] = (highBits >> 16) & 0xff; + buffer[index++] = (highBits >> 24) & 0xff; + return index; +}; + +var serializeRegExp = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_REGEXP; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + if (value.source && value.source.match(regexp) != null) { + throw Error('value ' + value.source + ' must not contain null bytes'); + } + // Adjust the index + index = index + buffer.write(value.source, index, 'utf8'); + // Write zero + buffer[index++] = 0x00; + // Write the parameters + if (value.global) buffer[index++] = 0x73; // s + if (value.ignoreCase) buffer[index++] = 0x69; // i + if (value.multiline) buffer[index++] = 0x6d; // m + // Add ending zero + buffer[index++] = 0x00; + return index; +}; + +var serializeBSONRegExp = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_REGEXP; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Check the pattern for 0 bytes + if (value.pattern.match(regexp) != null) { + // The BSON spec doesn't allow keys with null bytes because keys are + // null-terminated. + throw Error('pattern ' + value.pattern + ' must not contain null bytes'); + } + + // Adjust the index + index = index + buffer.write(value.pattern, index, 'utf8'); + // Write zero + buffer[index++] = 0x00; + // Write the options + index = + index + + buffer.write( + value.options + .split('') + .sort() + .join(''), + index, + 'utf8' + ); + // Add ending zero + buffer[index++] = 0x00; + return index; +}; + +var serializeMinMax = function(buffer, key, value, index, isArray) { + // Write the type of either min or max key + if (value === null) { + buffer[index++] = BSON.BSON_DATA_NULL; + } else if (value._bsontype === 'MinKey') { + buffer[index++] = BSON.BSON_DATA_MIN_KEY; + } else { + buffer[index++] = BSON.BSON_DATA_MAX_KEY; + } + + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + return index; +}; + +var serializeObjectId = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_OID; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Write the objectId into the shared buffer + if (typeof value.id === 'string') { + buffer.write(value.id, index, 'binary'); + } else if (value.id && value.id.copy) { + value.id.copy(buffer, index, 0, 12); + } else { + throw new Error('object [' + JSON.stringify(value) + '] is not a valid ObjectId'); + } + + // Ajust index + return index + 12; +}; + +var serializeBuffer = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_BINARY; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Get size of the buffer (current write point) + var size = value.length; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the default subtype + buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT; + // Copy the content form the binary field to the buffer + value.copy(buffer, index, 0, size); + // Adjust the index + index = index + size; + return index; +}; + +var serializeObject = function( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined, + isArray, + path +) { + for (var i = 0; i < path.length; i++) { + if (path[i] === value) throw new Error('cyclic dependency detected'); + } + + // Push value to stack + path.push(value); + // Write the type + buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + var endIndex = serializeInto( + buffer, + value, + checkKeys, + index, + depth + 1, + serializeFunctions, + ignoreUndefined, + path + ); + // Pop stack + path.pop(); + // Write size + return endIndex; +}; + +var serializeDecimal128 = function(buffer, key, value, index, isArray) { + buffer[index++] = BSON.BSON_DATA_DECIMAL128; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the data from the value + value.bytes.copy(buffer, index, 0, 16); + return index + 16; +}; + +var serializeLong = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = value._bsontype === 'Long' ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the date + var lowBits = value.getLowBits(); + var highBits = value.getHighBits(); + // Encode low bits + buffer[index++] = lowBits & 0xff; + buffer[index++] = (lowBits >> 8) & 0xff; + buffer[index++] = (lowBits >> 16) & 0xff; + buffer[index++] = (lowBits >> 24) & 0xff; + // Encode high bits + buffer[index++] = highBits & 0xff; + buffer[index++] = (highBits >> 8) & 0xff; + buffer[index++] = (highBits >> 16) & 0xff; + buffer[index++] = (highBits >> 24) & 0xff; + return index; +}; + +var serializeInt32 = function(buffer, key, value, index, isArray) { + // Set int type 32 bits or less + buffer[index++] = BSON.BSON_DATA_INT; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the int value + buffer[index++] = value & 0xff; + buffer[index++] = (value >> 8) & 0xff; + buffer[index++] = (value >> 16) & 0xff; + buffer[index++] = (value >> 24) & 0xff; + return index; +}; + +var serializeDouble = function(buffer, key, value, index, isArray) { + // Encode as double + buffer[index++] = BSON.BSON_DATA_NUMBER; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write float + writeIEEE754(buffer, value, index, 'little', 52, 8); + // Ajust index + index = index + 8; + return index; +}; + +var serializeFunction = function(buffer, key, value, index, checkKeys, depth, isArray) { + buffer[index++] = BSON.BSON_DATA_CODE; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Function string + var functionString = normalizedFunctionString(value); + + // Write the string + var size = buffer.write(functionString, index + 4, 'utf8') + 1; + // Write the size of the string to buffer + buffer[index] = size & 0xff; + buffer[index + 1] = (size >> 8) & 0xff; + buffer[index + 2] = (size >> 16) & 0xff; + buffer[index + 3] = (size >> 24) & 0xff; + // Update index + index = index + 4 + size - 1; + // Write zero + buffer[index++] = 0; + return index; +}; + +var serializeCode = function( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined, + isArray +) { + if (value.scope && typeof value.scope === 'object') { + // Write the type + buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Starting index + var startIndex = index; + + // Serialize the function + // Get the function string + var functionString = typeof value.code === 'string' ? value.code : value.code.toString(); + // Index adjustment + index = index + 4; + // Write string into buffer + var codeSize = buffer.write(functionString, index + 4, 'utf8') + 1; + // Write the size of the string to buffer + buffer[index] = codeSize & 0xff; + buffer[index + 1] = (codeSize >> 8) & 0xff; + buffer[index + 2] = (codeSize >> 16) & 0xff; + buffer[index + 3] = (codeSize >> 24) & 0xff; + // Write end 0 + buffer[index + 4 + codeSize - 1] = 0; + // Write the + index = index + codeSize + 4; + + // + // Serialize the scope value + var endIndex = serializeInto( + buffer, + value.scope, + checkKeys, + index, + depth + 1, + serializeFunctions, + ignoreUndefined + ); + index = endIndex - 1; + + // Writ the total + var totalSize = endIndex - startIndex; + + // Write the total size of the object + buffer[startIndex++] = totalSize & 0xff; + buffer[startIndex++] = (totalSize >> 8) & 0xff; + buffer[startIndex++] = (totalSize >> 16) & 0xff; + buffer[startIndex++] = (totalSize >> 24) & 0xff; + // Write trailing zero + buffer[index++] = 0; + } else { + buffer[index++] = BSON.BSON_DATA_CODE; + // Number of written bytes + numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Function string + functionString = value.code.toString(); + // Write the string + var size = buffer.write(functionString, index + 4, 'utf8') + 1; + // Write the size of the string to buffer + buffer[index] = size & 0xff; + buffer[index + 1] = (size >> 8) & 0xff; + buffer[index + 2] = (size >> 16) & 0xff; + buffer[index + 3] = (size >> 24) & 0xff; + // Update index + index = index + 4 + size - 1; + // Write zero + buffer[index++] = 0; + } + + return index; +}; + +var serializeBinary = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_BINARY; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Extract the buffer + var data = value.value(true); + // Calculate size + var size = value.position; + // Add the deprecated 02 type 4 bytes of size to total + if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) size = size + 4; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the subtype to the buffer + buffer[index++] = value.sub_type; + + // If we have binary type 2 the 4 first bytes are the size + if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) { + size = size - 4; + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + } + + // Write the data to the object + data.copy(buffer, index, 0, value.position); + // Adjust the index + index = index + value.position; + return index; +}; + +var serializeSymbol = function(buffer, key, value, index, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_SYMBOL; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the string + var size = buffer.write(value.value, index + 4, 'utf8') + 1; + // Write the size of the string to buffer + buffer[index] = size & 0xff; + buffer[index + 1] = (size >> 8) & 0xff; + buffer[index + 2] = (size >> 16) & 0xff; + buffer[index + 3] = (size >> 24) & 0xff; + // Update index + index = index + 4 + size - 1; + // Write zero + buffer[index++] = 0x00; + return index; +}; + +var serializeDBRef = function(buffer, key, value, index, depth, serializeFunctions, isArray) { + // Write the type + buffer[index++] = BSON.BSON_DATA_OBJECT; + // Number of written bytes + var numberOfWrittenBytes = !isArray + ? buffer.write(key, index, 'utf8') + : buffer.write(key, index, 'ascii'); + + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + var startIndex = index; + var endIndex; + + // Serialize object + if (null != value.db) { + endIndex = serializeInto( + buffer, + { + $ref: value.namespace, + $id: value.oid, + $db: value.db + }, + false, + index, + depth + 1, + serializeFunctions + ); + } else { + endIndex = serializeInto( + buffer, + { + $ref: value.namespace, + $id: value.oid + }, + false, + index, + depth + 1, + serializeFunctions + ); + } + + // Calculate object size + var size = endIndex - startIndex; + // Write the size + buffer[startIndex++] = size & 0xff; + buffer[startIndex++] = (size >> 8) & 0xff; + buffer[startIndex++] = (size >> 16) & 0xff; + buffer[startIndex++] = (size >> 24) & 0xff; + // Set index + return endIndex; +}; + +var serializeInto = function serializeInto( + buffer, + object, + checkKeys, + startingIndex, + depth, + serializeFunctions, + ignoreUndefined, + path +) { + startingIndex = startingIndex || 0; + path = path || []; + + // Push the object to the path + path.push(object); + + // Start place to serialize into + var index = startingIndex + 4; + // var self = this; + + // Special case isArray + if (Array.isArray(object)) { + // Get object keys + for (var i = 0; i < object.length; i++) { + var key = '' + i; + var value = object[i]; + + // Is there an override value + if (value && value.toBSON) { + if (typeof value.toBSON !== 'function') throw new Error('toBSON is not a function'); + value = value.toBSON(); + } + + var type = typeof value; + if (type === 'string') { + index = serializeString(buffer, key, value, index, true); + } else if (type === 'number') { + index = serializeNumber(buffer, key, value, index, true); + } else if(type === 'bigint') { + throw new TypeError('Unsupported type BigInt, please use Decimal128'); + } else if (type === 'boolean') { + index = serializeBoolean(buffer, key, value, index, true); + } else if (value instanceof Date || isDate(value)) { + index = serializeDate(buffer, key, value, index, true); + } else if (value === undefined) { + index = serializeNull(buffer, key, value, index, true); + } else if (value === null) { + index = serializeNull(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') { + index = serializeObjectId(buffer, key, value, index, true); + } else if (Buffer.isBuffer(value)) { + index = serializeBuffer(buffer, key, value, index, true); + } else if (value instanceof RegExp || isRegExp(value)) { + index = serializeRegExp(buffer, key, value, index, true); + } else if (type === 'object' && value['_bsontype'] == null) { + index = serializeObject( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined, + true, + path + ); + } else if (type === 'object' && value['_bsontype'] === 'Decimal128') { + index = serializeDecimal128(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') { + index = serializeLong(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'Double') { + index = serializeDouble(buffer, key, value, index, true); + } else if (typeof value === 'function' && serializeFunctions) { + index = serializeFunction( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + true + ); + } else if (value['_bsontype'] === 'Code') { + index = serializeCode( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined, + true + ); + } else if (value['_bsontype'] === 'Binary') { + index = serializeBinary(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'Symbol') { + index = serializeSymbol(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'DBRef') { + index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true); + } else if (value['_bsontype'] === 'BSONRegExp') { + index = serializeBSONRegExp(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'Int32') { + index = serializeInt32(buffer, key, value, index, true); + } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') { + index = serializeMinMax(buffer, key, value, index, true); + } else if (typeof value['_bsontype'] !== 'undefined') { + throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']); + } + } + } else if (object instanceof Map) { + var iterator = object.entries(); + var done = false; + + while (!done) { + // Unpack the next entry + var entry = iterator.next(); + done = entry.done; + // Are we done, then skip and terminate + if (done) continue; + + // Get the entry values + key = entry.value[0]; + value = entry.value[1]; + + // Check the type of the value + type = typeof value; + + // Check the key and throw error if it's illegal + if (typeof key === 'string' && ignoreKeys.indexOf(key) === -1) { + if (key.match(regexp) != null) { + // The BSON spec doesn't allow keys with null bytes because keys are + // null-terminated. + throw Error('key ' + key + ' must not contain null bytes'); + } + + if (checkKeys) { + if ('$' === key[0]) { + throw Error('key ' + key + " must not start with '$'"); + } else if (~key.indexOf('.')) { + throw Error('key ' + key + " must not contain '.'"); + } + } + } + + if (type === 'string') { + index = serializeString(buffer, key, value, index); + } else if (type === 'number') { + index = serializeNumber(buffer, key, value, index); + } else if(type === 'bigint') { + throw new TypeError('Unsupported type BigInt, please use Decimal128'); + } else if (type === 'boolean') { + index = serializeBoolean(buffer, key, value, index); + } else if (value instanceof Date || isDate(value)) { + index = serializeDate(buffer, key, value, index); + // } else if (value === undefined && ignoreUndefined === true) { + } else if (value === null || (value === undefined && ignoreUndefined === false)) { + index = serializeNull(buffer, key, value, index); + } else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') { + index = serializeObjectId(buffer, key, value, index); + } else if (Buffer.isBuffer(value)) { + index = serializeBuffer(buffer, key, value, index); + } else if (value instanceof RegExp || isRegExp(value)) { + index = serializeRegExp(buffer, key, value, index); + } else if (type === 'object' && value['_bsontype'] == null) { + index = serializeObject( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined, + false, + path + ); + } else if (type === 'object' && value['_bsontype'] === 'Decimal128') { + index = serializeDecimal128(buffer, key, value, index); + } else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') { + index = serializeLong(buffer, key, value, index); + } else if (value['_bsontype'] === 'Double') { + index = serializeDouble(buffer, key, value, index); + } else if (value['_bsontype'] === 'Code') { + index = serializeCode( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined + ); + } else if (typeof value === 'function' && serializeFunctions) { + index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions); + } else if (value['_bsontype'] === 'Binary') { + index = serializeBinary(buffer, key, value, index); + } else if (value['_bsontype'] === 'Symbol') { + index = serializeSymbol(buffer, key, value, index); + } else if (value['_bsontype'] === 'DBRef') { + index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions); + } else if (value['_bsontype'] === 'BSONRegExp') { + index = serializeBSONRegExp(buffer, key, value, index); + } else if (value['_bsontype'] === 'Int32') { + index = serializeInt32(buffer, key, value, index); + } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') { + index = serializeMinMax(buffer, key, value, index); + } else if (typeof value['_bsontype'] !== 'undefined') { + throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']); + } + } + } else { + // Did we provide a custom serialization method + if (object.toBSON) { + if (typeof object.toBSON !== 'function') throw new Error('toBSON is not a function'); + object = object.toBSON(); + if (object != null && typeof object !== 'object') + throw new Error('toBSON function did not return an object'); + } + + // Iterate over all the keys + for (key in object) { + value = object[key]; + // Is there an override value + if (value && value.toBSON) { + if (typeof value.toBSON !== 'function') throw new Error('toBSON is not a function'); + value = value.toBSON(); + } + + // Check the type of the value + type = typeof value; + + // Check the key and throw error if it's illegal + if (typeof key === 'string' && ignoreKeys.indexOf(key) === -1) { + if (key.match(regexp) != null) { + // The BSON spec doesn't allow keys with null bytes because keys are + // null-terminated. + throw Error('key ' + key + ' must not contain null bytes'); + } + + if (checkKeys) { + if ('$' === key[0]) { + throw Error('key ' + key + " must not start with '$'"); + } else if (~key.indexOf('.')) { + throw Error('key ' + key + " must not contain '.'"); + } + } + } + + if (type === 'string') { + index = serializeString(buffer, key, value, index); + } else if (type === 'number') { + index = serializeNumber(buffer, key, value, index); + } else if(type === 'bigint') { + throw new TypeError('Unsupported type BigInt, please use Decimal128'); + } else if (type === 'boolean') { + index = serializeBoolean(buffer, key, value, index); + } else if (value instanceof Date || isDate(value)) { + index = serializeDate(buffer, key, value, index); + } else if (value === undefined) { + if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index); + } else if (value === null) { + index = serializeNull(buffer, key, value, index); + } else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') { + index = serializeObjectId(buffer, key, value, index); + } else if (Buffer.isBuffer(value)) { + index = serializeBuffer(buffer, key, value, index); + } else if (value instanceof RegExp || isRegExp(value)) { + index = serializeRegExp(buffer, key, value, index); + } else if (type === 'object' && value['_bsontype'] == null) { + index = serializeObject( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined, + false, + path + ); + } else if (type === 'object' && value['_bsontype'] === 'Decimal128') { + index = serializeDecimal128(buffer, key, value, index); + } else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') { + index = serializeLong(buffer, key, value, index); + } else if (value['_bsontype'] === 'Double') { + index = serializeDouble(buffer, key, value, index); + } else if (value['_bsontype'] === 'Code') { + index = serializeCode( + buffer, + key, + value, + index, + checkKeys, + depth, + serializeFunctions, + ignoreUndefined + ); + } else if (typeof value === 'function' && serializeFunctions) { + index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions); + } else if (value['_bsontype'] === 'Binary') { + index = serializeBinary(buffer, key, value, index); + } else if (value['_bsontype'] === 'Symbol') { + index = serializeSymbol(buffer, key, value, index); + } else if (value['_bsontype'] === 'DBRef') { + index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions); + } else if (value['_bsontype'] === 'BSONRegExp') { + index = serializeBSONRegExp(buffer, key, value, index); + } else if (value['_bsontype'] === 'Int32') { + index = serializeInt32(buffer, key, value, index); + } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') { + index = serializeMinMax(buffer, key, value, index); + } else if (typeof value['_bsontype'] !== 'undefined') { + throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']); + } + } + } + + // Remove the path + path.pop(); + + // Final padding byte for object + buffer[index++] = 0x00; + + // Final size + var size = index - startingIndex; + // Write the size of the object + buffer[startingIndex++] = size & 0xff; + buffer[startingIndex++] = (size >> 8) & 0xff; + buffer[startingIndex++] = (size >> 16) & 0xff; + buffer[startingIndex++] = (size >> 24) & 0xff; + return index; +}; + +var BSON = {}; + +/** + * Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5 + * + * @ignore + * @api private + */ +// var functionCache = (BSON.functionCache = {}); + +/** + * Number BSON Type + * + * @classconstant BSON_DATA_NUMBER + **/ +BSON.BSON_DATA_NUMBER = 1; +/** + * String BSON Type + * + * @classconstant BSON_DATA_STRING + **/ +BSON.BSON_DATA_STRING = 2; +/** + * Object BSON Type + * + * @classconstant BSON_DATA_OBJECT + **/ +BSON.BSON_DATA_OBJECT = 3; +/** + * Array BSON Type + * + * @classconstant BSON_DATA_ARRAY + **/ +BSON.BSON_DATA_ARRAY = 4; +/** + * Binary BSON Type + * + * @classconstant BSON_DATA_BINARY + **/ +BSON.BSON_DATA_BINARY = 5; +/** + * ObjectID BSON Type, deprecated + * + * @classconstant BSON_DATA_UNDEFINED + **/ +BSON.BSON_DATA_UNDEFINED = 6; +/** + * ObjectID BSON Type + * + * @classconstant BSON_DATA_OID + **/ +BSON.BSON_DATA_OID = 7; +/** + * Boolean BSON Type + * + * @classconstant BSON_DATA_BOOLEAN + **/ +BSON.BSON_DATA_BOOLEAN = 8; +/** + * Date BSON Type + * + * @classconstant BSON_DATA_DATE + **/ +BSON.BSON_DATA_DATE = 9; +/** + * null BSON Type + * + * @classconstant BSON_DATA_NULL + **/ +BSON.BSON_DATA_NULL = 10; +/** + * RegExp BSON Type + * + * @classconstant BSON_DATA_REGEXP + **/ +BSON.BSON_DATA_REGEXP = 11; +/** + * Code BSON Type + * + * @classconstant BSON_DATA_CODE + **/ +BSON.BSON_DATA_CODE = 13; +/** + * Symbol BSON Type + * + * @classconstant BSON_DATA_SYMBOL + **/ +BSON.BSON_DATA_SYMBOL = 14; +/** + * Code with Scope BSON Type + * + * @classconstant BSON_DATA_CODE_W_SCOPE + **/ +BSON.BSON_DATA_CODE_W_SCOPE = 15; +/** + * 32 bit Integer BSON Type + * + * @classconstant BSON_DATA_INT + **/ +BSON.BSON_DATA_INT = 16; +/** + * Timestamp BSON Type + * + * @classconstant BSON_DATA_TIMESTAMP + **/ +BSON.BSON_DATA_TIMESTAMP = 17; +/** + * Long BSON Type + * + * @classconstant BSON_DATA_LONG + **/ +BSON.BSON_DATA_LONG = 18; +/** + * Long BSON Type + * + * @classconstant BSON_DATA_DECIMAL128 + **/ +BSON.BSON_DATA_DECIMAL128 = 19; +/** + * MinKey BSON Type + * + * @classconstant BSON_DATA_MIN_KEY + **/ +BSON.BSON_DATA_MIN_KEY = 0xff; +/** + * MaxKey BSON Type + * + * @classconstant BSON_DATA_MAX_KEY + **/ +BSON.BSON_DATA_MAX_KEY = 0x7f; +/** + * Binary Default Type + * + * @classconstant BSON_BINARY_SUBTYPE_DEFAULT + **/ +BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; +/** + * Binary Function Type + * + * @classconstant BSON_BINARY_SUBTYPE_FUNCTION + **/ +BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1; +/** + * Binary Byte Array Type + * + * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY + **/ +BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +/** + * Binary UUID Type + * + * @classconstant BSON_BINARY_SUBTYPE_UUID + **/ +BSON.BSON_BINARY_SUBTYPE_UUID = 3; +/** + * Binary MD5 Type + * + * @classconstant BSON_BINARY_SUBTYPE_MD5 + **/ +BSON.BSON_BINARY_SUBTYPE_MD5 = 4; +/** + * Binary User Defined Type + * + * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED + **/ +BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +// BSON MAX VALUES +BSON.BSON_INT32_MAX = 0x7fffffff; +BSON.BSON_INT32_MIN = -0x80000000; + +BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1; +BSON.BSON_INT64_MIN = -Math.pow(2, 63); + +// JS MAX PRECISE VALUES +BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double. +BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double. + +// Internal long versions +// var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double. +// var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double. + +module.exports = serializeInto; + +}).call(this)}).call(this,{"isBuffer":require("../../../../is-buffer/index.js")}) +},{"../../../../is-buffer/index.js":209,"../binary":29,"../float_parser":35,"../long":37,"../map":38,"./utils":45}],45:[function(require,module,exports){ +(function (Buffer){(function (){ +'use strict'; + +/** + * Normalizes our expected stringified form of a function across versions of node + * @param {Function} fn The function to stringify + */ +function normalizedFunctionString(fn) { + return fn.toString().replace(/function *\(/, 'function ('); +} + +function newBuffer(item, encoding) { + return new Buffer(item, encoding); +} + +function allocBuffer() { + return Buffer.alloc.apply(Buffer, arguments); +} + +function toBuffer() { + return Buffer.from.apply(Buffer, arguments); +} + +module.exports = { + normalizedFunctionString: normalizedFunctionString, + allocBuffer: typeof Buffer.alloc === 'function' ? allocBuffer : newBuffer, + toBuffer: typeof Buffer.from === 'function' ? toBuffer : newBuffer +}; + + +}).call(this)}).call(this,require("buffer").Buffer) +},{"buffer":49}],46:[function(require,module,exports){ +/** + * A class representation of the BSON RegExp type. + * + * @class + * @return {BSONRegExp} A MinKey instance + */ +function BSONRegExp(pattern, options) { + if (!(this instanceof BSONRegExp)) return new BSONRegExp(); + + // Execute + this._bsontype = 'BSONRegExp'; + this.pattern = pattern || ''; + this.options = options || ''; + + // Validate options + for (var i = 0; i < this.options.length; i++) { + if ( + !( + this.options[i] === 'i' || + this.options[i] === 'm' || + this.options[i] === 'x' || + this.options[i] === 'l' || + this.options[i] === 's' || + this.options[i] === 'u' + ) + ) { + throw new Error('the regular expression options [' + this.options[i] + '] is not supported'); + } + } +} + +module.exports = BSONRegExp; +module.exports.BSONRegExp = BSONRegExp; + +},{}],47:[function(require,module,exports){ +(function (Buffer){(function (){ +// Custom inspect property name / symbol. +var inspect = Buffer ? require('util').inspect.custom || 'inspect' : 'inspect'; + +/** + * A class representation of the BSON Symbol type. + * + * @class + * @deprecated + * @param {string} value the string representing the symbol. + * @return {Symbol} + */ +function Symbol(value) { + if (!(this instanceof Symbol)) return new Symbol(value); + this._bsontype = 'Symbol'; + this.value = value; +} + +/** + * Access the wrapped string value. + * + * @method + * @return {String} returns the wrapped string. + */ +Symbol.prototype.valueOf = function() { + return this.value; +}; + +/** + * @ignore + */ +Symbol.prototype.toString = function() { + return this.value; +}; + +/** + * @ignore + */ +Symbol.prototype[inspect] = function() { + return this.value; +}; + +/** + * @ignore + */ +Symbol.prototype.toJSON = function() { + return this.value; +}; + +module.exports = Symbol; +module.exports.Symbol = Symbol; + +}).call(this)}).call(this,require("buffer").Buffer) +},{"buffer":49,"util":370}],48:[function(require,module,exports){ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright 2009 Google Inc. All Rights Reserved + +/** + * This type is for INTERNAL use in MongoDB only and should not be used in applications. + * The appropriate corresponding type is the JavaScript Date type. + * + * Defines a Timestamp class for representing a 64-bit two's-complement + * integer value, which faithfully simulates the behavior of a Java "Timestamp". This + * implementation is derived from TimestampLib in GWT. + * + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Timestamps. + * + * The internal representation of a Timestamp is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @class + * @param {number} low the low (signed) 32 bits of the Timestamp. + * @param {number} high the high (signed) 32 bits of the Timestamp. + */ +function Timestamp(low, high) { + if (!(this instanceof Timestamp)) return new Timestamp(low, high); + this._bsontype = 'Timestamp'; + /** + * @type {number} + * @ignore + */ + this.low_ = low | 0; // force into 32 signed bits. + + /** + * @type {number} + * @ignore + */ + this.high_ = high | 0; // force into 32 signed bits. +} + +/** + * Return the int value. + * + * @return {number} the value, assuming it is a 32-bit integer. + */ +Timestamp.prototype.toInt = function() { + return this.low_; +}; + +/** + * Return the Number value. + * + * @method + * @return {number} the closest floating-point representation to this value. + */ +Timestamp.prototype.toNumber = function() { + return this.high_ * Timestamp.TWO_PWR_32_DBL_ + this.getLowBitsUnsigned(); +}; + +/** + * Return the JSON value. + * + * @method + * @return {string} the JSON representation. + */ +Timestamp.prototype.toJSON = function() { + return this.toString(); +}; + +/** + * Return the String value. + * + * @method + * @param {number} [opt_radix] the radix in which the text should be written. + * @return {string} the textual representation of this value. + */ +Timestamp.prototype.toString = function(opt_radix) { + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (this.isZero()) { + return '0'; + } + + if (this.isNegative()) { + if (this.equals(Timestamp.MIN_VALUE)) { + // We need to change the Timestamp value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixTimestamp = Timestamp.fromNumber(radix); + var div = this.div(radixTimestamp); + var rem = div.multiply(radixTimestamp).subtract(this); + return div.toString(radix) + rem.toInt().toString(radix); + } else { + return '-' + this.negate().toString(radix); + } + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Timestamp.fromNumber(Math.pow(radix, 6)); + + rem = this; + var result = ''; + + while (!rem.isZero()) { + var remDiv = rem.div(radixToPower); + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt(); + var digits = intval.toString(radix); + + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } +}; + +/** + * Return the high 32-bits value. + * + * @method + * @return {number} the high 32-bits as a signed value. + */ +Timestamp.prototype.getHighBits = function() { + return this.high_; +}; + +/** + * Return the low 32-bits value. + * + * @method + * @return {number} the low 32-bits as a signed value. + */ +Timestamp.prototype.getLowBits = function() { + return this.low_; +}; + +/** + * Return the low unsigned 32-bits value. + * + * @method + * @return {number} the low 32-bits as an unsigned value. + */ +Timestamp.prototype.getLowBitsUnsigned = function() { + return this.low_ >= 0 ? this.low_ : Timestamp.TWO_PWR_32_DBL_ + this.low_; +}; + +/** + * Returns the number of bits needed to represent the absolute value of this Timestamp. + * + * @method + * @return {number} Returns the number of bits needed to represent the absolute value of this Timestamp. + */ +Timestamp.prototype.getNumBitsAbs = function() { + if (this.isNegative()) { + if (this.equals(Timestamp.MIN_VALUE)) { + return 64; + } else { + return this.negate().getNumBitsAbs(); + } + } else { + var val = this.high_ !== 0 ? this.high_ : this.low_; + for (var bit = 31; bit > 0; bit--) { + if ((val & (1 << bit)) !== 0) { + break; + } + } + return this.high_ !== 0 ? bit + 33 : bit + 1; + } +}; + +/** + * Return whether this value is zero. + * + * @method + * @return {boolean} whether this value is zero. + */ +Timestamp.prototype.isZero = function() { + return this.high_ === 0 && this.low_ === 0; +}; + +/** + * Return whether this value is negative. + * + * @method + * @return {boolean} whether this value is negative. + */ +Timestamp.prototype.isNegative = function() { + return this.high_ < 0; +}; + +/** + * Return whether this value is odd. + * + * @method + * @return {boolean} whether this value is odd. + */ +Timestamp.prototype.isOdd = function() { + return (this.low_ & 1) === 1; +}; + +/** + * Return whether this Timestamp equals the other + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} whether this Timestamp equals the other + */ +Timestamp.prototype.equals = function(other) { + return this.high_ === other.high_ && this.low_ === other.low_; +}; + +/** + * Return whether this Timestamp does not equal the other. + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} whether this Timestamp does not equal the other. + */ +Timestamp.prototype.notEquals = function(other) { + return this.high_ !== other.high_ || this.low_ !== other.low_; +}; + +/** + * Return whether this Timestamp is less than the other. + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} whether this Timestamp is less than the other. + */ +Timestamp.prototype.lessThan = function(other) { + return this.compare(other) < 0; +}; + +/** + * Return whether this Timestamp is less than or equal to the other. + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} whether this Timestamp is less than or equal to the other. + */ +Timestamp.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; +}; + +/** + * Return whether this Timestamp is greater than the other. + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} whether this Timestamp is greater than the other. + */ +Timestamp.prototype.greaterThan = function(other) { + return this.compare(other) > 0; +}; + +/** + * Return whether this Timestamp is greater than or equal to the other. + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} whether this Timestamp is greater than or equal to the other. + */ +Timestamp.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; +}; + +/** + * Compares this Timestamp with the given one. + * + * @method + * @param {Timestamp} other Timestamp to compare against. + * @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater. + */ +Timestamp.prototype.compare = function(other) { + if (this.equals(other)) { + return 0; + } + + var thisNeg = this.isNegative(); + var otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + + // at this point, the signs are the same, so subtraction will not overflow + if (this.subtract(other).isNegative()) { + return -1; + } else { + return 1; + } +}; + +/** + * The negation of this value. + * + * @method + * @return {Timestamp} the negation of this value. + */ +Timestamp.prototype.negate = function() { + if (this.equals(Timestamp.MIN_VALUE)) { + return Timestamp.MIN_VALUE; + } else { + return this.not().add(Timestamp.ONE); + } +}; + +/** + * Returns the sum of this and the given Timestamp. + * + * @method + * @param {Timestamp} other Timestamp to add to this one. + * @return {Timestamp} the sum of this and the given Timestamp. + */ +Timestamp.prototype.add = function(other) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xffff; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xffff; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xffff; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xffff; + + var c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xffff; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xffff; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xffff; + c48 += a48 + b48; + c48 &= 0xffff; + return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns the difference of this and the given Timestamp. + * + * @method + * @param {Timestamp} other Timestamp to subtract from this. + * @return {Timestamp} the difference of this and the given Timestamp. + */ +Timestamp.prototype.subtract = function(other) { + return this.add(other.negate()); +}; + +/** + * Returns the product of this and the given Timestamp. + * + * @method + * @param {Timestamp} other Timestamp to multiply with this. + * @return {Timestamp} the product of this and the other. + */ +Timestamp.prototype.multiply = function(other) { + if (this.isZero()) { + return Timestamp.ZERO; + } else if (other.isZero()) { + return Timestamp.ZERO; + } + + if (this.equals(Timestamp.MIN_VALUE)) { + return other.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO; + } else if (other.equals(Timestamp.MIN_VALUE)) { + return this.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate() + .multiply(other) + .negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + + // If both Timestamps are small, use float multiplication + if (this.lessThan(Timestamp.TWO_PWR_24_) && other.lessThan(Timestamp.TWO_PWR_24_)) { + return Timestamp.fromNumber(this.toNumber() * other.toNumber()); + } + + // Divide each Timestamp into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xffff; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xffff; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xffff; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xffff; + + var c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xffff; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xffff; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xffff; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xffff; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xffff; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xffff; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xffff; + return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns this Timestamp divided by the given one. + * + * @method + * @param {Timestamp} other Timestamp by which to divide. + * @return {Timestamp} this Timestamp divided by the given one. + */ +Timestamp.prototype.div = function(other) { + if (other.isZero()) { + throw Error('division by zero'); + } else if (this.isZero()) { + return Timestamp.ZERO; + } + + if (this.equals(Timestamp.MIN_VALUE)) { + if (other.equals(Timestamp.ONE) || other.equals(Timestamp.NEG_ONE)) { + return Timestamp.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + } else if (other.equals(Timestamp.MIN_VALUE)) { + return Timestamp.ONE; + } else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shiftRight(1); + var approx = halfThis.div(other).shiftLeft(1); + if (approx.equals(Timestamp.ZERO)) { + return other.isNegative() ? Timestamp.ONE : Timestamp.NEG_ONE; + } else { + var rem = this.subtract(other.multiply(approx)); + var result = approx.add(rem.div(other)); + return result; + } + } + } else if (other.equals(Timestamp.MIN_VALUE)) { + return Timestamp.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().div(other.negate()); + } else { + return this.negate() + .div(other) + .negate(); + } + } else if (other.isNegative()) { + return this.div(other.negate()).negate(); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + var res = Timestamp.ZERO; + rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48); + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + var approxRes = Timestamp.fromNumber(approx); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = Timestamp.fromNumber(approx); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = Timestamp.ONE; + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; +}; + +/** + * Returns this Timestamp modulo the given one. + * + * @method + * @param {Timestamp} other Timestamp by which to mod. + * @return {Timestamp} this Timestamp modulo the given one. + */ +Timestamp.prototype.modulo = function(other) { + return this.subtract(this.div(other).multiply(other)); +}; + +/** + * The bitwise-NOT of this value. + * + * @method + * @return {Timestamp} the bitwise-NOT of this value. + */ +Timestamp.prototype.not = function() { + return Timestamp.fromBits(~this.low_, ~this.high_); +}; + +/** + * Returns the bitwise-AND of this Timestamp and the given one. + * + * @method + * @param {Timestamp} other the Timestamp with which to AND. + * @return {Timestamp} the bitwise-AND of this and the other. + */ +Timestamp.prototype.and = function(other) { + return Timestamp.fromBits(this.low_ & other.low_, this.high_ & other.high_); +}; + +/** + * Returns the bitwise-OR of this Timestamp and the given one. + * + * @method + * @param {Timestamp} other the Timestamp with which to OR. + * @return {Timestamp} the bitwise-OR of this and the other. + */ +Timestamp.prototype.or = function(other) { + return Timestamp.fromBits(this.low_ | other.low_, this.high_ | other.high_); +}; + +/** + * Returns the bitwise-XOR of this Timestamp and the given one. + * + * @method + * @param {Timestamp} other the Timestamp with which to XOR. + * @return {Timestamp} the bitwise-XOR of this and the other. + */ +Timestamp.prototype.xor = function(other) { + return Timestamp.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_); +}; + +/** + * Returns this Timestamp with bits shifted to the left by the given amount. + * + * @method + * @param {number} numBits the number of bits by which to shift. + * @return {Timestamp} this shifted to the left by the given amount. + */ +Timestamp.prototype.shiftLeft = function(numBits) { + numBits &= 63; + if (numBits === 0) { + return this; + } else { + var low = this.low_; + if (numBits < 32) { + var high = this.high_; + return Timestamp.fromBits(low << numBits, (high << numBits) | (low >>> (32 - numBits))); + } else { + return Timestamp.fromBits(0, low << (numBits - 32)); + } + } +}; + +/** + * Returns this Timestamp with bits shifted to the right by the given amount. + * + * @method + * @param {number} numBits the number of bits by which to shift. + * @return {Timestamp} this shifted to the right by the given amount. + */ +Timestamp.prototype.shiftRight = function(numBits) { + numBits &= 63; + if (numBits === 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Timestamp.fromBits((low >>> numBits) | (high << (32 - numBits)), high >> numBits); + } else { + return Timestamp.fromBits(high >> (numBits - 32), high >= 0 ? 0 : -1); + } + } +}; + +/** + * Returns this Timestamp with bits shifted to the right by the given amount, with the new top bits matching the current sign bit. + * + * @method + * @param {number} numBits the number of bits by which to shift. + * @return {Timestamp} this shifted to the right by the given amount, with zeros placed into the new leading bits. + */ +Timestamp.prototype.shiftRightUnsigned = function(numBits) { + numBits &= 63; + if (numBits === 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Timestamp.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits); + } else if (numBits === 32) { + return Timestamp.fromBits(high, 0); + } else { + return Timestamp.fromBits(high >>> (numBits - 32), 0); + } + } +}; + +/** + * Returns a Timestamp representing the given (32-bit) integer value. + * + * @method + * @param {number} value the 32-bit integer in question. + * @return {Timestamp} the corresponding Timestamp value. + */ +Timestamp.fromInt = function(value) { + if (-128 <= value && value < 128) { + var cachedObj = Timestamp.INT_CACHE_[value]; + if (cachedObj) { + return cachedObj; + } + } + + var obj = new Timestamp(value | 0, value < 0 ? -1 : 0); + if (-128 <= value && value < 128) { + Timestamp.INT_CACHE_[value] = obj; + } + return obj; +}; + +/** + * Returns a Timestamp representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * + * @method + * @param {number} value the number in question. + * @return {Timestamp} the corresponding Timestamp value. + */ +Timestamp.fromNumber = function(value) { + if (isNaN(value) || !isFinite(value)) { + return Timestamp.ZERO; + } else if (value <= -Timestamp.TWO_PWR_63_DBL_) { + return Timestamp.MIN_VALUE; + } else if (value + 1 >= Timestamp.TWO_PWR_63_DBL_) { + return Timestamp.MAX_VALUE; + } else if (value < 0) { + return Timestamp.fromNumber(-value).negate(); + } else { + return new Timestamp( + (value % Timestamp.TWO_PWR_32_DBL_) | 0, + (value / Timestamp.TWO_PWR_32_DBL_) | 0 + ); + } +}; + +/** + * Returns a Timestamp representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits. + * + * @method + * @param {number} lowBits the low 32-bits. + * @param {number} highBits the high 32-bits. + * @return {Timestamp} the corresponding Timestamp value. + */ +Timestamp.fromBits = function(lowBits, highBits) { + return new Timestamp(lowBits, highBits); +}; + +/** + * Returns a Timestamp representation of the given string, written using the given radix. + * + * @method + * @param {string} str the textual representation of the Timestamp. + * @param {number} opt_radix the radix in which the text is written. + * @return {Timestamp} the corresponding Timestamp value. + */ +Timestamp.fromString = function(str, opt_radix) { + if (str.length === 0) { + throw Error('number format error: empty string'); + } + + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (str.charAt(0) === '-') { + return Timestamp.fromString(str.substring(1), radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw Error('number format error: interior "-" character: ' + str); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Timestamp.fromNumber(Math.pow(radix, 8)); + + var result = Timestamp.ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i); + var value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = Timestamp.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(Timestamp.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(Timestamp.fromNumber(value)); + } + } + return result; +}; + +// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the +// from* methods on which they depend. + +/** + * A cache of the Timestamp representations of small integer values. + * @type {Object} + * @ignore + */ +Timestamp.INT_CACHE_ = {}; + +// NOTE: the compiler should inline these constant values below and then remove +// these variables, so there should be no runtime penalty for these. + +/** + * Number used repeated below in calculations. This must appear before the + * first call to any from* function below. + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_16_DBL_ = 1 << 16; + +/** + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_24_DBL_ = 1 << 24; + +/** + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_32_DBL_ = Timestamp.TWO_PWR_16_DBL_ * Timestamp.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_31_DBL_ = Timestamp.TWO_PWR_32_DBL_ / 2; + +/** + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_48_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_64_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_32_DBL_; + +/** + * @type {number} + * @ignore + */ +Timestamp.TWO_PWR_63_DBL_ = Timestamp.TWO_PWR_64_DBL_ / 2; + +/** @type {Timestamp} */ +Timestamp.ZERO = Timestamp.fromInt(0); + +/** @type {Timestamp} */ +Timestamp.ONE = Timestamp.fromInt(1); + +/** @type {Timestamp} */ +Timestamp.NEG_ONE = Timestamp.fromInt(-1); + +/** @type {Timestamp} */ +Timestamp.MAX_VALUE = Timestamp.fromBits(0xffffffff | 0, 0x7fffffff | 0); + +/** @type {Timestamp} */ +Timestamp.MIN_VALUE = Timestamp.fromBits(0, 0x80000000 | 0); + +/** + * @type {Timestamp} + * @ignore + */ +Timestamp.TWO_PWR_24_ = Timestamp.fromInt(1 << 24); + +/** + * Expose. + */ +module.exports = Timestamp; +module.exports.Timestamp = Timestamp; + +},{}],49:[function(require,module,exports){ +(function (Buffer){(function (){ +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +/* eslint-disable no-proto */ + +'use strict' + +var base64 = require('base64-js') +var ieee754 = require('ieee754') + +exports.Buffer = Buffer +exports.SlowBuffer = SlowBuffer +exports.INSPECT_MAX_BYTES = 50 + +var K_MAX_LENGTH = 0x7fffffff +exports.kMaxLength = K_MAX_LENGTH + +/** + * If `Buffer.TYPED_ARRAY_SUPPORT`: + * === true Use Uint8Array implementation (fastest) + * === false Print warning and recommend using `buffer` v4.x which has an Object + * implementation (most compatible, even IE6) + * + * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, + * Opera 11.6+, iOS 4.2+. + * + * We report that the browser does not support typed arrays if the are not subclassable + * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` + * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support + * for __proto__ and has a buggy typed array implementation. + */ +Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() + +if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && + typeof console.error === 'function') { + console.error( + 'This browser lacks typed array (Uint8Array) support which is required by ' + + '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' + ) +} + +function typedArraySupport () { + // Can typed array instances can be augmented? + try { + var arr = new Uint8Array(1) + arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } } + return arr.foo() === 42 + } catch (e) { + return false + } +} + +Object.defineProperty(Buffer.prototype, 'parent', { + enumerable: true, + get: function () { + if (!Buffer.isBuffer(this)) return undefined + return this.buffer + } +}) + +Object.defineProperty(Buffer.prototype, 'offset', { + enumerable: true, + get: function () { + if (!Buffer.isBuffer(this)) return undefined + return this.byteOffset + } +}) + +function createBuffer (length) { + if (length > K_MAX_LENGTH) { + throw new RangeError('The value "' + length + '" is invalid for option "size"') + } + // Return an augmented `Uint8Array` instance + var buf = new Uint8Array(length) + buf.__proto__ = Buffer.prototype + return buf +} + +/** + * The Buffer constructor returns instances of `Uint8Array` that have their + * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of + * `Uint8Array`, so the returned instances will have all the node `Buffer` methods + * and the `Uint8Array` methods. Square bracket notation works as expected -- it + * returns a single octet. + * + * The `Uint8Array` prototype remains unmodified. + */ + +function Buffer (arg, encodingOrOffset, length) { + // Common case. + if (typeof arg === 'number') { + if (typeof encodingOrOffset === 'string') { + throw new TypeError( + 'The "string" argument must be of type string. Received type number' + ) + } + return allocUnsafe(arg) + } + return from(arg, encodingOrOffset, length) +} + +// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 +if (typeof Symbol !== 'undefined' && Symbol.species != null && + Buffer[Symbol.species] === Buffer) { + Object.defineProperty(Buffer, Symbol.species, { + value: null, + configurable: true, + enumerable: false, + writable: false + }) +} + +Buffer.poolSize = 8192 // not used by this implementation + +function from (value, encodingOrOffset, length) { + if (typeof value === 'string') { + return fromString(value, encodingOrOffset) + } + + if (ArrayBuffer.isView(value)) { + return fromArrayLike(value) + } + + if (value == null) { + throw TypeError( + 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + + 'or Array-like Object. Received type ' + (typeof value) + ) + } + + if (isInstance(value, ArrayBuffer) || + (value && isInstance(value.buffer, ArrayBuffer))) { + return fromArrayBuffer(value, encodingOrOffset, length) + } + + if (typeof value === 'number') { + throw new TypeError( + 'The "value" argument must not be of type number. Received type number' + ) + } + + var valueOf = value.valueOf && value.valueOf() + if (valueOf != null && valueOf !== value) { + return Buffer.from(valueOf, encodingOrOffset, length) + } + + var b = fromObject(value) + if (b) return b + + if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null && + typeof value[Symbol.toPrimitive] === 'function') { + return Buffer.from( + value[Symbol.toPrimitive]('string'), encodingOrOffset, length + ) + } + + throw new TypeError( + 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + + 'or Array-like Object. Received type ' + (typeof value) + ) +} + +/** + * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError + * if value is a number. + * Buffer.from(str[, encoding]) + * Buffer.from(array) + * Buffer.from(buffer) + * Buffer.from(arrayBuffer[, byteOffset[, length]]) + **/ +Buffer.from = function (value, encodingOrOffset, length) { + return from(value, encodingOrOffset, length) +} + +// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: +// https://github.com/feross/buffer/pull/148 +Buffer.prototype.__proto__ = Uint8Array.prototype +Buffer.__proto__ = Uint8Array + +function assertSize (size) { + if (typeof size !== 'number') { + throw new TypeError('"size" argument must be of type number') + } else if (size < 0) { + throw new RangeError('The value "' + size + '" is invalid for option "size"') + } +} + +function alloc (size, fill, encoding) { + assertSize(size) + if (size <= 0) { + return createBuffer(size) + } + if (fill !== undefined) { + // Only pay attention to encoding if it's a string. This + // prevents accidentally sending in a number that would + // be interpretted as a start offset. + return typeof encoding === 'string' + ? createBuffer(size).fill(fill, encoding) + : createBuffer(size).fill(fill) + } + return createBuffer(size) +} + +/** + * Creates a new filled Buffer instance. + * alloc(size[, fill[, encoding]]) + **/ +Buffer.alloc = function (size, fill, encoding) { + return alloc(size, fill, encoding) +} + +function allocUnsafe (size) { + assertSize(size) + return createBuffer(size < 0 ? 0 : checked(size) | 0) +} + +/** + * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. + * */ +Buffer.allocUnsafe = function (size) { + return allocUnsafe(size) +} +/** + * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. + */ +Buffer.allocUnsafeSlow = function (size) { + return allocUnsafe(size) +} + +function fromString (string, encoding) { + if (typeof encoding !== 'string' || encoding === '') { + encoding = 'utf8' + } + + if (!Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding) + } + + var length = byteLength(string, encoding) | 0 + var buf = createBuffer(length) + + var actual = buf.write(string, encoding) + + if (actual !== length) { + // Writing a hex string, for example, that contains invalid characters will + // cause everything after the first invalid character to be ignored. (e.g. + // 'abxxcd' will be treated as 'ab') + buf = buf.slice(0, actual) + } + + return buf +} + +function fromArrayLike (array) { + var length = array.length < 0 ? 0 : checked(array.length) | 0 + var buf = createBuffer(length) + for (var i = 0; i < length; i += 1) { + buf[i] = array[i] & 255 + } + return buf +} + +function fromArrayBuffer (array, byteOffset, length) { + if (byteOffset < 0 || array.byteLength < byteOffset) { + throw new RangeError('"offset" is outside of buffer bounds') + } + + if (array.byteLength < byteOffset + (length || 0)) { + throw new RangeError('"length" is outside of buffer bounds') + } + + var buf + if (byteOffset === undefined && length === undefined) { + buf = new Uint8Array(array) + } else if (length === undefined) { + buf = new Uint8Array(array, byteOffset) + } else { + buf = new Uint8Array(array, byteOffset, length) + } + + // Return an augmented `Uint8Array` instance + buf.__proto__ = Buffer.prototype + return buf +} + +function fromObject (obj) { + if (Buffer.isBuffer(obj)) { + var len = checked(obj.length) | 0 + var buf = createBuffer(len) + + if (buf.length === 0) { + return buf + } + + obj.copy(buf, 0, 0, len) + return buf + } + + if (obj.length !== undefined) { + if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { + return createBuffer(0) + } + return fromArrayLike(obj) + } + + if (obj.type === 'Buffer' && Array.isArray(obj.data)) { + return fromArrayLike(obj.data) + } +} + +function checked (length) { + // Note: cannot use `length < K_MAX_LENGTH` here because that fails when + // length is NaN (which is otherwise coerced to zero.) + if (length >= K_MAX_LENGTH) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') + } + return length | 0 +} + +function SlowBuffer (length) { + if (+length != length) { // eslint-disable-line eqeqeq + length = 0 + } + return Buffer.alloc(+length) +} + +Buffer.isBuffer = function isBuffer (b) { + return b != null && b._isBuffer === true && + b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false +} + +Buffer.compare = function compare (a, b) { + if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength) + if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength) + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + throw new TypeError( + 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' + ) + } + + if (a === b) return 0 + + var x = a.length + var y = b.length + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i] + y = b[i] + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 +} + +Buffer.isEncoding = function isEncoding (encoding) { + switch (String(encoding).toLowerCase()) { + case 'hex': + case 'utf8': + case 'utf-8': + case 'ascii': + case 'latin1': + case 'binary': + case 'base64': + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return true + default: + return false + } +} + +Buffer.concat = function concat (list, length) { + if (!Array.isArray(list)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + + if (list.length === 0) { + return Buffer.alloc(0) + } + + var i + if (length === undefined) { + length = 0 + for (i = 0; i < list.length; ++i) { + length += list[i].length + } + } + + var buffer = Buffer.allocUnsafe(length) + var pos = 0 + for (i = 0; i < list.length; ++i) { + var buf = list[i] + if (isInstance(buf, Uint8Array)) { + buf = Buffer.from(buf) + } + if (!Buffer.isBuffer(buf)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + buf.copy(buffer, pos) + pos += buf.length + } + return buffer +} + +function byteLength (string, encoding) { + if (Buffer.isBuffer(string)) { + return string.length + } + if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) { + return string.byteLength + } + if (typeof string !== 'string') { + throw new TypeError( + 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' + + 'Received type ' + typeof string + ) + } + + var len = string.length + var mustMatch = (arguments.length > 2 && arguments[2] === true) + if (!mustMatch && len === 0) return 0 + + // Use a for loop to avoid recursion + var loweredCase = false + for (;;) { + switch (encoding) { + case 'ascii': + case 'latin1': + case 'binary': + return len + case 'utf8': + case 'utf-8': + return utf8ToBytes(string).length + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return len * 2 + case 'hex': + return len >>> 1 + case 'base64': + return base64ToBytes(string).length + default: + if (loweredCase) { + return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8 + } + encoding = ('' + encoding).toLowerCase() + loweredCase = true + } + } +} +Buffer.byteLength = byteLength + +function slowToString (encoding, start, end) { + var loweredCase = false + + // No need to verify that "this.length <= MAX_UINT32" since it's a read-only + // property of a typed array. + + // This behaves neither like String nor Uint8Array in that we set start/end + // to their upper/lower bounds if the value passed is out of range. + // undefined is handled specially as per ECMA-262 6th Edition, + // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. + if (start === undefined || start < 0) { + start = 0 + } + // Return early if start > this.length. Done here to prevent potential uint32 + // coercion fail below. + if (start > this.length) { + return '' + } + + if (end === undefined || end > this.length) { + end = this.length + } + + if (end <= 0) { + return '' + } + + // Force coersion to uint32. This will also coerce falsey/NaN values to 0. + end >>>= 0 + start >>>= 0 + + if (end <= start) { + return '' + } + + if (!encoding) encoding = 'utf8' + + while (true) { + switch (encoding) { + case 'hex': + return hexSlice(this, start, end) + + case 'utf8': + case 'utf-8': + return utf8Slice(this, start, end) + + case 'ascii': + return asciiSlice(this, start, end) + + case 'latin1': + case 'binary': + return latin1Slice(this, start, end) + + case 'base64': + return base64Slice(this, start, end) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return utf16leSlice(this, start, end) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = (encoding + '').toLowerCase() + loweredCase = true + } + } +} + +// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) +// to detect a Buffer instance. It's not possible to use `instanceof Buffer` +// reliably in a browserify context because there could be multiple different +// copies of the 'buffer' package in use. This method works even for Buffer +// instances that were created from another copy of the `buffer` package. +// See: https://github.com/feross/buffer/issues/154 +Buffer.prototype._isBuffer = true + +function swap (b, n, m) { + var i = b[n] + b[n] = b[m] + b[m] = i +} + +Buffer.prototype.swap16 = function swap16 () { + var len = this.length + if (len % 2 !== 0) { + throw new RangeError('Buffer size must be a multiple of 16-bits') + } + for (var i = 0; i < len; i += 2) { + swap(this, i, i + 1) + } + return this +} + +Buffer.prototype.swap32 = function swap32 () { + var len = this.length + if (len % 4 !== 0) { + throw new RangeError('Buffer size must be a multiple of 32-bits') + } + for (var i = 0; i < len; i += 4) { + swap(this, i, i + 3) + swap(this, i + 1, i + 2) + } + return this +} + +Buffer.prototype.swap64 = function swap64 () { + var len = this.length + if (len % 8 !== 0) { + throw new RangeError('Buffer size must be a multiple of 64-bits') + } + for (var i = 0; i < len; i += 8) { + swap(this, i, i + 7) + swap(this, i + 1, i + 6) + swap(this, i + 2, i + 5) + swap(this, i + 3, i + 4) + } + return this +} + +Buffer.prototype.toString = function toString () { + var length = this.length + if (length === 0) return '' + if (arguments.length === 0) return utf8Slice(this, 0, length) + return slowToString.apply(this, arguments) +} + +Buffer.prototype.toLocaleString = Buffer.prototype.toString + +Buffer.prototype.equals = function equals (b) { + if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return true + return Buffer.compare(this, b) === 0 +} + +Buffer.prototype.inspect = function inspect () { + var str = '' + var max = exports.INSPECT_MAX_BYTES + str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim() + if (this.length > max) str += ' ... ' + return '' +} + +Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { + if (isInstance(target, Uint8Array)) { + target = Buffer.from(target, target.offset, target.byteLength) + } + if (!Buffer.isBuffer(target)) { + throw new TypeError( + 'The "target" argument must be one of type Buffer or Uint8Array. ' + + 'Received type ' + (typeof target) + ) + } + + if (start === undefined) { + start = 0 + } + if (end === undefined) { + end = target ? target.length : 0 + } + if (thisStart === undefined) { + thisStart = 0 + } + if (thisEnd === undefined) { + thisEnd = this.length + } + + if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { + throw new RangeError('out of range index') + } + + if (thisStart >= thisEnd && start >= end) { + return 0 + } + if (thisStart >= thisEnd) { + return -1 + } + if (start >= end) { + return 1 + } + + start >>>= 0 + end >>>= 0 + thisStart >>>= 0 + thisEnd >>>= 0 + + if (this === target) return 0 + + var x = thisEnd - thisStart + var y = end - start + var len = Math.min(x, y) + + var thisCopy = this.slice(thisStart, thisEnd) + var targetCopy = target.slice(start, end) + + for (var i = 0; i < len; ++i) { + if (thisCopy[i] !== targetCopy[i]) { + x = thisCopy[i] + y = targetCopy[i] + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 +} + +// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, +// OR the last index of `val` in `buffer` at offset <= `byteOffset`. +// +// Arguments: +// - buffer - a Buffer to search +// - val - a string, Buffer, or number +// - byteOffset - an index into `buffer`; will be clamped to an int32 +// - encoding - an optional encoding, relevant is val is a string +// - dir - true for indexOf, false for lastIndexOf +function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { + // Empty buffer means no match + if (buffer.length === 0) return -1 + + // Normalize byteOffset + if (typeof byteOffset === 'string') { + encoding = byteOffset + byteOffset = 0 + } else if (byteOffset > 0x7fffffff) { + byteOffset = 0x7fffffff + } else if (byteOffset < -0x80000000) { + byteOffset = -0x80000000 + } + byteOffset = +byteOffset // Coerce to Number. + if (numberIsNaN(byteOffset)) { + // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer + byteOffset = dir ? 0 : (buffer.length - 1) + } + + // Normalize byteOffset: negative offsets start from the end of the buffer + if (byteOffset < 0) byteOffset = buffer.length + byteOffset + if (byteOffset >= buffer.length) { + if (dir) return -1 + else byteOffset = buffer.length - 1 + } else if (byteOffset < 0) { + if (dir) byteOffset = 0 + else return -1 + } + + // Normalize val + if (typeof val === 'string') { + val = Buffer.from(val, encoding) + } + + // Finally, search either indexOf (if dir is true) or lastIndexOf + if (Buffer.isBuffer(val)) { + // Special case: looking for empty string/buffer always fails + if (val.length === 0) { + return -1 + } + return arrayIndexOf(buffer, val, byteOffset, encoding, dir) + } else if (typeof val === 'number') { + val = val & 0xFF // Search for a byte value [0-255] + if (typeof Uint8Array.prototype.indexOf === 'function') { + if (dir) { + return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) + } else { + return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) + } + } + return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) + } + + throw new TypeError('val must be string, number or Buffer') +} + +function arrayIndexOf (arr, val, byteOffset, encoding, dir) { + var indexSize = 1 + var arrLength = arr.length + var valLength = val.length + + if (encoding !== undefined) { + encoding = String(encoding).toLowerCase() + if (encoding === 'ucs2' || encoding === 'ucs-2' || + encoding === 'utf16le' || encoding === 'utf-16le') { + if (arr.length < 2 || val.length < 2) { + return -1 + } + indexSize = 2 + arrLength /= 2 + valLength /= 2 + byteOffset /= 2 + } + } + + function read (buf, i) { + if (indexSize === 1) { + return buf[i] + } else { + return buf.readUInt16BE(i * indexSize) + } + } + + var i + if (dir) { + var foundIndex = -1 + for (i = byteOffset; i < arrLength; i++) { + if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { + if (foundIndex === -1) foundIndex = i + if (i - foundIndex + 1 === valLength) return foundIndex * indexSize + } else { + if (foundIndex !== -1) i -= i - foundIndex + foundIndex = -1 + } + } + } else { + if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength + for (i = byteOffset; i >= 0; i--) { + var found = true + for (var j = 0; j < valLength; j++) { + if (read(arr, i + j) !== read(val, j)) { + found = false + break + } + } + if (found) return i + } + } + + return -1 +} + +Buffer.prototype.includes = function includes (val, byteOffset, encoding) { + return this.indexOf(val, byteOffset, encoding) !== -1 +} + +Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, true) +} + +Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, false) +} + +function hexWrite (buf, string, offset, length) { + offset = Number(offset) || 0 + var remaining = buf.length - offset + if (!length) { + length = remaining + } else { + length = Number(length) + if (length > remaining) { + length = remaining + } + } + + var strLen = string.length + + if (length > strLen / 2) { + length = strLen / 2 + } + for (var i = 0; i < length; ++i) { + var parsed = parseInt(string.substr(i * 2, 2), 16) + if (numberIsNaN(parsed)) return i + buf[offset + i] = parsed + } + return i +} + +function utf8Write (buf, string, offset, length) { + return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) +} + +function asciiWrite (buf, string, offset, length) { + return blitBuffer(asciiToBytes(string), buf, offset, length) +} + +function latin1Write (buf, string, offset, length) { + return asciiWrite(buf, string, offset, length) +} + +function base64Write (buf, string, offset, length) { + return blitBuffer(base64ToBytes(string), buf, offset, length) +} + +function ucs2Write (buf, string, offset, length) { + return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) +} + +Buffer.prototype.write = function write (string, offset, length, encoding) { + // Buffer#write(string) + if (offset === undefined) { + encoding = 'utf8' + length = this.length + offset = 0 + // Buffer#write(string, encoding) + } else if (length === undefined && typeof offset === 'string') { + encoding = offset + length = this.length + offset = 0 + // Buffer#write(string, offset[, length][, encoding]) + } else if (isFinite(offset)) { + offset = offset >>> 0 + if (isFinite(length)) { + length = length >>> 0 + if (encoding === undefined) encoding = 'utf8' + } else { + encoding = length + length = undefined + } + } else { + throw new Error( + 'Buffer.write(string, encoding, offset[, length]) is no longer supported' + ) + } + + var remaining = this.length - offset + if (length === undefined || length > remaining) length = remaining + + if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { + throw new RangeError('Attempt to write outside buffer bounds') + } + + if (!encoding) encoding = 'utf8' + + var loweredCase = false + for (;;) { + switch (encoding) { + case 'hex': + return hexWrite(this, string, offset, length) + + case 'utf8': + case 'utf-8': + return utf8Write(this, string, offset, length) + + case 'ascii': + return asciiWrite(this, string, offset, length) + + case 'latin1': + case 'binary': + return latin1Write(this, string, offset, length) + + case 'base64': + // Warning: maxLength not taken into account in base64Write + return base64Write(this, string, offset, length) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return ucs2Write(this, string, offset, length) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = ('' + encoding).toLowerCase() + loweredCase = true + } + } +} + +Buffer.prototype.toJSON = function toJSON () { + return { + type: 'Buffer', + data: Array.prototype.slice.call(this._arr || this, 0) + } +} + +function base64Slice (buf, start, end) { + if (start === 0 && end === buf.length) { + return base64.fromByteArray(buf) + } else { + return base64.fromByteArray(buf.slice(start, end)) + } +} + +function utf8Slice (buf, start, end) { + end = Math.min(buf.length, end) + var res = [] + + var i = start + while (i < end) { + var firstByte = buf[i] + var codePoint = null + var bytesPerSequence = (firstByte > 0xEF) ? 4 + : (firstByte > 0xDF) ? 3 + : (firstByte > 0xBF) ? 2 + : 1 + + if (i + bytesPerSequence <= end) { + var secondByte, thirdByte, fourthByte, tempCodePoint + + switch (bytesPerSequence) { + case 1: + if (firstByte < 0x80) { + codePoint = firstByte + } + break + case 2: + secondByte = buf[i + 1] + if ((secondByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) + if (tempCodePoint > 0x7F) { + codePoint = tempCodePoint + } + } + break + case 3: + secondByte = buf[i + 1] + thirdByte = buf[i + 2] + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) + if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { + codePoint = tempCodePoint + } + } + break + case 4: + secondByte = buf[i + 1] + thirdByte = buf[i + 2] + fourthByte = buf[i + 3] + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) + if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { + codePoint = tempCodePoint + } + } + } + } + + if (codePoint === null) { + // we did not generate a valid codePoint so insert a + // replacement char (U+FFFD) and advance only 1 byte + codePoint = 0xFFFD + bytesPerSequence = 1 + } else if (codePoint > 0xFFFF) { + // encode to utf16 (surrogate pair dance) + codePoint -= 0x10000 + res.push(codePoint >>> 10 & 0x3FF | 0xD800) + codePoint = 0xDC00 | codePoint & 0x3FF + } + + res.push(codePoint) + i += bytesPerSequence + } + + return decodeCodePointsArray(res) +} + +// Based on http://stackoverflow.com/a/22747272/680742, the browser with +// the lowest limit is Chrome, with 0x10000 args. +// We go 1 magnitude less, for safety +var MAX_ARGUMENTS_LENGTH = 0x1000 + +function decodeCodePointsArray (codePoints) { + var len = codePoints.length + if (len <= MAX_ARGUMENTS_LENGTH) { + return String.fromCharCode.apply(String, codePoints) // avoid extra slice() + } + + // Decode in chunks to avoid "call stack size exceeded". + var res = '' + var i = 0 + while (i < len) { + res += String.fromCharCode.apply( + String, + codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) + ) + } + return res +} + +function asciiSlice (buf, start, end) { + var ret = '' + end = Math.min(buf.length, end) + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i] & 0x7F) + } + return ret +} + +function latin1Slice (buf, start, end) { + var ret = '' + end = Math.min(buf.length, end) + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i]) + } + return ret +} + +function hexSlice (buf, start, end) { + var len = buf.length + + if (!start || start < 0) start = 0 + if (!end || end < 0 || end > len) end = len + + var out = '' + for (var i = start; i < end; ++i) { + out += toHex(buf[i]) + } + return out +} + +function utf16leSlice (buf, start, end) { + var bytes = buf.slice(start, end) + var res = '' + for (var i = 0; i < bytes.length; i += 2) { + res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) + } + return res +} + +Buffer.prototype.slice = function slice (start, end) { + var len = this.length + start = ~~start + end = end === undefined ? len : ~~end + + if (start < 0) { + start += len + if (start < 0) start = 0 + } else if (start > len) { + start = len + } + + if (end < 0) { + end += len + if (end < 0) end = 0 + } else if (end > len) { + end = len + } + + if (end < start) end = start + + var newBuf = this.subarray(start, end) + // Return an augmented `Uint8Array` instance + newBuf.__proto__ = Buffer.prototype + return newBuf +} + +/* + * Need to make sure that buffer isn't trying to write out of bounds. + */ +function checkOffset (offset, ext, length) { + if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') + if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') +} + +Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var val = this[offset] + var mul = 1 + var i = 0 + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul + } + + return val +} + +Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + checkOffset(offset, byteLength, this.length) + } + + var val = this[offset + --byteLength] + var mul = 1 + while (byteLength > 0 && (mul *= 0x100)) { + val += this[offset + --byteLength] * mul + } + + return val +} + +Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 1, this.length) + return this[offset] +} + +Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + return this[offset] | (this[offset + 1] << 8) +} + +Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + return (this[offset] << 8) | this[offset + 1] +} + +Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return ((this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16)) + + (this[offset + 3] * 0x1000000) +} + +Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset] * 0x1000000) + + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]) +} + +Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var val = this[offset] + var mul = 1 + var i = 0 + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul + } + mul *= 0x80 + + if (val >= mul) val -= Math.pow(2, 8 * byteLength) + + return val +} + +Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var i = byteLength + var mul = 1 + var val = this[offset + --i] + while (i > 0 && (mul *= 0x100)) { + val += this[offset + --i] * mul + } + mul *= 0x80 + + if (val >= mul) val -= Math.pow(2, 8 * byteLength) + + return val +} + +Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 1, this.length) + if (!(this[offset] & 0x80)) return (this[offset]) + return ((0xff - this[offset] + 1) * -1) +} + +Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + var val = this[offset] | (this[offset + 1] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val +} + +Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + var val = this[offset + 1] | (this[offset] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val +} + +Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24) +} + +Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset] << 24) | + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]) +} + +Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, true, 23, 4) +} + +Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, false, 23, 4) +} + +Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, true, 52, 8) +} + +Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, false, 52, 8) +} + +function checkInt (buf, value, offset, ext, max, min) { + if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') + if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') + if (offset + ext > buf.length) throw new RangeError('Index out of range') +} + +Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1 + checkInt(this, value, offset, byteLength, maxBytes, 0) + } + + var mul = 1 + var i = 0 + this[offset] = value & 0xFF + while (++i < byteLength && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1 + checkInt(this, value, offset, byteLength, maxBytes, 0) + } + + var i = byteLength - 1 + var mul = 1 + this[offset + i] = value & 0xFF + while (--i >= 0 && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) + this[offset] = (value & 0xff) + return offset + 1 +} + +Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + return offset + 2 +} + +Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) + this[offset] = (value >>> 8) + this[offset + 1] = (value & 0xff) + return offset + 2 +} + +Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) + this[offset + 3] = (value >>> 24) + this[offset + 2] = (value >>> 16) + this[offset + 1] = (value >>> 8) + this[offset] = (value & 0xff) + return offset + 4 +} + +Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = (value & 0xff) + return offset + 4 +} + +Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1) + + checkInt(this, value, offset, byteLength, limit - 1, -limit) + } + + var i = 0 + var mul = 1 + var sub = 0 + this[offset] = value & 0xFF + while (++i < byteLength && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { + sub = 1 + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1) + + checkInt(this, value, offset, byteLength, limit - 1, -limit) + } + + var i = byteLength - 1 + var mul = 1 + var sub = 0 + this[offset + i] = value & 0xFF + while (--i >= 0 && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { + sub = 1 + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) + if (value < 0) value = 0xff + value + 1 + this[offset] = (value & 0xff) + return offset + 1 +} + +Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + return offset + 2 +} + +Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) + this[offset] = (value >>> 8) + this[offset + 1] = (value & 0xff) + return offset + 2 +} + +Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + this[offset + 2] = (value >>> 16) + this[offset + 3] = (value >>> 24) + return offset + 4 +} + +Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (value < 0) value = 0xffffffff + value + 1 + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = (value & 0xff) + return offset + 4 +} + +function checkIEEE754 (buf, value, offset, ext, max, min) { + if (offset + ext > buf.length) throw new RangeError('Index out of range') + if (offset < 0) throw new RangeError('Index out of range') +} + +function writeFloat (buf, value, offset, littleEndian, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) + } + ieee754.write(buf, value, offset, littleEndian, 23, 4) + return offset + 4 +} + +Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { + return writeFloat(this, value, offset, true, noAssert) +} + +Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { + return writeFloat(this, value, offset, false, noAssert) +} + +function writeDouble (buf, value, offset, littleEndian, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) + } + ieee754.write(buf, value, offset, littleEndian, 52, 8) + return offset + 8 +} + +Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { + return writeDouble(this, value, offset, true, noAssert) +} + +Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { + return writeDouble(this, value, offset, false, noAssert) +} + +// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) +Buffer.prototype.copy = function copy (target, targetStart, start, end) { + if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer') + if (!start) start = 0 + if (!end && end !== 0) end = this.length + if (targetStart >= target.length) targetStart = target.length + if (!targetStart) targetStart = 0 + if (end > 0 && end < start) end = start + + // Copy 0 bytes; we're done + if (end === start) return 0 + if (target.length === 0 || this.length === 0) return 0 + + // Fatal error conditions + if (targetStart < 0) { + throw new RangeError('targetStart out of bounds') + } + if (start < 0 || start >= this.length) throw new RangeError('Index out of range') + if (end < 0) throw new RangeError('sourceEnd out of bounds') + + // Are we oob? + if (end > this.length) end = this.length + if (target.length - targetStart < end - start) { + end = target.length - targetStart + start + } + + var len = end - start + + if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { + // Use built-in when available, missing from IE11 + this.copyWithin(targetStart, start, end) + } else if (this === target && start < targetStart && targetStart < end) { + // descending copy from end + for (var i = len - 1; i >= 0; --i) { + target[i + targetStart] = this[i + start] + } + } else { + Uint8Array.prototype.set.call( + target, + this.subarray(start, end), + targetStart + ) + } + + return len +} + +// Usage: +// buffer.fill(number[, offset[, end]]) +// buffer.fill(buffer[, offset[, end]]) +// buffer.fill(string[, offset[, end]][, encoding]) +Buffer.prototype.fill = function fill (val, start, end, encoding) { + // Handle string cases: + if (typeof val === 'string') { + if (typeof start === 'string') { + encoding = start + start = 0 + end = this.length + } else if (typeof end === 'string') { + encoding = end + end = this.length + } + if (encoding !== undefined && typeof encoding !== 'string') { + throw new TypeError('encoding must be a string') + } + if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding) + } + if (val.length === 1) { + var code = val.charCodeAt(0) + if ((encoding === 'utf8' && code < 128) || + encoding === 'latin1') { + // Fast path: If `val` fits into a single byte, use that numeric value. + val = code + } + } + } else if (typeof val === 'number') { + val = val & 255 + } + + // Invalid ranges are not set to a default, so can range check early. + if (start < 0 || this.length < start || this.length < end) { + throw new RangeError('Out of range index') + } + + if (end <= start) { + return this + } + + start = start >>> 0 + end = end === undefined ? this.length : end >>> 0 + + if (!val) val = 0 + + var i + if (typeof val === 'number') { + for (i = start; i < end; ++i) { + this[i] = val + } + } else { + var bytes = Buffer.isBuffer(val) + ? val + : Buffer.from(val, encoding) + var len = bytes.length + if (len === 0) { + throw new TypeError('The value "' + val + + '" is invalid for argument "value"') + } + for (i = 0; i < end - start; ++i) { + this[i + start] = bytes[i % len] + } + } + + return this +} + +// HELPER FUNCTIONS +// ================ + +var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g + +function base64clean (str) { + // Node takes equal signs as end of the Base64 encoding + str = str.split('=')[0] + // Node strips out invalid characters like \n and \t from the string, base64-js does not + str = str.trim().replace(INVALID_BASE64_RE, '') + // Node converts strings with length < 2 to '' + if (str.length < 2) return '' + // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not + while (str.length % 4 !== 0) { + str = str + '=' + } + return str +} + +function toHex (n) { + if (n < 16) return '0' + n.toString(16) + return n.toString(16) +} + +function utf8ToBytes (string, units) { + units = units || Infinity + var codePoint + var length = string.length + var leadSurrogate = null + var bytes = [] + + for (var i = 0; i < length; ++i) { + codePoint = string.charCodeAt(i) + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (!leadSurrogate) { + // no lead yet + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } + + // valid lead + leadSurrogate = codePoint + + continue + } + + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = codePoint + continue + } + + // valid surrogate pair + codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + } + + leadSurrogate = null + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break + bytes.push(codePoint) + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x110000) { + if ((units -= 4) < 0) break + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else { + throw new Error('Invalid code point') + } + } + + return bytes +} + +function asciiToBytes (str) { + var byteArray = [] + for (var i = 0; i < str.length; ++i) { + // Node's code seems to be doing this and not & 0x7F.. + byteArray.push(str.charCodeAt(i) & 0xFF) + } + return byteArray +} + +function utf16leToBytes (str, units) { + var c, hi, lo + var byteArray = [] + for (var i = 0; i < str.length; ++i) { + if ((units -= 2) < 0) break + + c = str.charCodeAt(i) + hi = c >> 8 + lo = c % 256 + byteArray.push(lo) + byteArray.push(hi) + } + + return byteArray +} + +function base64ToBytes (str) { + return base64.toByteArray(base64clean(str)) +} + +function blitBuffer (src, dst, offset, length) { + for (var i = 0; i < length; ++i) { + if ((i + offset >= dst.length) || (i >= src.length)) break + dst[i + offset] = src[i] + } + return i +} + +// ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass +// the `instanceof` check but they should be treated as of that type. +// See: https://github.com/feross/buffer/issues/166 +function isInstance (obj, type) { + return obj instanceof type || + (obj != null && obj.constructor != null && obj.constructor.name != null && + obj.constructor.name === type.name) +} +function numberIsNaN (obj) { + // For IE11 support + return obj !== obj // eslint-disable-line no-self-compare +} + +}).call(this)}).call(this,require("buffer").Buffer) +},{"base64-js":27,"buffer":49,"ieee754":206}],50:[function(require,module,exports){ +'use strict'; + +var GetIntrinsic = require('get-intrinsic'); + +var callBind = require('./'); + +var $indexOf = callBind(GetIntrinsic('String.prototype.indexOf')); + +module.exports = function callBoundIntrinsic(name, allowMissing) { + var intrinsic = GetIntrinsic(name, !!allowMissing); + if (typeof intrinsic === 'function' && $indexOf(name, '.prototype.') > -1) { + return callBind(intrinsic); + } + return intrinsic; +}; + +},{"./":51,"get-intrinsic":198}],51:[function(require,module,exports){ +'use strict'; + +var bind = require('function-bind'); +var GetIntrinsic = require('get-intrinsic'); +var setFunctionLength = require('set-function-length'); + +var $TypeError = GetIntrinsic('%TypeError%'); +var $apply = GetIntrinsic('%Function.prototype.apply%'); +var $call = GetIntrinsic('%Function.prototype.call%'); +var $reflectApply = GetIntrinsic('%Reflect.apply%', true) || bind.call($call, $apply); + +var $defineProperty = GetIntrinsic('%Object.defineProperty%', true); +var $max = GetIntrinsic('%Math.max%'); + +if ($defineProperty) { + try { + $defineProperty({}, 'a', { value: 1 }); + } catch (e) { + // IE 8 has a broken defineProperty + $defineProperty = null; + } +} + +module.exports = function callBind(originalFunction) { + if (typeof originalFunction !== 'function') { + throw new $TypeError('a function is required'); + } + var func = $reflectApply(bind, $call, arguments); + return setFunctionLength( + func, + 1 + $max(0, originalFunction.length - (arguments.length - 1)), + true + ); +}; + +var applyBind = function applyBind() { + return $reflectApply(bind, $apply, arguments); +}; + +if ($defineProperty) { + $defineProperty(module.exports, 'apply', { value: applyBind }); +} else { + module.exports.apply = applyBind; +} + +},{"function-bind":197,"get-intrinsic":198,"set-function-length":365}],52:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.CodeService = void 0; +var datatypes_1 = require("./datatypes/datatypes"); +var CodeService = /** @class */ (function () { + function CodeService(valueSetsJson) { + if (valueSetsJson === void 0) { valueSetsJson = {}; } + this.valueSets = {}; + for (var oid in valueSetsJson) { + this.valueSets[oid] = {}; + for (var version in valueSetsJson[oid]) { + var codes = valueSetsJson[oid][version].map(function (code) { return new datatypes_1.Code(code.code, code.system, code.version); }); + this.valueSets[oid][version] = new datatypes_1.ValueSet(oid, version, codes); + } + } + } + CodeService.prototype.findValueSetsByOid = function (oid) { + return this.valueSets[oid] ? Object.values(this.valueSets[oid]) : []; + }; + CodeService.prototype.findValueSet = function (oid, version) { + if (version != null) { + return this.valueSets[oid] != null ? this.valueSets[oid][version] : null; + } + else { + var results = this.findValueSetsByOid(oid); + if (results.length === 0) { + return null; + } + else { + return results.reduce(function (a, b) { + if (a.version > b.version) { + return a; + } + else { + return b; + } + }); + } + } + }; + return CodeService; +}()); +exports.CodeService = CodeService; + +},{"./datatypes/datatypes":56}],53:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.PatientSource = exports.Patient = exports.Record = void 0; +var DT = __importStar(require("./datatypes/datatypes")); +var Record = /** @class */ (function () { + function Record(json) { + this.json = json; + this.id = this.json.id; + } + Record.prototype._is = function (typeSpecifier) { + return this._typeHierarchy().some(function (t) { return t.type === typeSpecifier.type && t.name == typeSpecifier.name; }); + }; + Record.prototype._typeHierarchy = function () { + return [ + { + name: "{https://github.com/cqframework/cql-execution/simple}".concat(this.json.recordType), + type: 'NamedTypeSpecifier' + }, + { + name: '{https://github.com/cqframework/cql-execution/simple}Record', + type: 'NamedTypeSpecifier' + }, + { name: '{urn:hl7-org:elm-types:r1}Any', type: 'NamedTypeSpecifier' } + ]; + }; + Record.prototype._recursiveGet = function (field) { + if (field != null && field.indexOf('.') >= 0) { + var _a = field.split('.', 2), root = _a[0], rest = _a[1]; + return new Record(this._recursiveGet(root))._recursiveGet(rest); + } + return this.json[field]; + }; + Record.prototype.get = function (field) { + // the model should return the correct type for the field. For this simple model example, + // we just cheat and use the shape of the value to determine it. Real implementations should + // have a more sophisticated approach + var value = this._recursiveGet(field); + if (typeof value === 'string' && /\d{4}-\d{2}-\d{2}(T[\d\-.]+)?/.test(value)) { + return this.getDate(field); + } + if (value != null && typeof value === 'object' && value.code != null && value.system != null) { + return this.getCode(field); + } + if (value != null && typeof value === 'object' && (value.low != null || value.high != null)) { + return this.getInterval(field); + } + return value; + }; + Record.prototype.getId = function () { + return this.id; + }; + Record.prototype.getDate = function (field) { + var val = this._recursiveGet(field); + if (val != null) { + return DT.DateTime.parse(val); + } + else { + return null; + } + }; + Record.prototype.getInterval = function (field) { + var val = this._recursiveGet(field); + if (val != null && typeof val === 'object') { + var low = val.low != null ? DT.DateTime.parse(val.low) : null; + var high = val.high != null ? DT.DateTime.parse(val.high) : null; + return new DT.Interval(low, high); + } + }; + Record.prototype.getDateOrInterval = function (field) { + var val = this._recursiveGet(field); + if (val != null && typeof val === 'object') { + return this.getInterval(field); + } + else { + return this.getDate(field); + } + }; + Record.prototype.getCode = function (field) { + var val = this._recursiveGet(field); + if (val != null && typeof val === 'object') { + return new DT.Code(val.code, val.system, val.version); + } + }; + return Record; +}()); +exports.Record = Record; +var Patient = /** @class */ (function (_super) { + __extends(Patient, _super); + function Patient(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.gender = json.gender; + _this.birthDate = json.birthDate != null ? DT.DateTime.parse(json.birthDate) : undefined; + _this.records = {}; + (json.records || []).forEach(function (r) { + if (_this.records[r.recordType] == null) { + _this.records[r.recordType] = []; + } + _this.records[r.recordType].push(new Record(r)); + }); + return _this; + } + Patient.prototype.findRecords = function (profile) { + if (profile == null) { + return []; + } + var match = profile.match(/(\{https:\/\/github\.com\/cqframework\/cql-execution\/simple\})?(.*)/); + if (match == null) + return []; + var recordType = match[2]; + if (recordType === 'Patient') { + return [this]; + } + else { + return this.records[recordType] || []; + } + }; + return Patient; +}(Record)); +exports.Patient = Patient; +var PatientSource = /** @class */ (function () { + function PatientSource(patients) { + this.patients = patients; + this.nextPatient(); + } + PatientSource.prototype.currentPatient = function () { + return this.current; + }; + PatientSource.prototype.nextPatient = function () { + var currentJSON = this.patients.shift(); + this.current = currentJSON ? new Patient(currentJSON) : undefined; + return this.current; + }; + return PatientSource; +}()); +exports.PatientSource = PatientSource; + +},{"./datatypes/datatypes":56}],54:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ValueSet = exports.Ratio = exports.Quantity = exports.Interval = exports.DateTime = exports.Date = exports.Concept = exports.CodeSystem = exports.Code = exports.CodeService = exports.PatientSource = exports.Patient = exports.NullMessageListener = exports.ConsoleMessageListener = exports.Results = exports.Executor = exports.UnfilteredContext = exports.PatientContext = exports.Context = exports.Expression = exports.Repository = exports.Library = void 0; +// Library-related classes +var library_1 = require("./elm/library"); +Object.defineProperty(exports, "Library", { enumerable: true, get: function () { return library_1.Library; } }); +var repository_1 = require("./runtime/repository"); +Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_1.Repository; } }); +var expression_1 = require("./elm/expression"); +Object.defineProperty(exports, "Expression", { enumerable: true, get: function () { return expression_1.Expression; } }); +// Execution-related classes +var context_1 = require("./runtime/context"); +Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return context_1.Context; } }); +Object.defineProperty(exports, "PatientContext", { enumerable: true, get: function () { return context_1.PatientContext; } }); +Object.defineProperty(exports, "UnfilteredContext", { enumerable: true, get: function () { return context_1.UnfilteredContext; } }); +var executor_1 = require("./runtime/executor"); +Object.defineProperty(exports, "Executor", { enumerable: true, get: function () { return executor_1.Executor; } }); +var results_1 = require("./runtime/results"); +Object.defineProperty(exports, "Results", { enumerable: true, get: function () { return results_1.Results; } }); +var messageListeners_1 = require("./runtime/messageListeners"); +Object.defineProperty(exports, "ConsoleMessageListener", { enumerable: true, get: function () { return messageListeners_1.ConsoleMessageListener; } }); +Object.defineProperty(exports, "NullMessageListener", { enumerable: true, get: function () { return messageListeners_1.NullMessageListener; } }); +// PatientSource-related classes +var cql_patient_1 = require("./cql-patient"); +Object.defineProperty(exports, "Patient", { enumerable: true, get: function () { return cql_patient_1.Patient; } }); +Object.defineProperty(exports, "PatientSource", { enumerable: true, get: function () { return cql_patient_1.PatientSource; } }); +// TerminologyService-related classes +var cql_code_service_1 = require("./cql-code-service"); +Object.defineProperty(exports, "CodeService", { enumerable: true, get: function () { return cql_code_service_1.CodeService; } }); +// DataType classes +var datatypes_1 = require("./datatypes/datatypes"); +Object.defineProperty(exports, "Code", { enumerable: true, get: function () { return datatypes_1.Code; } }); +Object.defineProperty(exports, "CodeSystem", { enumerable: true, get: function () { return datatypes_1.CodeSystem; } }); +Object.defineProperty(exports, "Concept", { enumerable: true, get: function () { return datatypes_1.Concept; } }); +Object.defineProperty(exports, "Date", { enumerable: true, get: function () { return datatypes_1.Date; } }); +Object.defineProperty(exports, "DateTime", { enumerable: true, get: function () { return datatypes_1.DateTime; } }); +Object.defineProperty(exports, "Interval", { enumerable: true, get: function () { return datatypes_1.Interval; } }); +Object.defineProperty(exports, "Quantity", { enumerable: true, get: function () { return datatypes_1.Quantity; } }); +Object.defineProperty(exports, "Ratio", { enumerable: true, get: function () { return datatypes_1.Ratio; } }); +Object.defineProperty(exports, "ValueSet", { enumerable: true, get: function () { return datatypes_1.ValueSet; } }); +// Custom Types +__exportStar(require("./types"), exports); +exports.default = { + Library: library_1.Library, + Repository: repository_1.Repository, + Expression: expression_1.Expression, + Context: context_1.Context, + PatientContext: context_1.PatientContext, + UnfilteredContext: context_1.UnfilteredContext, + Executor: executor_1.Executor, + Results: results_1.Results, + ConsoleMessageListener: messageListeners_1.ConsoleMessageListener, + NullMessageListener: messageListeners_1.NullMessageListener, + Patient: cql_patient_1.Patient, + PatientSource: cql_patient_1.PatientSource, + CodeService: cql_code_service_1.CodeService, + Code: datatypes_1.Code, + CodeSystem: datatypes_1.CodeSystem, + Concept: datatypes_1.Concept, + Date: datatypes_1.Date, + DateTime: datatypes_1.DateTime, + Interval: datatypes_1.Interval, + Quantity: datatypes_1.Quantity, + Ratio: datatypes_1.Ratio, + ValueSet: datatypes_1.ValueSet +}; + +},{"./cql-code-service":52,"./cql-patient":53,"./datatypes/datatypes":56,"./elm/expression":72,"./elm/library":77,"./runtime/context":92,"./runtime/executor":93,"./runtime/messageListeners":94,"./runtime/repository":95,"./runtime/results":96,"./types":99}],55:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.CodeSystem = exports.ValueSet = exports.Concept = exports.Code = void 0; +var util_1 = require("../util/util"); +var Code = /** @class */ (function () { + function Code(code, system, version, display) { + this.code = code; + this.system = system; + this.version = version; + this.display = display; + } + Object.defineProperty(Code.prototype, "isCode", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Code.prototype.hasMatch = function (code) { + if (typeof code === 'string') { + // the specific behavior for this is not in the specification. Matching codesystem behavior. + return code === this.code; + } + else { + return codesInList(toCodeList(code), [this]); + } + }; + return Code; +}()); +exports.Code = Code; +var Concept = /** @class */ (function () { + function Concept(codes, display) { + this.codes = codes; + this.display = display; + this.codes || (this.codes = []); + } + Object.defineProperty(Concept.prototype, "isConcept", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Concept.prototype.hasMatch = function (code) { + return codesInList(toCodeList(code), this.codes); + }; + return Concept; +}()); +exports.Concept = Concept; +var ValueSet = /** @class */ (function () { + function ValueSet(oid, version, codes) { + if (codes === void 0) { codes = []; } + this.oid = oid; + this.version = version; + this.codes = codes; + this.codes || (this.codes = []); + } + Object.defineProperty(ValueSet.prototype, "isValueSet", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + ValueSet.prototype.hasMatch = function (code) { + var codesList = toCodeList(code); + // InValueSet String Overload + if (codesList.length === 1 && typeof codesList[0] === 'string') { + var matchFound = false; + var multipleCodeSystemsExist = false; + for (var _i = 0, _a = this.codes; _i < _a.length; _i++) { + var codeItem = _a[_i]; + // Confirm all code systems match + if (codeItem.system !== this.codes[0].system) { + multipleCodeSystemsExist = true; + } + if (codeItem.code === codesList[0]) { + matchFound = true; + } + if (multipleCodeSystemsExist && matchFound) { + throw new Error('In (valueset) is ambiguous -- multiple codes with multiple code systems exist in value set.'); + } + } + return matchFound; + } + else { + return codesInList(codesList, this.codes); + } + }; + return ValueSet; +}()); +exports.ValueSet = ValueSet; +function toCodeList(c) { + if (c == null) { + return []; + } + else if ((0, util_1.typeIsArray)(c)) { + var list = []; + for (var _i = 0, c_1 = c; _i < c_1.length; _i++) { + var c2 = c_1[_i]; + list = list.concat(toCodeList(c2)); + } + return list; + } + else if ((0, util_1.typeIsArray)(c.codes)) { + return c.codes; + } + else { + return [c]; + } +} +function codesInList(cl1, cl2) { + // test each code in c1 against each code in c2 looking for a match + return cl1.some(function (c1) { + return cl2.some(function (c2) { + // only the left argument (cl1) can contain strings. cl2 will only contain codes. + if (typeof c1 === 'string') { + // for "string in codesystem" this should compare the string to + // the code's "code" field according to the specification. + return c1 === c2.code; + } + else { + return codesMatch(c1, c2); + } + }); + }); +} +function codesMatch(code1, code2) { + return code1.code === code2.code && code1.system === code2.system; +} +var CodeSystem = /** @class */ (function () { + function CodeSystem(id, version) { + this.id = id; + this.version = version; + } + return CodeSystem; +}()); +exports.CodeSystem = CodeSystem; + +},{"../util/util":105}],56:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./logic"), exports); +__exportStar(require("./clinical"), exports); +__exportStar(require("./uncertainty"), exports); +__exportStar(require("./datetime"), exports); +__exportStar(require("./interval"), exports); +__exportStar(require("./quantity"), exports); +__exportStar(require("./ratio"), exports); + +},{"./clinical":55,"./datetime":57,"./interval":59,"./logic":60,"./quantity":61,"./ratio":62,"./uncertainty":63}],57:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +}; +var _a, _b; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.MAX_TIME_VALUE = exports.MIN_TIME_VALUE = exports.MAX_DATE_VALUE = exports.MIN_DATE_VALUE = exports.MAX_DATETIME_VALUE = exports.MIN_DATETIME_VALUE = exports.Date = exports.DateTime = void 0; +/* eslint-disable @typescript-eslint/ban-ts-comment */ +var uncertainty_1 = require("./uncertainty"); +var util_1 = require("../util/util"); +var luxon_1 = require("luxon"); +// It's easiest and most performant to organize formats by length of the supported strings. +// This way we can test strings only against the formats that have a chance of working. +// NOTE: Formats use Luxon formats, documented here: https://moment.github.io/luxon/docs/manual/parsing.html#table-of-tokens +var LENGTH_TO_DATE_FORMAT_MAP = (function () { + var ltdfMap = new Map(); + ltdfMap.set(4, 'yyyy'); + ltdfMap.set(7, 'yyyy-MM'); + ltdfMap.set(10, 'yyyy-MM-dd'); + return ltdfMap; +})(); +var LENGTH_TO_DATETIME_FORMATS_MAP = (function () { + var formats = { + yyyy: '2012', + 'yyyy-MM': '2012-01', + 'yyyy-MM-dd': '2012-01-31', + "yyyy-MM-dd'T''Z'": '2012-01-31TZ', + "yyyy-MM-dd'T'ZZ": '2012-01-31T-04:00', + "yyyy-MM-dd'T'HH": '2012-01-31T12', + "yyyy-MM-dd'T'HH'Z'": '2012-01-31T12Z', + "yyyy-MM-dd'T'HHZZ": '2012-01-31T12-04:00', + "yyyy-MM-dd'T'HH:mm": '2012-01-31T12:30', + "yyyy-MM-dd'T'HH:mm'Z'": '2012-01-31T12:30Z', + "yyyy-MM-dd'T'HH:mmZZ": '2012-01-31T12:30-04:00', + "yyyy-MM-dd'T'HH:mm:ss": '2012-01-31T12:30:59', + "yyyy-MM-dd'T'HH:mm:ss'Z'": '2012-01-31T12:30:59Z', + "yyyy-MM-dd'T'HH:mm:ssZZ": '2012-01-31T12:30:59-04:00', + "yyyy-MM-dd'T'HH:mm:ss.SSS": '2012-01-31T12:30:59.000', + "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'": '2012-01-31T12:30:59.000Z', + "yyyy-MM-dd'T'HH:mm:ss.SSSZZ": '2012-01-31T12:30:59.000-04:00' + }; + var ltdtfMap = new Map(); + Object.keys(formats).forEach(function (k) { + var example = formats[k]; + if (!ltdtfMap.has(example.length)) { + ltdtfMap.set(example.length, [k]); + } + else { + ltdtfMap.get(example.length).push(k); + } + }); + return ltdtfMap; +})(); +function wholeLuxonDuration(duration, unit) { + var value = duration.get(unit); + return value >= 0 ? Math.floor(value) : Math.ceil(value); +} +function truncateLuxonDateTime(luxonDT, unit) { + // Truncating by week (to the previous Sunday) requires different logic than the rest + if (unit === DateTime.Unit.WEEK) { + // Sunday is ISO weekday 7 + if (luxonDT.weekday !== 7) { + luxonDT = luxonDT.set({ weekday: 7 }).minus({ weeks: 1 }); + } + unit = DateTime.Unit.DAY; + } + return luxonDT.startOf(unit); +} +/* + * Base class for Date and DateTime to extend from + * Implements shared functions by both classes + * TODO: we can probably iterate on this more to improve the accessing of "FIELDS" and the overall structure + * TODO: we can also investigate if it's reasonable for DateTime to extend Date directly instead + */ +var AbstractDate = /** @class */ (function () { + function AbstractDate(year, month, day) { + if (year === void 0) { year = null; } + if (month === void 0) { month = null; } + if (day === void 0) { day = null; } + this.year = year; + this.month = month; + this.day = day; + } + // Shared functions + AbstractDate.prototype.isPrecise = function () { + var _this = this; + // @ts-ignore + return this.constructor.FIELDS.every(function (field) { return _this[field] != null; }); + }; + AbstractDate.prototype.isImprecise = function () { + return !this.isPrecise(); + }; + AbstractDate.prototype.isMorePrecise = function (other) { + // @ts-ignore + if (typeof other === 'string' && this.constructor.FIELDS.includes(other)) { + // @ts-ignore + if (this[other] == null) { + return false; + } + } + else { + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // @ts-ignore + if (other[field] != null && this[field] == null) { + return false; + } + } + } + return !this.isSamePrecision(other); + }; + // This function can take another Date-ish object, or a precision string (e.g. 'month') + AbstractDate.prototype.isLessPrecise = function (other) { + return !this.isSamePrecision(other) && !this.isMorePrecise(other); + }; + // This function can take another Date-ish object, or a precision string (e.g. 'month') + AbstractDate.prototype.isSamePrecision = function (other) { + // @ts-ignore + if (typeof other === 'string' && this.constructor.FIELDS.includes(other)) { + return other === this.getPrecision(); + } + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // @ts-ignore + if (this[field] != null && other[field] == null) { + return false; + } + // @ts-ignore + if (this[field] == null && other[field] != null) { + return false; + } + } + return true; + }; + AbstractDate.prototype.equals = function (other) { + return compareWithDefaultResult(this, other, null); + }; + AbstractDate.prototype.equivalent = function (other) { + return compareWithDefaultResult(this, other, false); + }; + AbstractDate.prototype.sameAs = function (other, precision) { + if (!(other.isDate || other.isDateTime)) { + return null; + } + else if (this.isDate && other.isDateTime) { + return this.getDateTime().sameAs(other, precision); + } + else if (this.isDateTime && other.isDate) { + other = other.getDateTime(); + } + // @ts-ignore + if (precision != null && this.constructor.FIELDS.indexOf(precision) < 0) { + throw new Error("Invalid precision: ".concat(precision)); + } + // make a copy of other in the correct timezone offset if they don't match. + // When comparing DateTime values with different timezone offsets, implementations + // should normalize to the timezone offset of the evaluation request timestamp, + // but only when the comparison precision is hours, minutes, seconds, or milliseconds. + if (isPrecisionUnspecifiedOrGreaterThanDay(precision)) { + if (this.timezoneOffset !== other.timezoneOffset) { + other = other.convertToTimezoneOffset(this.timezoneOffset); + } + } + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // if both have this precision defined + // @ts-ignore + if (this[field] != null && other[field] != null) { + // if they are different then return with false + // @ts-ignore + if (this[field] !== other[field]) { + return false; + } + // if both dont have this precision, return true of precision is not defined + // @ts-ignore + } + else if (this[field] == null && other[field] == null) { + if (precision == null) { + return true; + } + else { + // we havent met precision yet + return null; + } + // otherwise they have inconclusive precision, return null + } + else { + return null; + } + // if precision is defined and we have reached expected precision, we can leave the loop + if (precision != null && precision === field) { + break; + } + } + // if we made it here, then all fields matched. + return true; + }; + AbstractDate.prototype.sameOrBefore = function (other, precision) { + if (!(other.isDate || other.isDateTime)) { + return null; + } + else if (this.isDate && other.isDateTime) { + return this.getDateTime().sameOrBefore(other, precision); + } + else if (this.isDateTime && other.isDate) { + other = other.getDateTime(); + } + // @ts-ignore + if (precision != null && this.constructor.FIELDS.indexOf(precision) < 0) { + throw new Error("Invalid precision: ".concat(precision)); + } + // make a copy of other in the correct timezone offset if they don't match. + // When comparing DateTime values with different timezone offsets, implementations + // should normalize to the timezone offset of the evaluation request timestamp, + // but only when the comparison precision is hours, minutes, seconds, or milliseconds. + if (isPrecisionUnspecifiedOrGreaterThanDay(precision)) { + if (this.timezoneOffset !== other.timezoneOffset) { + other = other.convertToTimezoneOffset(this.timezoneOffset); + } + } + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // if both have this precision defined + // @ts-ignore + if (this[field] != null && other[field] != null) { + // if this value is less than the other return with true. this is before other + // @ts-ignore + if (this[field] < other[field]) { + return true; + // if this value is greater than the other return with false. this is after + // @ts-ignore + } + else if (this[field] > other[field]) { + return false; + } + // execution continues if the values are the same + // if both dont have this precision, return true if precision is not defined + // @ts-ignore + } + else if (this[field] == null && other[field] == null) { + if (precision == null) { + return true; + } + else { + // we havent met precision yet + return null; + } + // otherwise they have inconclusive precision, return null + } + else { + return null; + } + // if precision is defined and we have reached expected precision, we can leave the loop + if (precision != null && precision === field) { + break; + } + } + // if we made it here, then all fields matched and they are same + return true; + }; + AbstractDate.prototype.sameOrAfter = function (other, precision) { + if (!(other.isDate || other.isDateTime)) { + return null; + } + else if (this.isDate && other.isDateTime) { + return this.getDateTime().sameOrAfter(other, precision); + } + else if (this.isDateTime && other.isDate) { + other = other.getDateTime(); + } + // @ts-ignore + if (precision != null && this.constructor.FIELDS.indexOf(precision) < 0) { + throw new Error("Invalid precision: ".concat(precision)); + } + // make a copy of other in the correct timezone offset if they don't match. + // When comparing DateTime values with different timezone offsets, implementations + // should normalize to the timezone offset of the evaluation request timestamp, + // but only when the comparison precision is hours, minutes, seconds, or milliseconds. + if (isPrecisionUnspecifiedOrGreaterThanDay(precision)) { + if (this.timezoneOffset !== other.timezoneOffset) { + other = other.convertToTimezoneOffset(this.timezoneOffset); + } + } + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // if both have this precision defined + // @ts-ignore + if (this[field] != null && other[field] != null) { + // if this value is greater than the other return with true. this is after other + // @ts-ignore + if (this[field] > other[field]) { + return true; + // if this value is greater than the other return with false. this is before + // @ts-ignore + } + else if (this[field] < other[field]) { + return false; + } + // execution continues if the values are the same + // if both dont have this precision, return true if precision is not defined + // @ts-ignore + } + else if (this[field] == null && other[field] == null) { + if (precision == null) { + return true; + } + else { + // we havent met precision yet + return null; + } + // otherwise they have inconclusive precision, return null + } + else { + return null; + } + // if precision is defined and we have reached expected precision, we can leave the loop + if (precision != null && precision === field) { + break; + } + } + // if we made it here, then all fields matched and they are same + return true; + }; + AbstractDate.prototype.before = function (other, precision) { + if (!(other.isDate || other.isDateTime)) { + return null; + } + else if (this.isDate && other.isDateTime) { + return this.getDateTime().before(other, precision); + } + else if (this.isDateTime && other.isDate) { + other = other.getDateTime(); + } + // @ts-ignore + if (precision != null && this.constructor.FIELDS.indexOf(precision) < 0) { + throw new Error("Invalid precision: ".concat(precision)); + } + // make a copy of other in the correct timezone offset if they don't match. + // When comparing DateTime values with different timezone offsets, implementations + // should normalize to the timezone offset of the evaluation request timestamp, + // but only when the comparison precision is hours, minutes, seconds, or milliseconds. + if (isPrecisionUnspecifiedOrGreaterThanDay(precision)) { + if (this.timezoneOffset !== other.timezoneOffset) { + other = other.convertToTimezoneOffset(this.timezoneOffset); + } + } + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // if both have this precision defined + // @ts-ignore + if (this[field] != null && other[field] != null) { + // if this value is less than the other return with true. this is before other + // @ts-ignore + if (this[field] < other[field]) { + return true; + // if this value is greater than the other return with false. this is after + // @ts-ignore + } + else if (this[field] > other[field]) { + return false; + } + // execution continues if the values are the same + // if both dont have this precision, return false if precision is not defined + // @ts-ignore + } + else if (this[field] == null && other[field] == null) { + if (precision == null) { + return false; + } + else { + // we havent met precision yet + return null; + } + // otherwise they have inconclusive precision, return null + } + else { + return null; + } + // if precision is defined and we have reached expected precision, we can leave the loop + if (precision != null && precision === field) { + break; + } + } + // if we made it here, then all fields matched and they are same + return false; + }; + AbstractDate.prototype.after = function (other, precision) { + if (!(other.isDate || other.isDateTime)) { + return null; + } + else if (this.isDate && other.isDateTime) { + return this.getDateTime().after(other, precision); + } + else if (this.isDateTime && other.isDate) { + other = other.getDateTime(); + } + // @ts-ignore + if (precision != null && this.constructor.FIELDS.indexOf(precision) < 0) { + throw new Error("Invalid precision: ".concat(precision)); + } + // make a copy of other in the correct timezone offset if they don't match. + // When comparing DateTime values with different timezone offsets, implementations + // should normalize to the timezone offset of the evaluation request timestamp, + // but only when the comparison precision is hours, minutes, seconds, or milliseconds. + if (isPrecisionUnspecifiedOrGreaterThanDay(precision)) { + if (this.timezoneOffset !== other.timezoneOffset) { + other = other.convertToTimezoneOffset(this.timezoneOffset); + } + } + // @ts-ignore + for (var _i = 0, _a = this.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // if both have this precision defined + // @ts-ignore + if (this[field] != null && other[field] != null) { + // if this value is greater than the other return with true. this is after other + // @ts-ignore + if (this[field] > other[field]) { + return true; + // if this value is greater than the other return with false. this is before + // @ts-ignore + } + else if (this[field] < other[field]) { + return false; + } + // execution continues if the values are the same + // if both dont have this precision, return false if precision is not defined + // @ts-ignore + } + else if (this[field] == null && other[field] == null) { + if (precision == null) { + return false; + } + else { + // we havent met precision yet + return null; + } + // otherwise they have inconclusive precision, return null + } + else { + return null; + } + // if precision is defined and we have reached expected precision, we can leave the loop + if (precision != null && precision === field) { + break; + } + } + // if we made it here, then all fields matched and they are same + return false; + }; + AbstractDate.prototype.add = function (offset, field) { + var _a; + if (offset === 0 || this.year == null) { + return this.copy(); + } + // Use luxon to do the date math because it honors DST and it has the leap-year/end-of-month semantics we want. + // NOTE: The luxonDateTime will contain default values where this[unit] is null, but we'll account for that. + var luxonDateTime = this.toLuxonDateTime(); + // From the spec: "The operation is performed by converting the time-based quantity to the most precise value + // specified in the date/time (truncating any resulting decimal portion) and then adding it to the date/time value." + // However, since you can't really convert days to months, if "this" is less precise than the field being added, we can + // add to the earliest possible value of "this" or subtract from the latest possible value of "this" (depending on the + // sign of the offset), and then null out the imprecise fields again after doing the calculation. Due to the way + // luxonDateTime is constructed above, it is already at the earliest value, so only adjust if the offset is negative. + // @ts-ignore + var offsetIsMorePrecise = this[field] == null; //whether the quantity we are adding is more precise than "this". + if (offsetIsMorePrecise && offset < 0) { + luxonDateTime = luxonDateTime.endOf(this.getPrecision()); + } + // Now do the actual math and convert it back to a Date/DateTime w/ originally null fields nulled out again + var luxonResult = luxonDateTime.plus((_a = {}, _a[field] = offset, _a)); + var result = this.constructor + .fromLuxonDateTime(luxonResult) + .reducedPrecision(this.getPrecision()); + // Luxon never has a null offset, but sometimes "this" does, so reset to null if applicable + if (this.isDateTime && this.timezoneOffset == null) { + result.timezoneOffset = null; + } + // Can't use overflowsOrUnderflows from math.js due to circular dependencies when we require it + if (result.after(exports.MAX_DATETIME_VALUE || result.before(exports.MIN_DATETIME_VALUE))) { + return null; + } + else { + return result; + } + }; + AbstractDate.prototype.getFieldFloor = function (field) { + switch (field) { + case 'month': + return 1; + case 'day': + return 1; + case 'hour': + return 0; + case 'minute': + return 0; + case 'second': + return 0; + case 'millisecond': + return 0; + default: + throw new Error('Tried to floor a field that has no floor value: ' + field); + } + }; + AbstractDate.prototype.getFieldCieling = function (field) { + switch (field) { + case 'month': + return 12; + case 'day': + return daysInMonth(this.year, this.month); + case 'hour': + return 23; + case 'minute': + return 59; + case 'second': + return 59; + case 'millisecond': + return 999; + default: + throw new Error('Tried to clieling a field that has no cieling value: ' + field); + } + }; + return AbstractDate; +}()); +var DateTime = /** @class */ (function (_super) { + __extends(DateTime, _super); + function DateTime(year, month, day, hour, minute, second, millisecond, timezoneOffset) { + if (year === void 0) { year = null; } + if (month === void 0) { month = null; } + if (day === void 0) { day = null; } + if (hour === void 0) { hour = null; } + if (minute === void 0) { minute = null; } + if (second === void 0) { second = null; } + if (millisecond === void 0) { millisecond = null; } + var _this = + // from the spec: If no timezone is specified, the timezone of the evaluation request timestamp is used. + // NOTE: timezoneOffset will be explicitly null for the Time overload, whereas + // it will be undefined if simply unspecified + _super.call(this, year, month, day) || this; + _this.hour = hour; + _this.minute = minute; + _this.second = second; + _this.millisecond = millisecond; + if (timezoneOffset === undefined) { + _this.timezoneOffset = (new util_1.jsDate().getTimezoneOffset() / 60) * -1; + } + else { + _this.timezoneOffset = timezoneOffset; + } + return _this; + } + DateTime.parse = function (string) { + if (string === null) { + return null; + } + var matches = /(\d{4})(-(\d{2}))?(-(\d{2}))?(T((\d{2})(:(\d{2})(:(\d{2})(\.(\d+))?)?)?)?(Z|(([+-])(\d{2})(:?(\d{2}))?))?)?/.exec(string); + if (matches == null) { + return null; + } + var years = matches[1]; + var months = matches[3]; + var days = matches[5]; + var hours = matches[8]; + var minutes = matches[10]; + var seconds = matches[12]; + var milliseconds = matches[14]; + if (milliseconds != null) { + milliseconds = (0, util_1.normalizeMillisecondsField)(milliseconds); + } + if (milliseconds != null) { + string = (0, util_1.normalizeMillisecondsFieldInString)(string, matches[14]); + } + if (!isValidDateTimeStringFormat(string)) { + return null; + } + // convert the args to integers + var args = [years, months, days, hours, minutes, seconds, milliseconds].map(function (arg) { + return arg != null ? parseInt(arg) : arg; + }); + // convert timezone offset to decimal and add it to arguments + if (matches[18] != null) { + var num = parseInt(matches[18]) + (matches[20] != null ? parseInt(matches[20]) / 60 : 0); + args.push(matches[17] === '+' ? num : num * -1); + } + else if (matches[15] === 'Z') { + args.push(0); + } + // @ts-ignore + return new (DateTime.bind.apply(DateTime, __spreadArray([void 0], args, false)))(); + }; + // TODO: Note: using the jsDate type causes issues, fix later + DateTime.fromJSDate = function (date, timezoneOffset) { + //This is from a JS Date, not a CQL Date + if (date instanceof DateTime) { + return date; + } + if (timezoneOffset != null) { + date = new util_1.jsDate(date.getTime() + timezoneOffset * 60 * 60 * 1000); + return new DateTime(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds(), timezoneOffset); + } + else { + return new DateTime(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()); + } + }; + DateTime.fromLuxonDateTime = function (luxonDT) { + if (luxonDT instanceof DateTime) { + return luxonDT; + } + return new DateTime(luxonDT.year, luxonDT.month, luxonDT.day, luxonDT.hour, luxonDT.minute, luxonDT.second, luxonDT.millisecond, luxonDT.offset / 60); + }; + Object.defineProperty(DateTime.prototype, "isDateTime", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Object.defineProperty(DateTime.prototype, "isDate", { + get: function () { + return false; + }, + enumerable: false, + configurable: true + }); + DateTime.prototype.copy = function () { + return new DateTime(this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond, this.timezoneOffset); + }; + DateTime.prototype.successor = function () { + if (this.millisecond != null) { + return this.add(1, DateTime.Unit.MILLISECOND); + } + else if (this.second != null) { + return this.add(1, DateTime.Unit.SECOND); + } + else if (this.minute != null) { + return this.add(1, DateTime.Unit.MINUTE); + } + else if (this.hour != null) { + return this.add(1, DateTime.Unit.HOUR); + } + else if (this.day != null) { + return this.add(1, DateTime.Unit.DAY); + } + else if (this.month != null) { + return this.add(1, DateTime.Unit.MONTH); + } + else if (this.year != null) { + return this.add(1, DateTime.Unit.YEAR); + } + }; + DateTime.prototype.predecessor = function () { + if (this.millisecond != null) { + return this.add(-1, DateTime.Unit.MILLISECOND); + } + else if (this.second != null) { + return this.add(-1, DateTime.Unit.SECOND); + } + else if (this.minute != null) { + return this.add(-1, DateTime.Unit.MINUTE); + } + else if (this.hour != null) { + return this.add(-1, DateTime.Unit.HOUR); + } + else if (this.day != null) { + return this.add(-1, DateTime.Unit.DAY); + } + else if (this.month != null) { + return this.add(-1, DateTime.Unit.MONTH); + } + else if (this.year != null) { + return this.add(-1, DateTime.Unit.YEAR); + } + }; + DateTime.prototype.convertToTimezoneOffset = function (timezoneOffset) { + if (timezoneOffset === void 0) { timezoneOffset = 0; } + var shiftedLuxonDT = this.toLuxonDateTime().setZone(luxon_1.FixedOffsetZone.instance(timezoneOffset * 60)); + var shiftedDT = DateTime.fromLuxonDateTime(shiftedLuxonDT); + return shiftedDT.reducedPrecision(this.getPrecision()); + }; + DateTime.prototype.differenceBetween = function (other, unitField) { + other = this._implicitlyConvert(other); + if (other == null || !other.isDateTime) { + return null; + } + // According to CQL spec: + // * "Difference calculations are performed by truncating the datetime values at the next precision, + // and then performing the corresponding duration calculation on the truncated values." + // * "When difference is calculated for hours or finer units, timezone offsets should be normalized + // prior to truncation to correctly consider real (actual elapsed) time. When difference is calculated + // for days or coarser units, however, the time components (including timezone offset) should be truncated + // without normalization to correctly reflect the difference in calendar days, months, and years." + var a = this.toLuxonUncertainty(); + var b = other.toLuxonUncertainty(); + // If unit is days or above, reset all the DateTimes to UTC since TZ offset should not be considered; + // Otherwise, we don't actually have to "normalize" to a common TZ because Luxon takes TZ into account. + if ([DateTime.Unit.YEAR, DateTime.Unit.MONTH, DateTime.Unit.WEEK, DateTime.Unit.DAY].includes(unitField)) { + a.low = a.low.toUTC(0, { keepLocalTime: true }); + a.high = a.high.toUTC(0, { keepLocalTime: true }); + b.low = b.low.toUTC(0, { keepLocalTime: true }); + b.high = b.high.toUTC(0, { keepLocalTime: true }); + } + // Truncate all dates at precision below specified unit + a.low = truncateLuxonDateTime(a.low, unitField); + a.high = truncateLuxonDateTime(a.high, unitField); + b.low = truncateLuxonDateTime(b.low, unitField); + b.high = truncateLuxonDateTime(b.high, unitField); + // Return the duration based on the normalize and truncated values + return new uncertainty_1.Uncertainty(wholeLuxonDuration(b.low.diff(a.high, unitField), unitField), wholeLuxonDuration(b.high.diff(a.low, unitField), unitField)); + }; + DateTime.prototype.durationBetween = function (other, unitField) { + other = this._implicitlyConvert(other); + if (other == null || !other.isDateTime) { + return null; + } + var a = this.toLuxonUncertainty(); + var b = other.toLuxonUncertainty(); + return new uncertainty_1.Uncertainty(wholeLuxonDuration(b.low.diff(a.high, unitField), unitField), wholeLuxonDuration(b.high.diff(a.low, unitField), unitField)); + }; + DateTime.prototype.isUTC = function () { + // A timezoneOffset of 0 indicates UTC time. + return !this.timezoneOffset; + }; + DateTime.prototype.getPrecision = function () { + var result = null; + if (this.year != null) { + result = DateTime.Unit.YEAR; + } + else { + return result; + } + if (this.month != null) { + result = DateTime.Unit.MONTH; + } + else { + return result; + } + if (this.day != null) { + result = DateTime.Unit.DAY; + } + else { + return result; + } + if (this.hour != null) { + result = DateTime.Unit.HOUR; + } + else { + return result; + } + if (this.minute != null) { + result = DateTime.Unit.MINUTE; + } + else { + return result; + } + if (this.second != null) { + result = DateTime.Unit.SECOND; + } + else { + return result; + } + if (this.millisecond != null) { + result = DateTime.Unit.MILLISECOND; + } + return result; + }; + DateTime.prototype.getPrecisionValue = function () { + return this.isTime() + ? TIME_PRECISION_VALUE_MAP.get(this.getPrecision()) + : DATETIME_PRECISION_VALUE_MAP.get(this.getPrecision()); + }; + DateTime.prototype.toLuxonDateTime = function () { + var _a, _b, _c, _d, _e, _f, _g; + var offsetMins = this.timezoneOffset != null + ? this.timezoneOffset * 60 + : new util_1.jsDate().getTimezoneOffset() * -1; + return luxon_1.DateTime.fromObject({ + year: (_a = this.year) !== null && _a !== void 0 ? _a : undefined, + month: (_b = this.month) !== null && _b !== void 0 ? _b : undefined, + day: (_c = this.day) !== null && _c !== void 0 ? _c : undefined, + hour: (_d = this.hour) !== null && _d !== void 0 ? _d : undefined, + minute: (_e = this.minute) !== null && _e !== void 0 ? _e : undefined, + second: (_f = this.second) !== null && _f !== void 0 ? _f : undefined, + millisecond: (_g = this.millisecond) !== null && _g !== void 0 ? _g : undefined, + zone: luxon_1.FixedOffsetZone.instance(offsetMins) + }); + }; + DateTime.prototype.toLuxonUncertainty = function () { + var low = this.toLuxonDateTime(); + var high = low.endOf(this.getPrecision()); + return new uncertainty_1.Uncertainty(low, high); + }; + DateTime.prototype.toJSDate = function (ignoreTimezone) { + if (ignoreTimezone === void 0) { ignoreTimezone = false; } + var luxonDT = this.toLuxonDateTime(); + // I don't know if anyone is using "ignoreTimezone" anymore (we aren't), but just in case + if (ignoreTimezone) { + var offset = new util_1.jsDate().getTimezoneOffset() * -1; + luxonDT = luxonDT.setZone(luxon_1.FixedOffsetZone.instance(offset), { keepLocalTime: true }); + } + return luxonDT.toJSDate(); + }; + DateTime.prototype.toJSON = function () { + return this.toString(); + }; + DateTime.prototype._pad = function (num) { + return String('0' + num).slice(-2); + }; + DateTime.prototype.toString = function () { + if (this.isTime()) { + return this.toStringTime(); + } + else { + return this.toStringDateTime(); + } + }; + DateTime.prototype.toStringTime = function () { + var str = ''; + if (this.hour != null) { + str += this._pad(this.hour); + if (this.minute != null) { + str += ':' + this._pad(this.minute); + if (this.second != null) { + str += ':' + this._pad(this.second); + if (this.millisecond != null) { + str += '.' + String('00' + this.millisecond).slice(-3); + } + } + } + } + return str; + }; + DateTime.prototype.toStringDateTime = function () { + var str = ''; + if (this.year != null) { + str += this.year; + if (this.month != null) { + str += '-' + this._pad(this.month); + if (this.day != null) { + str += '-' + this._pad(this.day); + if (this.hour != null) { + str += 'T' + this._pad(this.hour); + if (this.minute != null) { + str += ':' + this._pad(this.minute); + if (this.second != null) { + str += ':' + this._pad(this.second); + if (this.millisecond != null) { + str += '.' + String('00' + this.millisecond).slice(-3); + } + } + } + } + } + } + } + if (str.indexOf('T') !== -1 && this.timezoneOffset != null) { + str += this.timezoneOffset < 0 ? '-' : '+'; + var offsetHours = Math.floor(Math.abs(this.timezoneOffset)); + str += this._pad(offsetHours); + var offsetMin = (Math.abs(this.timezoneOffset) - offsetHours) * 60; + str += ':' + this._pad(offsetMin); + } + return str; + }; + DateTime.prototype.getDateTime = function () { + return this; + }; + DateTime.prototype.getDate = function () { + return new Date(this.year, this.month, this.day); + }; + DateTime.prototype.getTime = function () { + // Times no longer have timezoneOffets, so we must explicitly set it to null + return new DateTime(0, 1, 1, this.hour, this.minute, this.second, this.millisecond, null); + }; + DateTime.prototype.isTime = function () { + return this.year === 0 && this.month === 1 && this.day === 1; + }; + DateTime.prototype._implicitlyConvert = function (other) { + if (other != null && other.isDate) { + return other.getDateTime(); + } + return other; + }; + DateTime.prototype.reducedPrecision = function (unitField) { + if (unitField === void 0) { unitField = DateTime.Unit.MILLISECOND; } + var reduced = this.copy(); + if (unitField != null && unitField !== DateTime.Unit.MILLISECOND) { + var fieldIndex = DateTime.FIELDS.indexOf(unitField); + var fieldsToRemove = DateTime.FIELDS.slice(fieldIndex + 1); + for (var _i = 0, fieldsToRemove_1 = fieldsToRemove; _i < fieldsToRemove_1.length; _i++) { + var field = fieldsToRemove_1[_i]; + // @ts-ignore + reduced[field] = null; + } + } + return reduced; + }; + DateTime.Unit = { + YEAR: 'year', + MONTH: 'month', + WEEK: 'week', + DAY: 'day', + HOUR: 'hour', + MINUTE: 'minute', + SECOND: 'second', + MILLISECOND: 'millisecond' + }; + DateTime.FIELDS = [ + DateTime.Unit.YEAR, + DateTime.Unit.MONTH, + DateTime.Unit.DAY, + DateTime.Unit.HOUR, + DateTime.Unit.MINUTE, + DateTime.Unit.SECOND, + DateTime.Unit.MILLISECOND + ]; + return DateTime; +}(AbstractDate)); +exports.DateTime = DateTime; +var Date = /** @class */ (function (_super) { + __extends(Date, _super); + function Date(year, month, day) { + if (year === void 0) { year = null; } + if (month === void 0) { month = null; } + if (day === void 0) { day = null; } + return _super.call(this, year, month, day) || this; + } + Date.parse = function (string) { + if (string === null) { + return null; + } + var matches = /(\d{4})(-(\d{2}))?(-(\d{2}))?/.exec(string); + if (matches == null) { + return null; + } + var years = matches[1]; + var months = matches[3]; + var days = matches[5]; + if (!isValidDateStringFormat(string)) { + return null; + } + // convert args to integers + var args = [years, months, days].map(function (arg) { return (arg != null ? parseInt(arg) : arg); }); + // @ts-ignore + return new (Date.bind.apply(Date, __spreadArray([void 0], args, false)))(); + }; + Object.defineProperty(Date.prototype, "isDate", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Object.defineProperty(Date.prototype, "isDateTime", { + get: function () { + return false; + }, + enumerable: false, + configurable: true + }); + Date.prototype.copy = function () { + return new Date(this.year, this.month, this.day); + }; + Date.prototype.successor = function () { + if (this.day != null) { + return this.add(1, Date.Unit.DAY); + } + else if (this.month != null) { + return this.add(1, Date.Unit.MONTH); + } + else if (this.year != null) { + return this.add(1, Date.Unit.YEAR); + } + }; + Date.prototype.predecessor = function () { + if (this.day != null) { + return this.add(-1, Date.Unit.DAY); + } + else if (this.month != null) { + return this.add(-1, Date.Unit.MONTH); + } + else if (this.year != null) { + return this.add(-1, Date.Unit.YEAR); + } + }; + Date.prototype.differenceBetween = function (other, unitField) { + if (other != null && other.isDateTime) { + return this.getDateTime().differenceBetween(other, unitField); + } + if (other == null || !other.isDate) { + return null; + } + // According to CQL spec: + // * "Difference calculations are performed by truncating the datetime values at the next precision, + // and then performing the corresponding duration calculation on the truncated values." + var a = this.toLuxonUncertainty(); + var b = other.toLuxonUncertainty(); + // Truncate all dates at precision below specified unit + a.low = truncateLuxonDateTime(a.low, unitField); + a.high = truncateLuxonDateTime(a.high, unitField); + b.low = truncateLuxonDateTime(b.low, unitField); + b.high = truncateLuxonDateTime(b.high, unitField); + // Return the duration based on the normalize and truncated values + return new uncertainty_1.Uncertainty(wholeLuxonDuration(b.low.diff(a.high, unitField), unitField), wholeLuxonDuration(b.high.diff(a.low, unitField), unitField)); + }; + Date.prototype.durationBetween = function (other, unitField) { + if (other != null && other.isDateTime) { + return this.getDateTime().durationBetween(other, unitField); + } + if (other == null || !other.isDate) { + return null; + } + var a = this.toLuxonUncertainty(); + var b = other.toLuxonUncertainty(); + return new uncertainty_1.Uncertainty(wholeLuxonDuration(b.low.diff(a.high, unitField), unitField), wholeLuxonDuration(b.high.diff(a.low, unitField), unitField)); + }; + Date.prototype.getPrecision = function () { + var result = null; + if (this.year != null) { + result = Date.Unit.YEAR; + } + else { + return result; + } + if (this.month != null) { + result = Date.Unit.MONTH; + } + else { + return result; + } + if (this.day != null) { + result = Date.Unit.DAY; + } + else { + return result; + } + return result; + }; + Date.prototype.getPrecisionValue = function () { + return DATETIME_PRECISION_VALUE_MAP.get(this.getPrecision()); + }; + Date.prototype.toLuxonDateTime = function () { + var _a, _b, _c; + return luxon_1.DateTime.fromObject({ + year: (_a = this.year) !== null && _a !== void 0 ? _a : undefined, + month: (_b = this.month) !== null && _b !== void 0 ? _b : undefined, + day: (_c = this.day) !== null && _c !== void 0 ? _c : undefined, + zone: luxon_1.FixedOffsetZone.utcInstance + }); + }; + Date.prototype.toLuxonUncertainty = function () { + var low = this.toLuxonDateTime(); + var high = low.endOf(this.getPrecision()).startOf('day'); // Date type is always at T00:00:00.0 + return new uncertainty_1.Uncertainty(low, high); + }; + Date.prototype.toJSDate = function () { + var _a = [ + this.year, + this.month != null ? this.month - 1 : 0, + this.day != null ? this.day : 1 + ], y = _a[0], mo = _a[1], d = _a[2]; + return new util_1.jsDate(y, mo, d); + }; + Date.fromJSDate = function (date) { + if (date instanceof Date) { + return date; + } + return new Date(date.getFullYear(), date.getMonth() + 1, date.getDate()); + }; + Date.fromLuxonDateTime = function (luxonDT) { + if (luxonDT instanceof Date) { + return luxonDT; + } + return new Date(luxonDT.year, luxonDT.month, luxonDT.day); + }; + Date.prototype.toJSON = function () { + return this.toString(); + }; + Date.prototype.toString = function () { + var str = ''; + if (this.year != null) { + str += this.year.toString(); + if (this.month != null) { + str += '-' + this.month.toString().padStart(2, '0'); + if (this.day != null) { + str += '-' + this.day.toString().padStart(2, '0'); + } + } + } + return str; + }; + Date.prototype.getDateTime = function () { + // from the spec: the result will be a DateTime with the time components unspecified, + // except for the timezone offset, which will be set to the timezone offset of the evaluation + // request timestamp. (this last part is achieved by just not passing in timezone offset) + if (this.year != null && this.month != null && this.day != null) { + return new DateTime(this.year, this.month, this.day, null, null, null, null); + // from spec: no component may be specified at a precision below an unspecified precision. + // For example, hour may be null, but if it is, minute, second, and millisecond must all be null as well. + } + else { + return new DateTime(this.year, this.month, this.day); + } + }; + Date.prototype.reducedPrecision = function (unitField) { + if (unitField === void 0) { unitField = Date.Unit.DAY; } + var reduced = this.copy(); + if (unitField !== Date.Unit.DAY) { + var fieldIndex = Date.FIELDS.indexOf(unitField); + var fieldsToRemove = Date.FIELDS.slice(fieldIndex + 1); + for (var _i = 0, fieldsToRemove_2 = fieldsToRemove; _i < fieldsToRemove_2.length; _i++) { + var field = fieldsToRemove_2[_i]; + // @ts-ignore + reduced[field] = null; + } + } + return reduced; + }; + Date.Unit = { YEAR: 'year', MONTH: 'month', WEEK: 'week', DAY: 'day' }; + Date.FIELDS = [Date.Unit.YEAR, Date.Unit.MONTH, Date.Unit.DAY]; + return Date; +}(AbstractDate)); +exports.Date = Date; +// Require MIN/MAX here because math.js requires this file, and when we make this file require +// math.js before it exports DateTime and Date, it errors due to the circular dependency... +// const { MAX_DATETIME_VALUE, MIN_DATETIME_VALUE } = require('../util/math'); +exports.MIN_DATETIME_VALUE = DateTime.parse('0001-01-01T00:00:00.000'); +exports.MAX_DATETIME_VALUE = DateTime.parse('9999-12-31T23:59:59.999'); +exports.MIN_DATE_VALUE = Date.parse('0001-01-01'); +exports.MAX_DATE_VALUE = Date.parse('9999-12-31'); +exports.MIN_TIME_VALUE = (_a = DateTime.parse('0000-01-01T00:00:00.000')) === null || _a === void 0 ? void 0 : _a.getTime(); +exports.MAX_TIME_VALUE = (_b = DateTime.parse('0000-01-01T23:59:59.999')) === null || _b === void 0 ? void 0 : _b.getTime(); +var DATETIME_PRECISION_VALUE_MAP = (function () { + var dtpvMap = new Map(); + dtpvMap.set(DateTime.Unit.YEAR, 4); + dtpvMap.set(DateTime.Unit.MONTH, 6); + dtpvMap.set(DateTime.Unit.DAY, 8); + dtpvMap.set(DateTime.Unit.HOUR, 10); + dtpvMap.set(DateTime.Unit.MINUTE, 12); + dtpvMap.set(DateTime.Unit.SECOND, 14); + dtpvMap.set(DateTime.Unit.MILLISECOND, 17); + return dtpvMap; +})(); +var TIME_PRECISION_VALUE_MAP = (function () { + var tpvMap = new Map(); + tpvMap.set(DateTime.Unit.HOUR, 2); + tpvMap.set(DateTime.Unit.MINUTE, 4); + tpvMap.set(DateTime.Unit.SECOND, 6); + tpvMap.set(DateTime.Unit.MILLISECOND, 9); + return tpvMap; +})(); +function compareWithDefaultResult(a, b, defaultResult) { + // return false there is a type mismatch + if ((!a.isDate || !b.isDate) && (!a.isDateTime || !b.isDateTime)) { + return false; + } + // make a copy of other in the correct timezone offset if they don't match. + if (a.timezoneOffset !== b.timezoneOffset) { + b = b.convertToTimezoneOffset(a.timezoneOffset); + } + for (var _i = 0, _a = a.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + // if both have this precision defined + if (a[field] != null && b[field] != null) { + // For the purposes of comparison, seconds and milliseconds are combined + // as a single precision using a decimal, with decimal equality semantics + if (field === 'second') { + // NOTE: if millisecond is null it will calcualte like this anyway, but + // if millisecond is undefined, using it will result in NaN calculations + var aMillisecond = a['millisecond'] != null ? a['millisecond'] : 0; + var aSecondAndMillisecond = a[field] + aMillisecond / 1000; + var bMillisecond = b['millisecond'] != null ? b['millisecond'] : 0; + var bSecondAndMillisecond = b[field] + bMillisecond / 1000; + // second/millisecond is the most precise comparison, so we can directly return + return aSecondAndMillisecond === bSecondAndMillisecond; + } + // if they are different then return with false + if (a[field] !== b[field]) { + return false; + } + // if both dont have this precision, return true + } + else if (a[field] == null && b[field] == null) { + return true; + // otherwise they have inconclusive precision, return defaultResult + } + else { + return defaultResult; + } + } + // if we made it here, then all fields matched. + return true; +} +function daysInMonth(year, month) { + if (year == null || month == null) { + throw new Error('daysInMonth requires year and month as arguments'); + } + // Month is 1-indexed here because of the 0 day + return new util_1.jsDate(year, month, 0).getDate(); +} +function isValidDateStringFormat(string) { + if (typeof string !== 'string') { + return false; + } + var format = LENGTH_TO_DATE_FORMAT_MAP.get(string.length); + if (format == null) { + return false; + } + return luxon_1.DateTime.fromFormat(string, format).isValid; +} +function isValidDateTimeStringFormat(string) { + if (typeof string !== 'string') { + return false; + } + // Luxon doesn't support +hh offset, so change it to +hh:00 + if (/T[\d:.]*[+-]\d{2}$/.test(string)) { + string += ':00'; + } + var formats = LENGTH_TO_DATETIME_FORMATS_MAP.get(string.length); + if (formats == null) { + return false; + } + return formats.some(function (fmt) { return luxon_1.DateTime.fromFormat(string, fmt).isValid; }); +} +// Will return true if provided precision is unspecified or if +// precision is hours, minutes, seconds, or milliseconds +function isPrecisionUnspecifiedOrGreaterThanDay(precision) { + return precision == null || /^h|mi|s/.test(precision); +} + +},{"../util/util":105,"./uncertainty":63,"luxon":217}],58:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Exception = void 0; +var Exception = /** @class */ (function () { + function Exception(message, wrapped) { + this.message = message; + this.wrapped = wrapped; + } + return Exception; +}()); +exports.Exception = Exception; + +},{}],59:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Interval = void 0; +var uncertainty_1 = require("./uncertainty"); +var quantity_1 = require("./quantity"); +var logic_1 = require("./logic"); +var math_1 = require("../util/math"); +var cmp = __importStar(require("../util/comparison")); +var Interval = /** @class */ (function () { + function Interval(low, high, lowClosed, highClosed, defaultPointType // defaultPointType is used in the case that both endpoints are null + ) { + this.low = low; + this.high = high; + this.lowClosed = lowClosed; + this.highClosed = highClosed; + this.defaultPointType = defaultPointType; + this.lowClosed = lowClosed != null ? lowClosed : true; + this.highClosed = highClosed != null ? highClosed : true; + } + Object.defineProperty(Interval.prototype, "isInterval", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Object.defineProperty(Interval.prototype, "pointType", { + get: function () { + var pointType = null; + var point = this.low != null ? this.low : this.high; + if (point != null) { + if (typeof point === 'number') { + pointType = Number.isInteger(point) + ? '{urn:hl7-org:elm-types:r1}Integer' + : '{urn:hl7-org:elm-types:r1}Decimal'; + } + else if (point.isTime && point.isTime()) { + pointType = '{urn:hl7-org:elm-types:r1}Time'; + } + else if (point.isDate) { + pointType = '{urn:hl7-org:elm-types:r1}Date'; + } + else if (point.isDateTime) { + pointType = '{urn:hl7-org:elm-types:r1}DateTime'; + } + else if (point.isQuantity) { + pointType = '{urn:hl7-org:elm-types:r1}Quantity'; + } + } + if (pointType == null && this.defaultPointType != null) { + pointType = this.defaultPointType; + } + return pointType; + }, + enumerable: false, + configurable: true + }); + Interval.prototype.copy = function () { + var newLow = this.low; + var newHigh = this.high; + if (this.low != null && typeof this.low.copy === 'function') { + newLow = this.low.copy(); + } + if (this.high != null && typeof this.high.copy === 'function') { + newHigh = this.high.copy(); + } + return new Interval(newLow, newHigh, this.lowClosed, this.highClosed); + }; + Interval.prototype.contains = function (item, precision) { + // These first two checks ensure correct handling of edge case where an item equals the closed boundary + if (this.lowClosed && this.low != null && cmp.equals(this.low, item)) { + return true; + } + if (this.highClosed && this.high != null && cmp.equals(this.high, item)) { + return true; + } + if (item != null && item.isInterval) { + throw new Error('Argument to contains must be a point'); + } + var lowFn; + if (this.lowClosed && this.low == null) { + lowFn = function () { return true; }; + } + else if (this.lowClosed) { + lowFn = cmp.lessThanOrEquals; + } + else { + lowFn = cmp.lessThan; + } + var highFn; + if (this.highClosed && this.high == null) { + highFn = function () { return true; }; + } + else if (this.highClosed) { + highFn = cmp.greaterThanOrEquals; + } + else { + highFn = cmp.greaterThan; + } + return logic_1.ThreeValuedLogic.and(lowFn(this.low, item, precision), highFn(this.high, item, precision)); + }; + Interval.prototype.properlyIncludes = function (other, precision) { + if (other == null || !other.isInterval) { + throw new Error('Argument to properlyIncludes must be an interval'); + } + return logic_1.ThreeValuedLogic.and(this.includes(other, precision), logic_1.ThreeValuedLogic.not(other.includes(this, precision))); + }; + Interval.prototype.includes = function (other, precision) { + if (other == null || !other.isInterval) { + return this.contains(other, precision); + } + var a = this.toClosed(); + var b = other.toClosed(); + return logic_1.ThreeValuedLogic.and(cmp.lessThanOrEquals(a.low, b.low, precision), cmp.greaterThanOrEquals(a.high, b.high, precision)); + }; + Interval.prototype.includedIn = function (other, precision) { + // For the point overload, this operator is a synonym for the in operator + if (other == null || !other.isInterval) { + return this.contains(other, precision); + } + else { + return other.includes(this); + } + }; + Interval.prototype.overlaps = function (item, precision) { + var closed = this.toClosed(); + var _a = (function () { + if (item != null && item.isInterval) { + var itemClosed = item.toClosed(); + return [itemClosed.low, itemClosed.high]; + } + else { + return [item, item]; + } + })(), low = _a[0], high = _a[1]; + return logic_1.ThreeValuedLogic.and(cmp.lessThanOrEquals(closed.low, high, precision), cmp.greaterThanOrEquals(closed.high, low, precision)); + }; + Interval.prototype.overlapsAfter = function (item, precision) { + var closed = this.toClosed(); + var high = item != null && item.isInterval ? item.toClosed().high : item; + return logic_1.ThreeValuedLogic.and(cmp.lessThanOrEquals(closed.low, high, precision), cmp.greaterThan(closed.high, high, precision)); + }; + Interval.prototype.overlapsBefore = function (item, precision) { + var closed = this.toClosed(); + var low = item != null && item.isInterval ? item.toClosed().low : item; + return logic_1.ThreeValuedLogic.and(cmp.lessThan(closed.low, low, precision), cmp.greaterThanOrEquals(closed.high, low, precision)); + }; + Interval.prototype.union = function (other) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; + if (other == null || !other.isInterval) { + throw new Error('Argument to union must be an interval'); + } + // Note that interval union is only defined if the arguments overlap or meet. + if (this.overlaps(other) || this.meets(other)) { + var _l = [this.toClosed(), other.toClosed()], a = _l[0], b = _l[1]; + var l = void 0, lc = void 0; + if (cmp.lessThanOrEquals(a.low, b.low)) { + _a = [this.low, this.lowClosed], l = _a[0], lc = _a[1]; + } + else if (cmp.greaterThanOrEquals(a.low, b.low)) { + _b = [other.low, other.lowClosed], l = _b[0], lc = _b[1]; + } + else if (areNumeric(a.low, b.low)) { + _c = [lowestNumericUncertainty(a.low, b.low), true], l = _c[0], lc = _c[1]; + // TODO: Do we need to support quantities here? + } + else if (areDateTimes(a.low, b.low) && a.low.isMorePrecise(b.low)) { + _d = [other.low, other.lowClosed], l = _d[0], lc = _d[1]; + } + else { + _e = [this.low, this.lowClosed], l = _e[0], lc = _e[1]; + } + var h = void 0, hc = void 0; + if (cmp.greaterThanOrEquals(a.high, b.high)) { + _f = [this.high, this.highClosed], h = _f[0], hc = _f[1]; + } + else if (cmp.lessThanOrEquals(a.high, b.high)) { + _g = [other.high, other.highClosed], h = _g[0], hc = _g[1]; + } + else if (areNumeric(a.high, b.high)) { + _h = [highestNumericUncertainty(a.high, b.high), true], h = _h[0], hc = _h[1]; + // TODO: Do we need to support quantities here? + } + else if (areDateTimes(a.high, b.high) && a.high.isMorePrecise(b.high)) { + _j = [other.high, other.highClosed], h = _j[0], hc = _j[1]; + } + else { + _k = [this.high, this.highClosed], h = _k[0], hc = _k[1]; + } + return new Interval(l, h, lc, hc); + } + else { + return null; + } + }; + Interval.prototype.intersect = function (other) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; + if (other == null || !other.isInterval) { + throw new Error('Argument to union must be an interval'); + } + // Note that interval union is only defined if the arguments overlap. + if (this.overlaps(other)) { + var _l = [this.toClosed(), other.toClosed()], a = _l[0], b = _l[1]; + var l = void 0, lc = void 0; + if (cmp.greaterThanOrEquals(a.low, b.low)) { + _a = [this.low, this.lowClosed], l = _a[0], lc = _a[1]; + } + else if (cmp.lessThanOrEquals(a.low, b.low)) { + _b = [other.low, other.lowClosed], l = _b[0], lc = _b[1]; + } + else if (areNumeric(a.low, b.low)) { + _c = [highestNumericUncertainty(a.low, b.low), true], l = _c[0], lc = _c[1]; + // TODO: Do we need to support quantities here? + } + else if (areDateTimes(a.low, b.low) && b.low.isMorePrecise(a.low)) { + _d = [other.low, other.lowClosed], l = _d[0], lc = _d[1]; + } + else { + _e = [this.low, this.lowClosed], l = _e[0], lc = _e[1]; + } + var h = void 0, hc = void 0; + if (cmp.lessThanOrEquals(a.high, b.high)) { + _f = [this.high, this.highClosed], h = _f[0], hc = _f[1]; + } + else if (cmp.greaterThanOrEquals(a.high, b.high)) { + _g = [other.high, other.highClosed], h = _g[0], hc = _g[1]; + } + else if (areNumeric(a.high, b.high)) { + _h = [lowestNumericUncertainty(a.high, b.high), true], h = _h[0], hc = _h[1]; + // TODO: Do we need to support quantities here? + } + else if (areDateTimes(a.high, b.high) && b.high.isMorePrecise(a.high)) { + _j = [other.high, other.highClosed], h = _j[0], hc = _j[1]; + } + else { + _k = [this.high, this.highClosed], h = _k[0], hc = _k[1]; + } + return new Interval(l, h, lc, hc); + } + else { + return null; + } + }; + Interval.prototype.except = function (other) { + if (other === null) { + return null; + } + if (other == null || !other.isInterval) { + throw new Error('Argument to except must be an interval'); + } + var ol = this.overlaps(other); + if (ol === true) { + var olb = this.overlapsBefore(other); + var ola = this.overlapsAfter(other); + if (olb === true && ola === false) { + return new Interval(this.low, other.low, this.lowClosed, !other.lowClosed); + } + else if (ola === true && olb === false) { + return new Interval(other.high, this.high, !other.highClosed, this.highClosed); + } + else { + return null; + } + } + else if (ol === false) { + return this; + } + else { + // ol is null + return null; + } + }; + Interval.prototype.sameAs = function (other, precision) { + // This large if and else if block handles the scenarios where there is an open ended null + // If both lows or highs exists, it can be determined that intervals are not Same As + if ((this.low != null && + other.low != null && + this.high == null && + other.high != null && + !this.highClosed) || + (this.low != null && + other.low != null && + this.high != null && + other.high == null && + !other.highClosed) || + (this.low != null && + other.low != null && + this.high == null && + other.high == null && + !other.highClosed && + !this.highClosed)) { + if (typeof this.low === 'number') { + if (!(this.start() === other.start())) { + return false; + } + } + else { + if (!this.start().sameAs(other.start(), precision)) { + return false; + } + } + } + else if ((this.low != null && other.low == null && this.high != null && other.high != null) || + (this.low == null && other.low != null && this.high != null && other.high != null) || + (this.low == null && other.low == null && this.high != null && other.high != null)) { + if (typeof this.high === 'number') { + if (!(this.end() === other.end())) { + return false; + } + } + else { + if (!this.end().sameAs(other.end(), precision)) { + return false; + } + } + } + // Checks to see if any of the Intervals have a open, null boundary + if ((this.low == null && !this.lowClosed) || + (this.high == null && !this.highClosed) || + (other.low == null && !other.lowClosed) || + (other.high == null && !other.highClosed)) { + return null; + } + // For the special cases where @ is Interval[null,null] + if (this.lowClosed && this.low == null && this.highClosed && this.high == null) { + return other.lowClosed && other.low == null && other.highClosed && other.high == null; + } + // For the special case where Interval[...] same as Interval[null,null] should return false. + // This accounts for the inverse of the if statement above: where the second Interval is + // [null,null] and not the first Interval. + // The reason why this isn't caught below is due to how start() and end() work. + // There is no way to tell the datatype for MIN and MAX if both boundaries are null. + if (other.lowClosed && other.low == null && other.highClosed && other.high == null) { + return false; + } + if (typeof this.low === 'number') { + return this.start() === other.start() && this.end() === other.end(); + } + else { + return (this.start().sameAs(other.start(), precision) && this.end().sameAs(other.end(), precision)); + } + }; + Interval.prototype.sameOrBefore = function (other, precision) { + if (this.end() == null || other == null || other.start() == null) { + return null; + } + else { + return cmp.lessThanOrEquals(this.end(), other.start(), precision); + } + }; + Interval.prototype.sameOrAfter = function (other, precision) { + if (this.start() == null || other == null || other.end() == null) { + return null; + } + else { + return cmp.greaterThanOrEquals(this.start(), other.end(), precision); + } + }; + Interval.prototype.equals = function (other) { + if (other != null && other.isInterval) { + var _a = [this.toClosed(), other.toClosed()], a = _a[0], b = _a[1]; + return logic_1.ThreeValuedLogic.and(cmp.equals(a.low, b.low), cmp.equals(a.high, b.high)); + } + else { + return false; + } + }; + Interval.prototype.after = function (other, precision) { + var closed = this.toClosed(); + // Meets spec, but not 100% correct (e.g., (null, 5] after [6, 10] --> null) + // Simple way to fix it: and w/ not overlaps + if (other.toClosed) { + return cmp.greaterThan(closed.low, other.toClosed().high, precision); + } + else { + return cmp.greaterThan(closed.low, other, precision); + } + }; + Interval.prototype.before = function (other, precision) { + var closed = this.toClosed(); + // Meets spec, but not 100% correct (e.g., (null, 5] after [6, 10] --> null) + // Simple way to fix it: and w/ not overlaps + if (other.toClosed) { + return cmp.lessThan(closed.high, other.toClosed().low, precision); + } + else { + return cmp.lessThan(closed.high, other, precision); + } + }; + Interval.prototype.meets = function (other, precision) { + return logic_1.ThreeValuedLogic.or(this.meetsBefore(other, precision), this.meetsAfter(other, precision)); + }; + Interval.prototype.meetsAfter = function (other, precision) { + try { + if (precision != null && this.low != null && this.low.isDateTime) { + return this.toClosed().low.sameAs(other.toClosed().high != null ? other.toClosed().high.add(1, precision) : null, precision); + } + else { + return cmp.equals(this.toClosed().low, (0, math_1.successor)(other.toClosed().high)); + } + } + catch (error) { + return false; + } + }; + Interval.prototype.meetsBefore = function (other, precision) { + try { + if (precision != null && this.high != null && this.high.isDateTime) { + return this.toClosed().high.sameAs(other.toClosed().low != null ? other.toClosed().low.add(-1, precision) : null, precision); + } + else { + return cmp.equals(this.toClosed().high, (0, math_1.predecessor)(other.toClosed().low)); + } + } + catch (error) { + return false; + } + }; + Interval.prototype.start = function () { + if (this.low == null) { + if (this.lowClosed) { + return (0, math_1.minValueForInstance)(this.high); + } + else { + return this.low; + } + } + return this.toClosed().low; + }; + Interval.prototype.end = function () { + if (this.high == null) { + if (this.highClosed) { + return (0, math_1.maxValueForInstance)(this.low); + } + else { + return this.high; + } + } + return this.toClosed().high; + }; + Interval.prototype.starts = function (other, precision) { + var startEqual; + if (precision != null && this.low != null && this.low.isDateTime) { + startEqual = this.low.sameAs(other.low, precision); + } + else { + startEqual = cmp.equals(this.low, other.low); + } + var endLessThanOrEqual = cmp.lessThanOrEquals(this.high, other.high, precision); + return startEqual && endLessThanOrEqual; + }; + Interval.prototype.ends = function (other, precision) { + var endEqual; + var startGreaterThanOrEqual = cmp.greaterThanOrEquals(this.low, other.low, precision); + if (precision != null && (this.low != null ? this.low.isDateTime : undefined)) { + endEqual = this.high.sameAs(other.high, precision); + } + else { + endEqual = cmp.equals(this.high, other.high); + } + return startGreaterThanOrEqual && endEqual; + }; + Interval.prototype.width = function () { + if ((this.low != null && (this.low.isDateTime || this.low.isDate)) || + (this.high != null && (this.high.isDateTime || this.high.isDate))) { + throw new Error('Width of Date, DateTime, and Time intervals is not supported'); + } + var closed = this.toClosed(); + if ((closed.low != null && closed.low.isUncertainty) || + (closed.high != null && closed.high.isUncertainty)) { + return null; + } + else if (closed.low.isQuantity) { + if (closed.low.unit !== closed.high.unit) { + throw new Error('Cannot calculate width of Quantity Interval with different units'); + } + var lowValue = closed.low.value; + var highValue = closed.high.value; + var diff = Math.abs(highValue - lowValue); + diff = Math.round(diff * Math.pow(10, 8)) / Math.pow(10, 8); + return new quantity_1.Quantity(diff, closed.low.unit); + } + else { + // TODO: Fix precision to 8 decimals in other places that return numbers + var diff = Math.abs(closed.high - closed.low); + return Math.round(diff * Math.pow(10, 8)) / Math.pow(10, 8); + } + }; + Interval.prototype.size = function () { + var pointSize = this.getPointSize(); + if ((this.low != null && (this.low.isDateTime || this.low.isDate)) || + (this.high != null && (this.high.isDateTime || this.high.isDate))) { + throw new Error('Size of Date, DateTime, and Time intervals is not supported'); + } + var closed = this.toClosed(); + if ((closed.low != null && closed.low.isUncertainty) || + (closed.high != null && closed.high.isUncertainty)) { + return null; + } + else if (closed.low.isQuantity) { + if (closed.low.unit !== closed.high.unit) { + throw new Error('Cannot calculate size of Quantity Interval with different units'); + } + var lowValue = closed.low.value; + var highValue = closed.high.value; + var diff = Math.abs(highValue - lowValue) + pointSize.value; + Math.round(diff * Math.pow(10, 8)) / Math.pow(10, 8); + return new quantity_1.Quantity(diff, closed.low.unit); + } + else { + var diff = Math.abs(closed.high - closed.low) + pointSize.value; + return Math.round(diff * Math.pow(10, 8)) / Math.pow(10, 8); + } + }; + Interval.prototype.getPointSize = function () { + var pointSize; + if (this.low != null) { + if (this.low.isDateTime || this.low.isDate || this.low.isTime) { + pointSize = new quantity_1.Quantity(1, this.low.getPrecision()); + } + else if (this.low.isQuantity) { + pointSize = (0, quantity_1.doSubtraction)((0, math_1.successor)(this.low), this.low); + } + else { + pointSize = (0, math_1.successor)(this.low) - this.low; + } + } + else if (this.high != null) { + if (this.high.isDateTime || this.high.isDate || this.high.isTime) { + pointSize = new quantity_1.Quantity(1, this.high.getPrecision()); + } + else if (this.high.isQuantity) { + pointSize = (0, quantity_1.doSubtraction)((0, math_1.successor)(this.high), this.high); + } + else { + pointSize = (0, math_1.successor)(this.high) - this.high; + } + } + else { + throw new Error('Point type of intervals cannot be determined.'); + } + if (typeof pointSize === 'number') { + pointSize = new quantity_1.Quantity(pointSize, '1'); + } + return pointSize; + }; + Interval.prototype.toClosed = function () { + // Calculate the closed flags. Despite the name of this function, if a boundary is null open, + // we cannot close the boundary because that changes its meaning from "unknown" to "max/min value" + var lowClosed = this.lowClosed || this.low != null; + var highClosed = this.highClosed || this.high != null; + if (this.pointType != null) { + var low = void 0; + if (this.lowClosed && this.low == null) { + low = (0, math_1.minValueForType)(this.pointType); + } + else if (!this.lowClosed && this.low != null) { + low = (0, math_1.successor)(this.low); + } + else { + low = this.low; + } + var high = void 0; + if (this.highClosed && this.high == null) { + high = (0, math_1.maxValueForType)(this.pointType); + } + else if (!this.highClosed && this.high != null) { + high = (0, math_1.predecessor)(this.high); + } + else { + high = this.high; + } + if (low == null) { + low = new uncertainty_1.Uncertainty((0, math_1.minValueForType)(this.pointType), high); + } + if (high == null) { + high = new uncertainty_1.Uncertainty(low, (0, math_1.maxValueForType)(this.pointType)); + } + return new Interval(low, high, lowClosed, highClosed); + } + else { + return new Interval(this.low, this.high, lowClosed, highClosed); + } + }; + Interval.prototype.toString = function () { + var start = this.lowClosed ? '[' : '('; + var end = this.highClosed ? ']' : ')'; + return start + this.low.toString() + ', ' + this.high.toString() + end; + }; + return Interval; +}()); +exports.Interval = Interval; +function areDateTimes(x, y) { + return [x, y].every(function (z) { return z != null && z.isDateTime; }); +} +function areNumeric(x, y) { + return [x, y].every(function (z) { + return typeof z === 'number' || (z != null && z.isUncertainty && typeof z.low === 'number'); + }); +} +function lowestNumericUncertainty(x, y) { + if (x == null || !x.isUncertainty) { + x = new uncertainty_1.Uncertainty(x); + } + if (y == null || !y.isUncertainty) { + y = new uncertainty_1.Uncertainty(y); + } + var low = x.low < y.low ? x.low : y.low; + var high = x.high < y.high ? x.high : y.high; + if (low !== high) { + return new uncertainty_1.Uncertainty(low, high); + } + else { + return low; + } +} +function highestNumericUncertainty(x, y) { + if (x == null || !x.isUncertainty) { + x = new uncertainty_1.Uncertainty(x); + } + if (y == null || !y.isUncertainty) { + y = new uncertainty_1.Uncertainty(y); + } + var low = x.low > y.low ? x.low : y.low; + var high = x.high > y.high ? x.high : y.high; + if (low !== high) { + return new uncertainty_1.Uncertainty(low, high); + } + else { + return low; + } +} + +},{"../util/comparison":102,"../util/math":103,"./logic":60,"./quantity":61,"./uncertainty":63}],60:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ThreeValuedLogic = void 0; +var ThreeValuedLogic = /** @class */ (function () { + function ThreeValuedLogic() { + } + ThreeValuedLogic.and = function () { + var val = []; + for (var _i = 0; _i < arguments.length; _i++) { + val[_i] = arguments[_i]; + } + if (val.includes(false)) { + return false; + } + else if (val.includes(null)) { + return null; + } + else { + return true; + } + }; + ThreeValuedLogic.or = function () { + var val = []; + for (var _i = 0; _i < arguments.length; _i++) { + val[_i] = arguments[_i]; + } + if (val.includes(true)) { + return true; + } + else if (val.includes(null)) { + return null; + } + else { + return false; + } + }; + ThreeValuedLogic.xor = function () { + var val = []; + for (var _i = 0; _i < arguments.length; _i++) { + val[_i] = arguments[_i]; + } + if (val.includes(null)) { + return null; + } + else { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + return val.reduce(function (a, b) { return (!a ^ !b) === 1; }); + } + }; + ThreeValuedLogic.not = function (val) { + if (val != null) { + return !val; + } + else { + return null; + } + }; + return ThreeValuedLogic; +}()); +exports.ThreeValuedLogic = ThreeValuedLogic; + +},{}],61:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.doMultiplication = exports.doDivision = exports.doSubtraction = exports.doAddition = exports.parseQuantity = exports.Quantity = void 0; +var math_1 = require("../util/math"); +var units_1 = require("../util/units"); +var Quantity = /** @class */ (function () { + function Quantity(value, unit) { + this.value = value; + this.unit = unit; + if (this.value == null || isNaN(this.value)) { + throw new Error('Cannot create a quantity with an undefined value'); + } + else if (!(0, math_1.isValidDecimal)(this.value)) { + throw new Error('Cannot create a quantity with an invalid decimal value'); + } + // Attempt to parse the unit with UCUM. If it fails, throw a friendly error. + if (this.unit != null) { + var validation = (0, units_1.checkUnit)(this.unit); + if (!validation.valid) { + throw new Error(validation.message); + } + } + } + Object.defineProperty(Quantity.prototype, "isQuantity", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Quantity.prototype.clone = function () { + return new Quantity(this.value, this.unit); + }; + Quantity.prototype.toString = function () { + return "".concat(this.value, " '").concat(this.unit, "'"); + }; + Quantity.prototype.sameOrBefore = function (other) { + if (other != null && other.isQuantity) { + var otherVal = (0, units_1.convertUnit)(other.value, other.unit, this.unit); + if (otherVal == null) { + return null; + } + else { + return this.value <= otherVal; + } + } + }; + Quantity.prototype.sameOrAfter = function (other) { + if (other != null && other.isQuantity) { + var otherVal = (0, units_1.convertUnit)(other.value, other.unit, this.unit); + if (otherVal == null) { + return null; + } + else { + return this.value >= otherVal; + } + } + }; + Quantity.prototype.after = function (other) { + if (other != null && other.isQuantity) { + var otherVal = (0, units_1.convertUnit)(other.value, other.unit, this.unit); + if (otherVal == null) { + return null; + } + else { + return this.value > otherVal; + } + } + }; + Quantity.prototype.before = function (other) { + if (other != null && other.isQuantity) { + var otherVal = (0, units_1.convertUnit)(other.value, other.unit, this.unit); + if (otherVal == null) { + return null; + } + else { + return this.value < otherVal; + } + } + }; + Quantity.prototype.equals = function (other) { + if (other != null && other.isQuantity) { + if ((!this.unit && other.unit) || (this.unit && !other.unit)) { + return false; + } + else if (!this.unit && !other.unit) { + return this.value === other.value; + } + else { + var otherVal = (0, units_1.convertUnit)(other.value, other.unit, this.unit); + if (otherVal == null) { + return null; + } + else { + return (0, math_1.decimalAdjust)('round', this.value, -8) === otherVal; + } + } + } + }; + Quantity.prototype.convertUnit = function (toUnit) { + var value = (0, units_1.convertUnit)(this.value, this.unit, toUnit); + // Need to pass through constructor again to catch invalid units + return new Quantity(value, toUnit); + }; + Quantity.prototype.dividedBy = function (other) { + if (other == null || other === 0 || other.value === 0) { + return null; + } + else if (!other.isQuantity) { + // convert it to a quantity w/ unit 1 + other = new Quantity(other, '1'); + } + var _a = (0, units_1.normalizeUnitsWhenPossible)(this.value, this.unit, other.value, other.unit), val1 = _a[0], unit1 = _a[1], val2 = _a[2], unit2 = _a[3]; + var resultValue = val1 / val2; + var resultUnit = (0, units_1.getQuotientOfUnits)(unit1, unit2); + // Check for invalid unit or value + if (resultUnit == null || (0, math_1.overflowsOrUnderflows)(resultValue)) { + return null; + } + return new Quantity((0, math_1.decimalAdjust)('round', resultValue, -8), resultUnit); + }; + Quantity.prototype.multiplyBy = function (other) { + if (other == null) { + return null; + } + else if (!other.isQuantity) { + // convert it to a quantity w/ unit 1 + other = new Quantity(other, '1'); + } + var _a = (0, units_1.normalizeUnitsWhenPossible)(this.value, this.unit, other.value, other.unit), val1 = _a[0], unit1 = _a[1], val2 = _a[2], unit2 = _a[3]; + var resultValue = val1 * val2; + var resultUnit = (0, units_1.getProductOfUnits)(unit1, unit2); + // Check for invalid unit or value + if (resultUnit == null || (0, math_1.overflowsOrUnderflows)(resultValue)) { + return null; + } + return new Quantity((0, math_1.decimalAdjust)('round', resultValue, -8), resultUnit); + }; + return Quantity; +}()); +exports.Quantity = Quantity; +function parseQuantity(str) { + var components = /([+|-]?\d+\.?\d*)\s*('(.+)')?/.exec(str); + if (components != null && components[1] != null) { + var value = parseFloat(components[1]); + if (!(0, math_1.isValidDecimal)(value)) { + return null; + } + var unit = void 0; + if (components[3] != null) { + unit = components[3].trim(); + } + else { + unit = ''; + } + return new Quantity(value, unit); + } + else { + return null; + } +} +exports.parseQuantity = parseQuantity; +function doScaledAddition(a, b, scaleForB) { + if (a != null && a.isQuantity && b != null && b.isQuantity) { + var _a = (0, units_1.normalizeUnitsWhenPossible)(a.value, a.unit, b.value * scaleForB, b.unit), val1 = _a[0], unit1 = _a[1], val2 = _a[2], unit2 = _a[3]; + if (unit1 !== unit2) { + // not compatible units, so we can't do addition + return null; + } + var sum = val1 + val2; + if ((0, math_1.overflowsOrUnderflows)(sum)) { + return null; + } + return new Quantity(sum, unit1); + } + else if (a.copy && a.add) { + // Date / DateTime require a CQL time unit + var cqlUnitB = (0, units_1.convertToCQLDateUnit)(b.unit) || b.unit; + return a.copy().add(b.value * scaleForB, cqlUnitB); + } + else { + throw new Error('Unsupported argument types.'); + } +} +function doAddition(a, b) { + return doScaledAddition(a, b, 1); +} +exports.doAddition = doAddition; +function doSubtraction(a, b) { + return doScaledAddition(a, b, -1); +} +exports.doSubtraction = doSubtraction; +function doDivision(a, b) { + if (a != null && a.isQuantity) { + return a.dividedBy(b); + } +} +exports.doDivision = doDivision; +function doMultiplication(a, b) { + if (a != null && a.isQuantity) { + return a.multiplyBy(b); + } + else { + return b.multiplyBy(a); + } +} +exports.doMultiplication = doMultiplication; + +},{"../util/math":103,"../util/units":104}],62:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Ratio = void 0; +var Ratio = /** @class */ (function () { + function Ratio(numerator, denominator) { + this.numerator = numerator; + this.denominator = denominator; + if (numerator == null) { + throw new Error('Cannot create a ratio with an undefined numerator'); + } + if (denominator == null) { + throw new Error('Cannot create a ratio with an undefined denominator'); + } + } + Object.defineProperty(Ratio.prototype, "isRatio", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Ratio.prototype.clone = function () { + return new Ratio(this.numerator.clone(), this.denominator.clone()); + }; + Ratio.prototype.toString = function () { + return "".concat(this.numerator.toString(), " : ").concat(this.denominator.toString()); + }; + Ratio.prototype.equals = function (other) { + if (other != null && other.isRatio) { + var divided_this = this.numerator.dividedBy(this.denominator); + var divided_other = other.numerator.dividedBy(other.denominator); + return divided_this === null || divided_this === void 0 ? void 0 : divided_this.equals(divided_other); + } + else { + return false; + } + }; + Ratio.prototype.equivalent = function (other) { + var equal = this.equals(other); + return equal != null ? equal : false; + }; + return Ratio; +}()); +exports.Ratio = Ratio; + +},{}],63:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Uncertainty = void 0; +var logic_1 = require("./logic"); +var Uncertainty = /** @class */ (function () { + function Uncertainty(low, high) { + var _a; + if (low === void 0) { low = null; } + this.low = low; + this.high = high; + var gt = function (a, b) { + if (typeof a !== typeof b) { + // TODO: This should probably throw rather than return false. + // Uncertainties with different types probably shouldn't be supported. + return false; + } + if (typeof a.after === 'function') { + return a.after(b); + } + else { + return a > b; + } + }; + var isNonEnumerable = function (val) { + return val != null && (val.isCode || val.isConcept || val.isValueSet); + }; + if (typeof this.high === 'undefined') { + this.high = this.low; + } + if (isNonEnumerable(this.low) || isNonEnumerable(this.high)) { + this.low = this.high = null; + } + if (this.low != null && this.high != null && gt(this.low, this.high)) { + _a = [this.high, this.low], this.low = _a[0], this.high = _a[1]; + } + } + Uncertainty.from = function (obj) { + if (obj != null && obj.isUncertainty) { + return obj; + } + else { + return new Uncertainty(obj); + } + }; + Object.defineProperty(Uncertainty.prototype, "isUncertainty", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Uncertainty.prototype.copy = function () { + var newLow = this.low; + var newHigh = this.high; + if (typeof this.low.copy === 'function') { + newLow = this.low.copy(); + } + if (typeof this.high.copy === 'function') { + newHigh = this.high.copy(); + } + return new Uncertainty(newLow, newHigh); + }; + Uncertainty.prototype.isPoint = function () { + // Note: Can't use normal equality, as that fails for Javascript dates + // TODO: Fix after we don't need to support Javascript date uncertainties anymore + var lte = function (a, b) { + if (typeof a !== typeof b) { + return false; + } + if (typeof a.sameOrBefore === 'function') { + return a.sameOrBefore(b); + } + else { + return a <= b; + } + }; + var gte = function (a, b) { + if (typeof a !== typeof b) { + return false; + } + if (typeof a.sameOrBefore === 'function') { + return a.sameOrAfter(b); + } + else { + return a >= b; + } + }; + return (this.low != null && this.high != null && lte(this.low, this.high) && gte(this.low, this.high)); + }; + Uncertainty.prototype.equals = function (other) { + other = Uncertainty.from(other); + return logic_1.ThreeValuedLogic.not(logic_1.ThreeValuedLogic.or(this.lessThan(other), this.greaterThan(other))); + }; + Uncertainty.prototype.lessThan = function (other) { + var lt = function (a, b) { + if (typeof a !== typeof b) { + return false; + } + if (typeof a.before === 'function') { + return a.before(b); + } + else { + return a < b; + } + }; + other = Uncertainty.from(other); + var bestCase = this.low == null || other.high == null || lt(this.low, other.high); + var worstCase = this.high != null && other.low != null && lt(this.high, other.low); + if (bestCase === worstCase) { + return bestCase; + } + else { + return null; + } + }; + Uncertainty.prototype.greaterThan = function (other) { + return Uncertainty.from(other).lessThan(this); + }; + Uncertainty.prototype.lessThanOrEquals = function (other) { + return logic_1.ThreeValuedLogic.not(this.greaterThan(Uncertainty.from(other))); + }; + Uncertainty.prototype.greaterThanOrEquals = function (other) { + return logic_1.ThreeValuedLogic.not(this.lessThan(Uncertainty.from(other))); + }; + return Uncertainty; +}()); +exports.Uncertainty = Uncertainty; + +},{"./logic":60}],64:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.AnyTrue = exports.AllTrue = exports.PopulationVariance = exports.Variance = exports.PopulationStdDev = exports.GeometricMean = exports.Product = exports.StdDev = exports.Mode = exports.Median = exports.Avg = exports.Max = exports.Min = exports.Sum = exports.Count = void 0; +var expression_1 = require("./expression"); +var util_1 = require("../util/util"); +var datatypes_1 = require("../datatypes/datatypes"); +var exception_1 = require("../datatypes/exception"); +var comparison_1 = require("../util/comparison"); +var builder_1 = require("./builder"); +var AggregateExpression = /** @class */ (function (_super) { + __extends(AggregateExpression, _super); + function AggregateExpression(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + return _this; + } + return AggregateExpression; +}(expression_1.Expression)); +var Count = /** @class */ (function (_super) { + __extends(Count, _super); + function Count(json) { + return _super.call(this, json) || this; + } + Count.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if ((0, util_1.typeIsArray)(items)) { + return (0, util_1.removeNulls)(items).length; + } + return 0; + }; + return Count; +}(AggregateExpression)); +exports.Count = Count; +var Sum = /** @class */ (function (_super) { + __extends(Sum, _super); + function Sum(json) { + return _super.call(this, json) || this; + } + Sum.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + try { + items = processQuantities(items); + } + catch (e) { + return null; + } + if (items.length === 0) { + return null; + } + if (hasOnlyQuantities(items)) { + var values = getValuesFromQuantities(items); + var sum = values.reduce(function (x, y) { return x + y; }); + return new datatypes_1.Quantity(sum, items[0].unit); + } + else { + return items.reduce(function (x, y) { return x + y; }); + } + }; + return Sum; +}(AggregateExpression)); +exports.Sum = Sum; +var Min = /** @class */ (function (_super) { + __extends(Min, _super); + function Min(json) { + return _super.call(this, json) || this; + } + Min.prototype.exec = function (ctx) { + var list = this.source.execute(ctx); + if (list == null) { + return null; + } + var listWithoutNulls = (0, util_1.removeNulls)(list); + // Check for incompatible units and return null. We don't want to convert + // the units for Min/Max, so we throw away the converted array if it succeeds + try { + processQuantities(list); + } + catch (e) { + return null; + } + if (listWithoutNulls.length === 0) { + return null; + } + // We assume the list is an array of all the same type. + var minimum = listWithoutNulls[0]; + for (var _i = 0, listWithoutNulls_1 = listWithoutNulls; _i < listWithoutNulls_1.length; _i++) { + var element = listWithoutNulls_1[_i]; + if ((0, comparison_1.lessThan)(element, minimum)) { + minimum = element; + } + } + return minimum; + }; + return Min; +}(AggregateExpression)); +exports.Min = Min; +var Max = /** @class */ (function (_super) { + __extends(Max, _super); + function Max(json) { + return _super.call(this, json) || this; + } + Max.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (items == null) { + return null; + } + var listWithoutNulls = (0, util_1.removeNulls)(items); + // Check for incompatible units and return null. We don't want to convert + // the units for Min/Max, so we throw away the converted array if it succeeds + try { + processQuantities(items); + } + catch (e) { + return null; + } + if (listWithoutNulls.length === 0) { + return null; + } + // We assume the list is an array of all the same type. + var maximum = listWithoutNulls[0]; + for (var _i = 0, listWithoutNulls_2 = listWithoutNulls; _i < listWithoutNulls_2.length; _i++) { + var element = listWithoutNulls_2[_i]; + if ((0, comparison_1.greaterThan)(element, maximum)) { + maximum = element; + } + } + return maximum; + }; + return Max; +}(AggregateExpression)); +exports.Max = Max; +var Avg = /** @class */ (function (_super) { + __extends(Avg, _super); + function Avg(json) { + return _super.call(this, json) || this; + } + Avg.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + try { + items = processQuantities(items); + } + catch (e) { + return null; + } + if (items.length === 0) { + return null; + } + if (hasOnlyQuantities(items)) { + var values = getValuesFromQuantities(items); + var sum = values.reduce(function (x, y) { return x + y; }); + return new datatypes_1.Quantity(sum / values.length, items[0].unit); + } + else { + var sum = items.reduce(function (x, y) { return x + y; }); + return sum / items.length; + } + }; + return Avg; +}(AggregateExpression)); +exports.Avg = Avg; +var Median = /** @class */ (function (_super) { + __extends(Median, _super); + function Median(json) { + return _super.call(this, json) || this; + } + Median.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + if (items.length === 0) { + return null; + } + try { + items = processQuantities(items); + } + catch (e) { + return null; + } + if (!hasOnlyQuantities(items)) { + return medianOfNumbers(items); + } + var values = getValuesFromQuantities(items); + var median = medianOfNumbers(values); + return new datatypes_1.Quantity(median, items[0].unit); + }; + return Median; +}(AggregateExpression)); +exports.Median = Median; +var Mode = /** @class */ (function (_super) { + __extends(Mode, _super); + function Mode(json) { + return _super.call(this, json) || this; + } + Mode.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + if (items.length === 0) { + return null; + } + var filtered; + try { + filtered = processQuantities(items); + } + catch (e) { + return null; + } + if (hasOnlyQuantities(filtered)) { + var values = getValuesFromQuantities(filtered); + var mode = this.mode(values); + if (mode.length === 1) { + mode = mode[0]; + } + return new datatypes_1.Quantity(mode, items[0].unit); + } + else { + var mode = this.mode(filtered); + if (mode.length === 1) { + return mode[0]; + } + else { + return mode; + } + } + }; + Mode.prototype.mode = function (arr) { + var max = 0; + var counts = {}; + var results = []; + for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) { + var elem = arr_1[_i]; + var cnt = (counts[elem] = (counts[elem] != null ? counts[elem] : 0) + 1); + if (cnt === max && !results.includes(elem)) { + results.push(elem); + } + else if (cnt > max) { + results = [elem]; + max = cnt; + } + } + return results; + }; + return Mode; +}(AggregateExpression)); +exports.Mode = Mode; +var StdDev = /** @class */ (function (_super) { + __extends(StdDev, _super); + function StdDev(json) { + var _this = _super.call(this, json) || this; + _this.type = 'standard_deviation'; + return _this; + } + StdDev.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + try { + items = processQuantities(items); + } + catch (e) { + return null; + } + if (items.length === 0) { + return null; + } + if (hasOnlyQuantities(items)) { + var values = getValuesFromQuantities(items); + var stdDev = this.standardDeviation(values); + return new datatypes_1.Quantity(stdDev, items[0].unit); + } + else { + return this.standardDeviation(items); + } + }; + StdDev.prototype.standardDeviation = function (list) { + var val = this.stats(list); + if (val) { + return val[this.type]; + } + }; + StdDev.prototype.stats = function (list) { + var sum = list.reduce(function (x, y) { return x + y; }); + var mean = sum / list.length; + var sumOfSquares = 0; + for (var _i = 0, list_1 = list; _i < list_1.length; _i++) { + var sq = list_1[_i]; + sumOfSquares += Math.pow(sq - mean, 2); + } + var std_var = (1 / (list.length - 1)) * sumOfSquares; + var pop_var = (1 / list.length) * sumOfSquares; + var std_dev = Math.sqrt(std_var); + var pop_dev = Math.sqrt(pop_var); + return { + standard_variance: std_var, + population_variance: pop_var, + standard_deviation: std_dev, + population_deviation: pop_dev + }; + }; + return StdDev; +}(AggregateExpression)); +exports.StdDev = StdDev; +var Product = /** @class */ (function (_super) { + __extends(Product, _super); + function Product(json) { + return _super.call(this, json) || this; + } + Product.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + try { + items = processQuantities(items); + } + catch (e) { + return null; + } + if (items.length === 0) { + return null; + } + if (hasOnlyQuantities(items)) { + var values = getValuesFromQuantities(items); + var product = values.reduce(function (x, y) { return x * y; }); + // Units are not multiplied for the geometric product + return new datatypes_1.Quantity(product, items[0].unit); + } + else { + return items.reduce(function (x, y) { return x * y; }); + } + }; + return Product; +}(AggregateExpression)); +exports.Product = Product; +var GeometricMean = /** @class */ (function (_super) { + __extends(GeometricMean, _super); + function GeometricMean(json) { + return _super.call(this, json) || this; + } + GeometricMean.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + if (!(0, util_1.typeIsArray)(items)) { + return null; + } + try { + items = processQuantities(items); + } + catch (e) { + return null; + } + if (items.length === 0) { + return null; + } + if (hasOnlyQuantities(items)) { + var values = getValuesFromQuantities(items); + var product = values.reduce(function (x, y) { return x * y; }); + var geoMean = Math.pow(product, 1.0 / items.length); + return new datatypes_1.Quantity(geoMean, items[0].unit); + } + else { + var product = items.reduce(function (x, y) { return x * y; }); + return Math.pow(product, 1.0 / items.length); + } + }; + return GeometricMean; +}(AggregateExpression)); +exports.GeometricMean = GeometricMean; +var PopulationStdDev = /** @class */ (function (_super) { + __extends(PopulationStdDev, _super); + function PopulationStdDev(json) { + var _this = _super.call(this, json) || this; + _this.type = 'population_deviation'; + return _this; + } + return PopulationStdDev; +}(StdDev)); +exports.PopulationStdDev = PopulationStdDev; +var Variance = /** @class */ (function (_super) { + __extends(Variance, _super); + function Variance(json) { + var _this = _super.call(this, json) || this; + _this.type = 'standard_variance'; + return _this; + } + return Variance; +}(StdDev)); +exports.Variance = Variance; +var PopulationVariance = /** @class */ (function (_super) { + __extends(PopulationVariance, _super); + function PopulationVariance(json) { + var _this = _super.call(this, json) || this; + _this.type = 'population_variance'; + return _this; + } + return PopulationVariance; +}(StdDev)); +exports.PopulationVariance = PopulationVariance; +var AllTrue = /** @class */ (function (_super) { + __extends(AllTrue, _super); + function AllTrue(json) { + return _super.call(this, json) || this; + } + AllTrue.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + return (0, util_1.allTrue)((0, util_1.removeNulls)(items)); + }; + return AllTrue; +}(AggregateExpression)); +exports.AllTrue = AllTrue; +var AnyTrue = /** @class */ (function (_super) { + __extends(AnyTrue, _super); + function AnyTrue(json) { + return _super.call(this, json) || this; + } + AnyTrue.prototype.exec = function (ctx) { + var items = this.source.execute(ctx); + return (0, util_1.anyTrue)(items); + }; + return AnyTrue; +}(AggregateExpression)); +exports.AnyTrue = AnyTrue; +function processQuantities(values) { + var items = (0, util_1.removeNulls)(values); + if (hasOnlyQuantities(items)) { + return convertAllUnits(items); + } + else if (hasSomeQuantities(items)) { + throw new exception_1.Exception('Cannot perform aggregate operations on mixed values of Quantities and non Quantities'); + } + else { + return items; + } +} +function getValuesFromQuantities(quantities) { + return quantities.map(function (quantity) { return quantity.value; }); +} +function hasOnlyQuantities(arr) { + return arr.every(function (x) { return x.isQuantity; }); +} +function hasSomeQuantities(arr) { + return arr.some(function (x) { return x.isQuantity; }); +} +function convertAllUnits(arr) { + // convert all quantities in array to match the unit of the first item + return arr.map(function (q) { return q.convertUnit(arr[0].unit); }); +} +function medianOfNumbers(numbers) { + var items = (0, util_1.numerical_sort)(numbers, 'asc'); + if (items.length % 2 === 1) { + // Odd number of items + return items[(items.length - 1) / 2]; + } + else { + // Even number of items + return (items[items.length / 2 - 1] + items[items.length / 2]) / 2; + } +} + +},{"../datatypes/datatypes":56,"../datatypes/exception":58,"../util/comparison":102,"../util/util":105,"./builder":66,"./expression":72}],65:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Predecessor = exports.Successor = exports.MaxValue = exports.MinValue = exports.Power = exports.Log = exports.Exp = exports.Ln = exports.Round = exports.Negate = exports.Abs = exports.Truncate = exports.Floor = exports.Ceiling = exports.Modulo = exports.TruncatedDivide = exports.Divide = exports.Multiply = exports.Subtract = exports.Add = void 0; +var expression_1 = require("./expression"); +var MathUtil = __importStar(require("../util/math")); +var quantity_1 = require("../datatypes/quantity"); +var uncertainty_1 = require("../datatypes/uncertainty"); +var builder_1 = require("./builder"); +var Add = /** @class */ (function (_super) { + __extends(Add, _super); + function Add(json) { + return _super.call(this, json) || this; + } + Add.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var sum = args.reduce(function (x, y) { + if (x.isUncertainty && !y.isUncertainty) { + y = new uncertainty_1.Uncertainty(y, y); + } + else if (y.isUncertainty && !x.isUncertainty) { + x = new uncertainty_1.Uncertainty(x, x); + } + if (x.isQuantity || x.isDateTime || x.isDate || (x.isTime && x.isTime())) { + return (0, quantity_1.doAddition)(x, y); + } + else if (x.isUncertainty && y.isUncertainty) { + if (x.low.isQuantity || + x.low.isDateTime || + x.low.isDate || + (x.low.isTime && x.low.isTime())) { + return new uncertainty_1.Uncertainty((0, quantity_1.doAddition)(x.low, y.low), (0, quantity_1.doAddition)(x.high, y.high)); + } + else { + return new uncertainty_1.Uncertainty(x.low + y.low, x.high + y.high); + } + } + else { + return x + y; + } + }); + if (MathUtil.overflowsOrUnderflows(sum)) { + return null; + } + return sum; + }; + return Add; +}(expression_1.Expression)); +exports.Add = Add; +var Subtract = /** @class */ (function (_super) { + __extends(Subtract, _super); + function Subtract(json) { + return _super.call(this, json) || this; + } + Subtract.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var difference = args.reduce(function (x, y) { + if (x.isUncertainty && !y.isUncertainty) { + y = new uncertainty_1.Uncertainty(y, y); + } + else if (y.isUncertainty && !x.isUncertainty) { + x = new uncertainty_1.Uncertainty(x, x); + } + if (x.isQuantity || x.isDateTime || x.isDate) { + return (0, quantity_1.doSubtraction)(x, y); + } + else if (x.isUncertainty && y.isUncertainty) { + if (x.low.isQuantity || x.low.isDateTime || x.low.isDate) { + return new uncertainty_1.Uncertainty((0, quantity_1.doSubtraction)(x.low, y.high), (0, quantity_1.doSubtraction)(x.high, y.low)); + } + else { + return new uncertainty_1.Uncertainty(x.low - y.high, x.high - y.low); + } + } + else { + return x - y; + } + }); + if (MathUtil.overflowsOrUnderflows(difference)) { + return null; + } + return difference; + }; + return Subtract; +}(expression_1.Expression)); +exports.Subtract = Subtract; +var Multiply = /** @class */ (function (_super) { + __extends(Multiply, _super); + function Multiply(json) { + return _super.call(this, json) || this; + } + Multiply.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var product = args.reduce(function (x, y) { + if (x.isUncertainty && !y.isUncertainty) { + y = new uncertainty_1.Uncertainty(y, y); + } + else if (y.isUncertainty && !x.isUncertainty) { + x = new uncertainty_1.Uncertainty(x, x); + } + if (x.isQuantity || y.isQuantity) { + return (0, quantity_1.doMultiplication)(x, y); + } + else if (x.isUncertainty && y.isUncertainty) { + if (x.low.isQuantity) { + return new uncertainty_1.Uncertainty((0, quantity_1.doMultiplication)(x.low, y.low), (0, quantity_1.doMultiplication)(x.high, y.high)); + } + else { + return new uncertainty_1.Uncertainty(x.low * y.low, x.high * y.high); + } + } + else { + return x * y; + } + }); + if (MathUtil.overflowsOrUnderflows(product)) { + return null; + } + return product; + }; + return Multiply; +}(expression_1.Expression)); +exports.Multiply = Multiply; +var Divide = /** @class */ (function (_super) { + __extends(Divide, _super); + function Divide(json) { + return _super.call(this, json) || this; + } + Divide.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var quotient = args.reduce(function (x, y) { + if (x.isUncertainty && !y.isUncertainty) { + y = new uncertainty_1.Uncertainty(y, y); + } + else if (y.isUncertainty && !x.isUncertainty) { + x = new uncertainty_1.Uncertainty(x, x); + } + if (x.isQuantity) { + return (0, quantity_1.doDivision)(x, y); + } + else if (x.isUncertainty && y.isUncertainty) { + if (x.low.isQuantity) { + return new uncertainty_1.Uncertainty((0, quantity_1.doDivision)(x.low, y.high), (0, quantity_1.doDivision)(x.high, y.low)); + } + else { + return new uncertainty_1.Uncertainty(x.low / y.high, x.high / y.low); + } + } + else { + return x / y; + } + }); + // Note, anything divided by 0 is Infinity in Javascript, which will be + // considered as overflow by this check. + if (MathUtil.overflowsOrUnderflows(quotient)) { + return null; + } + return quotient; + }; + return Divide; +}(expression_1.Expression)); +exports.Divide = Divide; +var TruncatedDivide = /** @class */ (function (_super) { + __extends(TruncatedDivide, _super); + function TruncatedDivide(json) { + return _super.call(this, json) || this; + } + TruncatedDivide.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var quotient = args.reduce(function (x, y) { return x / y; }); + var truncatedQuotient = quotient >= 0 ? Math.floor(quotient) : Math.ceil(quotient); + if (MathUtil.overflowsOrUnderflows(truncatedQuotient)) { + return null; + } + return truncatedQuotient; + }; + return TruncatedDivide; +}(expression_1.Expression)); +exports.TruncatedDivide = TruncatedDivide; +var Modulo = /** @class */ (function (_super) { + __extends(Modulo, _super); + function Modulo(json) { + return _super.call(this, json) || this; + } + Modulo.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var modulo = args.reduce(function (x, y) { return x % y; }); + return MathUtil.decimalOrNull(modulo); + }; + return Modulo; +}(expression_1.Expression)); +exports.Modulo = Modulo; +var Ceiling = /** @class */ (function (_super) { + __extends(Ceiling, _super); + function Ceiling(json) { + return _super.call(this, json) || this; + } + Ceiling.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + return Math.ceil(arg); + }; + return Ceiling; +}(expression_1.Expression)); +exports.Ceiling = Ceiling; +var Floor = /** @class */ (function (_super) { + __extends(Floor, _super); + function Floor(json) { + return _super.call(this, json) || this; + } + Floor.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + return Math.floor(arg); + }; + return Floor; +}(expression_1.Expression)); +exports.Floor = Floor; +var Truncate = /** @class */ (function (_super) { + __extends(Truncate, _super); + function Truncate(json) { + return _super.call(this, json) || this; + } + Truncate.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + return arg >= 0 ? Math.floor(arg) : Math.ceil(arg); + }; + return Truncate; +}(expression_1.Expression)); +exports.Truncate = Truncate; +var Abs = /** @class */ (function (_super) { + __extends(Abs, _super); + function Abs(json) { + return _super.call(this, json) || this; + } + Abs.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + else if (arg.isQuantity) { + return new quantity_1.Quantity(Math.abs(arg.value), arg.unit); + } + else { + return Math.abs(arg); + } + }; + return Abs; +}(expression_1.Expression)); +exports.Abs = Abs; +var Negate = /** @class */ (function (_super) { + __extends(Negate, _super); + function Negate(json) { + return _super.call(this, json) || this; + } + Negate.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + else if (arg.isQuantity) { + return new quantity_1.Quantity(arg.value * -1, arg.unit); + } + else { + return arg * -1; + } + }; + return Negate; +}(expression_1.Expression)); +exports.Negate = Negate; +var Round = /** @class */ (function (_super) { + __extends(Round, _super); + function Round(json) { + var _this = _super.call(this, json) || this; + _this.precision = (0, builder_1.build)(json.precision); + return _this; + } + Round.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + var dec = this.precision != null ? this.precision.execute(ctx) : 0; + return Math.round(arg * Math.pow(10, dec)) / Math.pow(10, dec); + }; + return Round; +}(expression_1.Expression)); +exports.Round = Round; +var Ln = /** @class */ (function (_super) { + __extends(Ln, _super); + function Ln(json) { + return _super.call(this, json) || this; + } + Ln.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + var ln = Math.log(arg); + return MathUtil.decimalOrNull(ln); + }; + return Ln; +}(expression_1.Expression)); +exports.Ln = Ln; +var Exp = /** @class */ (function (_super) { + __extends(Exp, _super); + function Exp(json) { + return _super.call(this, json) || this; + } + Exp.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + var power = Math.exp(arg); + if (MathUtil.overflowsOrUnderflows(power)) { + return null; + } + return power; + }; + return Exp; +}(expression_1.Expression)); +exports.Exp = Exp; +var Log = /** @class */ (function (_super) { + __extends(Log, _super); + function Log(json) { + return _super.call(this, json) || this; + } + Log.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var log = args.reduce(function (x, y) { return Math.log(x) / Math.log(y); }); + return MathUtil.decimalOrNull(log); + }; + return Log; +}(expression_1.Expression)); +exports.Log = Log; +var Power = /** @class */ (function (_super) { + __extends(Power, _super); + function Power(json) { + return _super.call(this, json) || this; + } + Power.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args == null || args.some(function (x) { return x == null; })) { + return null; + } + var power = args.reduce(function (x, y) { return Math.pow(x, y); }); + if (MathUtil.overflowsOrUnderflows(power)) { + return null; + } + return power; + }; + return Power; +}(expression_1.Expression)); +exports.Power = Power; +var MinValue = /** @class */ (function (_super) { + __extends(MinValue, _super); + function MinValue(json) { + var _this = _super.call(this, json) || this; + _this.valueType = json.valueType; + return _this; + } + MinValue.prototype.exec = function (ctx) { + if (MinValue.MIN_VALUES[this.valueType]) { + if (this.valueType === '{urn:hl7-org:elm-types:r1}DateTime') { + var minDateTime = MinValue.MIN_VALUES[this.valueType].copy(); + minDateTime.timezoneOffset = ctx.getTimezoneOffset(); + return minDateTime; + } + else { + return MinValue.MIN_VALUES[this.valueType]; + } + } + else { + throw new Error("Minimum not supported for ".concat(this.valueType)); + } + }; + MinValue.MIN_VALUES = { + '{urn:hl7-org:elm-types:r1}Integer': MathUtil.MIN_INT_VALUE, + '{urn:hl7-org:elm-types:r1}Decimal': MathUtil.MIN_FLOAT_VALUE, + '{urn:hl7-org:elm-types:r1}DateTime': MathUtil.MIN_DATETIME_VALUE, + '{urn:hl7-org:elm-types:r1}Date': MathUtil.MIN_DATE_VALUE, + '{urn:hl7-org:elm-types:r1}Time': MathUtil.MIN_TIME_VALUE + }; + return MinValue; +}(expression_1.Expression)); +exports.MinValue = MinValue; +var MaxValue = /** @class */ (function (_super) { + __extends(MaxValue, _super); + function MaxValue(json) { + var _this = _super.call(this, json) || this; + _this.valueType = json.valueType; + return _this; + } + MaxValue.prototype.exec = function (ctx) { + if (MaxValue.MAX_VALUES[this.valueType] != null) { + if (this.valueType === '{urn:hl7-org:elm-types:r1}DateTime') { + var maxDateTime = MaxValue.MAX_VALUES[this.valueType].copy(); + maxDateTime.timezoneOffset = ctx.getTimezoneOffset(); + return maxDateTime; + } + else { + return MaxValue.MAX_VALUES[this.valueType]; + } + } + else { + throw new Error("Maximum not supported for ".concat(this.valueType)); + } + }; + MaxValue.MAX_VALUES = { + '{urn:hl7-org:elm-types:r1}Integer': MathUtil.MAX_INT_VALUE, + '{urn:hl7-org:elm-types:r1}Decimal': MathUtil.MAX_FLOAT_VALUE, + '{urn:hl7-org:elm-types:r1}DateTime': MathUtil.MAX_DATETIME_VALUE, + '{urn:hl7-org:elm-types:r1}Date': MathUtil.MAX_DATE_VALUE, + '{urn:hl7-org:elm-types:r1}Time': MathUtil.MAX_TIME_VALUE + }; + return MaxValue; +}(expression_1.Expression)); +exports.MaxValue = MaxValue; +var Successor = /** @class */ (function (_super) { + __extends(Successor, _super); + function Successor(json) { + return _super.call(this, json) || this; + } + Successor.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + var successor = null; + try { + // MathUtil.successor throws on overflow, and the exception is used in + // the logic for evaluating `meets`, so it can't be changed to just return null + successor = MathUtil.successor(arg); + } + catch (e) { + if (e instanceof MathUtil.OverFlowException) { + return null; + } + } + if (MathUtil.overflowsOrUnderflows(successor)) { + return null; + } + return successor; + }; + return Successor; +}(expression_1.Expression)); +exports.Successor = Successor; +var Predecessor = /** @class */ (function (_super) { + __extends(Predecessor, _super); + function Predecessor(json) { + return _super.call(this, json) || this; + } + Predecessor.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + var predecessor = null; + try { + // MathUtil.predecessor throws on underflow, and the exception is used in + // the logic for evaluating `meets`, so it can't be changed to just return null + predecessor = MathUtil.predecessor(arg); + } + catch (e) { + if (e instanceof MathUtil.OverFlowException) { + return null; + } + } + if (MathUtil.overflowsOrUnderflows(predecessor)) { + return null; + } + return predecessor; + }; + return Predecessor; +}(expression_1.Expression)); +exports.Predecessor = Predecessor; + +},{"../datatypes/quantity":61,"../datatypes/uncertainty":63,"../util/math":103,"./builder":66,"./expression":72}],66:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.build = void 0; +/* eslint-disable @typescript-eslint/ban-ts-comment */ +var E = __importStar(require("./expressions")); +var util_1 = require("../util/util"); +function build(json) { + if (json == null) { + return json; + } + if ((0, util_1.typeIsArray)(json)) { + return json.map(function (child) { return build(child); }); + } + if (json.type === 'FunctionRef') { + return new E.FunctionRef(json); + } + else if (json.type === 'Literal') { + return E.Literal.from(json); + } + else if (functionExists(json.type)) { + return constructByName(json.type, json); + } + else { + return null; + } +} +exports.build = build; +function functionExists(name) { + // @ts-ignore + return typeof E[name] === 'function'; +} +function constructByName(name, json) { + // @ts-ignore + return new E[name](json); +} + +},{"../util/util":105,"./expressions":73}],67:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.CalculateAgeAt = exports.CalculateAge = exports.Concept = exports.ConceptRef = exports.ConceptDef = exports.Code = exports.CodeRef = exports.CodeDef = exports.CodeSystemDef = exports.InValueSet = exports.AnyInValueSet = exports.ValueSetRef = exports.ValueSetDef = void 0; +var expression_1 = require("./expression"); +var dt = __importStar(require("../datatypes/datatypes")); +var builder_1 = require("./builder"); +var ValueSetDef = /** @class */ (function (_super) { + __extends(ValueSetDef, _super); + function ValueSetDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.id = json.id; + _this.version = json.version; + return _this; + } + //todo: code systems and versions + ValueSetDef.prototype.exec = function (ctx) { + var valueset = ctx.codeService.findValueSet(this.id, this.version) || new dt.ValueSet(this.id, this.version); + ctx.rootContext().set(this.name, valueset); + return valueset; + }; + return ValueSetDef; +}(expression_1.Expression)); +exports.ValueSetDef = ValueSetDef; +var ValueSetRef = /** @class */ (function (_super) { + __extends(ValueSetRef, _super); + function ValueSetRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.libraryName = json.libraryName; + return _this; + } + ValueSetRef.prototype.exec = function (ctx) { + // TODO: This calls the code service every time-- should be optimized + var valueset = ctx.getValueSet(this.name, this.libraryName); + if (valueset instanceof expression_1.Expression) { + valueset = valueset.execute(ctx); + } + return valueset; + }; + return ValueSetRef; +}(expression_1.Expression)); +exports.ValueSetRef = ValueSetRef; +var AnyInValueSet = /** @class */ (function (_super) { + __extends(AnyInValueSet, _super); + function AnyInValueSet(json) { + var _this = _super.call(this, json) || this; + _this.codes = (0, builder_1.build)(json.codes); + _this.valueset = new ValueSetRef(json.valueset); + return _this; + } + AnyInValueSet.prototype.exec = function (ctx) { + var valueset = this.valueset.execute(ctx); + // If the value set reference cannot be resolved, a run-time error is thrown. + if (valueset == null || !valueset.isValueSet) { + throw new Error('ValueSet must be provided to InValueSet function'); + } + var codes = this.codes.exec(ctx); + return codes != null && codes.some(function (code) { return valueset.hasMatch(code); }); + }; + return AnyInValueSet; +}(expression_1.Expression)); +exports.AnyInValueSet = AnyInValueSet; +var InValueSet = /** @class */ (function (_super) { + __extends(InValueSet, _super); + function InValueSet(json) { + var _this = _super.call(this, json) || this; + _this.code = (0, builder_1.build)(json.code); + _this.valueset = new ValueSetRef(json.valueset); + return _this; + } + InValueSet.prototype.exec = function (ctx) { + // If the code argument is null, the result is false + if (this.code == null) { + return false; + } + if (this.valueset == null) { + throw new Error('ValueSet must be provided to InValueSet function'); + } + var code = this.code.execute(ctx); + // spec indicates to return false if code is null, throw error if value set cannot be resolved + if (code == null) { + return false; + } + var valueset = this.valueset.execute(ctx); + if (valueset == null || !valueset.isValueSet) { + throw new Error('ValueSet must be provided to InValueSet function'); + } + // If there is a code and valueset return whether or not the valueset has the code + return valueset.hasMatch(code); + }; + return InValueSet; +}(expression_1.Expression)); +exports.InValueSet = InValueSet; +var CodeSystemDef = /** @class */ (function (_super) { + __extends(CodeSystemDef, _super); + function CodeSystemDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.id = json.id; + _this.version = json.version; + return _this; + } + CodeSystemDef.prototype.exec = function (_ctx) { + return new dt.CodeSystem(this.id, this.version); + }; + return CodeSystemDef; +}(expression_1.Expression)); +exports.CodeSystemDef = CodeSystemDef; +var CodeDef = /** @class */ (function (_super) { + __extends(CodeDef, _super); + function CodeDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.id = json.id; + _this.systemName = json.codeSystem.name; + _this.display = json.display; + return _this; + } + CodeDef.prototype.exec = function (ctx) { + var system = ctx.getCodeSystem(this.systemName).execute(ctx); + return new dt.Code(this.id, system.id, system.version, this.display); + }; + return CodeDef; +}(expression_1.Expression)); +exports.CodeDef = CodeDef; +var CodeRef = /** @class */ (function (_super) { + __extends(CodeRef, _super); + function CodeRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.library = json.libraryName; + return _this; + } + CodeRef.prototype.exec = function (ctx) { + ctx = this.library ? ctx.getLibraryContext(this.library) : ctx; + var codeDef = ctx.getCode(this.name); + return codeDef ? codeDef.execute(ctx) : undefined; + }; + return CodeRef; +}(expression_1.Expression)); +exports.CodeRef = CodeRef; +var Code = /** @class */ (function (_super) { + __extends(Code, _super); + function Code(json) { + var _this = _super.call(this, json) || this; + _this.code = json.code; + _this.systemName = json.system.name; + _this.version = json.version; + _this.display = json.display; + return _this; + } + Object.defineProperty(Code.prototype, "isCode", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Code.prototype.exec = function (ctx) { + var system = ctx.getCodeSystem(this.systemName) || {}; + return new dt.Code(this.code, system.id, this.version, this.display); + }; + return Code; +}(expression_1.Expression)); +exports.Code = Code; +var ConceptDef = /** @class */ (function (_super) { + __extends(ConceptDef, _super); + function ConceptDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.display = json.display; + _this.codes = json.code; + return _this; + } + ConceptDef.prototype.exec = function (ctx) { + var codes = this.codes.map(function (code) { + var codeDef = ctx.getCode(code.name); + return codeDef ? codeDef.execute(ctx) : undefined; + }); + return new dt.Concept(codes, this.display); + }; + return ConceptDef; +}(expression_1.Expression)); +exports.ConceptDef = ConceptDef; +var ConceptRef = /** @class */ (function (_super) { + __extends(ConceptRef, _super); + function ConceptRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + return _this; + } + ConceptRef.prototype.exec = function (ctx) { + var conceptDef = ctx.getConcept(this.name); + return conceptDef ? conceptDef.execute(ctx) : undefined; + }; + return ConceptRef; +}(expression_1.Expression)); +exports.ConceptRef = ConceptRef; +var Concept = /** @class */ (function (_super) { + __extends(Concept, _super); + function Concept(json) { + var _this = _super.call(this, json) || this; + _this.codes = json.code; + _this.display = json.display; + return _this; + } + Object.defineProperty(Concept.prototype, "isConcept", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Concept.prototype.toCode = function (ctx, code) { + var system = ctx.getCodeSystem(code.system.name) || {}; + return new dt.Code(code.code, system.id, code.version, code.display); + }; + Concept.prototype.exec = function (ctx) { + var _this = this; + var codes = this.codes.map(function (code) { return _this.toCode(ctx, code); }); + return new dt.Concept(codes, this.display); + }; + return Concept; +}(expression_1.Expression)); +exports.Concept = Concept; +var CalculateAge = /** @class */ (function (_super) { + __extends(CalculateAge, _super); + function CalculateAge(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + CalculateAge.prototype.exec = function (ctx) { + var date1 = this.execArgs(ctx); + var date2 = dt.DateTime.fromJSDate(ctx.getExecutionDateTime()); + var result = date1 != null ? date1.durationBetween(date2, this.precision.toLowerCase()) : undefined; + if (result != null && result.isPoint()) { + return result.low; + } + else { + return result; + } + }; + return CalculateAge; +}(expression_1.Expression)); +exports.CalculateAge = CalculateAge; +var CalculateAgeAt = /** @class */ (function (_super) { + __extends(CalculateAgeAt, _super); + function CalculateAgeAt(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + CalculateAgeAt.prototype.exec = function (ctx) { + // eslint-disable-next-line prefer-const + var _a = this.execArgs(ctx), date1 = _a[0], date2 = _a[1]; + if (date1 != null && date2 != null) { + // date1 is the birthdate, convert it to date if date2 is a date (to support ignoring time) + if (date2.isDate && date1.isDateTime) { + date1 = date1.getDate(); + } + var result = date1.durationBetween(date2, this.precision.toLowerCase()); + if (result != null && result.isPoint()) { + return result.low; + } + else { + return result; + } + } + return null; + }; + return CalculateAgeAt; +}(expression_1.Expression)); +exports.CalculateAgeAt = CalculateAgeAt; + +},{"../datatypes/datatypes":56,"./builder":66,"./expression":72}],68:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.GreaterOrEqual = exports.Greater = exports.LessOrEqual = exports.Less = void 0; +var expression_1 = require("./expression"); +var datatypes_1 = require("../datatypes/datatypes"); +// Equal is completely handled by overloaded#Equal +// NotEqual is completely handled by overloaded#Equal +var Less = /** @class */ (function (_super) { + __extends(Less, _super); + function Less(json) { + return _super.call(this, json) || this; + } + Less.prototype.exec = function (ctx) { + var args = this.execArgs(ctx).map(function (x) { return datatypes_1.Uncertainty.from(x); }); + if (args[0] == null || args[1] == null) { + return null; + } + return args[0].lessThan(args[1]); + }; + return Less; +}(expression_1.Expression)); +exports.Less = Less; +var LessOrEqual = /** @class */ (function (_super) { + __extends(LessOrEqual, _super); + function LessOrEqual(json) { + return _super.call(this, json) || this; + } + LessOrEqual.prototype.exec = function (ctx) { + var args = this.execArgs(ctx).map(function (x) { return datatypes_1.Uncertainty.from(x); }); + if (args[0] == null || args[1] == null) { + return null; + } + return args[0].lessThanOrEquals(args[1]); + }; + return LessOrEqual; +}(expression_1.Expression)); +exports.LessOrEqual = LessOrEqual; +var Greater = /** @class */ (function (_super) { + __extends(Greater, _super); + function Greater(json) { + return _super.call(this, json) || this; + } + Greater.prototype.exec = function (ctx) { + var args = this.execArgs(ctx).map(function (x) { return datatypes_1.Uncertainty.from(x); }); + if (args[0] == null || args[1] == null) { + return null; + } + return args[0].greaterThan(args[1]); + }; + return Greater; +}(expression_1.Expression)); +exports.Greater = Greater; +var GreaterOrEqual = /** @class */ (function (_super) { + __extends(GreaterOrEqual, _super); + function GreaterOrEqual(json) { + return _super.call(this, json) || this; + } + GreaterOrEqual.prototype.exec = function (ctx) { + var args = this.execArgs(ctx).map(function (x) { return datatypes_1.Uncertainty.from(x); }); + if (args[0] == null || args[1] == null) { + return null; + } + return args[0].greaterThanOrEquals(args[1]); + }; + return GreaterOrEqual; +}(expression_1.Expression)); +exports.GreaterOrEqual = GreaterOrEqual; + +},{"../datatypes/datatypes":56,"./expression":72}],69:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Case = exports.CaseItem = exports.If = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var comparison_1 = require("../util/comparison"); +// TODO: Spec lists "Conditional", but it's "If" in the XSD +var If = /** @class */ (function (_super) { + __extends(If, _super); + function If(json) { + var _this = _super.call(this, json) || this; + _this.condition = (0, builder_1.build)(json.condition); + _this.th = (0, builder_1.build)(json.then); + _this.els = (0, builder_1.build)(json.else); + return _this; + } + If.prototype.exec = function (ctx) { + if (this.condition.execute(ctx)) { + return this.th.execute(ctx); + } + else { + return this.els.execute(ctx); + } + }; + return If; +}(expression_1.Expression)); +exports.If = If; +var CaseItem = /** @class */ (function () { + function CaseItem(json) { + this.when = (0, builder_1.build)(json.when); + this.then = (0, builder_1.build)(json.then); + } + return CaseItem; +}()); +exports.CaseItem = CaseItem; +var Case = /** @class */ (function (_super) { + __extends(Case, _super); + function Case(json) { + var _this = _super.call(this, json) || this; + _this.comparand = (0, builder_1.build)(json.comparand); + _this.caseItems = json.caseItem.map(function (ci) { return new CaseItem(ci); }); + _this.els = (0, builder_1.build)(json.else); + return _this; + } + Case.prototype.exec = function (ctx) { + if (this.comparand) { + return this.exec_selected(ctx); + } + else { + return this.exec_standard(ctx); + } + }; + Case.prototype.exec_selected = function (ctx) { + var val = this.comparand.execute(ctx); + for (var _i = 0, _a = this.caseItems; _i < _a.length; _i++) { + var ci = _a[_i]; + if ((0, comparison_1.equals)(ci.when.execute(ctx), val)) { + return ci.then.execute(ctx); + } + } + return this.els.execute(ctx); + }; + Case.prototype.exec_standard = function (ctx) { + for (var _i = 0, _a = this.caseItems; _i < _a.length; _i++) { + var ci = _a[_i]; + if (ci.when.execute(ctx)) { + return ci.then.execute(ctx); + } + } + return this.els.execute(ctx); + }; + return Case; +}(expression_1.Expression)); +exports.Case = Case; + +},{"../util/comparison":102,"./builder":66,"./expression":72}],70:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.DurationBetween = exports.DifferenceBetween = exports.doBefore = exports.doAfter = exports.TimezoneOffsetFrom = exports.TimeFrom = exports.DateFrom = exports.DateTimeComponentFrom = exports.TimeOfDay = exports.Now = exports.Today = exports.Time = exports.Date = exports.DateTime = void 0; +/* eslint-disable @typescript-eslint/ban-ts-comment */ +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var literal_1 = require("./literal"); +var DT = __importStar(require("../datatypes/datatypes")); +var DateTime = /** @class */ (function (_super) { + __extends(DateTime, _super); + function DateTime(json) { + var _this = _super.call(this, json) || this; + _this.json = json; + return _this; + } + DateTime.prototype.exec = function (ctx) { + var _a; + var _this = this; + for (var _i = 0, _b = DateTime.PROPERTIES; _i < _b.length; _i++) { + var property = _b[_i]; + // if json does not contain 'timezoneOffset' set it to the executionDateTime from the context + if (this.json[property] != null) { + // @ts-ignore + this[property] = (0, builder_1.build)(this.json[property]); + } + else if (property === 'timezoneOffset' && ctx.getTimezoneOffset() != null) { + // @ts-ignore + this[property] = literal_1.Literal.from({ + type: 'Literal', + value: ctx.getTimezoneOffset(), + valueType: '{urn:hl7-org:elm-types:r1}Integer' + }); + } + } + // @ts-ignore + var args = DateTime.PROPERTIES.map(function (p) { return (_this[p] != null ? _this[p].execute(ctx) : undefined); }); + return new ((_a = DT.DateTime).bind.apply(_a, __spreadArray([void 0], args, false)))(); + }; + DateTime.PROPERTIES = [ + 'year', + 'month', + 'day', + 'hour', + 'minute', + 'second', + 'millisecond', + 'timezoneOffset' + ]; + return DateTime; +}(expression_1.Expression)); +exports.DateTime = DateTime; +var Date = /** @class */ (function (_super) { + __extends(Date, _super); + function Date(json) { + var _this = _super.call(this, json) || this; + _this.json = json; + return _this; + } + Date.prototype.exec = function (ctx) { + var _a; + var _this = this; + for (var _i = 0, _b = Date.PROPERTIES; _i < _b.length; _i++) { + var property = _b[_i]; + if (this.json[property] != null) { + // @ts-ignore + this[property] = (0, builder_1.build)(this.json[property]); + } + } + // @ts-ignore + var args = Date.PROPERTIES.map(function (p) { return (_this[p] != null ? _this[p].execute(ctx) : undefined); }); + return new ((_a = DT.Date).bind.apply(_a, __spreadArray([void 0], args, false)))(); + }; + Date.PROPERTIES = ['year', 'month', 'day']; + return Date; +}(expression_1.Expression)); +exports.Date = Date; +var Time = /** @class */ (function (_super) { + __extends(Time, _super); + function Time(json) { + var _this = _super.call(this, json) || this; + for (var _i = 0, _a = Time.PROPERTIES; _i < _a.length; _i++) { + var property = _a[_i]; + if (json[property] != null) { + // @ts-ignore + _this[property] = (0, builder_1.build)(json[property]); + } + } + return _this; + } + Time.prototype.exec = function (ctx) { + var _a; + var _this = this; + // @ts-ignore + var args = Time.PROPERTIES.map(function (p) { return (_this[p] != null ? _this[p].execute(ctx) : undefined); }); + return new ((_a = DT.DateTime).bind.apply(_a, __spreadArray([void 0, 0, 1, 1], args, false)))().getTime(); + }; + Time.PROPERTIES = ['hour', 'minute', 'second', 'millisecond']; + return Time; +}(expression_1.Expression)); +exports.Time = Time; +var Today = /** @class */ (function (_super) { + __extends(Today, _super); + function Today(json) { + return _super.call(this, json) || this; + } + Today.prototype.exec = function (ctx) { + return ctx.getExecutionDateTime().getDate(); + }; + return Today; +}(expression_1.Expression)); +exports.Today = Today; +var Now = /** @class */ (function (_super) { + __extends(Now, _super); + function Now(json) { + return _super.call(this, json) || this; + } + Now.prototype.exec = function (ctx) { + return ctx.getExecutionDateTime(); + }; + return Now; +}(expression_1.Expression)); +exports.Now = Now; +var TimeOfDay = /** @class */ (function (_super) { + __extends(TimeOfDay, _super); + function TimeOfDay(json) { + return _super.call(this, json) || this; + } + TimeOfDay.prototype.exec = function (ctx) { + return ctx.getExecutionDateTime().getTime(); + }; + return TimeOfDay; +}(expression_1.Expression)); +exports.TimeOfDay = TimeOfDay; +var DateTimeComponentFrom = /** @class */ (function (_super) { + __extends(DateTimeComponentFrom, _super); + function DateTimeComponentFrom(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + DateTimeComponentFrom.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return arg[this.precision.toLowerCase()]; + } + else { + return null; + } + }; + return DateTimeComponentFrom; +}(expression_1.Expression)); +exports.DateTimeComponentFrom = DateTimeComponentFrom; +var DateFrom = /** @class */ (function (_super) { + __extends(DateFrom, _super); + function DateFrom(json) { + return _super.call(this, json) || this; + } + DateFrom.prototype.exec = function (ctx) { + var date = this.execArgs(ctx); + if (date != null) { + return date.getDate(); + } + else { + return null; + } + }; + return DateFrom; +}(expression_1.Expression)); +exports.DateFrom = DateFrom; +var TimeFrom = /** @class */ (function (_super) { + __extends(TimeFrom, _super); + function TimeFrom(json) { + return _super.call(this, json) || this; + } + TimeFrom.prototype.exec = function (ctx) { + var date = this.execArgs(ctx); + if (date != null) { + return date.getTime(); + } + else { + return null; + } + }; + return TimeFrom; +}(expression_1.Expression)); +exports.TimeFrom = TimeFrom; +var TimezoneOffsetFrom = /** @class */ (function (_super) { + __extends(TimezoneOffsetFrom, _super); + function TimezoneOffsetFrom(json) { + return _super.call(this, json) || this; + } + TimezoneOffsetFrom.prototype.exec = function (ctx) { + var date = this.execArgs(ctx); + if (date != null) { + return date.timezoneOffset; + } + else { + return null; + } + }; + return TimezoneOffsetFrom; +}(expression_1.Expression)); +exports.TimezoneOffsetFrom = TimezoneOffsetFrom; +// Delegated to by overloaded#After +function doAfter(a, b, precision) { + return a.after(b, precision); +} +exports.doAfter = doAfter; +// Delegated to by overloaded#Before +function doBefore(a, b, precision) { + return a.before(b, precision); +} +exports.doBefore = doBefore; +var DifferenceBetween = /** @class */ (function (_super) { + __extends(DifferenceBetween, _super); + function DifferenceBetween(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + DifferenceBetween.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + // Check to make sure args exist and that they have differenceBetween functions so that they can be compared to one another + if (args[0] == null || + args[1] == null || + typeof args[0].differenceBetween !== 'function' || + typeof args[1].differenceBetween !== 'function') { + return null; + } + var result = args[0].differenceBetween(args[1], this.precision != null ? this.precision.toLowerCase() : undefined); + if (result != null && result.isPoint()) { + return result.low; + } + else { + return result; + } + }; + return DifferenceBetween; +}(expression_1.Expression)); +exports.DifferenceBetween = DifferenceBetween; +var DurationBetween = /** @class */ (function (_super) { + __extends(DurationBetween, _super); + function DurationBetween(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + DurationBetween.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + // Check to make sure args exist and that they have durationBetween functions so that they can be compared to one another + if (args[0] == null || + args[1] == null || + typeof args[0].durationBetween !== 'function' || + typeof args[1].durationBetween !== 'function') { + return null; + } + var result = args[0].durationBetween(args[1], this.precision != null ? this.precision.toLowerCase() : undefined); + if (result != null && result.isPoint()) { + return result.low; + } + else { + return result; + } + }; + return DurationBetween; +}(expression_1.Expression)); +exports.DurationBetween = DurationBetween; + +},{"../datatypes/datatypes":56,"./builder":66,"./expression":72,"./literal":79}],71:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.VersionedIdentifier = exports.IncludeDef = exports.UsingDef = void 0; +var expression_1 = require("./expression"); +var UsingDef = /** @class */ (function (_super) { + __extends(UsingDef, _super); + function UsingDef() { + return _super !== null && _super.apply(this, arguments) || this; + } + return UsingDef; +}(expression_1.UnimplementedExpression)); +exports.UsingDef = UsingDef; +var IncludeDef = /** @class */ (function (_super) { + __extends(IncludeDef, _super); + function IncludeDef() { + return _super !== null && _super.apply(this, arguments) || this; + } + return IncludeDef; +}(expression_1.UnimplementedExpression)); +exports.IncludeDef = IncludeDef; +var VersionedIdentifier = /** @class */ (function (_super) { + __extends(VersionedIdentifier, _super); + function VersionedIdentifier() { + return _super !== null && _super.apply(this, arguments) || this; + } + return VersionedIdentifier; +}(expression_1.UnimplementedExpression)); +exports.VersionedIdentifier = VersionedIdentifier; + +},{"./expression":72}],72:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.UnimplementedExpression = exports.Expression = void 0; +var util_1 = require("../util/util"); +var builder_1 = require("./builder"); +var Expression = /** @class */ (function () { + function Expression(json) { + if (json.operand != null) { + var op = (0, builder_1.build)(json.operand); + if ((0, util_1.typeIsArray)(json.operand)) { + this.args = op; + } + else { + this.arg = op; + } + } + if (json.localId != null) { + this.localId = json.localId; + } + } + Expression.prototype.execute = function (ctx) { + if (this.localId != null) { + // Store the localId and result on the root context of this library + var execValue = this.exec(ctx); + ctx.rootContext().setLocalIdWithResult(this.localId, execValue); + return execValue; + } + else { + return this.exec(ctx); + } + }; + Expression.prototype.exec = function (_ctx) { + return this; + }; + Expression.prototype.execArgs = function (ctx) { + if (this.args != null) { + return this.args.map(function (arg) { return arg.execute(ctx); }); + } + else if (this.arg != null) { + return this.arg.execute(ctx); + } + else { + return null; + } + }; + return Expression; +}()); +exports.Expression = Expression; +var UnimplementedExpression = /** @class */ (function (_super) { + __extends(UnimplementedExpression, _super); + function UnimplementedExpression(json) { + var _this = _super.call(this, json) || this; + _this.json = json; + return _this; + } + UnimplementedExpression.prototype.exec = function (_ctx) { + throw new Error("Unimplemented Expression: ".concat(this.json.type)); + }; + return UnimplementedExpression; +}(Expression)); +exports.UnimplementedExpression = UnimplementedExpression; + +},{"../util/util":105,"./builder":66}],73:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.doContains = exports.doExcept = exports.doIncludes = exports.doIntersect = exports.doProperIncludes = exports.doAfter = exports.doUnion = exports.doBefore = void 0; +__exportStar(require("./expression"), exports); +__exportStar(require("./aggregate"), exports); +__exportStar(require("./arithmetic"), exports); +__exportStar(require("./clinical"), exports); +__exportStar(require("./comparison"), exports); +__exportStar(require("./conditional"), exports); +__exportStar(require("./datetime"), exports); +__exportStar(require("./declaration"), exports); +__exportStar(require("./external"), exports); +__exportStar(require("./instance"), exports); +__exportStar(require("./interval"), exports); +__exportStar(require("./list"), exports); +__exportStar(require("./literal"), exports); +__exportStar(require("./logical"), exports); +__exportStar(require("./message"), exports); +__exportStar(require("./nullological"), exports); +__exportStar(require("./parameters"), exports); +__exportStar(require("./quantity"), exports); +__exportStar(require("./query"), exports); +__exportStar(require("./ratio"), exports); +__exportStar(require("./reusable"), exports); +__exportStar(require("./string"), exports); +__exportStar(require("./structured"), exports); +__exportStar(require("./type"), exports); +__exportStar(require("./overloaded"), exports); +// Re-exporting interval functions as overrides to avoid ambiguity +// https://stackoverflow.com/questions/41293108/how-to-do-re-export-with-overrides +// TODO: we should improve this by perhaps renaming and reworking these functions +// it's a bit confusing right now giving the interval exports precedence over the others +var interval_1 = require("./interval"); +Object.defineProperty(exports, "doBefore", { enumerable: true, get: function () { return interval_1.doBefore; } }); +Object.defineProperty(exports, "doUnion", { enumerable: true, get: function () { return interval_1.doUnion; } }); +Object.defineProperty(exports, "doAfter", { enumerable: true, get: function () { return interval_1.doAfter; } }); +Object.defineProperty(exports, "doProperIncludes", { enumerable: true, get: function () { return interval_1.doProperIncludes; } }); +Object.defineProperty(exports, "doIntersect", { enumerable: true, get: function () { return interval_1.doIntersect; } }); +Object.defineProperty(exports, "doIncludes", { enumerable: true, get: function () { return interval_1.doIncludes; } }); +Object.defineProperty(exports, "doExcept", { enumerable: true, get: function () { return interval_1.doExcept; } }); +Object.defineProperty(exports, "doContains", { enumerable: true, get: function () { return interval_1.doContains; } }); + +},{"./aggregate":64,"./arithmetic":65,"./clinical":67,"./comparison":68,"./conditional":69,"./datetime":70,"./declaration":71,"./expression":72,"./external":74,"./instance":75,"./interval":76,"./list":78,"./literal":79,"./logical":80,"./message":81,"./nullological":82,"./overloaded":83,"./parameters":84,"./quantity":85,"./query":86,"./ratio":87,"./reusable":88,"./string":89,"./structured":90,"./type":91}],74:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Retrieve = void 0; +var expression_1 = require("./expression"); +var util_1 = require("../util/util"); +var builder_1 = require("./builder"); +var Retrieve = /** @class */ (function (_super) { + __extends(Retrieve, _super); + function Retrieve(json) { + var _this = _super.call(this, json) || this; + _this.datatype = json.dataType; + _this.templateId = json.templateId; + _this.codeProperty = json.codeProperty; + _this.codes = (0, builder_1.build)(json.codes); + _this.dateProperty = json.dateProperty; + _this.dateRange = (0, builder_1.build)(json.dateRange); + return _this; + } + Retrieve.prototype.exec = function (ctx) { + var _a; + var _this = this; + // Object with retrieve information to pass back to patient source + // Always assign datatype. Assign codeProperty and dateProperty if present + var retrieveDetails = __assign(__assign({ datatype: this.datatype }, (this.codeProperty ? { codeProperty: this.codeProperty } : {})), (this.dateProperty ? { dateProperty: this.dateProperty } : {})); + if (this.codes) { + var resolvedCodes = this.codes.execute(ctx); + if (resolvedCodes == null) { + return []; + } + retrieveDetails.codes = resolvedCodes; + } + if (this.dateRange) { + retrieveDetails.dateRange = this.dateRange.execute(ctx); + } + if (this.templateId) { + retrieveDetails.templateId = this.templateId; + } + var records = ctx.findRecords(this.templateId != null ? this.templateId : this.datatype, retrieveDetails); + if (retrieveDetails.codes) { + records = records.filter(function (r) { return _this.recordMatchesCodesOrVS(r, retrieveDetails.codes); }); + } + if (retrieveDetails.dateRange && this.dateProperty) { + records = records.filter(function (r) { var _a; return (_a = retrieveDetails.dateRange) === null || _a === void 0 ? void 0 : _a.includes(r.getDateOrInterval(_this.dateProperty)); }); + } + if (Array.isArray(records)) { + (_a = ctx.evaluatedRecords).push.apply(_a, records); + } + else { + ctx.evaluatedRecords.push(records); + } + return records; + }; + Retrieve.prototype.recordMatchesCodesOrVS = function (record, codes) { + var _this = this; + if ((0, util_1.typeIsArray)(codes)) { + return codes.some(function (c) { return c.hasMatch(record.getCode(_this.codeProperty)); }); + } + else { + return codes.hasMatch(record.getCode(this.codeProperty)); + } + }; + return Retrieve; +}(expression_1.Expression)); +exports.Retrieve = Retrieve; + +},{"../util/util":105,"./builder":66,"./expression":72}],75:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Instance = void 0; +var expression_1 = require("./expression"); +var quantity_1 = require("../datatypes/quantity"); +var datatypes_1 = require("../datatypes/datatypes"); +var builder_1 = require("./builder"); +var Element = /** @class */ (function () { + function Element(json) { + this.name = json.name; + this.value = (0, builder_1.build)(json.value); + } + Element.prototype.exec = function (ctx) { + return this.value != null ? this.value.execute(ctx) : undefined; + }; + return Element; +}()); +var Instance = /** @class */ (function (_super) { + __extends(Instance, _super); + function Instance(json) { + var _this = _super.call(this, json) || this; + _this.classType = json.classType; + _this.element = json.element.map(function (child) { return new Element(child); }); + return _this; + } + Instance.prototype.exec = function (ctx) { + var obj = {}; + for (var _i = 0, _a = this.element; _i < _a.length; _i++) { + var el = _a[_i]; + obj[el.name] = el.exec(ctx); + } + switch (this.classType) { + case '{urn:hl7-org:elm-types:r1}Quantity': + return new quantity_1.Quantity(obj.value, obj.unit); + case '{urn:hl7-org:elm-types:r1}Code': + return new datatypes_1.Code(obj.code, obj.system, obj.version, obj.display); + case '{urn:hl7-org:elm-types:r1}Concept': + return new datatypes_1.Concept(obj.codes, obj.display); + default: + return obj; + } + }; + return Instance; +}(expression_1.Expression)); +exports.Instance = Instance; + +},{"../datatypes/datatypes":56,"../datatypes/quantity":61,"./builder":66,"./expression":72}],76:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Collapse = exports.Expand = exports.Ends = exports.Starts = exports.End = exports.Start = exports.Size = exports.Width = exports.doIntersect = exports.doExcept = exports.doUnion = exports.OverlapsBefore = exports.OverlapsAfter = exports.Overlaps = exports.MeetsBefore = exports.MeetsAfter = exports.Meets = exports.doBefore = exports.doAfter = exports.doProperIncludes = exports.doIncludes = exports.doContains = exports.Interval = void 0; +var expression_1 = require("./expression"); +var quantity_1 = require("../datatypes/quantity"); +var math_1 = require("../util/math"); +var units_1 = require("../util/units"); +var dtivl = __importStar(require("../datatypes/interval")); +var builder_1 = require("./builder"); +var Interval = /** @class */ (function (_super) { + __extends(Interval, _super); + function Interval(json) { + var _this = _super.call(this, json) || this; + _this.lowClosed = json.lowClosed; + _this.lowClosedExpression = (0, builder_1.build)(json.lowClosedExpression); + _this.highClosed = json.highClosed; + _this.highClosedExpression = (0, builder_1.build)(json.highClosedExpression); + _this.low = (0, builder_1.build)(json.low); + _this.high = (0, builder_1.build)(json.high); + return _this; + } + Object.defineProperty(Interval.prototype, "isInterval", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Interval.prototype.exec = function (ctx) { + var lowValue = this.low.execute(ctx); + var highValue = this.high.execute(ctx); + var lowClosed = this.lowClosed != null + ? this.lowClosed + : this.lowClosedExpression && this.lowClosedExpression.execute(ctx); + var highClosed = this.highClosed != null + ? this.highClosed + : this.highClosedExpression && this.highClosedExpression.execute(ctx); + var defaultPointType; + if (lowValue == null && highValue == null) { + // try to get the default point type from a cast + if (this.low.asTypeSpecifier && this.low.asTypeSpecifier.type === 'NamedTypeSpecifier') { + defaultPointType = this.low.asTypeSpecifier.name; + } + else if (this.high.asTypeSpecifier && + this.high.asTypeSpecifier.type === 'NamedTypeSpecifier') { + defaultPointType = this.high.asTypeSpecifier.name; + } + } + return new dtivl.Interval(lowValue, highValue, lowClosed, highClosed, defaultPointType); + }; + return Interval; +}(expression_1.Expression)); +exports.Interval = Interval; +// Equal is completely handled by overloaded#Equal +// NotEqual is completely handled by overloaded#Equal +// Delegated to by overloaded#Contains and overloaded#In +function doContains(interval, item, precision) { + return interval.contains(item, precision); +} +exports.doContains = doContains; +// Delegated to by overloaded#Includes and overloaded#IncludedIn +function doIncludes(interval, subinterval, precision) { + return interval.includes(subinterval, precision); +} +exports.doIncludes = doIncludes; +// Delegated to by overloaded#ProperIncludes and overloaded@ProperIncludedIn +function doProperIncludes(interval, subinterval, precision) { + return interval.properlyIncludes(subinterval, precision); +} +exports.doProperIncludes = doProperIncludes; +// Delegated to by overloaded#After +function doAfter(a, b, precision) { + return a.after(b, precision); +} +exports.doAfter = doAfter; +// Delegated to by overloaded#Before +function doBefore(a, b, precision) { + return a.before(b, precision); +} +exports.doBefore = doBefore; +var Meets = /** @class */ (function (_super) { + __extends(Meets, _super); + function Meets(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Meets.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.meets(b, this.precision); + } + else { + return null; + } + }; + return Meets; +}(expression_1.Expression)); +exports.Meets = Meets; +var MeetsAfter = /** @class */ (function (_super) { + __extends(MeetsAfter, _super); + function MeetsAfter(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + MeetsAfter.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.meetsAfter(b, this.precision); + } + else { + return null; + } + }; + return MeetsAfter; +}(expression_1.Expression)); +exports.MeetsAfter = MeetsAfter; +var MeetsBefore = /** @class */ (function (_super) { + __extends(MeetsBefore, _super); + function MeetsBefore(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + MeetsBefore.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.meetsBefore(b, this.precision); + } + else { + return null; + } + }; + return MeetsBefore; +}(expression_1.Expression)); +exports.MeetsBefore = MeetsBefore; +var Overlaps = /** @class */ (function (_super) { + __extends(Overlaps, _super); + function Overlaps(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Overlaps.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.overlaps(b, this.precision); + } + else { + return null; + } + }; + return Overlaps; +}(expression_1.Expression)); +exports.Overlaps = Overlaps; +var OverlapsAfter = /** @class */ (function (_super) { + __extends(OverlapsAfter, _super); + function OverlapsAfter(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + OverlapsAfter.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.overlapsAfter(b, this.precision); + } + else { + return null; + } + }; + return OverlapsAfter; +}(expression_1.Expression)); +exports.OverlapsAfter = OverlapsAfter; +var OverlapsBefore = /** @class */ (function (_super) { + __extends(OverlapsBefore, _super); + function OverlapsBefore(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + OverlapsBefore.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.overlapsBefore(b, this.precision); + } + else { + return null; + } + }; + return OverlapsBefore; +}(expression_1.Expression)); +exports.OverlapsBefore = OverlapsBefore; +// Delegated to by overloaded#Union +function doUnion(a, b) { + return a.union(b); +} +exports.doUnion = doUnion; +// Delegated to by overloaded#Except +function doExcept(a, b) { + if (a != null && b != null) { + return a.except(b); + } + else { + return null; + } +} +exports.doExcept = doExcept; +// Delegated to by overloaded#Intersect +function doIntersect(a, b) { + if (a != null && b != null) { + return a.intersect(b); + } + else { + return null; + } +} +exports.doIntersect = doIntersect; +var Width = /** @class */ (function (_super) { + __extends(Width, _super); + function Width(json) { + return _super.call(this, json) || this; + } + Width.prototype.exec = function (ctx) { + var interval = this.arg.execute(ctx); + if (interval == null) { + return null; + } + return interval.width(); + }; + return Width; +}(expression_1.Expression)); +exports.Width = Width; +var Size = /** @class */ (function (_super) { + __extends(Size, _super); + function Size(json) { + return _super.call(this, json) || this; + } + Size.prototype.exec = function (ctx) { + var interval = this.arg.execute(ctx); + if (interval == null) { + return null; + } + return interval.size(); + }; + return Size; +}(expression_1.Expression)); +exports.Size = Size; +var Start = /** @class */ (function (_super) { + __extends(Start, _super); + function Start(json) { + return _super.call(this, json) || this; + } + Start.prototype.exec = function (ctx) { + var interval = this.arg.execute(ctx); + if (interval == null) { + return null; + } + var start = interval.start(); + // fix the timezoneOffset of minimum Datetime to match context offset + if (start && start.isDateTime && start.equals(math_1.MIN_DATETIME_VALUE)) { + start.timezoneOffset = ctx.getTimezoneOffset(); + } + return start; + }; + return Start; +}(expression_1.Expression)); +exports.Start = Start; +var End = /** @class */ (function (_super) { + __extends(End, _super); + function End(json) { + return _super.call(this, json) || this; + } + End.prototype.exec = function (ctx) { + var interval = this.arg.execute(ctx); + if (interval == null) { + return null; + } + var end = interval.end(); + // fix the timezoneOffset of maximum Datetime to match context offset + if (end && end.isDateTime && end.equals(math_1.MAX_DATETIME_VALUE)) { + end.timezoneOffset = ctx.getTimezoneOffset(); + } + return end; + }; + return End; +}(expression_1.Expression)); +exports.End = End; +var Starts = /** @class */ (function (_super) { + __extends(Starts, _super); + function Starts(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Starts.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.starts(b, this.precision); + } + else { + return null; + } + }; + return Starts; +}(expression_1.Expression)); +exports.Starts = Starts; +var Ends = /** @class */ (function (_super) { + __extends(Ends, _super); + function Ends(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Ends.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.ends(b, this.precision); + } + else { + return null; + } + }; + return Ends; +}(expression_1.Expression)); +exports.Ends = Ends; +function intervalListType(intervals) { + // Returns one of null, 'time', 'date', 'datetime', 'quantity', 'integer', 'decimal' or 'mismatch' + var type = null; + for (var _i = 0, intervals_1 = intervals; _i < intervals_1.length; _i++) { + var itvl = intervals_1[_i]; + if (itvl == null) { + continue; + } + if (itvl.low == null && itvl.high == null) { + //can't really determine type from this + continue; + } + // if one end is null (but not both), the type can be determined from the other end + var low = itvl.low != null ? itvl.low : itvl.high; + var high = itvl.high != null ? itvl.high : itvl.low; + if (low.isTime && low.isTime() && high.isTime && high.isTime()) { + if (type == null) { + type = 'time'; + } + else if (type === 'time') { + continue; + } + else { + return 'mismatch'; + } + // if an interval mixes date and datetime, type is datetime (for implicit conversion) + } + else if ((low.isDateTime || high.isDateTime) && + (low.isDateTime || low.isDate) && + (high.isDateTime || high.isDate)) { + if (type == null || type === 'date') { + type = 'datetime'; + } + else if (type === 'datetime') { + continue; + } + else { + return 'mismatch'; + } + } + else if (low.isDate && high.isDate) { + if (type == null) { + type = 'date'; + } + else if (type === 'date' || type === 'datetime') { + continue; + } + else { + return 'mismatch'; + } + } + else if (low.isQuantity && high.isQuantity) { + if (type == null) { + type = 'quantity'; + } + else if (type === 'quantity') { + continue; + } + else { + return 'mismatch'; + } + } + else if (Number.isInteger(low) && Number.isInteger(high)) { + if (type == null) { + type = 'integer'; + } + else if (type === 'integer' || type === 'decimal') { + continue; + } + else { + return 'mismatch'; + } + } + else if (typeof low === 'number' && typeof high === 'number') { + if (type == null || type === 'integer') { + type = 'decimal'; + } + else if (type === 'decimal') { + continue; + } + else { + return 'mismatch'; + } + //if we are here ends are mismatched + } + else { + return 'mismatch'; + } + } + return type; +} +var Expand = /** @class */ (function (_super) { + __extends(Expand, _super); + function Expand(json) { + return _super.call(this, json) || this; + } + Expand.prototype.exec = function (ctx) { + // expand(argument List>, per Quantity) List> + var defaultPer, expandFunction; + var _a = this.execArgs(ctx), intervals = _a[0], per = _a[1]; + // CQL 1.5 introduced an overload to allow singular intervals; make it a list so we can use the same logic for either overload + if (!Array.isArray(intervals)) { + intervals = [intervals]; + } + var type = intervalListType(intervals); + if (type === 'mismatch') { + throw new Error('List of intervals contains mismatched types.'); + } + if (type == null) { + return null; + } + // this step collapses overlaps, and also returns a clone of intervals so we can feel free to mutate + intervals = collapseIntervals(intervals, per); + if (intervals.length === 0) { + return []; + } + if (['time', 'date', 'datetime'].includes(type)) { + expandFunction = this.expandDTishInterval; + defaultPer = function (interval) { return new quantity_1.Quantity(1, interval.low.getPrecision()); }; + } + else if (['quantity'].includes(type)) { + expandFunction = this.expandQuantityInterval; + defaultPer = function (interval) { return new quantity_1.Quantity(1, interval.low.unit); }; + } + else if (['integer', 'decimal'].includes(type)) { + expandFunction = this.expandNumericInterval; + defaultPer = function (_interval) { return new quantity_1.Quantity(1, '1'); }; + } + else { + throw new Error('Interval list type not yet supported.'); + } + var results = []; + for (var _i = 0, intervals_2 = intervals; _i < intervals_2.length; _i++) { + var interval = intervals_2[_i]; + if (interval == null) { + continue; + } + // We do not support open ended intervals since result would likely be too long + if (interval.low == null || interval.high == null) { + return null; + } + if (type === 'datetime') { + //support for implicitly converting dates to datetime + interval.low = interval.low.getDateTime(); + interval.high = interval.high.getDateTime(); + } + per = per != null ? per : defaultPer(interval); + var items = expandFunction.call(this, interval, per); + if (items === null) { + return null; + } + results.push.apply(results, (items || [])); + } + return results; + }; + Expand.prototype.expandDTishInterval = function (interval, per) { + per.unit = (0, units_1.convertToCQLDateUnit)(per.unit); + if (per.unit === 'week') { + per.value *= 7; + per.unit = 'day'; + } + // Precision Checks + // return null if precision not applicable (e.g. gram, or minutes for dates) + if (!interval.low.constructor.FIELDS.includes(per.unit)) { + return null; + } + // open interval with null boundaries do not contribute to output + // closed interval with null boundaries are not allowed for performance reasons + if (interval.low == null || interval.high == null) { + return null; + } + var low = interval.lowClosed ? interval.low : interval.low.successor(); + var high = interval.highClosed ? interval.high : interval.high.predecessor(); + if (low.after(high)) { + return []; + } + if (interval.low.isLessPrecise(per.unit) || interval.high.isLessPrecise(per.unit)) { + return []; + } + var current_low = low; + var results = []; + low = this.truncateToPrecision(low, per.unit); + high = this.truncateToPrecision(high, per.unit); + var current_high = current_low.add(per.value, per.unit).predecessor(); + var intervalToAdd = new dtivl.Interval(current_low, current_high, true, true); + while (intervalToAdd.high.sameOrBefore(high)) { + results.push(intervalToAdd); + current_low = current_low.add(per.value, per.unit); + current_high = current_low.add(per.value, per.unit).predecessor(); + intervalToAdd = new dtivl.Interval(current_low, current_high, true, true); + } + return results; + }; + Expand.prototype.truncateToPrecision = function (value, unit) { + // If interval boundaries are more precise than per quantity, truncate to + // the precision specified by the per + var shouldTruncate = false; + for (var _i = 0, _a = value.constructor.FIELDS; _i < _a.length; _i++) { + var field = _a[_i]; + if (shouldTruncate) { + value[field] = null; + } + if (field === unit) { + // Start truncating after this unit + shouldTruncate = true; + } + } + return value; + }; + Expand.prototype.expandQuantityInterval = function (interval, per) { + // we want to convert everything to the more precise of the interval.low or per + var result_units; + var res = (0, units_1.compareUnits)(interval.low.unit, per.unit); + if (res != null && res > 0) { + //interval.low.unit is 'bigger' aka les precise + result_units = per.unit; + } + else { + result_units = interval.low.unit; + } + var low_value = (0, units_1.convertUnit)(interval.low.value, interval.low.unit, result_units); + var high_value = (0, units_1.convertUnit)(interval.high.value, interval.high.unit, result_units); + var per_value = (0, units_1.convertUnit)(per.value, per.unit, result_units); + // return null if unit conversion failed, must have mismatched units + if (!(low_value != null && high_value != null && per_value != null)) { + return null; + } + var results = this.makeNumericIntervalList(low_value, high_value, interval.lowClosed, interval.highClosed, per_value); + for (var _i = 0, results_1 = results; _i < results_1.length; _i++) { + var itvl = results_1[_i]; + itvl.low = new quantity_1.Quantity(itvl.low, result_units); + itvl.high = new quantity_1.Quantity(itvl.high, result_units); + } + return results; + }; + Expand.prototype.expandNumericInterval = function (interval, per) { + if (per.unit !== '1' && per.unit !== '') { + return null; + } + return this.makeNumericIntervalList(interval.low, interval.high, interval.lowClosed, interval.highClosed, per.value); + }; + Expand.prototype.makeNumericIntervalList = function (low, high, lowClosed, highClosed, perValue) { + // If the per value is a Decimal (has a .), 8 decimal places are appropriate + // Integers should have 0 Decimal places + var perIsDecimal = perValue.toString().includes('.'); + var decimalPrecision = perIsDecimal ? 8 : 0; + low = lowClosed ? low : (0, math_1.successor)(low); + high = highClosed ? high : (0, math_1.predecessor)(high); + // If the interval boundaries are more precise than the per quantity, the + // more precise values will be truncated to the precision specified by the + // per quantity. + low = truncateDecimal(low, decimalPrecision); + high = truncateDecimal(high, decimalPrecision); + if (low > high) { + return []; + } + if (low == null || high == null) { + return []; + } + var perUnitSize = perIsDecimal ? 0.00000001 : 1; + if (low === high && + Number.isInteger(low) && + Number.isInteger(high) && + !Number.isInteger(perValue)) { + high = parseFloat((high + 1).toFixed(decimalPrecision)); + } + var current_low = low; + var results = []; + if (perValue > high - low + perUnitSize) { + return []; + } + var current_high = parseFloat((current_low + perValue - perUnitSize).toFixed(decimalPrecision)); + var intervalToAdd = new dtivl.Interval(current_low, current_high, true, true); + while (intervalToAdd.high <= high) { + results.push(intervalToAdd); + current_low = parseFloat((current_low + perValue).toFixed(decimalPrecision)); + current_high = parseFloat((current_low + perValue - perUnitSize).toFixed(decimalPrecision)); + intervalToAdd = new dtivl.Interval(current_low, current_high, true, true); + } + return results; + }; + return Expand; +}(expression_1.Expression)); +exports.Expand = Expand; +var Collapse = /** @class */ (function (_super) { + __extends(Collapse, _super); + function Collapse(json) { + return _super.call(this, json) || this; + } + Collapse.prototype.exec = function (ctx) { + // collapse(argument List>, per Quantity) List> + var _a = this.execArgs(ctx), intervals = _a[0], perWidth = _a[1]; + return collapseIntervals(intervals, perWidth); + }; + return Collapse; +}(expression_1.Expression)); +exports.Collapse = Collapse; +function collapseIntervals(intervals, perWidth) { + // Clone intervals so this function remains idempotent + var intervalsClone = []; + for (var _i = 0, intervals_3 = intervals; _i < intervals_3.length; _i++) { + var interval = intervals_3[_i]; + // The spec says to ignore null intervals + if (interval != null) { + intervalsClone.push(interval.copy()); + } + } + // If the list is null, return null + if (intervals == null) { + return null; + } + else if (intervalsClone.length <= 1) { + return intervalsClone; + } + else { + // If the per argument is null, the default unit interval for the point type + // of the intervals involved will be used (i.e. the interval that has a + // width equal to the result of the successor function for the point type). + if (perWidth == null) { + perWidth = intervalsClone[0].getPointSize(); + } + // sort intervalsClone by start + intervalsClone.sort(function (a, b) { + if (a.low && typeof a.low.before === 'function') { + if (b.low != null && a.low.before(b.low)) { + return -1; + } + if (b.low == null || a.low.after(b.low)) { + return 1; + } + } + else if (a.low != null && b.low != null) { + if (a.low < b.low) { + return -1; + } + if (a.low > b.low) { + return 1; + } + } + else if (a.low != null && b.low == null) { + return 1; + } + else if (a.low == null && b.low != null) { + return -1; + } + // if both lows are undefined, sort by high + if (a.high && typeof a.high.before === 'function') { + if (b.high == null || a.high.before(b.high)) { + return -1; + } + if (a.high.after(b.high)) { + return 1; + } + } + else if (a.high != null && b.high != null) { + if (a.high < b.high) { + return -1; + } + if (a.high > b.high) { + return 1; + } + } + else if (a.high != null && b.high == null) { + return -1; + } + else if (a.high == null && b.high != null) { + return 1; + } + return 0; + }); + // collapse intervals as necessary + var collapsedIntervals = []; + var a = intervalsClone.shift(); + var b = intervalsClone.shift(); + while (b) { + if (b.low && typeof b.low.durationBetween === 'function') { + // handle DateTimes using durationBetween + if (a.high != null ? a.high.sameOrAfter(b.low) : undefined) { + // overlap + if (b.high == null || b.high.after(a.high)) { + a.high = b.high; + } + } + else if ((a.high != null ? a.high.durationBetween(b.low, perWidth.unit).high : undefined) <= + perWidth.value) { + a.high = b.high; + } + else { + collapsedIntervals.push(a); + a = b; + } + } + else if (b.low && typeof b.low.sameOrBefore === 'function') { + if (a.high != null && b.low.sameOrBefore((0, quantity_1.doAddition)(a.high, perWidth))) { + if (b.high == null || b.high.after(a.high)) { + a.high = b.high; + } + } + else { + collapsedIntervals.push(a); + a = b; + } + } + else { + if (b.low - a.high <= perWidth.value) { + if (b.high > a.high || b.high == null) { + a.high = b.high; + } + } + else { + collapsedIntervals.push(a); + a = b; + } + } + b = intervalsClone.shift(); + } + collapsedIntervals.push(a); + return collapsedIntervals; + } +} +function truncateDecimal(decimal, decimalPlaces) { + // like parseFloat().toFixed() but floor rather than round + // Needed for when per precision is less than the interval input precision + var re = new RegExp('^-?\\d+(?:.\\d{0,' + (decimalPlaces || -1) + '})?'); + return parseFloat(decimal.toString().match(re)[0]); +} + +},{"../datatypes/interval":59,"../datatypes/quantity":61,"../util/math":103,"../util/units":104,"./builder":66,"./expression":72}],77:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Library = void 0; +var expressions_1 = require("./expressions"); +var Library = /** @class */ (function () { + function Library(json, libraryManager) { + this.source = json; + // usings + var usingDefs = (json.library.usings && json.library.usings.def) || []; + this.usings = usingDefs + .filter(function (u) { return u.localIdentifier !== 'System'; }) + .map(function (u) { + return { name: u.localIdentifier, version: u.version }; + }); + // parameters + var paramDefs = (json.library.parameters && json.library.parameters.def) || []; + this.parameters = {}; + for (var _i = 0, paramDefs_1 = paramDefs; _i < paramDefs_1.length; _i++) { + var param = paramDefs_1[_i]; + this.parameters[param.name] = new expressions_1.ParameterDef(param); + } + // code systems + var csDefs = (json.library.codeSystems && json.library.codeSystems.def) || []; + this.codesystems = {}; + for (var _a = 0, csDefs_1 = csDefs; _a < csDefs_1.length; _a++) { + var codesystem = csDefs_1[_a]; + this.codesystems[codesystem.name] = new expressions_1.CodeSystemDef(codesystem); + } + // value sets + var vsDefs = (json.library.valueSets && json.library.valueSets.def) || []; + this.valuesets = {}; + for (var _b = 0, vsDefs_1 = vsDefs; _b < vsDefs_1.length; _b++) { + var valueset = vsDefs_1[_b]; + this.valuesets[valueset.name] = new expressions_1.ValueSetDef(valueset); + } + // codes + var codeDefs = (json.library.codes && json.library.codes.def) || []; + this.codes = {}; + for (var _c = 0, codeDefs_1 = codeDefs; _c < codeDefs_1.length; _c++) { + var code = codeDefs_1[_c]; + this.codes[code.name] = new expressions_1.CodeDef(code); + } + // concepts + var conceptDefs = (json.library.concepts && json.library.concepts.def) || []; + this.concepts = {}; + for (var _d = 0, conceptDefs_1 = conceptDefs; _d < conceptDefs_1.length; _d++) { + var concept = conceptDefs_1[_d]; + this.concepts[concept.name] = new expressions_1.ConceptDef(concept); + } + // expressions + var exprDefs = (json.library.statements && json.library.statements.def) || []; + this.expressions = {}; + this.functions = {}; + for (var _e = 0, exprDefs_1 = exprDefs; _e < exprDefs_1.length; _e++) { + var expr = exprDefs_1[_e]; + if (expr.type === 'FunctionDef') { + if (!this.functions[expr.name]) { + this.functions[expr.name] = []; + } + this.functions[expr.name].push(new expressions_1.FunctionDef(expr)); + } + else { + this.expressions[expr.name] = new expressions_1.ExpressionDef(expr); + } + } + // includes + var inclDefs = (json.library.includes && json.library.includes.def) || []; + this.includes = {}; + for (var _f = 0, inclDefs_1 = inclDefs; _f < inclDefs_1.length; _f++) { + var incl = inclDefs_1[_f]; + if (libraryManager) { + this.includes[incl.localIdentifier] = libraryManager.resolve(incl.path, incl.version); + } + } + // Include codesystems from includes + for (var iProperty in this.includes) { + if (this.includes[iProperty] && this.includes[iProperty].codesystems) { + for (var csProperty in this.includes[iProperty].codesystems) { + this.codesystems[csProperty] = this.includes[iProperty].codesystems[csProperty]; + } + } + } + } + Library.prototype.getFunction = function (identifier) { + return this.functions[identifier]; + }; + Library.prototype.get = function (identifier) { + return (this.expressions[identifier] || this.includes[identifier] || this.getFunction(identifier)); + }; + Library.prototype.getValueSet = function (identifier, libraryName) { + if (this.valuesets[identifier] != null) { + return this.valuesets[identifier]; + } + return this.includes[libraryName] != null + ? this.includes[libraryName].valuesets[identifier] + : undefined; + }; + Library.prototype.getCodeSystem = function (identifier) { + return this.codesystems[identifier]; + }; + Library.prototype.getCode = function (identifier) { + return this.codes[identifier]; + }; + Library.prototype.getConcept = function (identifier) { + return this.concepts[identifier]; + }; + Library.prototype.getParameter = function (name) { + return this.parameters[name]; + }; + return Library; +}()); +exports.Library = Library; + +},{"./expressions":73}],78:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Slice = exports.Last = exports.First = exports.Current = exports.Distinct = exports.Flatten = exports.ForEach = exports.doProperIncludes = exports.doIncludes = exports.doContains = exports.IndexOf = exports.ToList = exports.SingletonFrom = exports.Filter = exports.Times = exports.doIntersect = exports.doExcept = exports.doUnion = exports.Exists = exports.List = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var util_1 = require("../util/util"); +var comparison_1 = require("../util/comparison"); +var List = /** @class */ (function (_super) { + __extends(List, _super); + function List(json) { + var _this = _super.call(this, json) || this; + _this.elements = (0, builder_1.build)(json.element) || []; + return _this; + } + Object.defineProperty(List.prototype, "isList", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + List.prototype.exec = function (ctx) { + return this.elements.map(function (item) { return item.execute(ctx); }); + }; + return List; +}(expression_1.Expression)); +exports.List = List; +var Exists = /** @class */ (function (_super) { + __extends(Exists, _super); + function Exists(json) { + return _super.call(this, json) || this; + } + Exists.prototype.exec = function (ctx) { + var list = this.execArgs(ctx); + // if list exists and has non empty length we need to make sure it isnt just full of nulls + if (list) { + return list.some(function (item) { return item != null; }); + } + return false; + }; + return Exists; +}(expression_1.Expression)); +exports.Exists = Exists; +// Equal is completely handled by overloaded#Equal +// NotEqual is completely handled by overloaded#Equal +// Delegated to by overloaded#Union +function doUnion(a, b) { + var distinct = doDistinct(a.concat(b)); + return removeDuplicateNulls(distinct); +} +exports.doUnion = doUnion; +// Delegated to by overloaded#Except +function doExcept(a, b) { + var distinct = doDistinct(a); + var setList = removeDuplicateNulls(distinct); + return setList.filter(function (item) { return !doContains(b, item, true); }); +} +exports.doExcept = doExcept; +// Delegated to by overloaded#Intersect +function doIntersect(a, b) { + var distinct = doDistinct(a); + var setList = removeDuplicateNulls(distinct); + return setList.filter(function (item) { return doContains(b, item, true); }); +} +exports.doIntersect = doIntersect; +// ELM-only, not a product of CQL +var Times = /** @class */ (function (_super) { + __extends(Times, _super); + function Times() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Times; +}(expression_1.UnimplementedExpression)); +exports.Times = Times; +// ELM-only, not a product of CQL +var Filter = /** @class */ (function (_super) { + __extends(Filter, _super); + function Filter() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Filter; +}(expression_1.UnimplementedExpression)); +exports.Filter = Filter; +var SingletonFrom = /** @class */ (function (_super) { + __extends(SingletonFrom, _super); + function SingletonFrom(json) { + return _super.call(this, json) || this; + } + SingletonFrom.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null && arg.length > 1) { + throw new Error("IllegalArgument: 'SingletonFrom' requires a 0 or 1 arg array"); + } + else if (arg != null && arg.length === 1) { + return arg[0]; + } + else { + return null; + } + }; + return SingletonFrom; +}(expression_1.Expression)); +exports.SingletonFrom = SingletonFrom; +var ToList = /** @class */ (function (_super) { + __extends(ToList, _super); + function ToList(json) { + return _super.call(this, json) || this; + } + ToList.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return [arg]; + } + else { + return []; + } + }; + return ToList; +}(expression_1.Expression)); +exports.ToList = ToList; +var IndexOf = /** @class */ (function (_super) { + __extends(IndexOf, _super); + function IndexOf(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + _this.element = (0, builder_1.build)(json.element); + return _this; + } + IndexOf.prototype.exec = function (ctx) { + var index; + var src = this.source.execute(ctx); + var el = this.element.execute(ctx); + if (src == null || el == null) { + return null; + } + for (var i = 0; i < src.length; i++) { + var itm = src[i]; + if ((0, comparison_1.equals)(itm, el)) { + index = i; + break; + } + } + if (index != null) { + return index; + } + else { + return -1; + } + }; + return IndexOf; +}(expression_1.Expression)); +exports.IndexOf = IndexOf; +// Indexer is completely handled by overloaded#Indexer +// Delegated to by overloaded#Contains and overloaded#In +function doContains(container, item, nullEquivalence) { + if (nullEquivalence === void 0) { nullEquivalence = false; } + return container.some(function (element) { return (0, comparison_1.equals)(element, item) || (nullEquivalence && element == null && item == null); }); +} +exports.doContains = doContains; +// Delegated to by overloaded#Includes and overloaded@IncludedIn +function doIncludes(list, sublist) { + return sublist.every(function (x) { return doContains(list, x); }); +} +exports.doIncludes = doIncludes; +// Delegated to by overloaded#ProperIncludes and overloaded@ProperIncludedIn +function doProperIncludes(list, sublist) { + return list.length > sublist.length && doIncludes(list, sublist); +} +exports.doProperIncludes = doProperIncludes; +// ELM-only, not a product of CQL +var ForEach = /** @class */ (function (_super) { + __extends(ForEach, _super); + function ForEach() { + return _super !== null && _super.apply(this, arguments) || this; + } + return ForEach; +}(expression_1.UnimplementedExpression)); +exports.ForEach = ForEach; +var Flatten = /** @class */ (function (_super) { + __extends(Flatten, _super); + function Flatten(json) { + return _super.call(this, json) || this; + } + Flatten.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if ((0, util_1.typeIsArray)(arg) && arg.every(function (x) { return (0, util_1.typeIsArray)(x); })) { + return arg.reduce(function (x, y) { return x.concat(y); }, []); + } + else { + return arg; + } + }; + return Flatten; +}(expression_1.Expression)); +exports.Flatten = Flatten; +var Distinct = /** @class */ (function (_super) { + __extends(Distinct, _super); + function Distinct(json) { + return _super.call(this, json) || this; + } + Distinct.prototype.exec = function (ctx) { + var result = this.execArgs(ctx); + if (result == null) { + return null; + } + return doDistinct(result); + }; + return Distinct; +}(expression_1.Expression)); +exports.Distinct = Distinct; +function doDistinct(list) { + var distinct = []; + list.forEach(function (item) { + var isNew = distinct.every(function (seenItem) { return !(0, comparison_1.equals)(item, seenItem); }); + if (isNew) { + distinct.push(item); + } + }); + return removeDuplicateNulls(distinct); +} +function removeDuplicateNulls(list) { + // Remove duplicate null elements + var firstNullFound = false; + var setList = []; + for (var _i = 0, list_1 = list; _i < list_1.length; _i++) { + var item = list_1[_i]; + if (item !== null) { + setList.push(item); + } + else if (item === null && !firstNullFound) { + setList.push(item); + firstNullFound = true; + } + } + return setList; +} +// ELM-only, not a product of CQL +var Current = /** @class */ (function (_super) { + __extends(Current, _super); + function Current() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Current; +}(expression_1.UnimplementedExpression)); +exports.Current = Current; +var First = /** @class */ (function (_super) { + __extends(First, _super); + function First(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + return _this; + } + First.prototype.exec = function (ctx) { + var src = this.source.exec(ctx); + if (src != null && (0, util_1.typeIsArray)(src) && src.length > 0) { + return src[0]; + } + else { + return null; + } + }; + return First; +}(expression_1.Expression)); +exports.First = First; +var Last = /** @class */ (function (_super) { + __extends(Last, _super); + function Last(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + return _this; + } + Last.prototype.exec = function (ctx) { + var src = this.source.exec(ctx); + if (src != null && (0, util_1.typeIsArray)(src) && src.length > 0) { + return src[src.length - 1]; + } + else { + return null; + } + }; + return Last; +}(expression_1.Expression)); +exports.Last = Last; +var Slice = /** @class */ (function (_super) { + __extends(Slice, _super); + function Slice(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + _this.startIndex = (0, builder_1.build)(json.startIndex); + _this.endIndex = (0, builder_1.build)(json.endIndex); + return _this; + } + Slice.prototype.exec = function (ctx) { + var src = this.source.exec(ctx); + if (src != null && (0, util_1.typeIsArray)(src)) { + var startIndex = this.startIndex.exec(ctx); + var endIndex = this.endIndex.exec(ctx); + var start = startIndex != null ? startIndex : 0; + var end = endIndex != null ? endIndex : src.length; + if (src.length === 0 || start < 0 || end < 0 || end < start) { + return []; + } + return src.slice(start, end); + } + return null; + }; + return Slice; +}(expression_1.Expression)); +exports.Slice = Slice; +// Length is completely handled by overloaded#Length + +},{"../util/comparison":102,"../util/util":105,"./builder":66,"./expression":72}],79:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.StringLiteral = exports.DecimalLiteral = exports.IntegerLiteral = exports.BooleanLiteral = exports.Literal = void 0; +var expression_1 = require("./expression"); +var Literal = /** @class */ (function (_super) { + __extends(Literal, _super); + function Literal(json) { + var _this = _super.call(this, json) || this; + _this.valueType = json.valueType; + _this.value = json.value; + return _this; + } + Literal.from = function (json) { + switch (json.valueType) { + case '{urn:hl7-org:elm-types:r1}Boolean': + return new BooleanLiteral(json); + case '{urn:hl7-org:elm-types:r1}Integer': + return new IntegerLiteral(json); + case '{urn:hl7-org:elm-types:r1}Decimal': + return new DecimalLiteral(json); + case '{urn:hl7-org:elm-types:r1}String': + return new StringLiteral(json); + default: + return new Literal(json); + } + }; + Literal.prototype.exec = function (_ctx) { + return this.value; + }; + return Literal; +}(expression_1.Expression)); +exports.Literal = Literal; +// The following are not defined in ELM, but helpful for execution +var BooleanLiteral = /** @class */ (function (_super) { + __extends(BooleanLiteral, _super); + function BooleanLiteral(json) { + var _this = _super.call(this, json) || this; + _this.value = _this.value === 'true'; + return _this; + } + Object.defineProperty(BooleanLiteral.prototype, "isBooleanLiteral", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + BooleanLiteral.prototype.exec = function (_ctx) { + return this.value; + }; + return BooleanLiteral; +}(Literal)); +exports.BooleanLiteral = BooleanLiteral; +var IntegerLiteral = /** @class */ (function (_super) { + __extends(IntegerLiteral, _super); + function IntegerLiteral(json) { + var _this = _super.call(this, json) || this; + _this.value = parseInt(_this.value, 10); + return _this; + } + Object.defineProperty(IntegerLiteral.prototype, "isIntegerLiteral", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + IntegerLiteral.prototype.exec = function (_ctx) { + return this.value; + }; + return IntegerLiteral; +}(Literal)); +exports.IntegerLiteral = IntegerLiteral; +var DecimalLiteral = /** @class */ (function (_super) { + __extends(DecimalLiteral, _super); + function DecimalLiteral(json) { + var _this = _super.call(this, json) || this; + _this.value = parseFloat(_this.value); + return _this; + } + Object.defineProperty(DecimalLiteral.prototype, "isDecimalLiteral", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + DecimalLiteral.prototype.exec = function (_ctx) { + return this.value; + }; + return DecimalLiteral; +}(Literal)); +exports.DecimalLiteral = DecimalLiteral; +var StringLiteral = /** @class */ (function (_super) { + __extends(StringLiteral, _super); + function StringLiteral(json) { + return _super.call(this, json) || this; + } + Object.defineProperty(StringLiteral.prototype, "isStringLiteral", { + // Define a simple getter to allow type-checking of this class without instanceof + // and in a way that survives minification (as opposed to checking constructor.name) + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + StringLiteral.prototype.exec = function (_ctx) { + // TODO: Remove these replacements when CQL-to-ELM fixes bug: https://github.com/cqframework/clinical_quality_language/issues/82 + return this.value.replace(/\\'/g, "'").replace(/\\"/g, '"'); + }; + return StringLiteral; +}(Literal)); +exports.StringLiteral = StringLiteral; + +},{"./expression":72}],80:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.IsFalse = exports.IsTrue = exports.Xor = exports.Not = exports.Or = exports.And = void 0; +var expression_1 = require("./expression"); +var datatypes_1 = require("../datatypes/datatypes"); +var And = /** @class */ (function (_super) { + __extends(And, _super); + function And(json) { + return _super.call(this, json) || this; + } + And.prototype.exec = function (ctx) { + return datatypes_1.ThreeValuedLogic.and.apply(datatypes_1.ThreeValuedLogic, this.execArgs(ctx)); + }; + return And; +}(expression_1.Expression)); +exports.And = And; +var Or = /** @class */ (function (_super) { + __extends(Or, _super); + function Or(json) { + return _super.call(this, json) || this; + } + Or.prototype.exec = function (ctx) { + return datatypes_1.ThreeValuedLogic.or.apply(datatypes_1.ThreeValuedLogic, this.execArgs(ctx)); + }; + return Or; +}(expression_1.Expression)); +exports.Or = Or; +var Not = /** @class */ (function (_super) { + __extends(Not, _super); + function Not(json) { + return _super.call(this, json) || this; + } + Not.prototype.exec = function (ctx) { + return datatypes_1.ThreeValuedLogic.not(this.execArgs(ctx)); + }; + return Not; +}(expression_1.Expression)); +exports.Not = Not; +var Xor = /** @class */ (function (_super) { + __extends(Xor, _super); + function Xor(json) { + return _super.call(this, json) || this; + } + Xor.prototype.exec = function (ctx) { + return datatypes_1.ThreeValuedLogic.xor.apply(datatypes_1.ThreeValuedLogic, this.execArgs(ctx)); + }; + return Xor; +}(expression_1.Expression)); +exports.Xor = Xor; +var IsTrue = /** @class */ (function (_super) { + __extends(IsTrue, _super); + function IsTrue(json) { + return _super.call(this, json) || this; + } + IsTrue.prototype.exec = function (ctx) { + return true === this.execArgs(ctx); + }; + return IsTrue; +}(expression_1.Expression)); +exports.IsTrue = IsTrue; +var IsFalse = /** @class */ (function (_super) { + __extends(IsFalse, _super); + function IsFalse(json) { + return _super.call(this, json) || this; + } + IsFalse.prototype.exec = function (ctx) { + return false === this.execArgs(ctx); + }; + return IsFalse; +}(expression_1.Expression)); +exports.IsFalse = IsFalse; + +},{"../datatypes/datatypes":56,"./expression":72}],81:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Message = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var Message = /** @class */ (function (_super) { + __extends(Message, _super); + function Message(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + _this.condition = (0, builder_1.build)(json.condition); + _this.code = (0, builder_1.build)(json.code); + _this.severity = (0, builder_1.build)(json.severity); + _this.message = (0, builder_1.build)(json.message); + return _this; + } + Message.prototype.exec = function (ctx) { + var source = this.source.execute(ctx); + var condition = this.condition.execute(ctx); + if (condition) { + var code = this.code.execute(ctx); + var severity = this.severity.execute(ctx); + var message = this.message.execute(ctx); + var listener = ctx.getMessageListener(); + if (listener && typeof listener.onMessage === 'function') { + listener.onMessage(source, code, severity, message); + } + } + return source; + }; + return Message; +}(expression_1.Expression)); +exports.Message = Message; + +},{"./builder":66,"./expression":72}],82:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Coalesce = exports.IsNull = exports.Null = void 0; +var expression_1 = require("./expression"); +var Null = /** @class */ (function (_super) { + __extends(Null, _super); + function Null(json) { + return _super.call(this, json) || this; + } + Null.prototype.exec = function (_ctx) { + return null; + }; + return Null; +}(expression_1.Expression)); +exports.Null = Null; +var IsNull = /** @class */ (function (_super) { + __extends(IsNull, _super); + function IsNull(json) { + return _super.call(this, json) || this; + } + IsNull.prototype.exec = function (ctx) { + return this.execArgs(ctx) == null; + }; + return IsNull; +}(expression_1.Expression)); +exports.IsNull = IsNull; +var Coalesce = /** @class */ (function (_super) { + __extends(Coalesce, _super); + function Coalesce(json) { + return _super.call(this, json) || this; + } + Coalesce.prototype.exec = function (ctx) { + if (this.args) { + for (var _i = 0, _a = this.args; _i < _a.length; _i++) { + var arg = _a[_i]; + var result = arg.execute(ctx); + // if a single arg that's a list, coalesce over the list + if (this.args.length === 1 && Array.isArray(result)) { + var item = result.find(function (item) { return item != null; }); + if (item != null) { + return item; + } + } + else { + if (result != null) { + return result; + } + } + } + } + return null; + }; + return Coalesce; +}(expression_1.Expression)); +exports.Coalesce = Coalesce; + +},{"./expression":72}],83:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Precision = exports.SameOrBefore = exports.SameOrAfter = exports.SameAs = exports.Before = exports.After = exports.Length = exports.ProperIncludedIn = exports.ProperIncludes = exports.IncludedIn = exports.Includes = exports.Contains = exports.In = exports.Indexer = exports.Intersect = exports.Except = exports.Union = exports.NotEqual = exports.Equivalent = exports.Equal = void 0; +/* eslint-disable @typescript-eslint/ban-ts-comment */ +var expression_1 = require("./expression"); +var logic_1 = require("../datatypes/logic"); +var datetime_1 = require("../datatypes/datetime"); +var util_1 = require("../util/util"); +var comparison_1 = require("../util/comparison"); +var DT = __importStar(require("./datetime")); +var LIST = __importStar(require("./list")); +var IVL = __importStar(require("./interval")); +var Equal = /** @class */ (function (_super) { + __extends(Equal, _super); + function Equal(json) { + return _super.call(this, json) || this; + } + Equal.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args[0] == null || args[1] == null) { + return null; + } + // @ts-ignore + return comparison_1.equals.apply(void 0, args); + }; + return Equal; +}(expression_1.Expression)); +exports.Equal = Equal; +var Equivalent = /** @class */ (function (_super) { + __extends(Equivalent, _super); + function Equivalent(json) { + return _super.call(this, json) || this; + } + Equivalent.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a == null && b == null) { + return true; + } + else if (a == null || b == null) { + return false; + } + else { + return (0, comparison_1.equivalent)(a, b); + } + }; + return Equivalent; +}(expression_1.Expression)); +exports.Equivalent = Equivalent; +var NotEqual = /** @class */ (function (_super) { + __extends(NotEqual, _super); + function NotEqual(json) { + return _super.call(this, json) || this; + } + NotEqual.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args[0] == null || args[1] == null) { + return null; + } + // @ts-ignore + return logic_1.ThreeValuedLogic.not(comparison_1.equals.apply(void 0, this.execArgs(ctx))); + }; + return NotEqual; +}(expression_1.Expression)); +exports.NotEqual = NotEqual; +var Union = /** @class */ (function (_super) { + __extends(Union, _super); + function Union(json) { + return _super.call(this, json) || this; + } + Union.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a == null && b == null) { + return this.listTypeArgs() ? [] : null; + } + if (a == null || b == null) { + var notNull = a || b; + if ((0, util_1.typeIsArray)(notNull)) { + return notNull; + } + else { + return null; + } + } + var lib = (0, util_1.typeIsArray)(a) ? LIST : IVL; + return lib.doUnion(a, b); + }; + Union.prototype.listTypeArgs = function () { + var _a; + return (_a = this.args) === null || _a === void 0 ? void 0 : _a.some(function (arg) { + return arg.asTypeSpecifier != null && arg.asTypeSpecifier.type === 'ListTypeSpecifier'; + }); + }; + return Union; +}(expression_1.Expression)); +exports.Union = Union; +var Except = /** @class */ (function (_super) { + __extends(Except, _super); + function Except(json) { + return _super.call(this, json) || this; + } + Except.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a == null) { + return null; + } + if (b == null) { + return (0, util_1.typeIsArray)(a) ? a : null; + } + var lib = (0, util_1.typeIsArray)(a) ? LIST : IVL; + return lib.doExcept(a, b); + }; + return Except; +}(expression_1.Expression)); +exports.Except = Except; +var Intersect = /** @class */ (function (_super) { + __extends(Intersect, _super); + function Intersect(json) { + return _super.call(this, json) || this; + } + Intersect.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a == null || b == null) { + return null; + } + var lib = (0, util_1.typeIsArray)(a) ? LIST : IVL; + return lib.doIntersect(a, b); + }; + return Intersect; +}(expression_1.Expression)); +exports.Intersect = Intersect; +var Indexer = /** @class */ (function (_super) { + __extends(Indexer, _super); + function Indexer(json) { + return _super.call(this, json) || this; + } + Indexer.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), operand = _a[0], index = _a[1]; + if (operand == null || index == null) { + return null; + } + if (index < 0 || index >= operand.length) { + return null; + } + return operand[index]; + }; + return Indexer; +}(expression_1.Expression)); +exports.Indexer = Indexer; +var In = /** @class */ (function (_super) { + __extends(In, _super); + function In(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + In.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), item = _a[0], container = _a[1]; + if (item == null) { + return null; + } + if (container == null) { + return false; + } + var lib = (0, util_1.typeIsArray)(container) ? LIST : IVL; + return lib.doContains(container, item, this.precision); + }; + return In; +}(expression_1.Expression)); +exports.In = In; +var Contains = /** @class */ (function (_super) { + __extends(Contains, _super); + function Contains(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Contains.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), container = _a[0], item = _a[1]; + if (container == null) { + return false; + } + if (item == null) { + return null; + } + var lib = (0, util_1.typeIsArray)(container) ? LIST : IVL; + return lib.doContains(container, item, this.precision); + }; + return Contains; +}(expression_1.Expression)); +exports.Contains = Contains; +var Includes = /** @class */ (function (_super) { + __extends(Includes, _super); + function Includes(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Includes.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), container = _a[0], contained = _a[1]; + if (container == null || contained == null) { + return null; + } + var lib = (0, util_1.typeIsArray)(container) ? LIST : IVL; + return lib.doIncludes(container, contained, this.precision); + }; + return Includes; +}(expression_1.Expression)); +exports.Includes = Includes; +var IncludedIn = /** @class */ (function (_super) { + __extends(IncludedIn, _super); + function IncludedIn(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + IncludedIn.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), contained = _a[0], container = _a[1]; + if (container == null || contained == null) { + return null; + } + var lib = (0, util_1.typeIsArray)(container) ? LIST : IVL; + return lib.doIncludes(container, contained, this.precision); + }; + return IncludedIn; +}(expression_1.Expression)); +exports.IncludedIn = IncludedIn; +var ProperIncludes = /** @class */ (function (_super) { + __extends(ProperIncludes, _super); + function ProperIncludes(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + ProperIncludes.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), container = _a[0], contained = _a[1]; + if (container == null || contained == null) { + return null; + } + var lib = (0, util_1.typeIsArray)(container) ? LIST : IVL; + return lib.doProperIncludes(container, contained, this.precision); + }; + return ProperIncludes; +}(expression_1.Expression)); +exports.ProperIncludes = ProperIncludes; +var ProperIncludedIn = /** @class */ (function (_super) { + __extends(ProperIncludedIn, _super); + function ProperIncludedIn(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + ProperIncludedIn.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), contained = _a[0], container = _a[1]; + if (container == null || contained == null) { + return null; + } + var lib = (0, util_1.typeIsArray)(container) ? LIST : IVL; + return lib.doProperIncludes(container, contained, this.precision); + }; + return ProperIncludedIn; +}(expression_1.Expression)); +exports.ProperIncludedIn = ProperIncludedIn; +var Length = /** @class */ (function (_super) { + __extends(Length, _super); + function Length(json) { + return _super.call(this, json) || this; + } + Length.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return arg.length; + } + else if (this.arg.asTypeSpecifier.type === 'ListTypeSpecifier') { + return 0; + } + else { + return null; + } + }; + return Length; +}(expression_1.Expression)); +exports.Length = Length; +var After = /** @class */ (function (_super) { + __extends(After, _super); + function After(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + After.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a == null || b == null) { + return null; + } + var lib = a instanceof datetime_1.DateTime ? DT : IVL; + return lib.doAfter(a, b, this.precision); + }; + return After; +}(expression_1.Expression)); +exports.After = After; +var Before = /** @class */ (function (_super) { + __extends(Before, _super); + function Before(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision != null ? json.precision.toLowerCase() : undefined; + return _this; + } + Before.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a == null || b == null) { + return null; + } + var lib = a instanceof datetime_1.DateTime ? DT : IVL; + return lib.doBefore(a, b, this.precision); + }; + return Before; +}(expression_1.Expression)); +exports.Before = Before; +var SameAs = /** @class */ (function (_super) { + __extends(SameAs, _super); + function SameAs(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + SameAs.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), a = _a[0], b = _a[1]; + if (a != null && b != null) { + return a.sameAs(b, this.precision != null ? this.precision.toLowerCase() : undefined); + } + else { + return null; + } + }; + return SameAs; +}(expression_1.Expression)); +exports.SameAs = SameAs; +var SameOrAfter = /** @class */ (function (_super) { + __extends(SameOrAfter, _super); + function SameOrAfter(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + SameOrAfter.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), d1 = _a[0], d2 = _a[1]; + if (d1 != null && d2 != null) { + return d1.sameOrAfter(d2, this.precision != null ? this.precision.toLowerCase() : undefined); + } + else { + return null; + } + }; + return SameOrAfter; +}(expression_1.Expression)); +exports.SameOrAfter = SameOrAfter; +var SameOrBefore = /** @class */ (function (_super) { + __extends(SameOrBefore, _super); + function SameOrBefore(json) { + var _this = _super.call(this, json) || this; + _this.precision = json.precision; + return _this; + } + SameOrBefore.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), d1 = _a[0], d2 = _a[1]; + if (d1 != null && d2 != null) { + return d1.sameOrBefore(d2, this.precision != null ? this.precision.toLowerCase() : undefined); + } + else { + return null; + } + }; + return SameOrBefore; +}(expression_1.Expression)); +exports.SameOrBefore = SameOrBefore; +// Implemented for DateTime, Date, and Time but not for Decimal yet +var Precision = /** @class */ (function (_super) { + __extends(Precision, _super); + function Precision(json) { + return _super.call(this, json) || this; + } + Precision.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + // Since we can't extend UnimplementedExpression directly for this overloaded function, + // we have to copy the error to throw here if we are not using the correct type + if (!arg.getPrecisionValue) { + throw new Error("Unimplemented Expression: Precision"); + } + return arg.getPrecisionValue(); + }; + return Precision; +}(expression_1.Expression)); +exports.Precision = Precision; + +},{"../datatypes/datetime":57,"../datatypes/logic":60,"../util/comparison":102,"../util/util":105,"./datetime":70,"./expression":72,"./interval":76,"./list":78}],84:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ParameterRef = exports.ParameterDef = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var ParameterDef = /** @class */ (function (_super) { + __extends(ParameterDef, _super); + function ParameterDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.default = (0, builder_1.build)(json.default); + _this.parameterTypeSpecifier = json.parameterTypeSpecifier; + return _this; + } + ParameterDef.prototype.exec = function (ctx) { + // If context parameters contains the name, return value. + if (ctx && ctx.parameters[this.name] !== undefined) { + return ctx.parameters[this.name]; + // If the parent context contains the name, return that + } + else if (ctx.getParentParameter(this.name) !== undefined) { + var parentParam = ctx.getParentParameter(this.name); + return parentParam.default != null ? parentParam.default.execute(ctx) : parentParam; + // If default type exists, execute the default type + } + else if (this.default != null) { + this.default.execute(ctx); + } + }; + return ParameterDef; +}(expression_1.Expression)); +exports.ParameterDef = ParameterDef; +var ParameterRef = /** @class */ (function (_super) { + __extends(ParameterRef, _super); + function ParameterRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.library = json.libraryName; + return _this; + } + ParameterRef.prototype.exec = function (ctx) { + ctx = this.library ? ctx.getLibraryContext(this.library) : ctx; + var param = ctx.getParameter(this.name); + return param != null ? param.execute(ctx) : undefined; + }; + return ParameterRef; +}(expression_1.Expression)); +exports.ParameterRef = ParameterRef; + +},{"./builder":66,"./expression":72}],85:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Quantity = void 0; +var expression_1 = require("./expression"); +var DT = __importStar(require("../datatypes/datatypes")); +// Unit conversation is currently implemented on for time duration comparison operations +// TODO: Implement unit conversation for time duration mathematical operations +var Quantity = /** @class */ (function (_super) { + __extends(Quantity, _super); + function Quantity(json) { + var _this = _super.call(this, json) || this; + _this.value = parseFloat(json.value); + _this.unit = json.unit; + return _this; + } + Quantity.prototype.exec = function (_ctx) { + return new DT.Quantity(this.value, this.unit); + }; + return Quantity; +}(expression_1.Expression)); +exports.Quantity = Quantity; + +},{"../datatypes/datatypes":56,"./expression":72}],86:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.QueryLetRef = exports.AliasRef = exports.Query = exports.SortClause = exports.ReturnClause = exports.ByColumn = exports.ByExpression = exports.ByDirection = exports.Sort = exports.Without = exports.With = exports.LetClause = exports.AliasedQuerySource = void 0; +var expression_1 = require("./expression"); +var context_1 = require("../runtime/context"); +var util_1 = require("../util/util"); +var comparison_1 = require("../util/comparison"); +var builder_1 = require("./builder"); +var AliasedQuerySource = /** @class */ (function () { + function AliasedQuerySource(json) { + this.alias = json.alias; + this.expression = (0, builder_1.build)(json.expression); + } + return AliasedQuerySource; +}()); +exports.AliasedQuerySource = AliasedQuerySource; +var LetClause = /** @class */ (function () { + function LetClause(json) { + this.identifier = json.identifier; + this.expression = (0, builder_1.build)(json.expression); + } + return LetClause; +}()); +exports.LetClause = LetClause; +var With = /** @class */ (function (_super) { + __extends(With, _super); + function With(json) { + var _this = _super.call(this, json) || this; + _this.alias = json.alias; + _this.expression = (0, builder_1.build)(json.expression); + _this.suchThat = (0, builder_1.build)(json.suchThat); + return _this; + } + With.prototype.exec = function (ctx) { + var _this = this; + var records = this.expression.execute(ctx); + if (!(0, util_1.typeIsArray)(records)) { + records = [records]; + } + var returns = records.map(function (rec) { + var childCtx = ctx.childContext(); + childCtx.set(_this.alias, rec); + return _this.suchThat.execute(childCtx); + }); + return returns.some(function (x) { return x; }); + }; + return With; +}(expression_1.Expression)); +exports.With = With; +var Without = /** @class */ (function (_super) { + __extends(Without, _super); + function Without(json) { + return _super.call(this, json) || this; + } + Without.prototype.exec = function (ctx) { + return !_super.prototype.exec.call(this, ctx); + }; + return Without; +}(With)); +exports.Without = Without; +// ELM-only, not a product of CQL +var Sort = /** @class */ (function (_super) { + __extends(Sort, _super); + function Sort() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Sort; +}(expression_1.UnimplementedExpression)); +exports.Sort = Sort; +var ByDirection = /** @class */ (function (_super) { + __extends(ByDirection, _super); + function ByDirection(json) { + var _this = _super.call(this, json) || this; + _this.direction = json.direction; + _this.low_order = _this.direction === 'asc' || _this.direction === 'ascending' ? -1 : 1; + _this.high_order = _this.low_order * -1; + return _this; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + ByDirection.prototype.exec = function (ctx, a, b) { + if (a === b) { + return 0; + } + else if (a.isQuantity && b.isQuantity) { + if (a.before(b)) { + return this.low_order; + } + else { + return this.high_order; + } + } + else if (a < b) { + return this.low_order; + } + else { + return this.high_order; + } + }; + return ByDirection; +}(expression_1.Expression)); +exports.ByDirection = ByDirection; +var ByExpression = /** @class */ (function (_super) { + __extends(ByExpression, _super); + function ByExpression(json) { + var _this = _super.call(this, json) || this; + _this.expression = (0, builder_1.build)(json.expression); + _this.direction = json.direction; + _this.low_order = _this.direction === 'asc' || _this.direction === 'ascending' ? -1 : 1; + _this.high_order = _this.low_order * -1; + return _this; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + ByExpression.prototype.exec = function (ctx, a, b) { + var sctx = ctx.childContext(a); + var a_val = this.expression.execute(sctx); + sctx = ctx.childContext(b); + var b_val = this.expression.execute(sctx); + if (a_val === b_val || (a_val == null && b_val == null)) { + return 0; + } + else if (a_val == null || b_val == null) { + return a_val == null ? this.low_order : this.high_order; + } + else if (a_val.isQuantity && b_val.isQuantity) { + return a_val.before(b_val) ? this.low_order : this.high_order; + } + else { + return a_val < b_val ? this.low_order : this.high_order; + } + }; + return ByExpression; +}(expression_1.Expression)); +exports.ByExpression = ByExpression; +var ByColumn = /** @class */ (function (_super) { + __extends(ByColumn, _super); + function ByColumn(json) { + var _this = _super.call(this, json) || this; + _this.expression = (0, builder_1.build)({ + name: json.path, + type: 'IdentifierRef' + }); + return _this; + } + return ByColumn; +}(ByExpression)); +exports.ByColumn = ByColumn; +var ReturnClause = /** @class */ (function () { + function ReturnClause(json) { + this.expression = (0, builder_1.build)(json.expression); + this.distinct = json.distinct != null ? json.distinct : true; + } + return ReturnClause; +}()); +exports.ReturnClause = ReturnClause; +var SortClause = /** @class */ (function () { + function SortClause(json) { + this.by = (0, builder_1.build)(json != null ? json.by : undefined); + } + SortClause.prototype.sort = function (ctx, values) { + var _this = this; + if (this.by) { + return values.sort(function (a, b) { + var order = 0; + for (var _i = 0, _a = _this.by; _i < _a.length; _i++) { + var item = _a[_i]; + // Do not use execute here because the value of the sort order is not important. + order = item.exec(ctx, a, b); + if (order !== 0) { + break; + } + } + return order; + }); + } + }; + return SortClause; +}()); +exports.SortClause = SortClause; +var toDistinctList = function (xList) { + var yList = []; + xList.forEach(function (x) { + if (!yList.some(function (y) { return (0, comparison_1.equals)(x, y); })) { + yList.push(x); + } + }); + return yList; +}; +var AggregateClause = /** @class */ (function (_super) { + __extends(AggregateClause, _super); + function AggregateClause(json) { + var _this = _super.call(this, json) || this; + _this.identifier = json.identifier; + _this.expression = (0, builder_1.build)(json.expression); + _this.starting = json.starting ? (0, builder_1.build)(json.starting) : null; + _this.distinct = json.distinct != null ? json.distinct : true; + return _this; + } + AggregateClause.prototype.aggregate = function (returnedValues, ctx) { + var _this = this; + var aggregateValue = this.starting != null ? this.starting.exec(ctx) : null; + returnedValues.forEach(function (contextValues) { + var childContext = ctx.childContext(contextValues); + childContext.set(_this.identifier, aggregateValue); + aggregateValue = _this.expression.exec(childContext); + }); + return aggregateValue; + }; + return AggregateClause; +}(expression_1.Expression)); +var Query = /** @class */ (function (_super) { + __extends(Query, _super); + function Query(json) { + var _this = _super.call(this, json) || this; + _this.sources = new MultiSource(json.source.map(function (s) { return new AliasedQuerySource(s); })); + _this.letClauses = json.let != null ? json.let.map(function (d) { return new LetClause(d); }) : []; + _this.relationship = json.relationship != null ? (0, builder_1.build)(json.relationship) : []; + _this.where = (0, builder_1.build)(json.where); + _this.returnClause = json.return != null ? new ReturnClause(json.return) : null; + _this.aggregateClause = json.aggregate != null ? new AggregateClause(json.aggregate) : null; + _this.aliases = _this.sources.aliases(); + _this.sortClause = json.sort != null ? new SortClause(json.sort) : null; + return _this; + } + Query.prototype.isDistinct = function () { + if (this.aggregateClause != null && this.aggregateClause.distinct != null) { + return this.aggregateClause.distinct; + } + else if (this.returnClause != null && this.returnClause.distinct != null) { + return this.returnClause.distinct; + } + return true; + }; + Query.prototype.exec = function (ctx) { + var _this = this; + var returnedValues = []; + this.sources.forEach(ctx, function (rctx) { + for (var _i = 0, _a = _this.letClauses; _i < _a.length; _i++) { + var def = _a[_i]; + rctx.set(def.identifier, def.expression.execute(rctx)); + } + var relations = _this.relationship.map(function (rel) { + var child_ctx = rctx.childContext(); + return rel.execute(child_ctx); + }); + var passed = (0, util_1.allTrue)(relations) && (_this.where ? _this.where.execute(rctx) : true); + if (passed) { + if (_this.returnClause != null) { + var val = _this.returnClause.expression.execute(rctx); + returnedValues.push(val); + } + else { + if (_this.aliases.length === 1 && _this.aggregateClause == null) { + returnedValues.push(rctx.get(_this.aliases[0])); + } + else { + returnedValues.push(rctx.context_values); + } + } + } + }); + if (this.isDistinct()) { + returnedValues = toDistinctList(returnedValues); + } + if (this.aggregateClause != null) { + returnedValues = this.aggregateClause.aggregate(returnedValues, ctx); + } + if (this.sortClause != null) { + this.sortClause.sort(ctx, returnedValues); + } + if (this.sources.returnsList() || this.aggregateClause != null) { + return returnedValues; + } + else { + return returnedValues[0]; + } + }; + return Query; +}(expression_1.Expression)); +exports.Query = Query; +var AliasRef = /** @class */ (function (_super) { + __extends(AliasRef, _super); + function AliasRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + return _this; + } + AliasRef.prototype.exec = function (ctx) { + return ctx != null ? ctx.get(this.name) : undefined; + }; + return AliasRef; +}(expression_1.Expression)); +exports.AliasRef = AliasRef; +var QueryLetRef = /** @class */ (function (_super) { + __extends(QueryLetRef, _super); + function QueryLetRef(json) { + return _super.call(this, json) || this; + } + return QueryLetRef; +}(AliasRef)); +exports.QueryLetRef = QueryLetRef; +// The following is not defined by ELM but is helpful for execution +var MultiSource = /** @class */ (function () { + function MultiSource(sources) { + this.sources = sources; + this.alias = this.sources[0].alias; + this.expression = this.sources[0].expression; + this.isList = true; + if (this.sources.length > 1) { + this.rest = new MultiSource(this.sources.slice(1)); + } + } + MultiSource.prototype.aliases = function () { + var a = [this.alias]; + if (this.rest) { + a = a.concat(this.rest.aliases()); + } + return a; + }; + MultiSource.prototype.returnsList = function () { + return this.isList || (this.rest && this.rest.returnsList()); + }; + MultiSource.prototype.forEach = function (ctx, func) { + var _this = this; + var records = this.expression.execute(ctx); + this.isList = (0, util_1.typeIsArray)(records); + records = this.isList ? records : [records]; + return records.map(function (rec) { + var rctx = new context_1.Context(ctx); + rctx.set(_this.alias, rec); + if (_this.rest) { + return _this.rest.forEach(rctx, func); + } + else { + return func(rctx); + } + }); + }; + return MultiSource; +}()); + +},{"../runtime/context":92,"../util/comparison":102,"../util/util":105,"./builder":66,"./expression":72}],87:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Ratio = void 0; +var expression_1 = require("./expression"); +var quantity_1 = require("../datatypes/quantity"); +var DT = __importStar(require("../datatypes/datatypes")); +var Ratio = /** @class */ (function (_super) { + __extends(Ratio, _super); + function Ratio(json) { + var _this = _super.call(this, json) || this; + if (json.numerator == null) { + throw new Error('Cannot create a ratio with an undefined numerator value'); + } + else { + _this.numerator = new quantity_1.Quantity(json.numerator.value, json.numerator.unit); + } + if (json.denominator == null) { + throw new Error('Cannot create a ratio with an undefined denominator value'); + } + else { + _this.denominator = new quantity_1.Quantity(json.denominator.value, json.denominator.unit); + } + return _this; + } + Ratio.prototype.exec = function (_ctx) { + return new DT.Ratio(this.numerator, this.denominator); + }; + return Ratio; +}(expression_1.Expression)); +exports.Ratio = Ratio; + +},{"../datatypes/datatypes":56,"../datatypes/quantity":61,"./expression":72}],88:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.IdentifierRef = exports.OperandRef = exports.FunctionRef = exports.FunctionDef = exports.ExpressionRef = exports.ExpressionDef = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var ExpressionDef = /** @class */ (function (_super) { + __extends(ExpressionDef, _super); + function ExpressionDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.context = json.context; + _this.expression = (0, builder_1.build)(json.expression); + return _this; + } + ExpressionDef.prototype.exec = function (ctx) { + var value = this.expression != null ? this.expression.execute(ctx) : undefined; + ctx.rootContext().set(this.name, value); + return value; + }; + return ExpressionDef; +}(expression_1.Expression)); +exports.ExpressionDef = ExpressionDef; +var ExpressionRef = /** @class */ (function (_super) { + __extends(ExpressionRef, _super); + function ExpressionRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.library = json.libraryName; + return _this; + } + ExpressionRef.prototype.exec = function (ctx) { + ctx = this.library ? ctx.getLibraryContext(this.library) : ctx; + var value = ctx.get(this.name); + if (value instanceof expression_1.Expression) { + value = value.execute(ctx); + } + return value; + }; + return ExpressionRef; +}(expression_1.Expression)); +exports.ExpressionRef = ExpressionRef; +var FunctionDef = /** @class */ (function (_super) { + __extends(FunctionDef, _super); + function FunctionDef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.expression = (0, builder_1.build)(json.expression); + _this.parameters = json.operand; + return _this; + } + FunctionDef.prototype.exec = function (_ctx) { + return this; + }; + return FunctionDef; +}(expression_1.Expression)); +exports.FunctionDef = FunctionDef; +var FunctionRef = /** @class */ (function (_super) { + __extends(FunctionRef, _super); + function FunctionRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.library = json.libraryName; + return _this; + } + FunctionRef.prototype.exec = function (ctx) { + var functionDefs, child_ctx; + if (this.library) { + var lib = ctx.get(this.library); + functionDefs = lib ? lib.getFunction(this.name) : undefined; + var libCtx = ctx.getLibraryContext(this.library); + child_ctx = libCtx ? libCtx.childContext() : undefined; + } + else { + functionDefs = ctx.get(this.name); + child_ctx = ctx.childContext(); + } + var args = this.execArgs(ctx); + // Filter out functions w/ wrong number of arguments. + functionDefs = functionDefs.filter(function (f) { return f.parameters.length === args.length; }); + // If there is still > 1 matching function, filter by argument types + if (functionDefs.length > 1) { + functionDefs = functionDefs.filter(function (f) { + var match = true; + for (var i = 0; i < args.length && match; i++) { + if (args[i] !== null) { + var operandTypeSpecifier = f.parameters[i].operandTypeSpecifier; + if (operandTypeSpecifier == null && f.parameters[i].operandType != null) { + // convert it to a NamedTypedSpecifier + operandTypeSpecifier = { + name: f.parameters[i].operandType, + type: 'NamedTypeSpecifier' + }; + } + match = ctx.matchesTypeSpecifier(args[i], operandTypeSpecifier); + } + } + return match; + }); + } + // If there is still > 1 matching function, calculate a score based on quality of matches + if (functionDefs.length > 1) { + // TODO + } + if (functionDefs.length === 0) { + throw new Error('no function with matching signature could be found'); + } + // By this point, we should have only one function, but until implementation is completed, + // use the last one (no matter how many still remain) + var functionDef = functionDefs[functionDefs.length - 1]; + for (var i = 0; i < functionDef.parameters.length; i++) { + child_ctx.set(functionDef.parameters[i].name, args[i]); + } + return functionDef.expression.execute(child_ctx); + }; + return FunctionRef; +}(expression_1.Expression)); +exports.FunctionRef = FunctionRef; +var OperandRef = /** @class */ (function (_super) { + __extends(OperandRef, _super); + function OperandRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + return _this; + } + OperandRef.prototype.exec = function (ctx) { + return ctx.get(this.name); + }; + return OperandRef; +}(expression_1.Expression)); +exports.OperandRef = OperandRef; +var IdentifierRef = /** @class */ (function (_super) { + __extends(IdentifierRef, _super); + function IdentifierRef(json) { + var _this = _super.call(this, json) || this; + _this.name = json.name; + _this.library = json.libraryName; + return _this; + } + IdentifierRef.prototype.exec = function (ctx) { + // TODO: Technically, the ELM Translator should never output one of these + // but this code is needed since it does, as a work-around to get queries + // to work properly when sorting by a field in a tuple + var lib = this.library ? ctx.get(this.library) : undefined; + var val = lib ? lib.get(this.name) : ctx.get(this.name); + if (val == null) { + var parts = this.name.split('.'); + val = ctx.get(parts[0]); + if (val != null && parts.length > 1) { + var curr_obj = val; + for (var _i = 0, _a = parts.slice(1); _i < _a.length; _i++) { + var part = _a[_i]; + // _obj = curr_obj?[part] ? curr_obj?.get?(part) + // curr_obj = if _obj instanceof Function then _obj.call(curr_obj) else _obj + var _obj = void 0; + if (curr_obj != null) { + _obj = curr_obj[part]; + if (_obj === undefined && typeof curr_obj.get === 'function') { + _obj = curr_obj.get(part); + } + } + curr_obj = _obj instanceof Function ? _obj.call(curr_obj) : _obj; + } + val = curr_obj; + } + } + if (val instanceof Function) { + return val.call(ctx.context_values); + } + else { + return val; + } + }; + return IdentifierRef; +}(expression_1.Expression)); +exports.IdentifierRef = IdentifierRef; + +},{"./builder":66,"./expression":72}],89:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ReplaceMatches = exports.EndsWith = exports.StartsWith = exports.Substring = exports.Matches = exports.LastPositionOf = exports.PositionOf = exports.Lower = exports.Upper = exports.SplitOnMatches = exports.Split = exports.Combine = exports.Concatenate = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var Concatenate = /** @class */ (function (_super) { + __extends(Concatenate, _super); + function Concatenate(json) { + return _super.call(this, json) || this; + } + Concatenate.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args.some(function (x) { return x == null; })) { + return null; + } + else { + return args.reduce(function (x, y) { return x + y; }); + } + }; + return Concatenate; +}(expression_1.Expression)); +exports.Concatenate = Concatenate; +var Combine = /** @class */ (function (_super) { + __extends(Combine, _super); + function Combine(json) { + var _this = _super.call(this, json) || this; + _this.source = (0, builder_1.build)(json.source); + _this.separator = (0, builder_1.build)(json.separator); + return _this; + } + Combine.prototype.exec = function (ctx) { + var source = this.source.execute(ctx); + var separator = this.separator != null ? this.separator.execute(ctx) : ''; + if (source == null) { + return null; + } + else { + var filteredArray = source.filter(function (x) { return x != null; }); + if (filteredArray.length === 0) { + return null; + } + else { + return filteredArray.join(separator); + } + } + }; + return Combine; +}(expression_1.Expression)); +exports.Combine = Combine; +var Split = /** @class */ (function (_super) { + __extends(Split, _super); + function Split(json) { + var _this = _super.call(this, json) || this; + _this.stringToSplit = (0, builder_1.build)(json.stringToSplit); + _this.separator = (0, builder_1.build)(json.separator); + return _this; + } + Split.prototype.exec = function (ctx) { + var stringToSplit = this.stringToSplit.execute(ctx); + var separator = this.separator.execute(ctx); + if (stringToSplit && separator) { + return stringToSplit.split(separator); + } + return stringToSplit ? [stringToSplit] : null; + }; + return Split; +}(expression_1.Expression)); +exports.Split = Split; +var SplitOnMatches = /** @class */ (function (_super) { + __extends(SplitOnMatches, _super); + function SplitOnMatches(json) { + var _this = _super.call(this, json) || this; + _this.stringToSplit = (0, builder_1.build)(json.stringToSplit); + _this.separatorPattern = (0, builder_1.build)(json.separatorPattern); + return _this; + } + SplitOnMatches.prototype.exec = function (ctx) { + var stringToSplit = this.stringToSplit.execute(ctx); + var separatorPattern = this.separatorPattern.execute(ctx); + if (stringToSplit && separatorPattern) { + return stringToSplit.split(new RegExp(separatorPattern)); + } + return stringToSplit ? [stringToSplit] : null; + }; + return SplitOnMatches; +}(expression_1.Expression)); +exports.SplitOnMatches = SplitOnMatches; +// Length is completely handled by overloaded#Length +var Upper = /** @class */ (function (_super) { + __extends(Upper, _super); + function Upper(json) { + return _super.call(this, json) || this; + } + Upper.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return arg.toUpperCase(); + } + else { + return null; + } + }; + return Upper; +}(expression_1.Expression)); +exports.Upper = Upper; +var Lower = /** @class */ (function (_super) { + __extends(Lower, _super); + function Lower(json) { + return _super.call(this, json) || this; + } + Lower.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return arg.toLowerCase(); + } + else { + return null; + } + }; + return Lower; +}(expression_1.Expression)); +exports.Lower = Lower; +// Indexer is completely handled by overloaded#Indexer +var PositionOf = /** @class */ (function (_super) { + __extends(PositionOf, _super); + function PositionOf(json) { + var _this = _super.call(this, json) || this; + _this.pattern = (0, builder_1.build)(json.pattern); + _this.string = (0, builder_1.build)(json.string); + return _this; + } + PositionOf.prototype.exec = function (ctx) { + var pattern = this.pattern.execute(ctx); + var string = this.string.execute(ctx); + if (pattern == null || string == null) { + return null; + } + else { + return string.indexOf(pattern); + } + }; + return PositionOf; +}(expression_1.Expression)); +exports.PositionOf = PositionOf; +var LastPositionOf = /** @class */ (function (_super) { + __extends(LastPositionOf, _super); + function LastPositionOf(json) { + var _this = _super.call(this, json) || this; + _this.pattern = (0, builder_1.build)(json.pattern); + _this.string = (0, builder_1.build)(json.string); + return _this; + } + LastPositionOf.prototype.exec = function (ctx) { + var pattern = this.pattern.execute(ctx); + var string = this.string.execute(ctx); + if (pattern == null || string == null) { + return null; + } + else { + return string.lastIndexOf(pattern); + } + }; + return LastPositionOf; +}(expression_1.Expression)); +exports.LastPositionOf = LastPositionOf; +var Matches = /** @class */ (function (_super) { + __extends(Matches, _super); + function Matches(json) { + return _super.call(this, json) || this; + } + Matches.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), string = _a[0], pattern = _a[1]; + if (string == null || pattern == null) { + return null; + } + return new RegExp('^' + pattern + '$').test(string); + }; + return Matches; +}(expression_1.Expression)); +exports.Matches = Matches; +var Substring = /** @class */ (function (_super) { + __extends(Substring, _super); + function Substring(json) { + var _this = _super.call(this, json) || this; + _this.stringToSub = (0, builder_1.build)(json.stringToSub); + _this.startIndex = (0, builder_1.build)(json.startIndex); + _this.length = (0, builder_1.build)(json['length']); + return _this; + } + Substring.prototype.exec = function (ctx) { + var stringToSub = this.stringToSub.execute(ctx); + var startIndex = this.startIndex.execute(ctx); + var length = this.length != null ? this.length.execute(ctx) : null; + // According to spec: If stringToSub or startIndex is null, or startIndex is out of range, the result is null. + if (stringToSub == null || + startIndex == null || + startIndex < 0 || + startIndex >= stringToSub.length) { + return null; + } + else if (length != null) { + return stringToSub.substr(startIndex, length); + } + else { + return stringToSub.substr(startIndex); + } + }; + return Substring; +}(expression_1.Expression)); +exports.Substring = Substring; +var StartsWith = /** @class */ (function (_super) { + __extends(StartsWith, _super); + function StartsWith(json) { + return _super.call(this, json) || this; + } + StartsWith.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args.some(function (x) { return x == null; })) { + return null; + } + else { + return args[0].slice(0, args[1].length) === args[1]; + } + }; + return StartsWith; +}(expression_1.Expression)); +exports.StartsWith = StartsWith; +var EndsWith = /** @class */ (function (_super) { + __extends(EndsWith, _super); + function EndsWith(json) { + return _super.call(this, json) || this; + } + EndsWith.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args.some(function (x) { return x == null; })) { + return null; + } + else { + return args[1] === '' || args[0].slice(-args[1].length) === args[1]; + } + }; + return EndsWith; +}(expression_1.Expression)); +exports.EndsWith = EndsWith; +var ReplaceMatches = /** @class */ (function (_super) { + __extends(ReplaceMatches, _super); + function ReplaceMatches(json) { + return _super.call(this, json) || this; + } + ReplaceMatches.prototype.exec = function (ctx) { + var args = this.execArgs(ctx); + if (args.some(function (x) { return x == null; })) { + return null; + } + else { + return args[0].replace(new RegExp(args[1], 'g'), args[2]); + } + }; + return ReplaceMatches; +}(expression_1.Expression)); +exports.ReplaceMatches = ReplaceMatches; + +},{"./builder":66,"./expression":72}],90:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.TupleElementDefinition = exports.TupleElement = exports.Tuple = exports.Property = void 0; +var expression_1 = require("./expression"); +var builder_1 = require("./builder"); +var Property = /** @class */ (function (_super) { + __extends(Property, _super); + function Property(json) { + var _this = _super.call(this, json) || this; + _this.scope = json.scope; + _this.source = (0, builder_1.build)(json.source); + _this.path = json.path; + return _this; + } + Property.prototype.exec = function (ctx) { + var obj = this.scope != null ? ctx.get(this.scope) : this.source; + if (obj instanceof expression_1.Expression) { + obj = obj.execute(ctx); + } + var val = getPropertyFromObject(obj, this.path); + if (val == null) { + var parts = this.path.split('.'); + var curr_obj = obj; + for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) { + var part = parts_1[_i]; + var _obj = getPropertyFromObject(curr_obj, part); + curr_obj = _obj instanceof Function ? _obj.call(curr_obj) : _obj; + } + val = curr_obj != null ? curr_obj : null; // convert undefined to null + } + if (val instanceof Function) { + return val.call(obj); + } + else { + return val; + } + }; + return Property; +}(expression_1.Expression)); +exports.Property = Property; +function getPropertyFromObject(obj, path) { + var val; + if (obj != null) { + val = obj[path]; + if (val === undefined && typeof obj.get === 'function') { + val = obj.get(path); + } + } + return val; +} +var Tuple = /** @class */ (function (_super) { + __extends(Tuple, _super); + function Tuple(json) { + var _this = _super.call(this, json) || this; + var elements = json.element != null ? json.element : []; + _this.elements = elements.map(function (el) { + return { + name: el.name, + value: (0, builder_1.build)(el.value) + }; + }); + return _this; + } + Object.defineProperty(Tuple.prototype, "isTuple", { + get: function () { + return true; + }, + enumerable: false, + configurable: true + }); + Tuple.prototype.exec = function (ctx) { + var val = {}; + for (var _i = 0, _a = this.elements; _i < _a.length; _i++) { + var el = _a[_i]; + val[el.name] = el.value != null ? el.value.execute(ctx) : undefined; + } + return val; + }; + return Tuple; +}(expression_1.Expression)); +exports.Tuple = Tuple; +var TupleElement = /** @class */ (function (_super) { + __extends(TupleElement, _super); + function TupleElement() { + return _super !== null && _super.apply(this, arguments) || this; + } + return TupleElement; +}(expression_1.UnimplementedExpression)); +exports.TupleElement = TupleElement; +var TupleElementDefinition = /** @class */ (function (_super) { + __extends(TupleElementDefinition, _super); + function TupleElementDefinition() { + return _super !== null && _super.apply(this, arguments) || this; + } + return TupleElementDefinition; +}(expression_1.UnimplementedExpression)); +exports.TupleElementDefinition = TupleElementDefinition; + +},{"./builder":66,"./expression":72}],91:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.TupleTypeSpecifier = exports.NamedTypeSpecifier = exports.ListTypeSpecifier = exports.IntervalTypeSpecifier = exports.Is = exports.CanConvertQuantity = exports.ConvertQuantity = exports.ConvertsToTime = exports.ConvertsToString = exports.ConvertsToRatio = exports.ConvertsToQuantity = exports.ConvertsToInteger = exports.ConvertsToDecimal = exports.ConvertsToDateTime = exports.ConvertsToDate = exports.ConvertsToBoolean = exports.Convert = exports.ToTime = exports.ToString = exports.ToRatio = exports.ToQuantity = exports.ToInteger = exports.ToDecimal = exports.ToDateTime = exports.ToDate = exports.ToConcept = exports.ToBoolean = exports.As = void 0; +var expression_1 = require("./expression"); +var datetime_1 = require("../datatypes/datetime"); +var clinical_1 = require("../datatypes/clinical"); +var quantity_1 = require("../datatypes/quantity"); +var math_1 = require("../util/math"); +var util_1 = require("../util/util"); +var ratio_1 = require("../datatypes/ratio"); +var uncertainty_1 = require("../datatypes/uncertainty"); +// TODO: Casting and Conversion needs unit tests! +var As = /** @class */ (function (_super) { + __extends(As, _super); + function As(json) { + var _this = _super.call(this, json) || this; + if (json.asTypeSpecifier) { + _this.asTypeSpecifier = json.asTypeSpecifier; + } + else if (json.asType) { + // convert it to a NamedTypedSpecifier + _this.asTypeSpecifier = { + name: json.asType, + type: 'NamedTypeSpecifier' + }; + } + _this.strict = json.strict != null ? json.strict : false; + return _this; + } + As.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + // If it is null, return null + if (arg == null) { + return null; + } + if (ctx.matchesTypeSpecifier(arg, this.asTypeSpecifier)) { + // TODO: request patient source to change type identification + return arg; + } + else if (this.strict) { + var argTypeString = specifierToString(guessSpecifierType(arg)); + var asTypeString = specifierToString(this.asTypeSpecifier); + throw new Error("Cannot cast ".concat(argTypeString, " as ").concat(asTypeString)); + } + else { + return null; + } + }; + return As; +}(expression_1.Expression)); +exports.As = As; +var ToBoolean = /** @class */ (function (_super) { + __extends(ToBoolean, _super); + function ToBoolean(json) { + return _super.call(this, json) || this; + } + ToBoolean.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + var strArg = arg.toString().toLowerCase(); + if (['true', 't', 'yes', 'y', '1'].includes(strArg)) { + return true; + } + else if (['false', 'f', 'no', 'n', '0'].includes(strArg)) { + return false; + } + } + return null; + }; + return ToBoolean; +}(expression_1.Expression)); +exports.ToBoolean = ToBoolean; +var ToConcept = /** @class */ (function (_super) { + __extends(ToConcept, _super); + function ToConcept(json) { + return _super.call(this, json) || this; + } + ToConcept.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return new clinical_1.Concept([arg], arg.display); + } + else { + return null; + } + }; + return ToConcept; +}(expression_1.Expression)); +exports.ToConcept = ToConcept; +var ToDate = /** @class */ (function (_super) { + __extends(ToDate, _super); + function ToDate(json) { + return _super.call(this, json) || this; + } + ToDate.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + else if (arg.isDateTime) { + return arg.getDate(); + } + else { + return datetime_1.Date.parse(arg.toString()); + } + }; + return ToDate; +}(expression_1.Expression)); +exports.ToDate = ToDate; +var ToDateTime = /** @class */ (function (_super) { + __extends(ToDateTime, _super); + function ToDateTime(json) { + return _super.call(this, json) || this; + } + ToDateTime.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg == null) { + return null; + } + else if (arg.isDate) { + return arg.getDateTime(); + } + else { + return datetime_1.DateTime.parse(arg.toString()); + } + }; + return ToDateTime; +}(expression_1.Expression)); +exports.ToDateTime = ToDateTime; +var ToDecimal = /** @class */ (function (_super) { + __extends(ToDecimal, _super); + function ToDecimal(json) { + return _super.call(this, json) || this; + } + ToDecimal.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + if (arg.isUncertainty) { + var low = (0, math_1.limitDecimalPrecision)(parseFloat(arg.low.toString())); + var high = (0, math_1.limitDecimalPrecision)(parseFloat(arg.high.toString())); + return new uncertainty_1.Uncertainty(low, high); + } + else { + var decimal = (0, math_1.limitDecimalPrecision)(parseFloat(arg.toString())); + if ((0, math_1.isValidDecimal)(decimal)) { + return decimal; + } + } + } + return null; + }; + return ToDecimal; +}(expression_1.Expression)); +exports.ToDecimal = ToDecimal; +var ToInteger = /** @class */ (function (_super) { + __extends(ToInteger, _super); + function ToInteger(json) { + return _super.call(this, json) || this; + } + ToInteger.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (typeof arg === 'string') { + var integer = parseInt(arg); + if ((0, math_1.isValidInteger)(integer)) { + return integer; + } + } + else if (typeof arg === 'boolean') { + return arg ? 1 : 0; + } + return null; + }; + return ToInteger; +}(expression_1.Expression)); +exports.ToInteger = ToInteger; +var ToQuantity = /** @class */ (function (_super) { + __extends(ToQuantity, _super); + function ToQuantity(json) { + return _super.call(this, json) || this; + } + ToQuantity.prototype.exec = function (ctx) { + return this.convertValue(this.execArgs(ctx)); + }; + ToQuantity.prototype.convertValue = function (val) { + if (val == null) { + return null; + } + else if (typeof val === 'number') { + return new quantity_1.Quantity(val, '1'); + } + else if (val.isRatio) { + // numerator and denominator are guaranteed non-null + return val.numerator.dividedBy(val.denominator); + } + else if (val.isUncertainty) { + return new uncertainty_1.Uncertainty(this.convertValue(val.low), this.convertValue(val.high)); + } + else { + // it's a string or something else we'll try to parse as a string + return (0, quantity_1.parseQuantity)(val.toString()); + } + }; + return ToQuantity; +}(expression_1.Expression)); +exports.ToQuantity = ToQuantity; +var ToRatio = /** @class */ (function (_super) { + __extends(ToRatio, _super); + function ToRatio(json) { + return _super.call(this, json) || this; + } + ToRatio.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + // Argument will be of form ':' + var denominator = void 0, numerator = void 0; + try { + // String will be split into an array. Numerator will be at index 1, Denominator will be at index 4 + var splitRatioString = arg + .toString() + .match(/^(\d+(\.\d+)?\s*('.+')?)\s*:\s*(\d+(\.\d+)?\s*('.+')?)$/); + if (splitRatioString == null) { + return null; + } + numerator = (0, quantity_1.parseQuantity)(splitRatioString[1]); + denominator = (0, quantity_1.parseQuantity)(splitRatioString[4]); + } + catch (error) { + // If the input string is not formatted correctly, or cannot be + // interpreted as a valid Quantity value, the result is null. + return null; + } + // The value element of a Quantity must be present. + if (numerator == null || denominator == null) { + return null; + } + return new ratio_1.Ratio(numerator, denominator); + } + else { + return null; + } + }; + return ToRatio; +}(expression_1.Expression)); +exports.ToRatio = ToRatio; +var ToString = /** @class */ (function (_super) { + __extends(ToString, _super); + function ToString(json) { + return _super.call(this, json) || this; + } + ToString.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + return arg.toString(); + } + else { + return null; + } + }; + return ToString; +}(expression_1.Expression)); +exports.ToString = ToString; +var ToTime = /** @class */ (function (_super) { + __extends(ToTime, _super); + function ToTime(json) { + return _super.call(this, json) || this; + } + ToTime.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg != null) { + var timeString = arg.toString(); + // Return null if string doesn't represent a valid ISO-8601 Time + // hh:mm:ss.fff or hh:mm:ss.fff + var matches = /^T?((\d{2})(:(\d{2})(:(\d{2})(\.(\d+))?)?)?)?(Z|(([+-])(\d{2})(:?(\d{2}))?))?$/.exec(timeString); + if (matches == null) { + return null; + } + var hours = matches[2]; + var minutes = matches[4]; + var seconds = matches[6]; + // Validate h/m/s if they exist, but allow null + if (hours != null) { + if (hours < 0 || hours > 23) { + return null; + } + hours = parseInt(hours, 10); + } + if (minutes != null) { + if (minutes < 0 || minutes > 59) { + return null; + } + minutes = parseInt(minutes, 10); + } + if (seconds != null) { + if (seconds < 0 || seconds > 59) { + return null; + } + seconds = parseInt(seconds, 10); + } + var milliseconds = matches[8]; + if (milliseconds != null) { + milliseconds = parseInt((0, util_1.normalizeMillisecondsField)(milliseconds)); + } + // Time is implemented as Datetime with year 0, month 1, day 1 and null timezoneOffset + return new datetime_1.DateTime(0, 1, 1, hours, minutes, seconds, milliseconds, null); + } + else { + return null; + } + }; + return ToTime; +}(expression_1.Expression)); +exports.ToTime = ToTime; +var Convert = /** @class */ (function (_super) { + __extends(Convert, _super); + function Convert(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + _this.toType = json.toType; + return _this; + } + Convert.prototype.exec = function (ctx) { + switch (this.toType) { + case '{urn:hl7-org:elm-types:r1}Boolean': + return new ToBoolean({ type: 'ToBoolean', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}Concept': + return new ToConcept({ type: 'ToConcept', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}Decimal': + return new ToDecimal({ type: 'ToDecimal', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}Integer': + return new ToInteger({ type: 'ToInteger', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}String': + return new ToString({ type: 'ToString', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}Quantity': + return new ToQuantity({ type: 'ToQuantity', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}DateTime': + return new ToDateTime({ type: 'ToDateTime', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}Date': + return new ToDate({ type: 'ToDate', operand: this.operand }).execute(ctx); + case '{urn:hl7-org:elm-types:r1}Time': + return new ToTime({ type: 'ToTime', operand: this.operand }).execute(ctx); + default: + return this.execArgs(ctx); + } + }; + return Convert; +}(expression_1.Expression)); +exports.Convert = Convert; +var ConvertsToBoolean = /** @class */ (function (_super) { + __extends(ConvertsToBoolean, _super); + function ConvertsToBoolean(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToBoolean.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToBoolean, this.operand, ctx); + } + }; + return ConvertsToBoolean; +}(expression_1.Expression)); +exports.ConvertsToBoolean = ConvertsToBoolean; +var ConvertsToDate = /** @class */ (function (_super) { + __extends(ConvertsToDate, _super); + function ConvertsToDate(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToDate.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToDate, this.operand, ctx); + } + }; + return ConvertsToDate; +}(expression_1.Expression)); +exports.ConvertsToDate = ConvertsToDate; +var ConvertsToDateTime = /** @class */ (function (_super) { + __extends(ConvertsToDateTime, _super); + function ConvertsToDateTime(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToDateTime.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToDateTime, this.operand, ctx); + } + }; + return ConvertsToDateTime; +}(expression_1.Expression)); +exports.ConvertsToDateTime = ConvertsToDateTime; +var ConvertsToDecimal = /** @class */ (function (_super) { + __extends(ConvertsToDecimal, _super); + function ConvertsToDecimal(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToDecimal.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToDecimal, this.operand, ctx); + } + }; + return ConvertsToDecimal; +}(expression_1.Expression)); +exports.ConvertsToDecimal = ConvertsToDecimal; +var ConvertsToInteger = /** @class */ (function (_super) { + __extends(ConvertsToInteger, _super); + function ConvertsToInteger(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToInteger.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToInteger, this.operand, ctx); + } + }; + return ConvertsToInteger; +}(expression_1.Expression)); +exports.ConvertsToInteger = ConvertsToInteger; +var ConvertsToQuantity = /** @class */ (function (_super) { + __extends(ConvertsToQuantity, _super); + function ConvertsToQuantity(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToQuantity.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToQuantity, this.operand, ctx); + } + }; + return ConvertsToQuantity; +}(expression_1.Expression)); +exports.ConvertsToQuantity = ConvertsToQuantity; +var ConvertsToRatio = /** @class */ (function (_super) { + __extends(ConvertsToRatio, _super); + function ConvertsToRatio(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToRatio.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToRatio, this.operand, ctx); + } + }; + return ConvertsToRatio; +}(expression_1.Expression)); +exports.ConvertsToRatio = ConvertsToRatio; +var ConvertsToString = /** @class */ (function (_super) { + __extends(ConvertsToString, _super); + function ConvertsToString(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToString.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToString, this.operand, ctx); + } + }; + return ConvertsToString; +}(expression_1.Expression)); +exports.ConvertsToString = ConvertsToString; +var ConvertsToTime = /** @class */ (function (_super) { + __extends(ConvertsToTime, _super); + function ConvertsToTime(json) { + var _this = _super.call(this, json) || this; + _this.operand = json.operand; + return _this; + } + ConvertsToTime.prototype.exec = function (ctx) { + var operatorValue = this.execArgs(ctx); + if (operatorValue === null) { + return null; + } + else { + return canConvertToType(ToTime, this.operand, ctx); + } + }; + return ConvertsToTime; +}(expression_1.Expression)); +exports.ConvertsToTime = ConvertsToTime; +function canConvertToType(toFunction, operand, ctx) { + try { + var value = new toFunction({ type: toFunction.name, operand: operand }).execute(ctx); + if (value != null) { + return true; + } + else { + return false; + } + } + catch (error) { + return false; + } +} +var ConvertQuantity = /** @class */ (function (_super) { + __extends(ConvertQuantity, _super); + function ConvertQuantity(json) { + return _super.call(this, json) || this; + } + ConvertQuantity.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), quantity = _a[0], newUnit = _a[1]; + if (quantity != null && newUnit != null) { + try { + return quantity.convertUnit(newUnit); + } + catch (error) { + // Cannot convert input to target unit, spec says to return null + return null; + } + } + }; + return ConvertQuantity; +}(expression_1.Expression)); +exports.ConvertQuantity = ConvertQuantity; +var CanConvertQuantity = /** @class */ (function (_super) { + __extends(CanConvertQuantity, _super); + function CanConvertQuantity(json) { + return _super.call(this, json) || this; + } + CanConvertQuantity.prototype.exec = function (ctx) { + var _a = this.execArgs(ctx), quantity = _a[0], newUnit = _a[1]; + if (quantity != null && newUnit != null) { + try { + quantity.convertUnit(newUnit); + return true; + } + catch (error) { + return false; + } + } + return null; + }; + return CanConvertQuantity; +}(expression_1.Expression)); +exports.CanConvertQuantity = CanConvertQuantity; +var Is = /** @class */ (function (_super) { + __extends(Is, _super); + function Is(json) { + var _this = _super.call(this, json) || this; + if (json.isTypeSpecifier) { + _this.isTypeSpecifier = json.isTypeSpecifier; + } + else if (json.isType) { + // Convert it to a NamedTypeSpecifier + _this.isTypeSpecifier = { + name: json.isType, + type: 'NamedTypeSpecifier' + }; + } + return _this; + } + Is.prototype.exec = function (ctx) { + var arg = this.execArgs(ctx); + if (arg === null) { + return false; + } + if (typeof arg._is !== 'function' && !isSystemType(this.isTypeSpecifier)) { + // We need an _is implementation in order to check non System types + throw new Error("Patient Source does not support Is operation for localId: ".concat(this.localId)); + } + return ctx.matchesTypeSpecifier(arg, this.isTypeSpecifier); + }; + return Is; +}(expression_1.Expression)); +exports.Is = Is; +function isSystemType(spec) { + switch (spec.type) { + case 'NamedTypeSpecifier': + return spec.name.startsWith('{urn:hl7-org:elm-types:r1}'); + case 'ListTypeSpecifier': + return isSystemType(spec.elementType); + case 'TupleTypeSpecifier': + return spec.element.every(function (e) { return isSystemType(e.elementType); }); + case 'IntervalTypeSpecifier': + return isSystemType(spec.pointType); + case 'ChoiceTypeSpecifier': + return spec.choice.every(function (c) { return isSystemType(c); }); + default: + return false; + } +} +function specifierToString(spec) { + if (typeof spec === 'string') { + return spec; + } + else if (spec == null || spec.type == null) { + return ''; + } + switch (spec.type) { + case 'NamedTypeSpecifier': + return spec.name; + case 'ListTypeSpecifier': + return "List<".concat(specifierToString(spec.elementType), ">"); + case 'TupleTypeSpecifier': + return "Tuple<".concat(spec.element + .map(function (e) { return "".concat(e.name, " ").concat(specifierToString(e.elementType)); }) + .join(', '), ">"); + case 'IntervalTypeSpecifier': + return "Interval<".concat(specifierToString(spec.pointType), ">"); + case 'ChoiceTypeSpecifier': + return "Choice<".concat(spec.choice.map(function (c) { return specifierToString(c); }).join(', '), ">"); + default: + return JSON.stringify(spec); + } +} +function guessSpecifierType(val) { + if (val == null) { + return 'Null'; + } + var typeHierarchy = typeof val._typeHierarchy === 'function' && val._typeHierarchy(); + if (typeHierarchy && typeHierarchy.length > 0) { + return typeHierarchy[0]; + } + else if (typeof val === 'boolean') { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Boolean' }; + } + else if (typeof val === 'number' && Math.floor(val) === val) { + // it could still be a decimal, but we have to just take our best guess! + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Integer' }; + } + else if (typeof val === 'number') { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Decimal' }; + } + else if (typeof val === 'string') { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}String' }; + } + else if (val.isConcept) { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Concept' }; + } + else if (val.isCode) { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Code' }; + } + else if (val.isDate) { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Date' }; + } + else if (val.isTime && val.isTime()) { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}Time' }; + } + else if (val.isDateTime) { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}DateTime' }; + } + else if (val.isQuantity) { + return { type: 'NamedTypeSpecifier', name: '{urn:hl7-org:elm-types:r1}DateTime' }; + } + else if (Array.isArray(val)) { + // Get unique types from the array (by converting to string and putting in a Set) + var typesAsStrings = Array.from(new Set(val.map(function (v) { return JSON.stringify(guessSpecifierType(v)); }))); + var types = typesAsStrings.map(function (ts) { return (/^{/.test(ts) ? JSON.parse(ts) : ts); }); + return { + type: 'ListTypeSpecifier', + elementType: types.length == 1 ? types[0] : { type: 'ChoiceTypeSpecifier', choice: types } + }; + } + else if (val.isInterval) { + return { + type: 'IntervalTypeSpecifier', + pointType: val.pointType + }; + } + else if (typeof val === 'object' && Object.keys(val).length > 0) { + return { + type: 'TupleTypeSpecifier', + element: Object.keys(val).map(function (k) { return ({ name: k, elementType: guessSpecifierType(val[k]) }); }) + }; + } + return 'Unknown'; +} +var IntervalTypeSpecifier = /** @class */ (function (_super) { + __extends(IntervalTypeSpecifier, _super); + function IntervalTypeSpecifier() { + return _super !== null && _super.apply(this, arguments) || this; + } + return IntervalTypeSpecifier; +}(expression_1.UnimplementedExpression)); +exports.IntervalTypeSpecifier = IntervalTypeSpecifier; +var ListTypeSpecifier = /** @class */ (function (_super) { + __extends(ListTypeSpecifier, _super); + function ListTypeSpecifier() { + return _super !== null && _super.apply(this, arguments) || this; + } + return ListTypeSpecifier; +}(expression_1.UnimplementedExpression)); +exports.ListTypeSpecifier = ListTypeSpecifier; +var NamedTypeSpecifier = /** @class */ (function (_super) { + __extends(NamedTypeSpecifier, _super); + function NamedTypeSpecifier() { + return _super !== null && _super.apply(this, arguments) || this; + } + return NamedTypeSpecifier; +}(expression_1.UnimplementedExpression)); +exports.NamedTypeSpecifier = NamedTypeSpecifier; +var TupleTypeSpecifier = /** @class */ (function (_super) { + __extends(TupleTypeSpecifier, _super); + function TupleTypeSpecifier() { + return _super !== null && _super.apply(this, arguments) || this; + } + return TupleTypeSpecifier; +}(expression_1.UnimplementedExpression)); +exports.TupleTypeSpecifier = TupleTypeSpecifier; + +},{"../datatypes/clinical":55,"../datatypes/datetime":57,"../datatypes/quantity":61,"../datatypes/ratio":62,"../datatypes/uncertainty":63,"../util/math":103,"../util/util":105,"./expression":72}],92:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.UnfilteredContext = exports.PatientContext = exports.Context = void 0; +var exception_1 = require("../datatypes/exception"); +var util_1 = require("../util/util"); +var dt = __importStar(require("../datatypes/datatypes")); +var messageListeners_1 = require("./messageListeners"); +var Context = /** @class */ (function () { + function Context(parent, _codeService, _parameters, executionDateTime, messageListener) { + this.parent = parent; + this._codeService = _codeService; + this.context_values = {}; + this.library_context = {}; + this.localId_context = {}; + this.evaluatedRecords = []; + // TODO: If there is an issue with number of parameters look into cql4browsers fix: 387ea77538182833283af65e6341e7a05192304c + this.checkParameters(_parameters !== null && _parameters !== void 0 ? _parameters : {}); // not crazy about possibly throwing an error in a constructor, but... + this._parameters = _parameters || {}; + this.executionDateTime = executionDateTime; + this.messageListener = messageListener; + } + Object.defineProperty(Context.prototype, "parameters", { + get: function () { + return this._parameters || (this.parent && this.parent.parameters); + }, + set: function (params) { + this.checkParameters(params); + this._parameters = params; + }, + enumerable: false, + configurable: true + }); + Object.defineProperty(Context.prototype, "codeService", { + get: function () { + return this._codeService || (this.parent && this.parent.codeService); + }, + set: function (cs) { + this._codeService = cs; + }, + enumerable: false, + configurable: true + }); + Context.prototype.withParameters = function (params) { + this.parameters = params || {}; + return this; + }; + Context.prototype.withCodeService = function (cs) { + this.codeService = cs; + return this; + }; + Context.prototype.rootContext = function () { + if (this.parent) { + return this.parent.rootContext(); + } + else { + return this; + } + }; + Context.prototype.findRecords = function (profile, retrieveDetails) { + return this.parent && this.parent.findRecords(profile, retrieveDetails); + }; + Context.prototype.childContext = function (context_values) { + if (context_values === void 0) { context_values = {}; } + var ctx = new Context(this); + ctx.context_values = context_values; + return ctx; + }; + Context.prototype.getLibraryContext = function (library) { + return this.parent && this.parent.getLibraryContext(library); + }; + Context.prototype.getLocalIdContext = function (localId) { + return this.parent && this.parent.getLocalIdContext(localId); + }; + Context.prototype.getParameter = function (name) { + return this.parent && this.parent.getParameter(name); + }; + Context.prototype.getParentParameter = function (name) { + if (this.parent) { + if (this.parent.parameters[name] != null) { + return this.parent.parameters[name]; + } + else { + return this.parent.getParentParameter(name); + } + } + }; + Context.prototype.getTimezoneOffset = function () { + if (this.executionDateTime != null) { + return this.executionDateTime.timezoneOffset; + } + else if (this.parent && this.parent.getTimezoneOffset != null) { + return this.parent.getTimezoneOffset(); + } + else { + throw new exception_1.Exception('No Timezone Offset has been set'); + } + }; + Context.prototype.getExecutionDateTime = function () { + if (this.executionDateTime != null) { + return this.executionDateTime; + } + else if (this.parent && this.parent.getExecutionDateTime != null) { + return this.parent.getExecutionDateTime(); + } + else { + throw new exception_1.Exception('No Execution DateTime has been set'); + } + }; + Context.prototype.getMessageListener = function () { + if (this.messageListener != null) { + return this.messageListener; + } + else if (this.parent && this.parent.getMessageListener != null) { + return this.parent.getMessageListener(); + } + else { + return new messageListeners_1.NullMessageListener(); + } + }; + Context.prototype.getValueSet = function (name, library) { + return this.parent && this.parent.getValueSet(name, library); + }; + Context.prototype.getCodeSystem = function (name) { + return this.parent && this.parent.getCodeSystem(name); + }; + Context.prototype.getCode = function (name) { + return this.parent && this.parent.getCode(name); + }; + Context.prototype.getConcept = function (name) { + return this.parent && this.parent.getConcept(name); + }; + Context.prototype.get = function (identifier) { + // Check for undefined because if its null, we actually *do* want to return null (rather than + // looking at parent), but if it's really undefined, *then* look at the parent + if (typeof this.context_values[identifier] !== 'undefined') { + return this.context_values[identifier]; + } + else if (identifier === '$this') { + return this.context_values; + } + else { + return this.parent != null && this.parent.get(identifier); + } + }; + Context.prototype.set = function (identifier, value) { + this.context_values[identifier] = value; + }; + Context.prototype.setLocalIdWithResult = function (localId, value) { + // Temporary fix. Real fix will be to return a list of all result values for a given localId. + var ctx = this.localId_context[localId]; + if (ctx === false || ctx === null || ctx === undefined || ctx.length === 0) { + this.localId_context[localId] = value; + } + }; + Context.prototype.getLocalIdResult = function (localId) { + return this.localId_context[localId]; + }; + // Returns an object of objects containing each library name + // with the localIds and result values + Context.prototype.getAllLocalIds = function () { + var localIdResults = {}; + // Add the localIds and result values from the main library + localIdResults[this.parent.source.library.identifier.id] = {}; + localIdResults[this.parent.source.library.identifier.id] = this.localId_context; + // Iterate over support libraries and store localIds + for (var libName in this.library_context) { + var lib = this.library_context[libName]; + this.supportLibraryLocalIds(lib, localIdResults); + } + return localIdResults; + }; + // Recursive function that will grab nested support library localId results + Context.prototype.supportLibraryLocalIds = function (lib, localIdResults) { + var _this = this; + // Set library identifier name as the key and the object of localIds with their results as the value + // if it already exists then we need to merge the results instead of overwriting + if (localIdResults[lib.library.source.library.identifier.id] != null) { + this.mergeLibraryLocalIdResults(localIdResults, lib.library.source.library.identifier.id, lib.localId_context); + } + else { + localIdResults[lib.library.source.library.identifier.id] = lib.localId_context; + } + // Iterate over any support libraries in the current support library + Object.values(lib.library_context).forEach(function (supportLib) { + _this.supportLibraryLocalIds(supportLib, localIdResults); + }); + }; + // Merges the localId results for a library into the already collected results. The logic used for which result + // to keep is the same as the logic used above in setLocalIdWithResult, "falsey" results are always replaced. + Context.prototype.mergeLibraryLocalIdResults = function (localIdResults, libraryId, libraryResults) { + for (var localId in libraryResults) { + var localIdResult = libraryResults[localId]; + var existingResult = localIdResults[libraryId][localId]; + // overwite this localid result if the existing result is "falsey". future work could track all results for each localid + if (existingResult === false || + existingResult === null || + existingResult === undefined || + existingResult.length === 0) { + localIdResults[libraryId][localId] = localIdResult; + } + } + }; + Context.prototype.checkParameters = function (params) { + for (var pName in params) { + var pVal = params[pName]; + var pDef = this.getParameter(pName); + if (pVal == null) { + return; // Null can theoretically be any type + } + if (typeof pDef === 'undefined') { + return; // This will happen if the parameter is declared in a different (included) library + } + else if (pDef.parameterTypeSpecifier != null && + !this.matchesTypeSpecifier(pVal, pDef.parameterTypeSpecifier)) { + throw new Error("Passed in parameter '".concat(pName, "' is wrong type")); + } + else if (pDef['default'] != null && !this.matchesInstanceType(pVal, pDef['default'])) { + throw new Error("Passed in parameter '".concat(pName, "' is wrong type")); + } + } + return true; + }; + Context.prototype.matchesTypeSpecifier = function (val, spec) { + switch (spec.type) { + case 'NamedTypeSpecifier': + return this.matchesNamedTypeSpecifier(val, spec); + case 'ListTypeSpecifier': + return this.matchesListTypeSpecifier(val, spec); + case 'TupleTypeSpecifier': + return this.matchesTupleTypeSpecifier(val, spec); + case 'IntervalTypeSpecifier': + return this.matchesIntervalTypeSpecifier(val, spec); + case 'ChoiceTypeSpecifier': + return this.matchesChoiceTypeSpecifier(val, spec); + default: + return true; // default to true when we don't know + } + }; + Context.prototype.matchesListTypeSpecifier = function (val, spec) { + var _this = this; + return ((0, util_1.typeIsArray)(val) && val.every(function (x) { return _this.matchesTypeSpecifier(x, spec.elementType); })); + }; + Context.prototype.matchesTupleTypeSpecifier = function (val, spec) { + var _this = this; + // TODO: Spec is not clear about exactly how tuples should be matched + return (val != null && + typeof val === 'object' && + !(0, util_1.typeIsArray)(val) && + !val.isInterval && + !val.isConcept && + !val.isCode && + !val.isDateTime && + !val.isDate && + !val.isQuantity && + spec.element.every(function (x) { + return typeof val[x.name] === 'undefined' || + _this.matchesTypeSpecifier(val[x.name], x.elementType); + })); + }; + Context.prototype.matchesIntervalTypeSpecifier = function (val, spec) { + return (val.isInterval && + (val.low == null || this.matchesTypeSpecifier(val.low, spec.pointType)) && + (val.high == null || this.matchesTypeSpecifier(val.high, spec.pointType))); + }; + Context.prototype.matchesChoiceTypeSpecifier = function (val, spec) { + var _this = this; + return spec.choice.some(function (c) { return _this.matchesTypeSpecifier(val, c); }); + }; + Context.prototype.matchesNamedTypeSpecifier = function (val, spec) { + if (val == null) { + return true; + } + switch (spec.name) { + case '{urn:hl7-org:elm-types:r1}Boolean': + return typeof val === 'boolean'; + case '{urn:hl7-org:elm-types:r1}Decimal': + return typeof val === 'number'; + case '{urn:hl7-org:elm-types:r1}Integer': + return typeof val === 'number' && Math.floor(val) === val; + case '{urn:hl7-org:elm-types:r1}String': + return typeof val === 'string'; + case '{urn:hl7-org:elm-types:r1}Concept': + return val && val.isConcept; + case '{urn:hl7-org:elm-types:r1}Code': + return val && val.isCode; + case '{urn:hl7-org:elm-types:r1}DateTime': + return val && val.isDateTime; + case '{urn:hl7-org:elm-types:r1}Date': + return val && val.isDate; + case '{urn:hl7-org:elm-types:r1}Quantity': + return val && val.isQuantity; + case '{urn:hl7-org:elm-types:r1}Time': + return val && val.isTime && val.isTime(); + default: + // Use the data model's implementation of _is, if it is available + if (typeof val._is === 'function') { + return val._is(spec); + } + // If the value is an array or interval, then we assume it cannot be cast to a + // named type. Technically, this is not 100% true because a modelinfo can define + // a named type whose base type is a list or interval. But none of our models + // (FHIR, QDM, QICore) do that, so for those models, this approach will always be + // correct. + if (Array.isArray(val) || val.isInterval) { + return false; + } + // Otherwise just default to true to match legacy behavior. + // + // NOTE: This is also where arbitrary tuples land because they will not have + // an "is" function and we don't encode the type information into the runtime + // objects so we can't easily determine their type. We can't reject them, + // else things like `Encounter{ id: "1" } is Encounter` would return false. + // So for now we allow false positives in order to avoid false negatives. + return true; + } + }; + Context.prototype.matchesInstanceType = function (val, inst) { + if (inst.isBooleanLiteral) { + return typeof val === 'boolean'; + } + else if (inst.isDecimalLiteral) { + return typeof val === 'number'; + } + else if (inst.isIntegerLiteral) { + return typeof val === 'number' && Math.floor(val) === val; + } + else if (inst.isStringLiteral) { + return typeof val === 'string'; + } + else if (inst.isCode) { + return val && val.isCode; + } + else if (inst.isConcept) { + return val && val.isConcept; + } + else if (inst.isTime && inst.isTime()) { + return val && val.isTime && val.isTime(); + } + else if (inst.isDate) { + return val && val.isDate; + } + else if (inst.isDateTime) { + return val && val.isDateTime; + } + else if (inst.isQuantity) { + return val && val.isQuantity; + } + else if (inst.isList) { + return this.matchesListInstanceType(val, inst); + } + else if (inst.isTuple) { + return this.matchesTupleInstanceType(val, inst); + } + else if (inst.isInterval) { + return this.matchesIntervalInstanceType(val, inst); + } + return true; // default to true when we don't know for sure + }; + Context.prototype.matchesListInstanceType = function (val, list) { + var _this = this; + return ((0, util_1.typeIsArray)(val) && val.every(function (x) { return _this.matchesInstanceType(x, list.elements[0]); })); + }; + Context.prototype.matchesTupleInstanceType = function (val, tpl) { + var _this = this; + return (typeof val === 'object' && + !(0, util_1.typeIsArray)(val) && + tpl.elements.every(function (x) { + return typeof val[x.name] === 'undefined' || _this.matchesInstanceType(val[x.name], x.value); + })); + }; + Context.prototype.matchesIntervalInstanceType = function (val, ivl) { + var pointType = ivl.low != null ? ivl.low : ivl.high; + return (val.isInterval && + (val.low == null || this.matchesInstanceType(val.low, pointType)) && + (val.high == null || this.matchesInstanceType(val.high, pointType))); + }; + return Context; +}()); +exports.Context = Context; +var PatientContext = /** @class */ (function (_super) { + __extends(PatientContext, _super); + function PatientContext(library, patient, codeService, parameters, executionDateTime, messageListener) { + if (executionDateTime === void 0) { executionDateTime = dt.DateTime.fromJSDate(new Date()); } + if (messageListener === void 0) { messageListener = new messageListeners_1.NullMessageListener(); } + var _this = _super.call(this, library, codeService, parameters, executionDateTime, messageListener) || this; + _this.library = library; + _this.patient = patient; + return _this; + } + PatientContext.prototype.rootContext = function () { + return this; + }; + PatientContext.prototype.getLibraryContext = function (library) { + if (this.library_context[library] == null) { + this.library_context[library] = new PatientContext(this.get(library), this.patient, this.codeService, this.parameters, this.executionDateTime); + } + return this.library_context[library]; + }; + PatientContext.prototype.getLocalIdContext = function (localId) { + if (this.localId_context[localId] == null) { + this.localId_context[localId] = new PatientContext(this.get(localId), this.patient, this.codeService, this.parameters, this.executionDateTime); + } + return this.localId_context[localId]; + }; + PatientContext.prototype.findRecords = function (profile, retrieveDetails) { + return this.patient && this.patient.findRecords(profile, retrieveDetails); + }; + return PatientContext; +}(Context)); +exports.PatientContext = PatientContext; +var UnfilteredContext = /** @class */ (function (_super) { + __extends(UnfilteredContext, _super); + function UnfilteredContext(library, results, codeService, parameters, executionDateTime, messageListener) { + if (executionDateTime === void 0) { executionDateTime = dt.DateTime.fromJSDate(new Date()); } + if (messageListener === void 0) { messageListener = new messageListeners_1.NullMessageListener(); } + var _this = _super.call(this, library, codeService, parameters, executionDateTime, messageListener) || this; + _this.library = library; + _this.results = results; + return _this; + } + UnfilteredContext.prototype.rootContext = function () { + return this; + }; + UnfilteredContext.prototype.findRecords = function (_template) { + throw new exception_1.Exception('Retreives are not currently supported in Unfiltered Context'); + }; + UnfilteredContext.prototype.getLibraryContext = function (_library) { + throw new exception_1.Exception('Library expressions are not currently supported in Unfiltered Context'); + }; + UnfilteredContext.prototype.get = function (identifier) { + //First check to see if the identifier is a unfiltered context expression that has already been cached + if (this.context_values[identifier]) { + return this.context_values[identifier]; + } + //if not look to see if the library has a unfiltered expression of that identifier + if (this.library[identifier] && this.library[identifier].context === 'Unfiltered') { + return this.library.expressions[identifier]; + } + //lastley attempt to gather all patient level results that have that identifier + // should this compact null values before return ? + return Object.values(this.results.patientResults).map(function (pr) { return pr[identifier]; }); + }; + return UnfilteredContext; +}(Context)); +exports.UnfilteredContext = UnfilteredContext; + +},{"../datatypes/datatypes":56,"../datatypes/exception":58,"../util/util":105,"./messageListeners":94}],93:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Executor = void 0; +var messageListeners_1 = require("./messageListeners"); +var results_1 = require("./results"); +var context_1 = require("./context"); +var Executor = /** @class */ (function () { + function Executor(library, codeService, parameters, messageListener) { + if (messageListener === void 0) { messageListener = new messageListeners_1.NullMessageListener(); } + this.library = library; + this.codeService = codeService; + this.parameters = parameters; + this.messageListener = messageListener; + } + Executor.prototype.withLibrary = function (lib) { + this.library = lib; + return this; + }; + Executor.prototype.withParameters = function (params) { + this.parameters = params != null ? params : {}; + return this; + }; + Executor.prototype.withCodeService = function (cs) { + this.codeService = cs; + return this; + }; + Executor.prototype.withMessageListener = function (ml) { + this.messageListener = ml; + return this; + }; + Executor.prototype.exec_expression = function (expression, patientSource, executionDateTime) { + var _a; + var r = new results_1.Results(); + var expr = this.library.expressions[expression]; + if (expr != null) { + while (patientSource.currentPatient()) { + var patient_ctx = new context_1.PatientContext(this.library, patientSource.currentPatient(), this.codeService, this.parameters, executionDateTime, this.messageListener); + r.recordPatientResults(patient_ctx, (_a = {}, _a[expression] = expr.execute(patient_ctx), _a)); + patientSource.nextPatient(); + } + } + return r; + }; + Executor.prototype.exec = function (patientSource, executionDateTime) { + var r = this.exec_patient_context(patientSource, executionDateTime); + var unfilteredContext = new context_1.UnfilteredContext(this.library, r, this.codeService, this.parameters, executionDateTime, this.messageListener); + var resultMap = {}; + for (var key in this.library.expressions) { + var expr = this.library.expressions[key]; + if (expr.context === 'Unfiltered') { + resultMap[key] = expr.exec(unfilteredContext); + } + } + r.recordUnfilteredResults(resultMap); + return r; + }; + Executor.prototype.exec_patient_context = function (patientSource, executionDateTime) { + var r = new results_1.Results(); + while (patientSource.currentPatient()) { + var patient_ctx = new context_1.PatientContext(this.library, patientSource.currentPatient(), this.codeService, this.parameters, executionDateTime, this.messageListener); + var resultMap = {}; + for (var key in this.library.expressions) { + var expr = this.library.expressions[key]; + if (expr.context === 'Patient') { + resultMap[key] = expr.execute(patient_ctx); + } + } + r.recordPatientResults(patient_ctx, resultMap); + patientSource.nextPatient(); + } + return r; + }; + return Executor; +}()); +exports.Executor = Executor; + +},{"./context":92,"./messageListeners":94,"./results":96}],94:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ConsoleMessageListener = exports.NullMessageListener = void 0; +var NullMessageListener = /** @class */ (function () { + function NullMessageListener() { + } + NullMessageListener.prototype.onMessage = function (_source, _code, _severity, _message) { + // do nothing + }; + return NullMessageListener; +}()); +exports.NullMessageListener = NullMessageListener; +var ConsoleMessageListener = /** @class */ (function () { + function ConsoleMessageListener(logSourceOnTrace) { + if (logSourceOnTrace === void 0) { logSourceOnTrace = false; } + this.logSourceOnTrace = logSourceOnTrace; + } + ConsoleMessageListener.prototype.onMessage = function (source, code, severity, message) { + // eslint-disable-next-line no-console + var print = severity === 'Error' ? console.error : console.log; + var content = "".concat(severity, ": [").concat(code, "] ").concat(message); + if (severity === 'Trace' && this.logSourceOnTrace) { + content += "\n<<<<< SOURCE:\n".concat(JSON.stringify(source), "\n>>>>>"); + } + print(content); + }; + return ConsoleMessageListener; +}()); +exports.ConsoleMessageListener = ConsoleMessageListener; + +},{}],95:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Repository = void 0; +var library_1 = require("../elm/library"); +var Repository = /** @class */ (function () { + function Repository(data) { + this.data = data; + this.libraries = Array.from(Object.values(data)); + } + Repository.prototype.resolve = function (path, version) { + for (var _i = 0, _a = this.libraries; _i < _a.length; _i++) { + var lib = _a[_i]; + if (lib.library && lib.library.identifier) { + var _b = lib.library.identifier, id = _b.id, system = _b.system, libraryVersion = _b.version; + var libraryUri = "".concat(system, "/").concat(id); + if (path === libraryUri || path === id) { + if (version) { + if (libraryVersion === version) { + return new library_1.Library(lib, this); + } + } + else { + return new library_1.Library(lib, this); + } + } + } + } + }; + return Repository; +}()); +exports.Repository = Repository; + +},{"../elm/library":77}],96:[function(require,module,exports){ +"use strict"; +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Results = void 0; +var Results = /** @class */ (function () { + function Results() { + this.patientResults = {}; + this.unfilteredResults = {}; + this.localIdPatientResultsMap = {}; + this.patientEvaluatedRecords = {}; + } + Object.defineProperty(Results.prototype, "evaluatedRecords", { + // Expose an evaluatedRecords array for backwards compatibility + get: function () { + return [].concat.apply([], Object.values(this.patientEvaluatedRecords)); + }, + enumerable: false, + configurable: true + }); + Results.prototype.recordPatientResults = function (patient_ctx, resultMap) { + var _this = this; + var p = patient_ctx.patient; + // NOTE: From now on prefer getId() over id() because some data models may have an id property + // that is not a string (e.g., FHIR) -- so reserve getId() for the API (and expect a string + // representation) but leave id() for data-model specific formats. + var patientId = typeof p.getId === 'function' ? p.getId() : p.id(); + // Record the results + this.patientResults[patientId] = resultMap; + // Record the local IDs + this.localIdPatientResultsMap[patientId] = patient_ctx.getAllLocalIds(); + // Record the evaluatedRecords, merging with an aggregated array across all libraries + this.patientEvaluatedRecords[patientId] = __spreadArray([], patient_ctx.evaluatedRecords, true); + Object.values(patient_ctx.library_context).forEach(function (ctx) { + var _a; + (_a = _this.patientEvaluatedRecords[patientId]).push.apply(_a, ctx.evaluatedRecords); + }); + }; + Results.prototype.recordUnfilteredResults = function (resultMap) { + this.unfilteredResults = resultMap; + }; + return Results; +}()); +exports.Results = Results; + +},{}],97:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); + +},{}],98:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); + +},{}],99:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./cql-code-service.interfaces"), exports); +__exportStar(require("./cql-patient.interfaces"), exports); +__exportStar(require("./runtime.types"), exports); +__exportStar(require("./type-specifiers.interfaces"), exports); + +},{"./cql-code-service.interfaces":97,"./cql-patient.interfaces":98,"./runtime.types":100,"./type-specifiers.interfaces":101}],100:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); + +},{}],101:[function(require,module,exports){ +"use strict"; +// Types derived from http://cql.hl7.org/04-logicalspecification.html#typespecifier +Object.defineProperty(exports, "__esModule", { value: true }); + +},{}],102:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.equals = exports.equivalent = exports.greaterThanOrEquals = exports.greaterThan = exports.lessThanOrEquals = exports.lessThan = void 0; +var datatypes_1 = require("../datatypes/datatypes"); +function areNumbers(a, b) { + return typeof a === 'number' && typeof b === 'number'; +} +function areStrings(a, b) { + return typeof a === 'string' && typeof b === 'string'; +} +function areDateTimesOrQuantities(a, b) { + return ((a && a.isDateTime && b && b.isDateTime) || + (a && a.isDate && b && b.isDate) || + (a && a.isQuantity && b && b.isQuantity)); +} +function isUncertainty(x) { + return x instanceof datatypes_1.Uncertainty; +} +function lessThan(a, b, precision) { + if (areNumbers(a, b) || areStrings(a, b)) { + return a < b; + } + else if (areDateTimesOrQuantities(a, b)) { + return a.before(b, precision); + } + else if (isUncertainty(a)) { + return a.lessThan(b); + } + else if (isUncertainty(b)) { + return datatypes_1.Uncertainty.from(a).lessThan(b); + } + else { + return null; + } +} +exports.lessThan = lessThan; +function lessThanOrEquals(a, b, precision) { + if (areNumbers(a, b) || areStrings(a, b)) { + return a <= b; + } + else if (areDateTimesOrQuantities(a, b)) { + return a.sameOrBefore(b, precision); + } + else if (isUncertainty(a)) { + return a.lessThanOrEquals(b); + } + else if (isUncertainty(b)) { + return datatypes_1.Uncertainty.from(a).lessThanOrEquals(b); + } + else { + return null; + } +} +exports.lessThanOrEquals = lessThanOrEquals; +function greaterThan(a, b, precision) { + if (areNumbers(a, b) || areStrings(a, b)) { + return a > b; + } + else if (areDateTimesOrQuantities(a, b)) { + return a.after(b, precision); + } + else if (isUncertainty(a)) { + return a.greaterThan(b); + } + else if (isUncertainty(b)) { + return datatypes_1.Uncertainty.from(a).greaterThan(b); + } + else { + return null; + } +} +exports.greaterThan = greaterThan; +function greaterThanOrEquals(a, b, precision) { + if (areNumbers(a, b) || areStrings(a, b)) { + return a >= b; + } + else if (areDateTimesOrQuantities(a, b)) { + return a.sameOrAfter(b, precision); + } + else if (isUncertainty(a)) { + return a.greaterThanOrEquals(b); + } + else if (isUncertainty(b)) { + return datatypes_1.Uncertainty.from(a).greaterThanOrEquals(b); + } + else { + return null; + } +} +exports.greaterThanOrEquals = greaterThanOrEquals; +function equivalent(a, b) { + if (a == null && b == null) { + return true; + } + if (a == null || b == null) { + return false; + } + if (isCode(a)) { + return codesAreEquivalent(a, b); + } + // Quantity equivalence is the same as Quantity equality + if (a.isQuantity) { + return a.equals(b); + } + // Use overloaded 'equivalent' function if it is available + if (typeof a.equivalent === 'function') { + return a.equivalent(b); + } + var _a = getClassOfObjects(a, b), aClass = _a[0], bClass = _a[1]; + switch (aClass) { + case '[object Array]': + return compareEveryItemInArrays(a, b, equivalent); + case '[object Object]': + return compareObjects(a, b, equivalent); + case '[object String]': + // Make sure b is also a string + if (bClass === '[object String]') { + // String equivalence is case- and locale insensitive + a = a.replace(/\s/g, ' '); + b = b.replace(/\s/g, ' '); + return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0; + } + break; + } + return equals(a, b); +} +exports.equivalent = equivalent; +function isCode(object) { + return object.hasMatch && typeof object.hasMatch === 'function'; +} +function codesAreEquivalent(code1, code2) { + return code1.hasMatch(code2); +} +function getClassOfObjects(object1, object2) { + return [object1, object2].map(function (obj) { return ({}.toString.call(obj)); }); +} +function compareEveryItemInArrays(array1, array2, comparisonFunction) { + return (array1.length === array2.length && + array1.every(function (item, i) { return comparisonFunction(item, array2[i]); })); +} +function compareObjects(a, b, comparisonFunction) { + if (!classesEqual(a, b)) { + return false; + } + return deepCompareKeysAndValues(a, b, comparisonFunction); +} +function classesEqual(object1, object2) { + return object2 instanceof object1.constructor && object1 instanceof object2.constructor; +} +function deepCompareKeysAndValues(a, b, comparisonFunction) { + var finalComparisonResult; + var aKeys = getKeysFromObject(a).sort(); + var bKeys = getKeysFromObject(b).sort(); + // Array.every() will only return true or false, so set a flag for if we should return null + var shouldReturnNull = false; + // Check if both arrays of keys are the same length and key names match + if (aKeys.length === bKeys.length && aKeys.every(function (value, index) { return value === bKeys[index]; })) { + finalComparisonResult = aKeys.every(function (key) { + // if both are null we should return true to satisfy ignoring empty values in tuples + if (a[key] == null && b[key] == null) { + return true; + } + var comparisonResult = comparisonFunction(a[key], b[key]); + if (comparisonResult === null) { + shouldReturnNull = true; + } + return comparisonResult; + }); + } + else { + finalComparisonResult = false; + } + if (shouldReturnNull) { + return null; + } + return finalComparisonResult; +} +function getKeysFromObject(object) { + return Object.keys(object).filter(function (k) { return !isFunction(object[k]); }); +} +function isFunction(input) { + return input instanceof Function || {}.toString.call(input) === '[object Function]'; +} +function equals(a, b) { + // Handle null cases first: spec says if either is null, return null + if (a == null || b == null) { + return null; + } + // If one is a Quantity, use the Quantity equals function + if (a && a.isQuantity) { + return a.equals(b); + } + // If one is a Ratio, use the ratio equals function + if (a && a.isRatio) { + return a.equals(b); + } + // If one is an Uncertainty, convert the other to an Uncertainty + if (a instanceof datatypes_1.Uncertainty) { + b = datatypes_1.Uncertainty.from(b); + } + else if (b instanceof datatypes_1.Uncertainty) { + a = datatypes_1.Uncertainty.from(a); + } + // Use overloaded 'equals' function if it is available + if (typeof a.equals === 'function') { + return a.equals(b); + } + // Return true of the objects are primitives and are strictly equal + if ((typeof a === typeof b && typeof a === 'string') || + typeof a === 'number' || + typeof a === 'boolean') { + return a === b; + } + // Return false if they are instances of different classes + var _a = getClassOfObjects(a, b), aClass = _a[0], bClass = _a[1]; + if (aClass !== bClass) { + return false; + } + switch (aClass) { + case '[object Date]': + // Compare the ms since epoch + return a.getTime() === b.getTime(); + case '[object RegExp]': + // Compare the components of the regular expression + return ['source', 'global', 'ignoreCase', 'multiline'].every(function (p) { return a[p] === b[p]; }); + case '[object Array]': + if (a.indexOf(null) >= 0 || + a.indexOf(undefined) >= 0 || + b.indexOf(null) >= 0 || + b.indexOf(undefined) >= 0) { + return null; + } + return compareEveryItemInArrays(a, b, equals); + case '[object Object]': + return compareObjects(a, b, equals); + case '[object Function]': + return a.toString() === b.toString(); + } + // If we made it this far, we can't handle it + return false; +} +exports.equals = equals; + +},{"../datatypes/datatypes":56}],103:[function(require,module,exports){ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.decimalOrNull = exports.decimalAdjust = exports.minValueForType = exports.minValueForInstance = exports.maxValueForType = exports.maxValueForInstance = exports.predecessor = exports.successor = exports.OverFlowException = exports.limitDecimalPrecision = exports.isValidDecimal = exports.isValidInteger = exports.overflowsOrUnderflows = exports.MAX_TIME_VALUE = exports.MIN_TIME_VALUE = exports.MAX_DATE_VALUE = exports.MIN_DATE_VALUE = exports.MAX_DATETIME_VALUE = exports.MIN_DATETIME_VALUE = exports.MIN_FLOAT_PRECISION_VALUE = exports.MIN_FLOAT_VALUE = exports.MAX_FLOAT_VALUE = exports.MIN_INT_VALUE = exports.MAX_INT_VALUE = void 0; +/* eslint-disable @typescript-eslint/no-loss-of-precision */ +var exception_1 = require("../datatypes/exception"); +var datetime_1 = require("../datatypes/datetime"); +var uncertainty_1 = require("../datatypes/uncertainty"); +exports.MAX_INT_VALUE = Math.pow(2, 31) - 1; +exports.MIN_INT_VALUE = Math.pow(-2, 31); +exports.MAX_FLOAT_VALUE = 99999999999999999999.99999999; +exports.MIN_FLOAT_VALUE = -99999999999999999999.99999999; +exports.MIN_FLOAT_PRECISION_VALUE = Math.pow(10, -8); +exports.MIN_DATETIME_VALUE = datetime_1.MIN_DATETIME_VALUE; +exports.MAX_DATETIME_VALUE = datetime_1.MAX_DATETIME_VALUE; +exports.MIN_DATE_VALUE = datetime_1.MIN_DATE_VALUE; +exports.MAX_DATE_VALUE = datetime_1.MAX_DATE_VALUE; +exports.MIN_TIME_VALUE = datetime_1.MIN_TIME_VALUE; +exports.MAX_TIME_VALUE = datetime_1.MAX_TIME_VALUE; +function overflowsOrUnderflows(value) { + if (value == null) { + return false; + } + if (value.isQuantity) { + if (!isValidDecimal(value.value)) { + return true; + } + } + else if (value.isTime && value.isTime()) { + if (value.after(exports.MAX_TIME_VALUE)) { + return true; + } + if (value.before(exports.MIN_TIME_VALUE)) { + return true; + } + } + else if (value.isDateTime) { + if (value.after(exports.MAX_DATETIME_VALUE)) { + return true; + } + if (value.before(exports.MIN_DATETIME_VALUE)) { + return true; + } + } + else if (value.isDate) { + if (value.after(exports.MAX_DATE_VALUE)) { + return true; + } + if (value.before(exports.MIN_DATE_VALUE)) { + return true; + } + } + else if (Number.isInteger(value)) { + if (!isValidInteger(value)) { + return true; + } + } + else if (value.isUncertainty) { + return overflowsOrUnderflows(value.low) || overflowsOrUnderflows(value.high); + } + else { + if (!isValidDecimal(value)) { + return true; + } + } + return false; +} +exports.overflowsOrUnderflows = overflowsOrUnderflows; +function isValidInteger(integer) { + if (isNaN(integer)) { + return false; + } + if (integer > exports.MAX_INT_VALUE) { + return false; + } + if (integer < exports.MIN_INT_VALUE) { + return false; + } + return true; +} +exports.isValidInteger = isValidInteger; +function isValidDecimal(decimal) { + if (isNaN(decimal)) { + return false; + } + if (decimal > exports.MAX_FLOAT_VALUE) { + return false; + } + if (decimal < exports.MIN_FLOAT_VALUE) { + return false; + } + return true; +} +exports.isValidDecimal = isValidDecimal; +function limitDecimalPrecision(decimal) { + var decimalString = decimal.toString(); + // For decimals so large that they are represented in scientific notation, javascript has already limited + // the decimal to its own constraints, so we can't determine the original precision. Leave as-is unless + // this becomes problematic, in which case we would need our own parseFloat. + if (decimalString.indexOf('e') !== -1) { + return decimal; + } + var splitDecimalString = decimalString.split('.'); + var decimalPoints = splitDecimalString[1]; + if (decimalPoints != null && decimalPoints.length > 8) { + decimalString = splitDecimalString[0] + '.' + splitDecimalString[1].substring(0, 8); + } + return parseFloat(decimalString); +} +exports.limitDecimalPrecision = limitDecimalPrecision; +var OverFlowException = /** @class */ (function (_super) { + __extends(OverFlowException, _super); + function OverFlowException() { + return _super !== null && _super.apply(this, arguments) || this; + } + return OverFlowException; +}(exception_1.Exception)); +exports.OverFlowException = OverFlowException; +function successor(val) { + if (typeof val === 'number') { + if (Number.isInteger(val)) { + if (val >= exports.MAX_INT_VALUE) { + throw new OverFlowException(); + } + else { + return val + 1; + } + } + else { + if (val >= exports.MAX_FLOAT_VALUE) { + throw new OverFlowException(); + } + else { + return val + exports.MIN_FLOAT_PRECISION_VALUE; + } + } + } + else if (val && val.isTime && val.isTime()) { + if (val.sameAs(exports.MAX_TIME_VALUE)) { + throw new OverFlowException(); + } + else { + return val.successor(); + } + } + else if (val && val.isDateTime) { + if (val.sameAs(exports.MAX_DATETIME_VALUE)) { + throw new OverFlowException(); + } + else { + return val.successor(); + } + } + else if (val && val.isDate) { + if (val.sameAs(exports.MAX_DATE_VALUE)) { + throw new OverFlowException(); + } + else { + return val.successor(); + } + } + else if (val && val.isUncertainty) { + // For uncertainties, if the high is the max val, don't increment it + var high = (function () { + try { + return successor(val.high); + } + catch (e) { + return val.high; + } + })(); + return new uncertainty_1.Uncertainty(successor(val.low), high); + } + else if (val && val.isQuantity) { + var succ = val.clone(); + succ.value = successor(val.value); + return succ; + } + else if (val == null) { + return null; + } +} +exports.successor = successor; +function predecessor(val) { + if (typeof val === 'number') { + if (Number.isInteger(val)) { + if (val <= exports.MIN_INT_VALUE) { + throw new OverFlowException(); + } + else { + return val - 1; + } + } + else { + if (val <= exports.MIN_FLOAT_VALUE) { + throw new OverFlowException(); + } + else { + return val - exports.MIN_FLOAT_PRECISION_VALUE; + } + } + } + else if (val && val.isTime && val.isTime()) { + if (val.sameAs(exports.MIN_TIME_VALUE)) { + throw new OverFlowException(); + } + else { + return val.predecessor(); + } + } + else if (val && val.isDateTime) { + if (val.sameAs(exports.MIN_DATETIME_VALUE)) { + throw new OverFlowException(); + } + else { + return val.predecessor(); + } + } + else if (val && val.isDate) { + if (val.sameAs(exports.MIN_DATE_VALUE)) { + throw new OverFlowException(); + } + else { + return val.predecessor(); + } + } + else if (val && val.isUncertainty) { + // For uncertainties, if the low is the min val, don't decrement it + var low = (function () { + try { + return predecessor(val.low); + } + catch (e) { + return val.low; + } + })(); + return new uncertainty_1.Uncertainty(low, predecessor(val.high)); + } + else if (val && val.isQuantity) { + var pred = val.clone(); + pred.value = predecessor(val.value); + return pred; + } + else if (val == null) { + return null; + } +} +exports.predecessor = predecessor; +function maxValueForInstance(val) { + if (typeof val === 'number') { + if (Number.isInteger(val)) { + return exports.MAX_INT_VALUE; + } + else { + return exports.MAX_FLOAT_VALUE; + } + } + else if (val && val.isTime && val.isTime()) { + return exports.MAX_TIME_VALUE === null || exports.MAX_TIME_VALUE === void 0 ? void 0 : exports.MAX_TIME_VALUE.copy(); + } + else if (val && val.isDateTime) { + return exports.MAX_DATETIME_VALUE === null || exports.MAX_DATETIME_VALUE === void 0 ? void 0 : exports.MAX_DATETIME_VALUE.copy(); + } + else if (val && val.isDate) { + return exports.MAX_DATE_VALUE === null || exports.MAX_DATE_VALUE === void 0 ? void 0 : exports.MAX_DATE_VALUE.copy(); + } + else if (val && val.isQuantity) { + var val2 = val.clone(); + val2.value = maxValueForInstance(val2.value); + return val2; + } + else { + return null; + } +} +exports.maxValueForInstance = maxValueForInstance; +function maxValueForType(type, quantityInstance) { + switch (type) { + case '{urn:hl7-org:elm-types:r1}Integer': + return exports.MAX_INT_VALUE; + case '{urn:hl7-org:elm-types:r1}Decimal': + return exports.MAX_FLOAT_VALUE; + case '{urn:hl7-org:elm-types:r1}DateTime': + return exports.MAX_DATETIME_VALUE === null || exports.MAX_DATETIME_VALUE === void 0 ? void 0 : exports.MAX_DATETIME_VALUE.copy(); + case '{urn:hl7-org:elm-types:r1}Date': + return exports.MAX_DATE_VALUE === null || exports.MAX_DATE_VALUE === void 0 ? void 0 : exports.MAX_DATE_VALUE.copy(); + case '{urn:hl7-org:elm-types:r1}Time': + return exports.MAX_TIME_VALUE === null || exports.MAX_TIME_VALUE === void 0 ? void 0 : exports.MAX_TIME_VALUE.copy(); + case '{urn:hl7-org:elm-types:r1}Quantity': { + if (quantityInstance == null) { + // can't infer a quantity unit type from nothing] + return null; + } + var maxQty = quantityInstance.clone(); + maxQty.value = maxValueForInstance(maxQty.value); + return maxQty; + } + } + return null; +} +exports.maxValueForType = maxValueForType; +function minValueForInstance(val) { + if (typeof val === 'number') { + if (Number.isInteger(val)) { + return exports.MIN_INT_VALUE; + } + else { + return exports.MIN_FLOAT_VALUE; + } + } + else if (val && val.isTime && val.isTime()) { + return exports.MIN_TIME_VALUE === null || exports.MIN_TIME_VALUE === void 0 ? void 0 : exports.MIN_TIME_VALUE.copy(); + } + else if (val && val.isDateTime) { + return exports.MIN_DATETIME_VALUE === null || exports.MIN_DATETIME_VALUE === void 0 ? void 0 : exports.MIN_DATETIME_VALUE.copy(); + } + else if (val && val.isDate) { + return exports.MIN_DATE_VALUE === null || exports.MIN_DATE_VALUE === void 0 ? void 0 : exports.MIN_DATE_VALUE.copy(); + } + else if (val && val.isQuantity) { + var val2 = val.clone(); + val2.value = minValueForInstance(val2.value); + return val2; + } + else { + return null; + } +} +exports.minValueForInstance = minValueForInstance; +function minValueForType(type, quantityInstance) { + switch (type) { + case '{urn:hl7-org:elm-types:r1}Integer': + return exports.MIN_INT_VALUE; + case '{urn:hl7-org:elm-types:r1}Decimal': + return exports.MIN_FLOAT_VALUE; + case '{urn:hl7-org:elm-types:r1}DateTime': + return exports.MIN_DATETIME_VALUE === null || exports.MIN_DATETIME_VALUE === void 0 ? void 0 : exports.MIN_DATETIME_VALUE.copy(); + case '{urn:hl7-org:elm-types:r1}Date': + return exports.MIN_DATE_VALUE === null || exports.MIN_DATE_VALUE === void 0 ? void 0 : exports.MIN_DATE_VALUE.copy(); + case '{urn:hl7-org:elm-types:r1}Time': + return exports.MIN_TIME_VALUE === null || exports.MIN_TIME_VALUE === void 0 ? void 0 : exports.MIN_TIME_VALUE.copy(); + case '{urn:hl7-org:elm-types:r1}Quantity': { + if (quantityInstance == null) { + // can't infer a quantity unit type from nothing] + return null; + } + var minQty = quantityInstance.clone(); + minQty.value = minValueForInstance(minQty.value); + return minQty; + } + } + return null; +} +exports.minValueForType = minValueForType; +function decimalAdjust(type, value, exp) { + //If the exp is undefined or zero... + if (typeof exp === 'undefined' || +exp === 0) { + return Math[type](value); + } + value = +value; + exp = +exp; + //If the value is not a number or the exp is not an integer... + if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { + return NaN; + } + //Shift + value = value.toString().split('e'); + var v = value[1] ? +value[1] - exp : -exp; + value = Math[type](+(value[0] + 'e' + v)); + //Shift back + value = value.toString().split('e'); + v = value[1] ? +value[1] + exp : exp; + return +(value[0] + 'e' + v); +} +exports.decimalAdjust = decimalAdjust; +function decimalOrNull(value) { + return isValidDecimal(value) ? value : null; +} +exports.decimalOrNull = decimalOrNull; + +},{"../datatypes/datetime":57,"../datatypes/exception":58,"../datatypes/uncertainty":63}],104:[function(require,module,exports){ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.getQuotientOfUnits = exports.getProductOfUnits = exports.compareUnits = exports.convertToCQLDateUnit = exports.normalizeUnitsWhenPossible = exports.convertUnit = exports.checkUnit = void 0; +var ucum = __importStar(require("@lhncbc/ucum-lhc")); +var math_1 = require("./math"); +var utils = ucum.UcumLhcUtils.getInstance(); +// The CQL specification says that dates are based on the Gregorian calendar, so CQL-based year and month +// identifiers will be matched to the UCUM gregorian units. See http://unitsofmeasure.org/ucum.html#para-31 +var CQL_TO_UCUM_DATE_UNITS = { + years: 'a_g', + year: 'a_g', + months: 'mo_g', + month: 'mo_g', + weeks: 'wk', + week: 'wk', + days: 'd', + day: 'd', + hours: 'h', + hour: 'h', + minutes: 'min', + minute: 'min', + seconds: 's', + second: 's', + milliseconds: 'ms', + millisecond: 'ms' +}; +var UCUM_TO_CQL_DATE_UNITS = { + a: 'year', + a_j: 'year', + a_g: 'year', + mo: 'month', + mo_j: 'month', + mo_g: 'month', + wk: 'week', + d: 'day', + h: 'hour', + min: 'minute', + s: 'second', + ms: 'millisecond' +}; +// Cache Map for unit validity results so we dont have to go to ucum-lhc for every check. +var unitValidityCache = new Map(); +function checkUnit(unit, allowEmptyUnits, allowCQLDateUnits) { + if (allowEmptyUnits === void 0) { allowEmptyUnits = true; } + if (allowCQLDateUnits === void 0) { allowCQLDateUnits = true; } + if (allowEmptyUnits) { + unit = fixEmptyUnit(unit); + } + if (allowCQLDateUnits) { + unit = fixCQLDateUnit(unit); + } + if (!unitValidityCache.has(unit)) { + var result = utils.validateUnitString(unit, true); + if (result.status === 'valid') { + unitValidityCache.set(unit, { valid: true }); + } + else { + var msg = "Invalid UCUM unit: '".concat(unit, "'."); + if (result.ucumCode != null) { + msg += " Did you mean '".concat(result.ucumCode, "'?"); + } + unitValidityCache.set(unit, { valid: false, message: msg }); + } + } + return unitValidityCache.get(unit); +} +exports.checkUnit = checkUnit; +function convertUnit(fromVal, fromUnit, toUnit, adjustPrecision) { + var _a; + if (adjustPrecision === void 0) { adjustPrecision = true; } + _a = [fromUnit, toUnit].map(fixUnit), fromUnit = _a[0], toUnit = _a[1]; + var result = utils.convertUnitTo(fixUnit(fromUnit), fromVal, fixUnit(toUnit)); + if (result.status !== 'succeeded') { + return; + } + return adjustPrecision ? (0, math_1.decimalAdjust)('round', result.toVal, -8) : result.toVal; +} +exports.convertUnit = convertUnit; +function normalizeUnitsWhenPossible(val1, unit1, val2, unit2) { + var _a; + // If both units are CQL date units, return CQL date units + var useCQLDateUnits = unit1 in CQL_TO_UCUM_DATE_UNITS && unit2 in CQL_TO_UCUM_DATE_UNITS; + var resultConverter = function (unit) { + return useCQLDateUnits ? convertToCQLDateUnit(unit) : unit; + }; + _a = [unit1, unit2].map(function (u) { return fixUnit(u); }), unit1 = _a[0], unit2 = _a[1]; + if (unit1 === unit2) { + return [val1, unit1, val2, unit2]; + } + var baseUnit1 = getBaseUnitAndPower(unit1)[0]; + var baseUnit2 = getBaseUnitAndPower(unit2)[0]; + var _b = convertToBaseUnit(val2, unit2, baseUnit1), newVal2 = _b[0], newUnit2 = _b[1]; + if (newVal2 == null) { + // it was not convertible, so just return the quantities as-is + return [val1, resultConverter(unit1), val2, resultConverter(unit2)]; + } + // If the new val2 > old val2, return since we prefer conversion to smaller units + if (newVal2 >= val2) { + return [val1, resultConverter(unit1), newVal2, resultConverter(newUnit2)]; + } + // else it was a conversion to a larger unit, so go the other way around + var _c = convertToBaseUnit(val1, unit1, baseUnit2), newVal1 = _c[0], newUnit1 = _c[1]; + if (newVal1 == null) { + // this should not happen since we established they are convertible, but just in case... + return [val1, resultConverter(unit1), newVal2, resultConverter(newUnit2)]; + } + return [newVal1, resultConverter(newUnit1), val2, resultConverter(unit2)]; +} +exports.normalizeUnitsWhenPossible = normalizeUnitsWhenPossible; +function convertToCQLDateUnit(unit) { + var dateUnit; + if (unit in CQL_TO_UCUM_DATE_UNITS) { + // it's already a CQL unit, so return it as-is, removing trailing 's' if necessary (e.g., years -> year) + dateUnit = unit.replace(/s$/, ''); + } + else if (unit in UCUM_TO_CQL_DATE_UNITS) { + dateUnit = UCUM_TO_CQL_DATE_UNITS[unit]; + } + return dateUnit; +} +exports.convertToCQLDateUnit = convertToCQLDateUnit; +function compareUnits(unit1, unit2) { + try { + var c = convertUnit(1, unit1, unit2); + if (c && c > 1) { + // unit1 is bigger (less precise) + return 1; + } + else if (c && c < 1) { + // unit1 is smaller + return -1; + } + //units are the same + return 0; + } + catch (e) { + return null; + } +} +exports.compareUnits = compareUnits; +function getProductOfUnits(unit1, unit2) { + var _a; + _a = [unit1, unit2].map(fixEmptyUnit), unit1 = _a[0], unit2 = _a[1]; + if (!checkUnit(unit1).valid || !checkUnit(unit2).valid) { + return null; + } + // If either unit contains a divisor,combine the numerators and denominators, then divide + if (unit1.indexOf('/') >= 0 || unit2.indexOf('/') >= 0) { + // NOTE: We're not trying to get perfection on unit simplification, but doing what is reasonable + var match1 = unit1.match(/([^/]*)(\/(.*))?/); + var match2 = unit2.match(/([^/]*)(\/(.*))?/); + // In the previous regexes, numerator is match[1], denominator is match[3] + var newNum = getProductOfUnits(match1[1], match2[1]); + var newDen = getProductOfUnits(match1[3], match2[3]); + return getQuotientOfUnits(newNum, newDen); + } + // Get all the individual units being combined, accounting for multipliers (e.g., 'm.L'), + // and then group like base units to combine powers (and remove '1's since they are no-ops) + // e.g., 'm.L' * 'm' ==> { m: 2, L: 1}; 'm.L' * '1' ==> { m: 1, L: 1 }; '1' : '1' ==> { } + var factorPowerMap = new Map(); + var factors = __spreadArray(__spreadArray([], unit1.split('.'), true), unit2.split('.'), true); + factors.forEach(function (factor) { + var _a = getBaseUnitAndPower(factor), baseUnit = _a[0], power = _a[1]; + if (baseUnit === '1' || power === 0) { + // skip factors that are 1 since 1 * N is N. + return; + } + var accumulatedPower = (factorPowerMap.get(baseUnit) || 0) + power; + factorPowerMap.set(baseUnit, accumulatedPower); + }); + // Loop through the factor map, rebuilding each factor w/ combined power and join them all + // back via the multiplier '.', treating a final '' (no non-1 units) as '1' + // e.g., { m: 2, L: 1 } ==> 'm2.L' + return fixUnit(Array.from(factorPowerMap.entries()) + .map(function (_a) { + var base = _a[0], power = _a[1]; + return "".concat(base).concat(power > 1 ? power : ''); + }) + .join('.')); +} +exports.getProductOfUnits = getProductOfUnits; +function getQuotientOfUnits(unit1, unit2) { + var _a; + _a = [unit1, unit2].map(fixEmptyUnit), unit1 = _a[0], unit2 = _a[1]; + if (!checkUnit(unit1).valid || !checkUnit(unit2).valid) { + return null; + } + // Try to simplify division when neither unit contains a divisor itself + if (unit1.indexOf('/') === -1 && unit2.indexOf('/') === -1) { + // Get all the individual units in numerator and denominator accounting for multipliers + // (e.g., 'm.L'), and then group like base units to combine powers, inversing denominator + // powers since they are being divided. + // e.g., 'm3.L' / 'm' ==> { m: 2, L: -1}; 'm.L' / '1' ==> { m: 1, L: 1 }; '1' / '1' ==> { 1: 0 } + var factorPowerMap_1 = new Map(); + unit1.split('.').forEach(function (factor) { + var _a = getBaseUnitAndPower(factor), baseUnit = _a[0], power = _a[1]; + var accumulatedPower = (factorPowerMap_1.get(baseUnit) || 0) + power; + factorPowerMap_1.set(baseUnit, accumulatedPower); + }); + unit2.split('.').forEach(function (factor) { + var _a = getBaseUnitAndPower(factor), baseUnit = _a[0], power = _a[1]; + var accumulatedPower = (factorPowerMap_1.get(baseUnit) || 0) - power; + factorPowerMap_1.set(baseUnit, accumulatedPower); + }); + // Construct the numerator from factors with positive power, and denominator from factors + // with negative power, filtering out base `1` and power 0 (which is also 1). + // e.g. numerator: { m: 2, L: -2 } ==> 'm2'; { 1: 1, L: -1 } => '' + // e.g. denominator: { m: 2, L: -2 } ==> 'L2'; { 1: 1, L: -1 } => 'L' + var numerator = Array.from(factorPowerMap_1.entries()) + .filter(function (_a) { + var base = _a[0], power = _a[1]; + return base !== '1' && power > 0; + }) + .map(function (_a) { + var base = _a[0], power = _a[1]; + return "".concat(base).concat(power > 1 ? power : ''); + }) + .join('.'); + var denominator = Array.from(factorPowerMap_1.entries()) + .filter(function (_a) { + var base = _a[0], power = _a[1]; + return base !== '1' && power < 0; + }) + .map(function (_a) { + var base = _a[0], power = _a[1]; + return "".concat(base).concat(power < -1 ? power * -1 : ''); + }) + .join('.'); + // wrap the denominator in parentheses if necessary + denominator = /[.]/.test(denominator) ? "(".concat(denominator, ")") : denominator; + return fixUnit("".concat(numerator).concat(denominator !== '' ? '/' + denominator : '')); + } + // One of the units had a divisor, so don't try to be too smart; just construct it from the parts + if (unit1 === unit2) { + // e.g. 'm/g' / 'm/g' ==> '1' + return '1'; + } + else if (unit2 === '1') { + // e.g., 'm/g' / '1' ==> 'm/g/' + return unit1; + } + else { + // denominator is unit2, wrapped in parentheses if necessary + var denominator = /[./]/.test(unit2) ? "(".concat(unit2, ")") : unit2; + if (unit1 === '1') { + // e.g., '1' / 'm' ==> '/m'; '1' / 'm.g' ==> '/(m.g)' + return "/".concat(denominator); + } + // e.g., 'L' / 'm' ==> 'L/m'; 'L' / 'm.g' ==> 'L/(m.g)' + return "".concat(unit1, "/").concat(denominator); + } +} +exports.getQuotientOfUnits = getQuotientOfUnits; +// UNEXPORTED FUNCTIONS +function convertToBaseUnit(fromVal, fromUnit, toBaseUnit) { + var fromPower = getBaseUnitAndPower(fromUnit)[1]; + var toUnit = fromPower === 1 ? toBaseUnit : "".concat(toBaseUnit).concat(fromPower); + var newVal = convertUnit(fromVal, fromUnit, toUnit); + return newVal != null ? [newVal, toUnit] : []; +} +function getBaseUnitAndPower(unit) { + // don't try to extract power from complex units (containing multipliers or divisors) + if (/[./]/.test(unit)) { + return [unit, 1]; + } + unit = fixUnit(unit); + var _a = unit.match(/^(.*[^-\d])?([-]?\d*)$/).slice(1), term = _a[0], power = _a[1]; + if (term == null || term === '') { + term = power; + power = '1'; + } + else if (power == null || power === '') { + power = '1'; + } + return [term, parseInt(power)]; +} +function fixEmptyUnit(unit) { + if (unit == null || (unit.trim && unit.trim() === '')) { + return '1'; + } + return unit; +} +function fixCQLDateUnit(unit) { + return CQL_TO_UCUM_DATE_UNITS[unit] || unit; +} +function fixUnit(unit) { + return fixCQLDateUnit(fixEmptyUnit(unit)); +} + +},{"./math":103,"@lhncbc/ucum-lhc":18}],105:[function(require,module,exports){ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.getTimezoneSeparatorFromString = exports.normalizeMillisecondsField = exports.normalizeMillisecondsFieldInString = exports.jsDate = exports.anyTrue = exports.allTrue = exports.typeIsArray = exports.isNull = exports.numerical_sort = exports.removeNulls = void 0; +function removeNulls(things) { + return things.filter(function (x) { return x != null; }); +} +exports.removeNulls = removeNulls; +function numerical_sort(things, direction) { + return things.sort(function (a, b) { + if (direction == null || direction === 'asc' || direction === 'ascending') { + return a - b; + } + else { + return b - a; + } + }); +} +exports.numerical_sort = numerical_sort; +function isNull(value) { + return value === null; +} +exports.isNull = isNull; +exports.typeIsArray = Array.isArray || (function (value) { return ({}.toString.call(value) === '[object Array]'); }); +function allTrue(things) { + if ((0, exports.typeIsArray)(things)) { + return things.every(function (x) { return x; }); + } + else { + return things; + } +} +exports.allTrue = allTrue; +function anyTrue(things) { + if ((0, exports.typeIsArray)(things)) { + return things.some(function (x) { return x; }); + } + else { + return things; + } +} +exports.anyTrue = anyTrue; +//The export below is to make it easier if js Date is overwritten with CQL Date +exports.jsDate = Date; +function normalizeMillisecondsFieldInString(string, msString) { + // TODO: verify we are only removing numeral digits + var timezoneField; + msString = normalizeMillisecondsField(msString); + var _a = string.split('.'), beforeMs = _a[0], msAndAfter = _a[1]; + var timezoneSeparator = getTimezoneSeparatorFromString(msAndAfter); + if (timezoneSeparator) { + timezoneField = msAndAfter != null ? msAndAfter.split(timezoneSeparator)[1] : undefined; + } + if (timezoneField == null) { + timezoneField = ''; + } + return (string = beforeMs + '.' + msString + timezoneSeparator + timezoneField); +} +exports.normalizeMillisecondsFieldInString = normalizeMillisecondsFieldInString; +function normalizeMillisecondsField(msString) { + // fix up milliseconds by padding zeros and/or truncating (5 --> 500, 50 --> 500, 54321 --> 543, etc.) + return (msString = (msString + '00').substring(0, 3)); +} +exports.normalizeMillisecondsField = normalizeMillisecondsField; +function getTimezoneSeparatorFromString(string) { + if (string != null) { + var matches = string.match(/-/); + if (matches && matches.length === 1) { + return '-'; + } + matches = string.match(/\+/); + if (matches && matches.length === 1) { + return '+'; + } + } + return ''; +} +exports.getTimezoneSeparatorFromString = getTimezoneSeparatorFromString; + +},{}],106:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const AdverseEventSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + severity: Code, + facilityLocation: FacilityLocationSchema, + type: Code, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Adverse Event' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.120' }, + qdmCategory: { type: String, default: 'adverse_event' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::AdverseEvent' }, + +}); + +module.exports.AdverseEventSchema = AdverseEventSchema; +class AdverseEvent extends mongoose.Document { + constructor(object) { + super(object, AdverseEventSchema); + this._type = 'QDM::AdverseEvent'; + } +} + +module.exports.AdverseEvent = AdverseEvent; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],107:[function(require,module,exports){ +module.exports.Identifier = require('./attributes/Identifier.js').Identifier; +module.exports.IdentifierSchema = require('./attributes/Identifier.js').IdentifierSchema; +module.exports.Entity = require('./attributes/Entity.js').Entity; +module.exports.EntitySchema = require('./attributes/Entity.js').EntitySchema; +module.exports.PatientEntity = require('./attributes/PatientEntity.js').PatientEntity; +module.exports.PatientEntitySchema = require('./attributes/PatientEntity.js').PatientEntitySchema; +module.exports.CarePartner = require('./attributes/CarePartner.js').CarePartner; +module.exports.CarePartnerSchema = require('./attributes/CarePartner.js').CarePartnerSchema; +module.exports.RelatedPerson = require('./RelatedPerson.js').RelatedPerson; +module.exports.RelatedPersonSchema = require('./RelatedPerson.js').RelatedPersonSchema; +module.exports.Practitioner = require('./attributes/Practitioner.js').Practitioner; +module.exports.PractitionerSchema = require('./attributes/Practitioner.js').PractitionerSchema; +module.exports.Organization = require('./attributes/Organization.js').Organization; +module.exports.OrganizationSchema = require('./attributes/Organization.js').OrganizationSchema; +module.exports.Location = require('./attributes/Location.js').Location; +module.exports.LocationSchema = require('./attributes/Location.js').LocationSchema; +module.exports.PhysicalExamOrder = require('./PhysicalExamOrder.js').PhysicalExamOrder; +module.exports.PhysicalExamOrderSchema = require('./PhysicalExamOrder.js').PhysicalExamOrderSchema; +module.exports.Participation = require('./Participation.js').Participation; +module.exports.ParticipationSchema = require('./Participation.js').ParticipationSchema; +module.exports.PatientCharacteristicSex = require('./PatientCharacteristicSex.js').PatientCharacteristicSex; +module.exports.PatientCharacteristicSexSchema = require('./PatientCharacteristicSex.js').PatientCharacteristicSexSchema; +module.exports.CareGoal = require('./CareGoal.js').CareGoal; +module.exports.CareGoalSchema = require('./CareGoal.js').CareGoalSchema; +module.exports.PatientCharacteristic = require('./PatientCharacteristic.js').PatientCharacteristic; +module.exports.PatientCharacteristicSchema = require('./PatientCharacteristic.js').PatientCharacteristicSchema; +module.exports.PatientCharacteristicEthnicity = require('./PatientCharacteristicEthnicity.js').PatientCharacteristicEthnicity; +module.exports.PatientCharacteristicEthnicitySchema = require('./PatientCharacteristicEthnicity.js').PatientCharacteristicEthnicitySchema; +module.exports.PatientCharacteristicRace = require('./PatientCharacteristicRace.js').PatientCharacteristicRace; +module.exports.PatientCharacteristicRaceSchema = require('./PatientCharacteristicRace.js').PatientCharacteristicRaceSchema; +module.exports.LaboratoryTestPerformed = require('./LaboratoryTestPerformed.js').LaboratoryTestPerformed; +module.exports.LaboratoryTestPerformedSchema = require('./LaboratoryTestPerformed.js').LaboratoryTestPerformedSchema; +module.exports.Symptom = require('./Symptom.js').Symptom; +module.exports.SymptomSchema = require('./Symptom.js').SymptomSchema; +module.exports.MedicationAdministered = require('./MedicationAdministered.js').MedicationAdministered; +module.exports.MedicationAdministeredSchema = require('./MedicationAdministered.js').MedicationAdministeredSchema; +module.exports.ProcedureRecommended = require('./ProcedureRecommended.js').ProcedureRecommended; +module.exports.ProcedureRecommendedSchema = require('./ProcedureRecommended.js').ProcedureRecommendedSchema; +module.exports.EncounterPerformed = require('./EncounterPerformed.js').EncounterPerformed; +module.exports.EncounterPerformedSchema = require('./EncounterPerformed.js').EncounterPerformedSchema; +module.exports.Diagnosis = require('./Diagnosis.js').Diagnosis; +module.exports.DiagnosisSchema = require('./Diagnosis.js').DiagnosisSchema; +module.exports.CommunicationPerformed = require('./CommunicationPerformed.js').CommunicationPerformed; +module.exports.CommunicationPerformedSchema = require('./CommunicationPerformed.js').CommunicationPerformedSchema; +module.exports.AssessmentPerformed = require('./AssessmentPerformed.js').AssessmentPerformed; +module.exports.AssessmentPerformedSchema = require('./AssessmentPerformed.js').AssessmentPerformedSchema; +module.exports.PatientCharacteristicClinicalTrialParticipant = require('./PatientCharacteristicClinicalTrialParticipant.js').PatientCharacteristicClinicalTrialParticipant; +module.exports.PatientCharacteristicClinicalTrialParticipantSchema = require('./PatientCharacteristicClinicalTrialParticipant.js').PatientCharacteristicClinicalTrialParticipantSchema; +module.exports.DeviceOrder = require('./DeviceOrder.js').DeviceOrder; +module.exports.DeviceOrderSchema = require('./DeviceOrder.js').DeviceOrderSchema; +module.exports.DiagnosticStudyPerformed = require('./DiagnosticStudyPerformed.js').DiagnosticStudyPerformed; +module.exports.DiagnosticStudyPerformedSchema = require('./DiagnosticStudyPerformed.js').DiagnosticStudyPerformedSchema; +module.exports.InterventionOrder = require('./InterventionOrder.js').InterventionOrder; +module.exports.InterventionOrderSchema = require('./InterventionOrder.js').InterventionOrderSchema; +module.exports.FamilyHistory = require('./FamilyHistory.js').FamilyHistory; +module.exports.FamilyHistorySchema = require('./FamilyHistory.js').FamilyHistorySchema; +module.exports.Component = require('./attributes/Component.js').Component; +module.exports.ComponentSchema = require('./attributes/Component.js').ComponentSchema; +module.exports.DiagnosisComponent = require('./attributes/DiagnosisComponent.js').DiagnosisComponent; +module.exports.DiagnosisComponentSchema = require('./attributes/DiagnosisComponent.js').DiagnosisComponentSchema; +module.exports.ResultComponent = require('./attributes/ResultComponent.js').ResultComponent; +module.exports.ResultComponentSchema = require('./attributes/ResultComponent.js').ResultComponentSchema; +module.exports.FacilityLocation = require('./attributes/FacilityLocation.js').FacilityLocation; +module.exports.FacilityLocationSchema = require('./attributes/FacilityLocation.js').FacilityLocationSchema; +module.exports.MedicationActive = require('./MedicationActive.js').MedicationActive; +module.exports.MedicationActiveSchema = require('./MedicationActive.js').MedicationActiveSchema; +module.exports.LaboratoryTestOrder = require('./LaboratoryTestOrder.js').LaboratoryTestOrder; +module.exports.LaboratoryTestOrderSchema = require('./LaboratoryTestOrder.js').LaboratoryTestOrderSchema; +module.exports.DiagnosticStudyOrder = require('./DiagnosticStudyOrder.js').DiagnosticStudyOrder; +module.exports.DiagnosticStudyOrderSchema = require('./DiagnosticStudyOrder.js').DiagnosticStudyOrderSchema; +module.exports.SubstanceOrder = require('./SubstanceOrder.js').SubstanceOrder; +module.exports.SubstanceOrderSchema = require('./SubstanceOrder.js').SubstanceOrderSchema; +module.exports.PatientCharacteristicPayer = require('./PatientCharacteristicPayer.js').PatientCharacteristicPayer; +module.exports.PatientCharacteristicPayerSchema = require('./PatientCharacteristicPayer.js').PatientCharacteristicPayerSchema; +module.exports.PatientCharacteristicExpired = require('./PatientCharacteristicExpired.js').PatientCharacteristicExpired; +module.exports.PatientCharacteristicExpiredSchema = require('./PatientCharacteristicExpired.js').PatientCharacteristicExpiredSchema; +module.exports.AssessmentOrder = require('./AssessmentOrder.js').AssessmentOrder; +module.exports.AssessmentOrderSchema = require('./AssessmentOrder.js').AssessmentOrderSchema; +module.exports.AssessmentRecommended = require('./AssessmentRecommended.js').AssessmentRecommended; +module.exports.AssessmentRecommendedSchema = require('./AssessmentRecommended.js').AssessmentRecommendedSchema; +module.exports.ImmunizationAdministered = require('./ImmunizationAdministered.js').ImmunizationAdministered; +module.exports.ImmunizationAdministeredSchema = require('./ImmunizationAdministered.js').ImmunizationAdministeredSchema; +module.exports.SubstanceAdministered = require('./SubstanceAdministered.js').SubstanceAdministered; +module.exports.SubstanceAdministeredSchema = require('./SubstanceAdministered.js').SubstanceAdministeredSchema; +module.exports.EncounterOrder = require('./EncounterOrder.js').EncounterOrder; +module.exports.EncounterOrderSchema = require('./EncounterOrder.js').EncounterOrderSchema; +module.exports.EncounterRecommended = require('./EncounterRecommended.js').EncounterRecommended; +module.exports.EncounterRecommendedSchema = require('./EncounterRecommended.js').EncounterRecommendedSchema; +module.exports.ProcedurePerformed = require('./ProcedurePerformed.js').ProcedurePerformed; +module.exports.ProcedurePerformedSchema = require('./ProcedurePerformed.js').ProcedurePerformedSchema; +module.exports.AllergyIntolerance = require('./AllergyIntolerance.js').AllergyIntolerance; +module.exports.AllergyIntoleranceSchema = require('./AllergyIntolerance.js').AllergyIntoleranceSchema; +module.exports.PhysicalExamRecommended = require('./PhysicalExamRecommended.js').PhysicalExamRecommended; +module.exports.PhysicalExamRecommendedSchema = require('./PhysicalExamRecommended.js').PhysicalExamRecommendedSchema; +module.exports.PatientCharacteristicBirthdate = require('./PatientCharacteristicBirthdate.js').PatientCharacteristicBirthdate; +module.exports.PatientCharacteristicBirthdateSchema = require('./PatientCharacteristicBirthdate.js').PatientCharacteristicBirthdateSchema; +module.exports.AdverseEvent = require('./AdverseEvent.js').AdverseEvent; +module.exports.AdverseEventSchema = require('./AdverseEvent.js').AdverseEventSchema; +module.exports.DeviceRecommended = require('./DeviceRecommended.js').DeviceRecommended; +module.exports.DeviceRecommendedSchema = require('./DeviceRecommended.js').DeviceRecommendedSchema; +module.exports.MedicationDischarge = require('./MedicationDischarge.js').MedicationDischarge; +module.exports.MedicationDischargeSchema = require('./MedicationDischarge.js').MedicationDischargeSchema; +module.exports.InterventionPerformed = require('./InterventionPerformed.js').InterventionPerformed; +module.exports.InterventionPerformedSchema = require('./InterventionPerformed.js').InterventionPerformedSchema; +module.exports.LaboratoryTestRecommended = require('./LaboratoryTestRecommended.js').LaboratoryTestRecommended; +module.exports.LaboratoryTestRecommendedSchema = require('./LaboratoryTestRecommended.js').LaboratoryTestRecommendedSchema; +module.exports.MedicationDispensed = require('./MedicationDispensed.js').MedicationDispensed; +module.exports.MedicationDispensedSchema = require('./MedicationDispensed.js').MedicationDispensedSchema; +module.exports.DiagnosticStudyRecommended = require('./DiagnosticStudyRecommended.js').DiagnosticStudyRecommended; +module.exports.DiagnosticStudyRecommendedSchema = require('./DiagnosticStudyRecommended.js').DiagnosticStudyRecommendedSchema; +module.exports.ImmunizationOrder = require('./ImmunizationOrder.js').ImmunizationOrder; +module.exports.ImmunizationOrderSchema = require('./ImmunizationOrder.js').ImmunizationOrderSchema; +module.exports.PatientCareExperience = require('./PatientCareExperience.js').PatientCareExperience; +module.exports.PatientCareExperienceSchema = require('./PatientCareExperience.js').PatientCareExperienceSchema; +module.exports.ProviderCareExperience = require('./ProviderCareExperience.js').ProviderCareExperience; +module.exports.ProviderCareExperienceSchema = require('./ProviderCareExperience.js').ProviderCareExperienceSchema; +module.exports.ProcedureOrder = require('./ProcedureOrder.js').ProcedureOrder; +module.exports.ProcedureOrderSchema = require('./ProcedureOrder.js').ProcedureOrderSchema; +module.exports.MedicationOrder = require('./MedicationOrder.js').MedicationOrder; +module.exports.MedicationOrderSchema = require('./MedicationOrder.js').MedicationOrderSchema; +module.exports.SubstanceRecommended = require('./SubstanceRecommended.js').SubstanceRecommended; +module.exports.SubstanceRecommendedSchema = require('./SubstanceRecommended.js').SubstanceRecommendedSchema; +module.exports.InterventionRecommended = require('./InterventionRecommended.js').InterventionRecommended; +module.exports.InterventionRecommendedSchema = require('./InterventionRecommended.js').InterventionRecommendedSchema; +module.exports.PhysicalExamPerformed = require('./PhysicalExamPerformed.js').PhysicalExamPerformed; +module.exports.PhysicalExamPerformedSchema = require('./PhysicalExamPerformed.js').PhysicalExamPerformedSchema; +module.exports.QDMPatient = require('./QDMPatient.js').QDMPatient; +module.exports.QDMPatientSchema = require('./QDMPatient.js').QDMPatientSchema; + +},{"./AdverseEvent.js":106,"./AllergyIntolerance.js":108,"./AssessmentOrder.js":109,"./AssessmentPerformed.js":110,"./AssessmentRecommended.js":111,"./CareGoal.js":112,"./CommunicationPerformed.js":113,"./DeviceOrder.js":114,"./DeviceRecommended.js":115,"./Diagnosis.js":116,"./DiagnosticStudyOrder.js":117,"./DiagnosticStudyPerformed.js":118,"./DiagnosticStudyRecommended.js":119,"./EncounterOrder.js":120,"./EncounterPerformed.js":121,"./EncounterRecommended.js":122,"./FamilyHistory.js":123,"./ImmunizationAdministered.js":124,"./ImmunizationOrder.js":125,"./InterventionOrder.js":126,"./InterventionPerformed.js":127,"./InterventionRecommended.js":128,"./LaboratoryTestOrder.js":129,"./LaboratoryTestPerformed.js":130,"./LaboratoryTestRecommended.js":131,"./MedicationActive.js":132,"./MedicationAdministered.js":133,"./MedicationDischarge.js":134,"./MedicationDispensed.js":135,"./MedicationOrder.js":136,"./Participation.js":137,"./PatientCareExperience.js":138,"./PatientCharacteristic.js":139,"./PatientCharacteristicBirthdate.js":140,"./PatientCharacteristicClinicalTrialParticipant.js":141,"./PatientCharacteristicEthnicity.js":142,"./PatientCharacteristicExpired.js":143,"./PatientCharacteristicPayer.js":144,"./PatientCharacteristicRace.js":145,"./PatientCharacteristicSex.js":146,"./PhysicalExamOrder.js":147,"./PhysicalExamPerformed.js":148,"./PhysicalExamRecommended.js":149,"./ProcedureOrder.js":150,"./ProcedurePerformed.js":151,"./ProcedureRecommended.js":152,"./ProviderCareExperience.js":153,"./QDMPatient.js":154,"./RelatedPerson.js":155,"./SubstanceAdministered.js":157,"./SubstanceOrder.js":158,"./SubstanceRecommended.js":159,"./Symptom.js":160,"./attributes/CarePartner.js":161,"./attributes/Component.js":162,"./attributes/DiagnosisComponent.js":163,"./attributes/Entity.js":164,"./attributes/FacilityLocation.js":165,"./attributes/Identifier.js":166,"./attributes/Location.js":167,"./attributes/Organization.js":168,"./attributes/PatientEntity.js":169,"./attributes/Practitioner.js":170,"./attributes/ResultComponent.js":171}],108:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const AllergyIntoleranceSchema = DataElementSchema({ + authorDatetime: DateTime, + prevalencePeriod: Interval, + type: Code, + severity: Code, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Allergy/Intolerance' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.119' }, + qdmCategory: { type: String, default: 'allergy' }, + qdmStatus: { type: String, default: 'intolerance' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::AllergyIntolerance' }, + +}); + +module.exports.AllergyIntoleranceSchema = AllergyIntoleranceSchema; +class AllergyIntolerance extends mongoose.Document { + constructor(object) { + super(object, AllergyIntoleranceSchema); + this._type = 'QDM::AllergyIntolerance'; + } +} + +module.exports.AllergyIntolerance = AllergyIntolerance; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],109:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const AssessmentOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + negationRationale: Code, + reason: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Assessment, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.131' }, + qdmCategory: { type: String, default: 'assessment' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::AssessmentOrder' }, + +}); + +module.exports.AssessmentOrderSchema = AssessmentOrderSchema; +class AssessmentOrder extends mongoose.Document { + constructor(object) { + super(object, AssessmentOrderSchema); + this._type = 'QDM::AssessmentOrder'; + } +} + +module.exports.AssessmentOrder = AssessmentOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],110:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const AssessmentPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + negationRationale: Code, + reason: Code, + method: Code, + result: Any, + interpretation: Code, + components: [ComponentSchema], + relatedTo: [String], + performer: [AnyEntity], + qdmTitle: { type: String, default: 'Assessment, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.117' }, + qdmCategory: { type: String, default: 'assessment' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::AssessmentPerformed' }, + +}); + +module.exports.AssessmentPerformedSchema = AssessmentPerformedSchema; +class AssessmentPerformed extends mongoose.Document { + constructor(object) { + super(object, AssessmentPerformedSchema); + this._type = 'QDM::AssessmentPerformed'; + } +} + +module.exports.AssessmentPerformed = AssessmentPerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],111:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const AssessmentRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + negationRationale: Code, + reason: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Assessment, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.118' }, + qdmCategory: { type: String, default: 'assessment' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::AssessmentRecommended' }, + +}); + +module.exports.AssessmentRecommendedSchema = AssessmentRecommendedSchema; +class AssessmentRecommended extends mongoose.Document { + constructor(object) { + super(object, AssessmentRecommendedSchema); + this._type = 'QDM::AssessmentRecommended'; + } +} + +module.exports.AssessmentRecommended = AssessmentRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],112:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const CareGoalSchema = DataElementSchema({ + statusDate: QDMDate, + relevantPeriod: Interval, + relatedTo: [String], + targetOutcome: Any, + performer: [AnyEntity], + qdmTitle: { type: String, default: 'Care Goal' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.7' }, + qdmCategory: { type: String, default: 'care_goal' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::CareGoal' }, + +}); + +module.exports.CareGoalSchema = CareGoalSchema; +class CareGoal extends mongoose.Document { + constructor(object) { + super(object, CareGoalSchema); + this._type = 'QDM::CareGoal'; + } +} + +module.exports.CareGoal = CareGoal; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],113:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const CommunicationPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + category: Code, + medium: Code, + sender: [AnyEntity], + recipient: [AnyEntity], + relatedTo: [String], + sentDatetime: DateTime, + receivedDatetime: DateTime, + negationRationale: Code, + qdmTitle: { type: String, default: 'Communication, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.132' }, + qdmCategory: { type: String, default: 'communication' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::CommunicationPerformed' }, + +}); + +module.exports.CommunicationPerformedSchema = CommunicationPerformedSchema; +class CommunicationPerformed extends mongoose.Document { + constructor(object) { + super(object, CommunicationPerformedSchema); + this._type = 'QDM::CommunicationPerformed'; + } +} + +module.exports.CommunicationPerformed = CommunicationPerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],114:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DeviceOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + negationRationale: Code, + reason: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Device, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.15' }, + qdmCategory: { type: String, default: 'device' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::DeviceOrder' }, + +}); + +module.exports.DeviceOrderSchema = DeviceOrderSchema; +class DeviceOrder extends mongoose.Document { + constructor(object) { + super(object, DeviceOrderSchema); + this._type = 'QDM::DeviceOrder'; + } +} + +module.exports.DeviceOrder = DeviceOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],115:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DeviceRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + negationRationale: Code, + reason: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Device, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.16' }, + qdmCategory: { type: String, default: 'device' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::DeviceRecommended' }, + +}); + +module.exports.DeviceRecommendedSchema = DeviceRecommendedSchema; +class DeviceRecommended extends mongoose.Document { + constructor(object) { + super(object, DeviceRecommendedSchema); + this._type = 'QDM::DeviceRecommended'; + } +} + +module.exports.DeviceRecommended = DeviceRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],116:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DiagnosisSchema = DataElementSchema({ + authorDatetime: DateTime, + prevalencePeriod: Interval, + anatomicalLocationSite: Code, + severity: Code, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Diagnosis' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.110' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.135' }, + qdmCategory: { type: String, default: 'condition' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Diagnosis' }, + +}); + +module.exports.DiagnosisSchema = DiagnosisSchema; +class Diagnosis extends mongoose.Document { + constructor(object) { + super(object, DiagnosisSchema); + this._type = 'QDM::Diagnosis'; + } +} + +module.exports.Diagnosis = Diagnosis; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],117:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DiagnosticStudyOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Diagnostic Study, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.22' }, + qdmCategory: { type: String, default: 'diagnostic_study' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::DiagnosticStudyOrder' }, + +}); + +module.exports.DiagnosticStudyOrderSchema = DiagnosticStudyOrderSchema; +class DiagnosticStudyOrder extends mongoose.Document { + constructor(object) { + super(object, DiagnosticStudyOrderSchema); + this._type = 'QDM::DiagnosticStudyOrder'; + } +} + +module.exports.DiagnosticStudyOrder = DiagnosticStudyOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],118:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DiagnosticStudyPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + reason: Code, + result: Any, + resultDatetime: DateTime, + interpretation: Code, + status: Code, + method: Code, + facilityLocation: FacilityLocationSchema, + negationRationale: Code, + components: [ComponentSchema], + performer: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Diagnostic Study, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.23' }, + qdmCategory: { type: String, default: 'diagnostic_study' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::DiagnosticStudyPerformed' }, + +}); + +module.exports.DiagnosticStudyPerformedSchema = DiagnosticStudyPerformedSchema; +class DiagnosticStudyPerformed extends mongoose.Document { + constructor(object) { + super(object, DiagnosticStudyPerformedSchema); + this._type = 'QDM::DiagnosticStudyPerformed'; + } +} + +module.exports.DiagnosticStudyPerformed = DiagnosticStudyPerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],119:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DiagnosticStudyRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Diagnostic Study, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.24' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.19' }, + qdmCategory: { type: String, default: 'diagnostic_study' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::DiagnosticStudyRecommended' }, + +}); + +module.exports.DiagnosticStudyRecommendedSchema = DiagnosticStudyRecommendedSchema; +class DiagnosticStudyRecommended extends mongoose.Document { + constructor(object) { + super(object, DiagnosticStudyRecommendedSchema); + this._type = 'QDM::DiagnosticStudyRecommended'; + } +} + +module.exports.DiagnosticStudyRecommended = DiagnosticStudyRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],120:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const EncounterOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + facilityLocation: FacilityLocationSchema, + negationRationale: Code, + requester: [AnyEntity], + priority: Code, + qdmTitle: { type: String, default: 'Encounter, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.27' }, + qdmCategory: { type: String, default: 'encounter' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::EncounterOrder' }, + +}); + +module.exports.EncounterOrderSchema = EncounterOrderSchema; +class EncounterOrder extends mongoose.Document { + constructor(object) { + super(object, EncounterOrderSchema); + this._type = 'QDM::EncounterOrder'; + } +} + +module.exports.EncounterOrder = EncounterOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],121:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); +const { DiagnosisComponentSchema } = require('./attributes/DiagnosisComponent'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const EncounterPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + admissionSource: Code, + class: Code, + relevantPeriod: Interval, + dischargeDisposition: Code, + facilityLocations: [FacilityLocationSchema], + diagnoses: [DiagnosisComponentSchema], + lengthOfStay: Quantity, + priority: Code, + participant: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Encounter, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.5' }, + qdmCategory: { type: String, default: 'encounter' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::EncounterPerformed' }, + +}); + +module.exports.EncounterPerformedSchema = EncounterPerformedSchema; +class EncounterPerformed extends mongoose.Document { + constructor(object) { + super(object, EncounterPerformedSchema); + this._type = 'QDM::EncounterPerformed'; + if (object && object.clazz) { + this.class = object.clazz; + } + } +} + +module.exports.EncounterPerformed = EncounterPerformed; + + +},{"./attributes/Component":162,"./attributes/DiagnosisComponent":163,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],122:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const EncounterRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + facilityLocation: FacilityLocationSchema, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Encounter, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.28' }, + qdmCategory: { type: String, default: 'encounter' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::EncounterRecommended' }, + +}); + +module.exports.EncounterRecommendedSchema = EncounterRecommendedSchema; +class EncounterRecommended extends mongoose.Document { + constructor(object) { + super(object, EncounterRecommendedSchema); + this._type = 'QDM::EncounterRecommended'; + } +} + +module.exports.EncounterRecommended = EncounterRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],123:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const FamilyHistorySchema = DataElementSchema({ + authorDatetime: DateTime, + relationship: Code, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Family History' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.111' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.12' }, + qdmCategory: { type: String, default: 'family_history' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::FamilyHistory' }, + +}); + +module.exports.FamilyHistorySchema = FamilyHistorySchema; +class FamilyHistory extends mongoose.Document { + constructor(object) { + super(object, FamilyHistorySchema); + this._type = 'QDM::FamilyHistory'; + } +} + +module.exports.FamilyHistory = FamilyHistory; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],124:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ImmunizationAdministeredSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + reason: Code, + dosage: Quantity, + route: Code, + negationRationale: Code, + performer: [AnyEntity], + qdmTitle: { type: String, default: 'Immunization, Administered' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.112' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.140' }, + qdmCategory: { type: String, default: 'immunization' }, + qdmStatus: { type: String, default: 'administered' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::ImmunizationAdministered' }, + +}); + +module.exports.ImmunizationAdministeredSchema = ImmunizationAdministeredSchema; +class ImmunizationAdministered extends mongoose.Document { + constructor(object) { + super(object, ImmunizationAdministeredSchema); + this._type = 'QDM::ImmunizationAdministered'; + } +} + +module.exports.ImmunizationAdministered = ImmunizationAdministered; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],125:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ImmunizationOrderSchema = DataElementSchema({ + activeDatetime: DateTime, + authorDatetime: DateTime, + dosage: Quantity, + supply: Quantity, + reason: Code, + route: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Immunization, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.113' }, + qdmCategory: { type: String, default: 'immunization' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::ImmunizationOrder' }, + +}); + +module.exports.ImmunizationOrderSchema = ImmunizationOrderSchema; +class ImmunizationOrder extends mongoose.Document { + constructor(object) { + super(object, ImmunizationOrderSchema); + this._type = 'QDM::ImmunizationOrder'; + } +} + +module.exports.ImmunizationOrder = ImmunizationOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],126:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const InterventionOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Intervention, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.35' }, + qdmCategory: { type: String, default: 'intervention' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::InterventionOrder' }, + +}); + +module.exports.InterventionOrderSchema = InterventionOrderSchema; +class InterventionOrder extends mongoose.Document { + constructor(object) { + super(object, InterventionOrderSchema); + this._type = 'QDM::InterventionOrder'; + } +} + +module.exports.InterventionOrder = InterventionOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],127:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const InterventionPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + reason: Code, + result: Any, + status: Code, + negationRationale: Code, + performer: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Intervention, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.36' }, + qdmCategory: { type: String, default: 'intervention' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::InterventionPerformed' }, + +}); + +module.exports.InterventionPerformedSchema = InterventionPerformedSchema; +class InterventionPerformed extends mongoose.Document { + constructor(object) { + super(object, InterventionPerformedSchema); + this._type = 'QDM::InterventionPerformed'; + } +} + +module.exports.InterventionPerformed = InterventionPerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],128:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const InterventionRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Intervention, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.37' }, + qdmCategory: { type: String, default: 'intervention' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::InterventionRecommended' }, + +}); + +module.exports.InterventionRecommendedSchema = InterventionRecommendedSchema; +class InterventionRecommended extends mongoose.Document { + constructor(object) { + super(object, InterventionRecommendedSchema); + this._type = 'QDM::InterventionRecommended'; + } +} + +module.exports.InterventionRecommended = InterventionRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],129:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const LaboratoryTestOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Laboratory Test, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.41' }, + qdmCategory: { type: String, default: 'laboratory_test' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::LaboratoryTestOrder' }, + +}); + +module.exports.LaboratoryTestOrderSchema = LaboratoryTestOrderSchema; +class LaboratoryTestOrder extends mongoose.Document { + constructor(object) { + super(object, LaboratoryTestOrderSchema); + this._type = 'QDM::LaboratoryTestOrder'; + } +} + +module.exports.LaboratoryTestOrder = LaboratoryTestOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],130:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const LaboratoryTestPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + status: Code, + method: Code, + result: Any, + resultDatetime: DateTime, + reason: Code, + referenceRange: Interval, + interpretation: Code, + negationRationale: Code, + components: [ComponentSchema], + performer: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Laboratory Test, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.42' }, + qdmCategory: { type: String, default: 'laboratory_test' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::LaboratoryTestPerformed' }, + +}); + +module.exports.LaboratoryTestPerformedSchema = LaboratoryTestPerformedSchema; +class LaboratoryTestPerformed extends mongoose.Document { + constructor(object) { + super(object, LaboratoryTestPerformedSchema); + this._type = 'QDM::LaboratoryTestPerformed'; + } +} + +module.exports.LaboratoryTestPerformed = LaboratoryTestPerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],131:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const LaboratoryTestRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Laboratory Test, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.43' }, + qdmCategory: { type: String, default: 'laboratory_test' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::LaboratoryTestRecommended' }, + +}); + +module.exports.LaboratoryTestRecommendedSchema = LaboratoryTestRecommendedSchema; +class LaboratoryTestRecommended extends mongoose.Document { + constructor(object) { + super(object, LaboratoryTestRecommendedSchema); + this._type = 'QDM::LaboratoryTestRecommended'; + } +} + +module.exports.LaboratoryTestRecommended = LaboratoryTestRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],132:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const MedicationActiveSchema = DataElementSchema({ + relevantDatetime: DateTime, + relevantPeriod: Interval, + dosage: Quantity, + frequency: Code, + route: Code, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Medication, Active' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.44' }, + qdmCategory: { type: String, default: 'medication' }, + qdmStatus: { type: String, default: 'active' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::MedicationActive' }, + +}); + +module.exports.MedicationActiveSchema = MedicationActiveSchema; +class MedicationActive extends mongoose.Document { + constructor(object) { + super(object, MedicationActiveSchema); + this._type = 'QDM::MedicationActive'; + } +} + +module.exports.MedicationActive = MedicationActive; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],133:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const MedicationAdministeredSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + dosage: Quantity, + frequency: Code, + route: Code, + reason: Code, + negationRationale: Code, + performer: [AnyEntity], + qdmTitle: { type: String, default: 'Medication, Administered' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.45' }, + qdmCategory: { type: String, default: 'medication' }, + qdmStatus: { type: String, default: 'administered' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::MedicationAdministered' }, + +}); + +module.exports.MedicationAdministeredSchema = MedicationAdministeredSchema; +class MedicationAdministered extends mongoose.Document { + constructor(object) { + super(object, MedicationAdministeredSchema); + this._type = 'QDM::MedicationAdministered'; + } +} + +module.exports.MedicationAdministered = MedicationAdministered; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],134:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const MedicationDischargeSchema = DataElementSchema({ + authorDatetime: DateTime, + refills: Number, + dosage: Quantity, + supply: Quantity, + frequency: Code, + daysSupplied: Number, + route: Code, + negationRationale: Code, + prescriber: [AnyEntity], + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Medication, Discharge' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.48' }, + qdmCategory: { type: String, default: 'medication' }, + qdmStatus: { type: String, default: 'discharge' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::MedicationDischarge' }, + +}); + +module.exports.MedicationDischargeSchema = MedicationDischargeSchema; +class MedicationDischarge extends mongoose.Document { + constructor(object) { + super(object, MedicationDischargeSchema); + this._type = 'QDM::MedicationDischarge'; + } +} + +module.exports.MedicationDischarge = MedicationDischarge; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],135:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const MedicationDispensedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + refills: Number, + dosage: Quantity, + supply: Quantity, + frequency: Code, + daysSupplied: Number, + route: Code, + prescriber: [AnyEntity], + dispenser: [AnyEntity], + negationRationale: Code, + relatedTo: [String], + qdmTitle: { type: String, default: 'Medication, Dispensed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.49' }, + qdmCategory: { type: String, default: 'medication' }, + qdmStatus: { type: String, default: 'dispensed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::MedicationDispensed' }, + +}); + +module.exports.MedicationDispensedSchema = MedicationDispensedSchema; +class MedicationDispensed extends mongoose.Document { + constructor(object) { + super(object, MedicationDispensedSchema); + this._type = 'QDM::MedicationDispensed'; + } +} + +module.exports.MedicationDispensed = MedicationDispensed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],136:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const MedicationOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantPeriod: Interval, + refills: Number, + dosage: Quantity, + supply: Quantity, + frequency: Code, + daysSupplied: Number, + route: Code, + setting: Code, + reason: Code, + negationRationale: Code, + prescriber: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Medication, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.51' }, + qdmCategory: { type: String, default: 'medication' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::MedicationOrder' }, + +}); + +module.exports.MedicationOrderSchema = MedicationOrderSchema; +class MedicationOrder extends mongoose.Document { + constructor(object) { + super(object, MedicationOrderSchema); + this._type = 'QDM::MedicationOrder'; + } +} + +module.exports.MedicationOrder = MedicationOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],137:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ParticipationSchema = DataElementSchema({ + participationPeriod: Interval, + qdmTitle: { type: String, default: 'Participation' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.130' }, + qdmCategory: { type: String, default: 'participation' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Participation' }, + +}); + +module.exports.ParticipationSchema = ParticipationSchema; +class Participation extends mongoose.Document { + constructor(object) { + super(object, ParticipationSchema); + this._type = 'QDM::Participation'; + } +} + +module.exports.Participation = Participation; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],138:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCareExperienceSchema = DataElementSchema({ + authorDatetime: DateTime, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Patient Care Experience' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.52' }, + qdmCategory: { type: String, default: 'care_experience' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCareExperience' }, + +}); + +module.exports.PatientCareExperienceSchema = PatientCareExperienceSchema; +class PatientCareExperience extends mongoose.Document { + constructor(object) { + super(object, PatientCareExperienceSchema); + this._type = 'QDM::PatientCareExperience'; + } +} + +module.exports.PatientCareExperience = PatientCareExperience; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],139:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicSchema = DataElementSchema({ + authorDatetime: DateTime, + qdmTitle: { type: String, default: 'Patient Characteristic' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.53' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristic' }, + +}); + +module.exports.PatientCharacteristicSchema = PatientCharacteristicSchema; +class PatientCharacteristic extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicSchema); + this._type = 'QDM::PatientCharacteristic'; + } +} + +module.exports.PatientCharacteristic = PatientCharacteristic; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],140:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicBirthdateSchema = DataElementSchema({ + birthDatetime: DateTime, + qdmTitle: { type: String, default: 'Patient Characteristic Birthdate' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.54' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'birthdate' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicBirthdate' }, + +}); + +module.exports.PatientCharacteristicBirthdateSchema = PatientCharacteristicBirthdateSchema; +class PatientCharacteristicBirthdate extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicBirthdateSchema); + this._type = 'QDM::PatientCharacteristicBirthdate'; + } +} + +module.exports.PatientCharacteristicBirthdate = PatientCharacteristicBirthdate; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],141:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicClinicalTrialParticipantSchema = DataElementSchema({ + reason: Code, + relevantPeriod: Interval, + qdmTitle: { type: String, default: 'Patient Characteristic Clinical Trial Participant' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.6' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.51' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'clinical_trial_participant' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicClinicalTrialParticipant' }, + +}); + +module.exports.PatientCharacteristicClinicalTrialParticipantSchema = PatientCharacteristicClinicalTrialParticipantSchema; +class PatientCharacteristicClinicalTrialParticipant extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicClinicalTrialParticipantSchema); + this._type = 'QDM::PatientCharacteristicClinicalTrialParticipant'; + } +} + +module.exports.PatientCharacteristicClinicalTrialParticipant = PatientCharacteristicClinicalTrialParticipant; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],142:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicEthnicitySchema = DataElementSchema({ + qdmTitle: { type: String, default: 'Patient Characteristic Ethnicity' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.56' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'ethnicity' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicEthnicity' }, + +}); + +module.exports.PatientCharacteristicEthnicitySchema = PatientCharacteristicEthnicitySchema; +class PatientCharacteristicEthnicity extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicEthnicitySchema); + this._type = 'QDM::PatientCharacteristicEthnicity'; + } +} + +module.exports.PatientCharacteristicEthnicity = PatientCharacteristicEthnicity; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],143:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicExpiredSchema = DataElementSchema({ + expiredDatetime: DateTime, + cause: Code, + qdmTitle: { type: String, default: 'Patient Characteristic Expired' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.57' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'expired' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicExpired' }, + +}); + +module.exports.PatientCharacteristicExpiredSchema = PatientCharacteristicExpiredSchema; +class PatientCharacteristicExpired extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicExpiredSchema); + this._type = 'QDM::PatientCharacteristicExpired'; + } +} + +module.exports.PatientCharacteristicExpired = PatientCharacteristicExpired; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],144:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicPayerSchema = DataElementSchema({ + relevantPeriod: Interval, + qdmTitle: { type: String, default: 'Patient Characteristic Payer' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.58' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'payer' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicPayer' }, + +}); + +module.exports.PatientCharacteristicPayerSchema = PatientCharacteristicPayerSchema; +class PatientCharacteristicPayer extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicPayerSchema); + this._type = 'QDM::PatientCharacteristicPayer'; + } +} + +module.exports.PatientCharacteristicPayer = PatientCharacteristicPayer; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],145:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicRaceSchema = DataElementSchema({ + qdmTitle: { type: String, default: 'Patient Characteristic Race' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.59' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'race' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicRace' }, + +}); + +module.exports.PatientCharacteristicRaceSchema = PatientCharacteristicRaceSchema; +class PatientCharacteristicRace extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicRaceSchema); + this._type = 'QDM::PatientCharacteristicRace'; + } +} + +module.exports.PatientCharacteristicRace = PatientCharacteristicRace; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],146:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientCharacteristicSexSchema = DataElementSchema({ + qdmTitle: { type: String, default: 'Patient Characteristic Sex' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.55' }, + qdmCategory: { type: String, default: 'patient_characteristic' }, + qdmStatus: { type: String, default: 'gender' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PatientCharacteristicSex' }, + +}); + +module.exports.PatientCharacteristicSexSchema = PatientCharacteristicSexSchema; +class PatientCharacteristicSex extends mongoose.Document { + constructor(object) { + super(object, PatientCharacteristicSexSchema); + this._type = 'QDM::PatientCharacteristicSex'; + } +} + +module.exports.PatientCharacteristicSex = PatientCharacteristicSex; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],147:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PhysicalExamOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + anatomicalLocationSite: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Physical Exam, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.61' }, + qdmCategory: { type: String, default: 'physical_exam' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PhysicalExamOrder' }, + +}); + +module.exports.PhysicalExamOrderSchema = PhysicalExamOrderSchema; +class PhysicalExamOrder extends mongoose.Document { + constructor(object) { + super(object, PhysicalExamOrderSchema); + this._type = 'QDM::PhysicalExamOrder'; + } +} + +module.exports.PhysicalExamOrder = PhysicalExamOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],148:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PhysicalExamPerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + reason: Code, + method: Code, + result: Any, + anatomicalLocationSite: Code, + negationRationale: Code, + components: [ComponentSchema], + performer: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Physical Exam, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.62' }, + qdmCategory: { type: String, default: 'physical_exam' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PhysicalExamPerformed' }, + +}); + +module.exports.PhysicalExamPerformedSchema = PhysicalExamPerformedSchema; +class PhysicalExamPerformed extends mongoose.Document { + constructor(object) { + super(object, PhysicalExamPerformedSchema); + this._type = 'QDM::PhysicalExamPerformed'; + } +} + +module.exports.PhysicalExamPerformed = PhysicalExamPerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],149:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PhysicalExamRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + anatomicalLocationSite: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Physical Exam, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.63' }, + qdmCategory: { type: String, default: 'physical_exam' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::PhysicalExamRecommended' }, + +}); + +module.exports.PhysicalExamRecommendedSchema = PhysicalExamRecommendedSchema; +class PhysicalExamRecommended extends mongoose.Document { + constructor(object) { + super(object, PhysicalExamRecommendedSchema); + this._type = 'QDM::PhysicalExamRecommended'; + } +} + +module.exports.PhysicalExamRecommended = PhysicalExamRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],150:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ProcedureOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + anatomicalLocationSite: Code, + rank: Number, + priority: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Procedure, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.66' }, + qdmCategory: { type: String, default: 'procedure' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::ProcedureOrder' }, + +}); + +module.exports.ProcedureOrderSchema = ProcedureOrderSchema; +class ProcedureOrder extends mongoose.Document { + constructor(object) { + super(object, ProcedureOrderSchema); + this._type = 'QDM::ProcedureOrder'; + } +} + +module.exports.ProcedureOrder = ProcedureOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],151:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ProcedurePerformedSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + reason: Code, + method: Code, + result: Any, + status: Code, + anatomicalLocationSite: Code, + rank: Number, + incisionDatetime: DateTime, + negationRationale: Code, + components: [ComponentSchema], + performer: [AnyEntity], + relatedTo: [String], + qdmTitle: { type: String, default: 'Procedure, Performed' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.67' }, + qdmCategory: { type: String, default: 'procedure' }, + qdmStatus: { type: String, default: 'performed' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::ProcedurePerformed' }, + +}); + +module.exports.ProcedurePerformedSchema = ProcedurePerformedSchema; +class ProcedurePerformed extends mongoose.Document { + constructor(object) { + super(object, ProcedurePerformedSchema); + this._type = 'QDM::ProcedurePerformed'; + } +} + +module.exports.ProcedurePerformed = ProcedurePerformed; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],152:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ProcedureRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + anatomicalLocationSite: Code, + rank: Number, + requester: [AnyEntity], + negationRationale: Code, + qdmTitle: { type: String, default: 'Procedure, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.68' }, + qdmCategory: { type: String, default: 'procedure' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::ProcedureRecommended' }, + +}); + +module.exports.ProcedureRecommendedSchema = ProcedureRecommendedSchema; +class ProcedureRecommended extends mongoose.Document { + constructor(object) { + super(object, ProcedureRecommendedSchema); + this._type = 'QDM::ProcedureRecommended'; + } +} + +module.exports.ProcedureRecommended = ProcedureRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],153:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ProviderCareExperienceSchema = DataElementSchema({ + authorDatetime: DateTime, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Provider Care Experience' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.70' }, + qdmCategory: { type: String, default: 'care_experience' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::ProviderCareExperience' }, + +}); + +module.exports.ProviderCareExperienceSchema = ProviderCareExperienceSchema; +class ProviderCareExperience extends mongoose.Document { + constructor(object) { + super(object, ProviderCareExperienceSchema); + this._type = 'QDM::ProviderCareExperience'; + } +} + +module.exports.ProviderCareExperience = ProviderCareExperience; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],154:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const AnyEntity = require('./basetypes/AnyEntity'); +const AllDataElements = require('./AllDataElements'); + +const [Schema, Number, String, Mixed] = [ + mongoose.Schema, + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, + mongoose.Schema.Types.Mixed, +]; + +const QDMPatientSchema = new Schema({ + birthDatetime: DateTime, + qdmVersion: { type: String, default: '5.6' }, + + // These are the "data criteria", or QDM datatype elements that exist on a + // patient. + dataElements: [], + // This field is for application specific information only. If both Bonnie + // Cypress use a common field, it should be made a field on this model, + // and not put into extendedData. + extendedData: { + type: Mixed, + default: {}, + }, +}, { id: false }); + +// After initialization of a Patient model, initialize every individual data element +// to its respective Mongoose Model +QDMPatientSchema.methods.initializeDataElements = function initializeDataElements() { + let typeStripped; + const dataElementsInit = []; + this.dataElements.forEach((element) => { + typeStripped = element._type.replace(/QDM::/, ''); + dataElementsInit.push(new AllDataElements[typeStripped](element)); + }); + this.set({ dataElements: dataElementsInit }); +}; + +QDMPatientSchema.methods.id = function id() { + return this._id; +}; + +// Returns an array of elements that exist on this patient, that +// match the given HQMF data criteria OID. +QDMPatientSchema.methods.getByHqmfOid = function getByHqmfOid(hqmfOid) { + return this.dataElements.filter(element => element.hqmfOid === hqmfOid); +}; + +// Returns an array of elements that exist on this patient, that +// match the given QRDA data criteria OID. +QDMPatientSchema.methods.getByQrdaOid = function getByQrdaOid(qrdaOid) { + return this.dataElements.filter(element => element.qrdaOid === qrdaOid); +}; + +// cql-execution prefers getId() over id() because some data models may have an id property +QDMPatientSchema.methods.getId = function getId() { + return this._id; +}; + +/* eslint no-underscore-dangle: 0 */ +QDMPatientSchema.methods._is = function _is(typeSpecifier) { + return this._typeHierarchy().some( + t => t.type === typeSpecifier.type && t.name === typeSpecifier.name + ); +}; + +/* eslint no-underscore-dangle: 0 */ +QDMPatientSchema.methods._typeHierarchy = function _typeHierarchy() { + const ver = this.qdmVersion.replace('.', '_'); + return [ + { + name: `{urn:healthit-gov:qdm:v${ver}}Patient`, + type: 'NamedTypeSpecifier', + }, + { name: '{urn:hl7-org:elm-types:r1}Tuple', type: 'NamedTypeSpecifier' }, + { name: '{urn:hl7-org:elm-types:r1}Any', type: 'NamedTypeSpecifier' }, + ]; +}; + +// Returns an array of elements that exist on this patient. Optionally +// takes a qdmCategory, which returns all elements of that QDM qdmCategory. +// Example: patient.getDataElements({qdmCategory: 'encounters'}) will return +// all Encounter QDM data types active on the patient. +QDMPatientSchema.methods.getDataElements = function getDataElements(params) { + if (params !== undefined && params.qdmCategory !== undefined && params.qdmStatus !== undefined) { + return this.dataElements.filter(element => (element.qdmCategory === params.qdmCategory) && (element.qdmStatus === params.qdmStatus)); + } else if (params !== undefined && params.qdmCategory !== undefined) { + return this.dataElements.filter(element => element.qdmCategory === params.qdmCategory); + } + return this.dataElements; +}; + +QDMPatientSchema.methods.addMethodsTo = function addMethodsTo(obj, methods) { + Object.entries(methods).forEach(([method_name, method]) => { + obj[method_name] = method; + }); +}; + +// Returns an array of dataElements that exist on the patient, queried by +// QDM profile +// @param {string} profile - the data criteria requested by the execution engine +// @param {boolean} isNegated - whether dataElements should be returned based on their negation status +// @returns {DataElement[]} +QDMPatientSchema.methods.getByProfile = function getByProfile(profile, isNegated = null) { + // If isNegated == true, only return data elements with a negationRationale that is not null. + // If isNegated == false, only return data elements with a null negationRationale. + // If isNegated == null, return all matching data elements by type, regardless of negationRationale. + const results = this.dataElements.filter(element => (element._type === `QDM::${profile}` || element._type === profile) && (isNegated === null || !!element.negationRationale === isNegated)); + return results.map((result) => { + // we need to convert mongoose document to an object. + // This is required for comparing the two objects(in cql-execution) as we can't compare two mongoose documents. + const removedMongooseItems = new AllDataElements[profile](result).toObject({ virtuals: true }); + // toObject() will remove all mongoose functions but also remove the schema methods, so we add them back + this.addMethodsTo(removedMongooseItems, result.schema.methods); + // add methods to entities + Object.entries(result).forEach(([key, values]) => { + // entities are always an array + if (Array.isArray(values)) { + try { + // check if the attribute is of Entity + if (AnyEntity.prototype.cast(values[0])) { + values.forEach((value, index) => { + const entity = removedMongooseItems[key][index]; + this.addMethodsTo(entity, value.schema.methods); + }); + } + // eslint-disable-next-line no-empty + } catch (e) {} + } + }); + return removedMongooseItems; + }); +}; + +// This method is called by the CQL execution engine on a CQLPatient when +// the execution engine wants information on a record. A record could be patient +// characteristic information about the patient, or it could be data criteria +// that currently exist on this patient (data criteria you drag on a patient +// in Bonnie patient builder). +// @param {String} profile - the data criteria requested by the execution engine +// @returns {Object} +QDMPatientSchema.methods.findRecords = function findRecords(profile) { + // Clear profile cache for this patient if there is no cache or the patient has changed + if (QDMPatientSchema.dataElementCache == null + || QDMPatientSchema.dataElementCachePatientId !== this._id) { + QDMPatientSchema.dataElementCache = {}; + QDMPatientSchema.dataElementCachePatientId = this._id; + } + // If there is a cache 'hit', return it + if (Object.prototype.hasOwnProperty.call(QDMPatientSchema.dataElementCache, profile)) { + return QDMPatientSchema.dataElementCache[profile]; + } + let profileStripped; + if (profile === 'Patient') { + // Requested generic patient info + const info = { birthDatetime: this.birthDatetime }; + QDMPatientSchema.dataElementCache[profile] = [info]; + return [info]; + } else if (/PatientCharacteristic/.test(profile)) { + // Requested a patient characteristic + profileStripped = profile.replace(/ *\{[^)]*\} */g, ''); + QDMPatientSchema.dataElementCache[profile] = this.getByProfile(profileStripped); + return QDMPatientSchema.dataElementCache[profile]; + } else if (profile != null) { + // Requested something else (probably a QDM data type). + + // Strip model details from request. The requested profile string contains + // a lot of things we do not need or care about. Example, we might see + // something like: + // "{urn:healthit-gov:qdm:v5_0_draft}PatientCharacteristicEthnicity" + // Where we only care about: "PatientCharacteristicEthnicity". + profileStripped = profile.replace(/ *\{[^)]*\} */g, ''); + + // Check and handle negation status + if (/Positive/.test(profileStripped)) { + profileStripped = profileStripped.replace(/Positive/, ''); + // Since the data criteria is 'Positive', it is not negated. + QDMPatientSchema.dataElementCache[profile] = this.getByProfile(profileStripped, false); + return QDMPatientSchema.dataElementCache[profile]; + } else if (/Negative/.test(profileStripped)) { + profileStripped = profileStripped.replace(/Negative/, ''); + // Since the data criteria is 'Negative', it is negated. + QDMPatientSchema.dataElementCache[profile] = this.getByProfile(profileStripped, true); + return QDMPatientSchema.dataElementCache[profile]; + } + // No negation status, proceed normally + QDMPatientSchema.dataElementCache[profile] = this.getByProfile(profileStripped); + return QDMPatientSchema.dataElementCache[profile]; + } + return []; +}; + +QDMPatientSchema.methods.adverse_events = function adverse_events() { + return this.getDataElements({ qdmCategory: 'adverse_event' }); +}; + +QDMPatientSchema.methods.allergies = function allergies() { + return this.getDataElements({ qdmCategory: 'allergy' }); +}; + +QDMPatientSchema.methods.assessments = function assessments() { + return this.getDataElements({ qdmCategory: 'assessment' }); +}; + +QDMPatientSchema.methods.care_experiences = function care_experiences() { + return this.getDataElements({ qdmCategory: 'care_experience' }); +}; + +QDMPatientSchema.methods.care_goals = function care_goals() { + return this.getDataElements({ qdmCategory: 'care_goal' }); +}; + +QDMPatientSchema.methods.communications = function communications() { + return this.getDataElements({ qdmCategory: 'communication' }); +}; + +QDMPatientSchema.methods.conditions = function conditions() { + return this.getDataElements({ qdmCategory: 'condition' }); +}; + +QDMPatientSchema.methods.devices = function devices() { + return this.getDataElements({ qdmCategory: 'device' }); +}; + +QDMPatientSchema.methods.diagnostic_studies = function diagnostic_studies() { + return this.getDataElements({ qdmCategory: 'diagnostic_study' }); +}; + +QDMPatientSchema.methods.encounters = function encounters() { + return this.getDataElements({ qdmCategory: 'encounter' }); +}; + +QDMPatientSchema.methods.family_history = function family_history() { + return this.getDataElements({ qdmCategory: 'family_history' }); +}; + +QDMPatientSchema.methods.functional_statuses = function functional_statuses() { + return this.getDataElements({ qdmCategory: 'functional_status' }); +}; + +QDMPatientSchema.methods.immunizations = function immunizations() { + return this.getDataElements({ qdmCategory: 'immunization' }); +}; + +QDMPatientSchema.methods.interventions = function interventions() { + return this.getDataElements({ qdmCategory: 'intervention' }); +}; + +QDMPatientSchema.methods.laboratory_tests = function laboratory_tests() { + return this.getDataElements({ qdmCategory: 'laboratory_test' }); +}; + +QDMPatientSchema.methods.medical_equipment = function medical_equipment() { + return this.getDataElements({ qdmCategory: 'medical_equipment' }); +}; + +QDMPatientSchema.methods.medications = function medications() { + return this.getDataElements({ qdmCategory: 'medication' }); +}; + +QDMPatientSchema.methods.patient_characteristics = function patient_characteristics() { + return this.getDataElements({ qdmCategory: 'patient_characteristic' }); +}; + +QDMPatientSchema.methods.physical_exams = function physical_exams() { + return this.getDataElements({ qdmCategory: 'physical_exam' }); +}; + +QDMPatientSchema.methods.preferences = function preferences() { + return this.getDataElements({ qdmCategory: 'preference' }); +}; + +QDMPatientSchema.methods.procedures = function procedures() { + return this.getDataElements({ qdmCategory: 'procedure' }); +}; + +QDMPatientSchema.methods.results = function results() { + return this.getDataElements({ qdmCategory: 'result' }); +}; + +QDMPatientSchema.methods.risk_category_assessments = function risk_category_assessments() { + return this.getDataElements({ qdmCategory: 'risk_category_assessment' }); +}; + +QDMPatientSchema.methods.social_history = function social_history() { + return this.getDataElements({ qdmCategory: 'social_history' }); +}; + +QDMPatientSchema.methods.substances = function substances() { + return this.getDataElements({ qdmCategory: 'substance' }); +}; + +QDMPatientSchema.methods.symptoms = function symptoms() { + return this.getDataElements({ qdmCategory: 'symptom' }); +}; + +QDMPatientSchema.methods.system_characteristics = function system_characteristics() { + return this.getDataElements({ qdmCategory: 'system_characteristic' }); +}; + +QDMPatientSchema.methods.transfers = function transfers() { + return this.getDataElements({ qdmCategory: 'transfer' }); +}; + +QDMPatientSchema.methods.vital_signs = function vital_signs() { + return this.getDataElements({ qdmCategory: 'vital_sign' }); +}; + +QDMPatientSchema.clearDataElementCache = function clearDataElementCache() { + QDMPatientSchema.dataElementCachePatientId = null; + QDMPatientSchema.dataElementCache = null; +}; + +module.exports.QDMPatientSchema = QDMPatientSchema; +class QDMPatient extends mongoose.Document { + constructor(object) { + super(object, QDMPatientSchema); + this.initializeDataElements(); + } +} +module.exports.QDMPatient = QDMPatient; + +},{"./AllDataElements":107,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/Quantity":179,"mongoose/browser":219}],155:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const RelatedPersonSchema = DataElementSchema({ + identifier: IdentifierSchema, + linkedPatientId: String, + qdmTitle: { type: String, default: 'Related Person' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.141' }, + qdmCategory: { type: String, default: 'related_person' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::RelatedPerson' }, + +}); + +module.exports.RelatedPersonSchema = RelatedPersonSchema; +class RelatedPerson extends mongoose.Document { + constructor(object) { + super(object, RelatedPersonSchema); + this._type = 'QDM::RelatedPerson'; + } +} + +module.exports.RelatedPerson = RelatedPerson; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],156:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const PlaceholderResultSchema = mongoose.Schema({ + cache_id: String, + measure_id: String, + sub_id: String, + test_id: String, + effective_date: Number, + filters: Object, + prefilter: Object, + calculation_time: Date, + status: Object, + population_ids: Object, + STRAT: Number, + IPP: Number, + DENOM: Number, + NUMER: Number, + NUMEX: Number, + DENEX: Number, + DENEXCEP: Number, + MSRPOPL: Number, + OBSERV: Number, + MSRPOPLEX: Number, + supplemental_data: Object, +}); + +module.exports.ResultSchema = PlaceholderResultSchema; +class PlaceholderResult extends mongoose.Document { + constructor(object) { + super(object, PlaceholderResultSchema); + } +} +module.exports.PlaceholderResult = PlaceholderResult; + +},{"mongoose/browser":219}],157:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const SubstanceAdministeredSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantDatetime: DateTime, + relevantPeriod: Interval, + dosage: Quantity, + frequency: Code, + route: Code, + negationRationale: Code, + performer: [AnyEntity], + qdmTitle: { type: String, default: 'Substance, Administered' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.73' }, + qdmCategory: { type: String, default: 'substance' }, + qdmStatus: { type: String, default: 'administered' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::SubstanceAdministered' }, + +}); + +module.exports.SubstanceAdministeredSchema = SubstanceAdministeredSchema; +class SubstanceAdministered extends mongoose.Document { + constructor(object) { + super(object, SubstanceAdministeredSchema); + this._type = 'QDM::SubstanceAdministered'; + } +} + +module.exports.SubstanceAdministered = SubstanceAdministered; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],158:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const SubstanceOrderSchema = DataElementSchema({ + authorDatetime: DateTime, + relevantPeriod: Interval, + reason: Code, + dosage: Quantity, + supply: Quantity, + frequency: Code, + refills: Number, + route: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Substance, Order' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.77' }, + qdmCategory: { type: String, default: 'substance' }, + qdmStatus: { type: String, default: 'order' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::SubstanceOrder' }, + +}); + +module.exports.SubstanceOrderSchema = SubstanceOrderSchema; +class SubstanceOrder extends mongoose.Document { + constructor(object) { + super(object, SubstanceOrderSchema); + this._type = 'QDM::SubstanceOrder'; + } +} + +module.exports.SubstanceOrder = SubstanceOrder; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],159:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const SubstanceRecommendedSchema = DataElementSchema({ + authorDatetime: DateTime, + reason: Code, + dosage: Quantity, + frequency: Code, + refills: Number, + route: Code, + negationRationale: Code, + requester: [AnyEntity], + qdmTitle: { type: String, default: 'Substance, Recommended' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.78' }, + qdmCategory: { type: String, default: 'substance' }, + qdmStatus: { type: String, default: 'recommended' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::SubstanceRecommended' }, + +}); + +module.exports.SubstanceRecommendedSchema = SubstanceRecommendedSchema; +class SubstanceRecommended extends mongoose.Document { + constructor(object) { + super(object, SubstanceRecommendedSchema); + this._type = 'QDM::SubstanceRecommended'; + } +} + +module.exports.SubstanceRecommended = SubstanceRecommended; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],160:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./attributes/Identifier'); +const { DataElementSchema } = require('./basetypes/DataElement'); +const Code = require('./basetypes/Code'); +const Interval = require('./basetypes/Interval'); +const Quantity = require('./basetypes/Quantity'); +const DateTime = require('./basetypes/DateTime'); +const QDMDate = require('./basetypes/QDMDate'); +const Any = require('./basetypes/Any'); +const AnyEntity = require('./basetypes/AnyEntity'); +const { ComponentSchema } = require('./attributes/Component'); +const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); +const { EntitySchema } = require('./attributes/Entity'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const SymptomSchema = DataElementSchema({ + prevalencePeriod: Interval, + severity: Code, + recorder: [AnyEntity], + qdmTitle: { type: String, default: 'Symptom' }, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.116' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.136' }, + qdmCategory: { type: String, default: 'symptom' }, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Symptom' }, + +}); + +module.exports.SymptomSchema = SymptomSchema; +class Symptom extends mongoose.Document { + constructor(object) { + super(object, SymptomSchema); + this._type = 'QDM::Symptom'; + } +} + +module.exports.Symptom = Symptom; + + +},{"./attributes/Component":162,"./attributes/Entity":164,"./attributes/FacilityLocation":165,"./attributes/Identifier":166,"./basetypes/Any":172,"./basetypes/AnyEntity":173,"./basetypes/Code":174,"./basetypes/DataElement":175,"./basetypes/DateTime":176,"./basetypes/Interval":177,"./basetypes/QDMDate":178,"./basetypes/Quantity":179,"mongoose/browser":219}],161:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { EntitySchemaFunction } = require('./Entity'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const CarePartnerSchema = EntitySchemaFunction({ + relationship: Code, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.134' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.160' }, + _type: { type: String, default: 'QDM::CarePartner' }, + +}); + +module.exports.CarePartnerSchema = CarePartnerSchema; +class CarePartner extends mongoose.Document { + constructor(object) { + super(object, CarePartnerSchema); + this._type = 'QDM::CarePartner'; + } +} + +module.exports.CarePartner = CarePartner; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Entity":164,"mongoose/browser":219}],162:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Schema] = [mongoose.Schema]; + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ComponentSchema = new mongoose.Schema({ + code: Code, + result: Any, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Component' }, + +}); + +module.exports.ComponentSchema = ComponentSchema; +class Component extends mongoose.Document { + constructor(object) { + super(object, ComponentSchema); + this._type = 'QDM::Component'; + } +} + +function ComponentSchemaFunction(add, options) { + const extended = new Schema({ + code: Code, + result: Any, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Component' }, + + + }, options); + + if (add) { + extended.add(add); + } + + /* eslint no-underscore-dangle: 0 */ + extended.methods._is = function _is(typeSpecifier) { + return this._typeHierarchy().some( + t => t.type === typeSpecifier.type && t.name === typeSpecifier.name + ); + }; + + extended.methods._typeHierarchy = function _typeHierarchy() { + const typeName = this._type.replace(/QDM::/, ''); + const ver = this.qdmVersion.replace('.', '_'); + return [ + { + name: `{urn:healthit-gov:qdm:v${ver}}${typeName}`, + type: 'NamedTypeSpecifier', + }, + ]; + }; + return extended; +} + +module.exports.Component = Component; +module.exports.ComponentSchemaFunction = ComponentSchemaFunction; + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"mongoose/browser":219}],163:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const DiagnosisComponentSchema = new mongoose.Schema({ + code: Code, + presentOnAdmissionIndicator: Code, + rank: Number, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::DiagnosisComponent' }, + +}); + +module.exports.DiagnosisComponentSchema = DiagnosisComponentSchema; +class DiagnosisComponent extends mongoose.Document { + constructor(object) { + super(object, DiagnosisComponentSchema); + this._type = 'QDM::DiagnosisComponent'; + } +} + +module.exports.DiagnosisComponent = DiagnosisComponent; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"mongoose/browser":219}],164:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { IdentifierSchema } = require('./Identifier'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Schema] = [mongoose.Schema]; + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const EntitySchema = new mongoose.Schema({ + id: String, + identifier: IdentifierSchema, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Entity' }, + +}); + +module.exports.EntitySchema = EntitySchema; +class Entity extends mongoose.Document { + constructor(object) { + super(object, EntitySchema); + this._type = 'QDM::Entity'; + } +} + +function EntitySchemaFunction(add, options) { + const extended = new Schema({ + identifier: IdentifierSchema, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Entity' }, + + + id: { + type: String, + default() { + return this._id ? this._id.toString() : mongoose.Types.ObjectId().toString(); + }, + }, + + }, options); + + if (add) { + extended.add(add); + } + + /* eslint no-underscore-dangle: 0 */ + extended.methods._is = function _is(typeSpecifier) { + return this._typeHierarchy().some( + t => t.type === typeSpecifier.type && t.name === typeSpecifier.name + ); + }; + + extended.methods._typeHierarchy = function _typeHierarchy() { + const typeName = this._type.replace(/QDM::/, ''); + const ver = this.qdmVersion.replace('.', '_'); + return [ + { + name: `{urn:healthit-gov:qdm:v${ver}}${typeName}`, + type: 'NamedTypeSpecifier', + }, + ]; + }; + return extended; +} + +module.exports.Entity = Entity; +module.exports.EntitySchemaFunction = EntitySchemaFunction; + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Identifier":166,"mongoose/browser":219}],165:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const FacilityLocationSchema = new mongoose.Schema({ + code: Code, + locationPeriod: Interval, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::FacilityLocation' }, + +}); + +module.exports.FacilityLocationSchema = FacilityLocationSchema; +class FacilityLocation extends mongoose.Document { + constructor(object) { + super(object, FacilityLocationSchema); + this._type = 'QDM::FacilityLocation'; + } +} + +module.exports.FacilityLocation = FacilityLocation; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"mongoose/browser":219}],166:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const IdentifierSchema = mongoose.Schema({ + namingSystem: String, + value: String, + qdmVersion: { type: String, default: '5.6' }, + _type: { type: String, default: 'QDM::Identifier' }, + +}, { _id: false, id: false }); + +module.exports.IdentifierSchema = IdentifierSchema; +class Identifier extends mongoose.Document { + constructor(object) { + super(object, IdentifierSchema); + this._type = 'QDM::Identifier'; + } +} +module.exports.Identifier = Identifier; + +},{"mongoose/browser":219}],167:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { EntitySchemaFunction } = require('./Entity'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const LocationSchema = EntitySchemaFunction({ + locationType: Code, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.142' }, + _type: { type: String, default: 'QDM::Location' }, + +}); + +module.exports.LocationSchema = LocationSchema; +class Location extends mongoose.Document { + constructor(object) { + super(object, LocationSchema); + this._type = 'QDM::Location'; + } +} + +module.exports.Location = Location; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Entity":164,"mongoose/browser":219}],168:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { EntitySchemaFunction } = require('./Entity'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const OrganizationSchema = EntitySchemaFunction({ + organizationType: Code, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.135' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.163' }, + _type: { type: String, default: 'QDM::Organization' }, + +}); + +module.exports.OrganizationSchema = OrganizationSchema; +class Organization extends mongoose.Document { + constructor(object) { + super(object, OrganizationSchema); + this._type = 'QDM::Organization'; + } +} + +module.exports.Organization = Organization; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Entity":164,"mongoose/browser":219}],169:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { EntitySchemaFunction } = require('./Entity'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PatientEntitySchema = EntitySchemaFunction({ + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.136' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.161' }, + _type: { type: String, default: 'QDM::PatientEntity' }, + +}); + +module.exports.PatientEntitySchema = PatientEntitySchema; +class PatientEntity extends mongoose.Document { + constructor(object) { + super(object, PatientEntitySchema); + this._type = 'QDM::PatientEntity'; + } +} + +module.exports.PatientEntity = PatientEntity; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Entity":164,"mongoose/browser":219}],170:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { EntitySchemaFunction } = require('./Entity'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const PractitionerSchema = EntitySchemaFunction({ + role: Code, + specialty: Code, + qualification: Code, + hqmfOid: { type: String, default: '2.16.840.1.113883.10.20.28.4.137' }, + qrdaOid: { type: String, default: '2.16.840.1.113883.10.20.24.3.162' }, + _type: { type: String, default: 'QDM::Practitioner' }, + +}); + +module.exports.PractitionerSchema = PractitionerSchema; +class Practitioner extends mongoose.Document { + constructor(object) { + super(object, PractitionerSchema); + this._type = 'QDM::Practitioner'; + } +} + +module.exports.Practitioner = Practitioner; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Entity":164,"mongoose/browser":219}],171:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const { ComponentSchemaFunction } = require('./Component'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const QDMDate = require('../basetypes/QDMDate'); +const Any = require('../basetypes/Any'); + +const [Number, String] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, +]; + +const ResultComponentSchema = ComponentSchemaFunction({ + referenceRange: Interval, + _type: { type: String, default: 'QDM::ResultComponent' }, + +}); + +module.exports.ResultComponentSchema = ResultComponentSchema; +class ResultComponent extends mongoose.Document { + constructor(object) { + super(object, ResultComponentSchema); + this._type = 'QDM::ResultComponent'; + } +} + +module.exports.ResultComponent = ResultComponent; + + +},{"../basetypes/Any":172,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/QDMDate":178,"../basetypes/Quantity":179,"./Component":162,"mongoose/browser":219}],172:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); + +function Any(key, options) { + mongoose.SchemaType.call(this, key, options, 'Any'); +} +Any.prototype = Object.create(mongoose.SchemaType.prototype); + +function RecursiveCast(any) { + if (any && any.value !== undefined && any.unit) { + return new cql.Quantity(any.value, any.unit); + } + + if (any && any.numerator && any.denominator) { + const numerator = new cql.Quantity(any.numerator.value, any.numerator.unit); + const denominator = new cql.Quantity(any.denominator.value, any.denominator.unit); + return new cql.Ratio(numerator, denominator); + } + + if (any.isCode || any.isConcept || any.isValueSet || any.isList || + any.isDateTime || any.isDate || any.isRatio || any.isQuantiy || + any.isInterval || any.isBooleanLiteral || any.isIntegerLiteral || + any.isDecimalLiteral || any.isStringLiteral || any.isTuple) { + return any; + } + + if (any && any.code && any.system) { + const val = { code: any.code, system: any.system }; + val.display = (typeof any.display !== 'undefined') ? any.display : null; + val.version = (typeof any.version !== 'undefined') ? any.version : null; + + return new cql.Code(val.code, val.system, val.version, val.display); + } + if (any && any.low) { + const casted = new cql.Interval(any.low, any.high, any.lowClosed, any.highClosed); + + // Cast Low and High values to Quantities if it is a quantity + if (casted.low && casted.low.unit && casted.low.value) { + casted.low = new cql.Quantity(casted.low.value, casted.low.unit); + if (casted.high && casted.high.unit && casted.high.value) { + casted.high = new cql.Quantity(casted.high.value, casted.high.unit); + } + return casted; + } + + // Cast to DateTime if it is a string representing a DateTime + if (casted.low && Date.parse(casted.low)) { + casted.low = cql.DateTime.fromJSDate(new Date(casted.low), 0); + } + if (casted.high && Date.parse(casted.high)) { + casted.high = cql.DateTime.fromJSDate(new Date(casted.high), 0); + } + return casted; + } + if (Array.isArray(any)) { + const casted = []; + any.forEach((val) => { + casted.push(RecursiveCast(val)); + }); + return casted; + } + if (Number.isFinite(any)) { + return any; + } + if (Date.parse(any) || Date.parse(`1984-01-01T${any}`)) { + if (any.match(/T/) || any.match(/\+/)) { + // If it has a T or a timezoneoffset, it must be a DateTime + return cql.DateTime.fromJSDate(new Date(any), 0); + } + if (any.match(/:/)) { + // If it has a : but no T or timezoneoffset, it must be a Time + return cql.DateTime.fromJSDate(new Date(`1984-01-01T${any}`), 0).getTime(); + } + // Must be a Date + return cql.DateTime.fromJSDate(new Date(any), 0).getDate(); + } + return any; +} + +Any.prototype.cast = any => RecursiveCast(any); + +mongoose.Schema.Types.Any = Any; +module.exports = Any; + +},{"cql-execution":54,"mongoose/browser":219}],173:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const { PatientEntity } = require('../attributes/PatientEntity'); +const { Practitioner } = require('../attributes/Practitioner'); +const { CarePartner } = require('../attributes/CarePartner'); +const { Organization } = require('../attributes/Organization'); +const { Location } = require('../attributes/Location'); + +function AnyEntity(key, options) { + mongoose.SchemaType.call(this, key, options, 'AnyEntity'); +} +AnyEntity.prototype = Object.create(mongoose.SchemaType.prototype); + +AnyEntity.prototype.cast = (entity) => { + if (entity == null) { + return null; + } + + if (entity instanceof PatientEntity || entity instanceof Practitioner || entity instanceof CarePartner || entity instanceof Organization || entity instanceof Location) { + return entity; + } + + if (entity._type != null) { + // copy _id to id if it isn't defined + if (entity.id == null && entity._id != null) { + entity.id = entity._id; + } + switch (entity._type) { + case 'QDM::PatientEntity': + return new PatientEntity(entity); + case 'QDM::Practitioner': + return new Practitioner(entity); + case 'QDM::CarePartner': + return new CarePartner(entity); + case 'QDM::Organization': + return new Organization(entity); + case 'QDM::Location': + return new Location(entity); + default: + throw new Error(`Could not find entity type "${entity._type}".`); + } + } else { + throw new Error('Could not find _type indicator for entity.'); + } +}; + +mongoose.Schema.Types.AnyEntity = AnyEntity; +module.exports = AnyEntity; + +},{"../attributes/CarePartner":161,"../attributes/Location":167,"../attributes/Organization":168,"../attributes/PatientEntity":169,"../attributes/Practitioner":170,"mongoose/browser":219}],174:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); + +function Code(key, options) { + mongoose.SchemaType.call(this, key, options, 'Code'); +} +Code.prototype = Object.create(mongoose.SchemaType.prototype); + +Code.prototype.cast = (code) => { + if (code != null) { + // return code if it doesn't even need casting + if (code.isCode) { + return code; + } + // handles codes that have not yet been cast to a code and those that have already been cast to a code + if (code.code && code.system) { + const val = { code: code.code, system: code.system }; + val.display = (typeof code.display !== 'undefined') ? code.display : null; + val.version = (typeof code.version !== 'undefined') ? code.version : null; + + return new cql.Code(val.code, val.system, val.version, val.display); + } + throw new Error(`Expected a code. Received ${code}.`); + } else { + // returns a null or undefined if what is passed in is null or undefined + return code; + } +}; + +mongoose.Schema.Types.Code = Code; +module.exports = Code; + +},{"cql-execution":54,"mongoose/browser":219}],175:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); +const Code = require('./Code.js'); +const Identifier = require('../attributes/Identifier'); + +const [Schema] = [mongoose.Schema]; + +function DataElementSchema(add, options) { + const extended = new Schema({ + dataElementCodes: { type: [Code] }, + description: { type: String }, + codeListId: { type: String }, + id: { + type: String, + default() { + return this._id ? this._id.toString() : mongoose.Types.ObjectId().toString(); + }, + }, + }, options); + + if (add) { + extended.add(add); + } + + // Returns all of the codes on this data element in a format usable by + // the cql-execution framework. + extended.methods.getCode = function getCode() { + if (this.dataElementCodes) { + return this.dataElementCodes.map((code) => { + if (code.isCode) { + return code; + } + return new cql.Code(code.code, code.system, code.version, code.display); + }); + } + return null; + }; + + // Return the first code on this data element in a format usable by + // the cql-execution framework. + extended.methods.code = function code() { + if (this.dataElementCodes && this.dataElementCodes[0]) { + const qdmCode = this.dataElementCodes[0]; + if (qdmCode.isCode) { + return qdmCode; + } + return new cql.Code(qdmCode.code, qdmCode.system, qdmCode.version, qdmCode.display); + } + return null; + }; + + /* eslint no-underscore-dangle: 0 */ + extended.methods._is = function _is(typeSpecifier) { + return this._typeHierarchy().some( + t => t.type === typeSpecifier.type && t.name === typeSpecifier.name + ); + }; + + /* eslint no-underscore-dangle: 0 */ + extended.methods._typeHierarchy = function _typeHierarchy() { + const typeName = this._type.replace(/QDM::/, ''); + const prefix = this.negationRationale ? 'Negative' : 'Positive'; + const ver = this.qdmVersion.replace('.', '_'); + return [ + { + name: `{urn:healthit-gov:qdm:v${ver}}${prefix}${typeName}`, + type: 'NamedTypeSpecifier', + }, + { + name: `{urn:healthit-gov:qdm:v${ver}}${typeName}`, + type: 'NamedTypeSpecifier', + }, + { name: '{urn:hl7-org:elm-types:r1}Tuple', type: 'NamedTypeSpecifier' }, + { name: '{urn:hl7-org:elm-types:r1}Any', type: 'NamedTypeSpecifier' }, + ]; + }; + return extended; +} + +module.exports.DataElementSchema = DataElementSchema; + +},{"../attributes/Identifier":166,"./Code.js":174,"cql-execution":54,"mongoose/browser":219}],176:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); + +function DateTime(key, options) { + mongoose.SchemaType.call(this, key, options, 'DateTime'); +} +DateTime.prototype = Object.create(mongoose.SchemaType.prototype); + +DateTime.prototype.cast = (dateTime) => { + if (dateTime.isDateTime) { + return dateTime; + } + + if (!Date.parse(dateTime)) { + throw new Error(`DateTime: ${dateTime} is not a valid DateTime`); + } + + return cql.DateTime.fromJSDate(new Date(dateTime), 0); +}; + +mongoose.Schema.Types.DateTime = DateTime; +module.exports = DateTime; + +},{"cql-execution":54,"mongoose/browser":219}],177:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); +const DateTime = require('./DateTime'); + +function Interval(key, options) { + mongoose.SchemaType.call(this, key, options, 'Interval'); +} +Interval.prototype = Object.create(mongoose.SchemaType.prototype); + +Interval.prototype.cast = (interval) => { + if (interval.isInterval) { + return interval; + } + const casted = new cql.Interval(interval.low, interval.high, interval.lowClosed, interval.highClosed); + + // Cast Low and High values to Quantities if it is a quantity + if (casted.low && casted.low.unit && casted.low.value) { + casted.low = new cql.Quantity(casted.low.value, casted.low.unit); + if (casted.high && casted.high.unit && casted.high.value) { + casted.high = new cql.Quantity(casted.high.value, casted.high.unit); + } + return casted; + } + + // Cast to DateTime if it is a string representing a DateTime + if (casted.low) { + casted.low = DateTime.prototype.cast(casted.low); + } + + if (casted.high) { + casted.high = DateTime.prototype.cast(casted.high); + } + return casted; +}; + +mongoose.Schema.Types.Interval = Interval; +module.exports = Interval; + +},{"./DateTime":176,"cql-execution":54,"mongoose/browser":219}],178:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); + +function QDMDate(key, options) { + mongoose.SchemaType.call(this, key, options, 'Date'); +} +QDMDate.prototype = Object.create(mongoose.SchemaType.prototype); + +QDMDate.prototype.cast = (date) => { + if (date == null) { + return date; + } + + // Already a CQL Date + if (date.isDate) { + return date; + } + + // Object + if (typeof date === 'object') { + const keys = Object.keys(date); + if (keys.includes('year') && keys.includes('month') && keys.includes('day')) { + return new cql.Date(date.year, date.month, date.day); + } + } + + // Date String + if (!cql.Date.parse(date)) { + throw new Error(`Date: ${date} is not a valid Date`); + } else { + return cql.Date.parse(date); + } +}; + +mongoose.Schema.Types.QDMDate = QDMDate; +module.exports = QDMDate; + +},{"cql-execution":54,"mongoose/browser":219}],179:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const cql = require('cql-execution'); + +function Quantity(key, options) { + mongoose.SchemaType.call(this, key, options, 'Quantity'); +} +Quantity.prototype = Object.create(mongoose.SchemaType.prototype); + +Quantity.prototype.cast = (quantity) => { + if (typeof quantity.value === 'undefined') { + throw new Error(`Quantity: ${quantity} does not have a value`); + } else if (typeof quantity.unit === 'undefined') { + throw new Error(`Quantity: ${quantity} does not have a unit`); + } + + return new cql.Quantity(quantity.value, quantity.unit); +}; + +mongoose.Schema.Types.Quantity = Quantity; +module.exports = Quantity; + +},{"cql-execution":54,"mongoose/browser":219}],180:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const { StatementDependencySchema } = require('./CQLStatementDependency'); + +const [Mixed, mDate] = [ + mongoose.Schema.Types.Mixed, + mongoose.Schema.Types.Date, +]; + +const CQLLibrarySchema = new mongoose.Schema( + { + library_name: String, + library_version: String, + cql: String, + elm: Mixed, + elm_annotations: Mixed, + is_main_library: { type: Boolean, default: false }, + is_top_level: { type: Boolean, default: true }, + statement_dependencies: [StatementDependencySchema], + }, + // Options + { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, // These are the Mongoid conventions for timestamps + } +); + +module.exports.CQLLibrarySchema = CQLLibrarySchema; +class CQLLibrary extends mongoose.Document { + constructor(object) { + super(object, CQLLibrarySchema); + } +} +module.exports.CQLLibrary = CQLLibrary; + +},{"./CQLStatementDependency":181,"mongoose/browser":219}],181:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const StatementReferenceSchema = new mongoose.Schema({ + library_name: String, + statement_name: String, + hqmf_id: String, +}); + +const StatementDependencySchema = new mongoose.Schema({ + statement_name: String, + statement_references: [StatementReferenceSchema], +}); + +module.exports.StatementReferenceSchema = StatementReferenceSchema; +class StatementReference extends mongoose.Document { + constructor(object) { + super(object, StatementReferenceSchema); + } +} +module.exports.StatementReference = StatementReference; + +module.exports.StatementDependencySchema = StatementDependencySchema; +class StatementDependency extends mongoose.Document { + constructor(object) { + super(object, StatementDependencySchema); + } +} +module.exports.StatementDependency = StatementDependency; + +},{"mongoose/browser":219}],182:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const [String, Mixed] = [ + mongoose.Schema.Types.String, + mongoose.Schema.Types.Mixed, +]; + +const ClauseResultSchema = mongoose.Schema({ + // Library the clause this result is for is in + library_name: String, + // Statement the clause this result is for is in + statement_name: String, + // LocalId of the clause this result is for + localId: String, + // Final, processed result of raw calculation + final: String, + // Raw result of clause calculation + raw: Mixed, +}); + + +module.exports.ClauseResultSchema = ClauseResultSchema; +class ClauseResult extends mongoose.Document { + constructor(object) { + super(object, ClauseResultSchema); + } +} +module.exports.ClauseResult = ClauseResult; + +},{"mongoose/browser":219}],183:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const ConceptSchema = new mongoose.Schema({ + code: String, + code_system_oid: String, + code_system_name: String, + code_system_version: String, + display_name: String, +}); + +module.exports.ConceptSchema = ConceptSchema; +class Concept extends mongoose.Document { + constructor(object) { + super(object, ConceptSchema); + } +} +module.exports.Concept = Concept; + +},{"mongoose/browser":219}],184:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const { ClauseResultSchema } = require('./ClauseResult'); +const { StatementResultSchema } = require('./StatementResult'); + +const [Number, String, Mixed, ObjectId] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, + mongoose.Schema.Types.Mixed, + mongoose.Schema.Types.ObjectId, +]; + +const IndividualResultSchema = mongoose.Schema( + { + // Population Attributes + STRAT: Number, + IPP: Number, + DENOM: Number, + NUMER: Number, + NUMEX: Number, + DENEX: Number, + DENEXCEP: Number, + MSRPOPL: Number, + OBSERV: Number, + MSRPOPLEX: Number, + + // Result Attributes + clause_results: [ClauseResultSchema], + statement_results: [StatementResultSchema], + population_relevance: Mixed, + episode_results: Mixed, + observation_values: [Number], + + // This field is for application specific information only. If both Bonnie and + // Cypress use a common field, it should be made a field on this model, + // and not put into extendedData. + extendedData: { + type: Mixed, + default: {}, + }, + + // Calculation State attributes + state: { + type: String, + enum: ['queued', 'running', 'complete', 'cancelled', 'failed'], + default: 'queued', + }, + + // Relations to other model classes + // 'alias' field makes it so you can call obj.measure, and get the object referenced by measure_id + measure_id: { type: ObjectId, ref: 'Measure', alias: 'measure' }, + patient_id: { type: ObjectId, ref: 'Patient', alias: 'patient' }, + + }, + // Options + { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, // These are the Mongoid conventions for timestamps + } +); + +IndividualResultSchema.methods.clause_results_by_clause = function clause_results_by_clause() { + const clause_results_hash = {}; + this.clause_results.forEach((result) => { + if (!clause_results_hash[result.library_name]) { + clause_results_hash[result.library_name] = {}; + } + clause_results_hash[result.library_name][result.localId] = result; + }); + return clause_results_hash; +}; + +IndividualResultSchema.methods.statement_results_by_statement = function statement_results_by_statement() { + const statement_results_hash = {}; + this.statement_results.forEach((result) => { + if (!statement_results_hash[result.library_name]) { + statement_results_hash[result.library_name] = {}; + } + statement_results_hash[result.library_name][result.statement_name] = result; + }); + return statement_results_hash; +}; + +module.exports.IndividualResultSchema = IndividualResultSchema; +class IndividualResult extends mongoose.Document { + constructor(object) { + super(object, IndividualResultSchema); + } +} +module.exports.IndividualResult = IndividualResult; + +},{"./ClauseResult":182,"./StatementResult":190,"mongoose/browser":219}],185:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DataElementSchema = require('../basetypes/DataElement').DataElementSchema(); +const AllDataElements = require('../AllDataElements'); +const { CQLLibrarySchema } = require('./CQLLibrary'); +const { PopulationSetSchema } = require('./PopulationSet'); + +const [Number, String, Boolean, Mixed, ObjectId, Date] = [ + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, + mongoose.Schema.Types.Boolean, + mongoose.Schema.Types.Mixed, + mongoose.Schema.Types.ObjectId, + mongoose.Schema.Types.Date, +]; + +const MeasureSchema = new mongoose.Schema( + { + // A version-specific UUID for the measure + hqmf_id: String, + // A version-neutral UUID for the measure + hqmf_set_id: String, + // A Semantic Version-compliant string (e.g. "2.3.4") for the measure + hqmf_version_number: String, + // A CMS-style string (e.g. "CMS2v4") for the measure + cms_id: String, + title: String, + description: String, + + // Composite/component measure fields + composite: { + type: Boolean, + default: false, + }, + component: { + type: Boolean, + default: false, + }, + component_hqmf_set_ids: [String], + composite_hqmf_set_id: String, + + // Measure type variables + measure_scoring: { + type: String, + enum: ['PROPORTION', 'RATIO', 'CONTINUOUS_VARIABLE', 'COHORT'], + default: 'PROPORTION', + }, + calculation_method: { + type: String, + enum: ['PATIENT', 'EPISODE_OF_CARE'], + default: 'PATIENT', + }, + calculate_sdes: Boolean, + + // ELM/CQL Measure-logic related data encapsulated in CQLLibrarySchema + // Field name changed from 'cql' to 'cql_libraries' because the semantics of + // embeds_many: cqls (on the Ruby side) sounded weird, + // and we wanted to keep the API consistent + cql_libraries: [CQLLibrarySchema], + main_cql_library: String, + + // HQMF/Tacoma-specific Measure-logic related data + population_criteria: Mixed, + source_data_criteria: [], + measure_period: Mixed, + measure_attributes: [], + + population_sets: [PopulationSetSchema], + + // Relations to other model classes + bundle: { type: ObjectId, ref: 'Bundle' }, // Cypress-specific, until we migrate the Bundle into cqm-models + package: { type: ObjectId, ref: 'MeasurePackage' }, // Bonnie-specific + patients: [{ type: ObjectId, ref: 'Patient', index: true }], + + value_sets: [{ type: ObjectId, ref: 'ValueSet' }], + }, + // Options + { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, // These are the Mongoid conventions for timestamps + } +); + +// After initialization of a Measure model, initialize every individual data element +// to its respective Mongoose Model +MeasureSchema.methods.initializeDataElements = function initializeDataElements() { + let typeStripped; + const sourceDataCriteriaInit = []; + this.source_data_criteria.forEach((element) => { + typeStripped = element._type.replace(/QDM::/, ''); + sourceDataCriteriaInit.push(new AllDataElements[typeStripped](element)); + }); + this.set('source_data_criteria', sourceDataCriteriaInit); +}; + +MeasureSchema.methods.all_stratifications = function all_stratifications() { + return this.population_sets.flatMap(ps => ps.stratifications); +}; + +module.exports.MeasureSchema = MeasureSchema; +class Measure extends mongoose.Document { + constructor(object) { + super(object, MeasureSchema); + this.initializeDataElements(); + } +} +module.exports.Measure = Measure; + +},{"../AllDataElements":107,"../basetypes/Code":174,"../basetypes/DataElement":175,"../basetypes/Interval":177,"../basetypes/Quantity":179,"./CQLLibrary":180,"./PopulationSet":188,"mongoose/browser":219}],186:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +// using mBuffer to not conflict with system Buffer +const [mBuffer, ObjectId] = [ + mongoose.Schema.Types.Buffer, + mongoose.Schema.Types.ObjectId, +]; + +const MeasurePackageSchema = new mongoose.Schema( + { + file: mBuffer, + measure: { type: ObjectId, ref: 'Measure' }, + }, + // Options + { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, + } +); + +module.exports.MeasurePackageSchema = MeasurePackageSchema; +class MeasurePackage extends mongoose.Document { + constructor(object) { + super(object, MeasurePackageSchema); + } +} +module.exports.MeasurePackage = MeasurePackage; + +},{"mongoose/browser":219}],187:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const Code = require('../basetypes/Code'); +const Interval = require('../basetypes/Interval'); +const Quantity = require('../basetypes/Quantity'); +const DateTime = require('../basetypes/DateTime'); +const { QDMPatientSchema, QDMPatient } = require('../QDMPatient'); +const { ProviderSchema } = require('./Provider'); + +const [Schema, Number, String, Mixed] = [ + mongoose.Schema, + mongoose.Schema.Types.Number, + mongoose.Schema.Types.String, + mongoose.Schema.Types.Mixed, +]; + +const PatientSchema = new Schema({ + + givenNames: [String], + familyName: String, + bundleId: String, + expectedValues: [], + notes: String, + qdmPatient: QDMPatientSchema, + providers: [ProviderSchema], + measure_ids: [String], + +}, { id: false }); + +module.exports.PatientSchema = PatientSchema; +class Patient extends mongoose.Document { + constructor(object) { + super(object, PatientSchema); + if (this.qdmPatient) { + this.qdmPatient = new QDMPatient(this.qdmPatient.toJSON()); + } + } +} +module.exports.Patient = Patient; + +},{"../QDMPatient":154,"../basetypes/Code":174,"../basetypes/DateTime":176,"../basetypes/Interval":177,"../basetypes/Quantity":179,"./Provider":189,"mongoose/browser":219}],188:[function(require,module,exports){ +/* eslint-disable no-unused-vars, no-param-reassign */ +const mongoose = require('mongoose/browser'); +const { StatementReferenceSchema } = require('./CQLStatementDependency'); + +const [Mixed] = [ + mongoose.Schema.Types.Mixed, +]; + +// TODO: figure out if there is a better way to handle inheritance like mongoid. +// _type is how mongoid stores the specific type of the embedded document. +const PopulationMapSchema = new mongoose.Schema({ + _type: String, + IPP: StatementReferenceSchema, + DENOM: StatementReferenceSchema, + NUMER: StatementReferenceSchema, + NUMEX: StatementReferenceSchema, + DENEX: StatementReferenceSchema, + DENEXCEP: StatementReferenceSchema, + MSRPOPL: StatementReferenceSchema, + MSRPOPLEX: StatementReferenceSchema, + // STRAT is only here so cqm-execution can handle stratification results compliation with the current approach. + STRAT: StatementReferenceSchema, +}); + +if (!PopulationMapSchema.options.toObject) PopulationMapSchema.options.toObject = {}; +PopulationMapSchema.options.toObject.transform = function transform(doc, ret, options) { + // remove the _id and _type of every document before returning the result + delete ret._id; + delete ret._type; + return ret; +}; + + +const StratificationSchema = new mongoose.Schema({ + title: String, + stratification_id: String, + hqmf_id: String, + statement: StatementReferenceSchema, +}); + +const ObservationSchema = new mongoose.Schema({ + title: String, + observation_function: StatementReferenceSchema, + observation_parameter: StatementReferenceSchema, + aggregation_type: String, + hqmf_id: String, +}); + +const PopulationSetSchema = new mongoose.Schema({ + title: String, + population_set_id: String, + populations: PopulationMapSchema, + stratifications: [StratificationSchema], + supplemental_data_elements: [StatementReferenceSchema], + observations: [ObservationSchema], +}); + +module.exports.StratificationSchema = StratificationSchema; +class Stratification extends mongoose.Document { + constructor(object) { + super(object, StratificationSchema); + } +} +module.exports.Stratification = Stratification; + +module.exports.ObservationSchema = ObservationSchema; +class Observation extends mongoose.Document { + constructor(object) { + super(object, ObservationSchema); + } +} +module.exports.Observation = Observation; + +module.exports.PopulationMapSchema = PopulationMapSchema; +class PopulationMap extends mongoose.Document { + constructor(object) { + super(object, PopulationMapSchema); + } +} +module.exports.PopulationMap = PopulationMap; + +module.exports.PopulationSetSchema = PopulationSetSchema; +class PopulationSet extends mongoose.Document { + constructor(object) { + super(object, PopulationSetSchema); + } +} +module.exports.PopulationSet = PopulationSet; + +},{"./CQLStatementDependency":181,"mongoose/browser":219}],189:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const [Schema, String, Boolean] = [ + mongoose.Schema, + mongoose.Schema.Types.String, + mongoose.Schema.Types.Boolean, +]; + +const AddressSchema = new mongoose.Schema({ + street: [String], + city: String, + state: String, + zip: String, + country: String, + use: String, +}); + +const TelecomSchema = new mongoose.Schema({ + use: String, + value: String, + preferred: Boolean, +}); + +const ProviderSchema = new Schema({ + _type: { type: String, default: 'Provider' }, + + givenNames: [String], + familyName: String, + specialty: String, + title: String, + addresses: [AddressSchema], + telecoms: [TelecomSchema], + +}, { id: false }); + +module.exports.ProviderSchema = ProviderSchema; +class Provider extends mongoose.Document { + constructor(object) { + super(object, ProviderSchema); + } +} +module.exports.Provider = Provider; + +},{"mongoose/browser":219}],190:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); + +const [String, Mixed] = [ + mongoose.Schema.Types.String, + mongoose.Schema.Types.Mixed, +]; + +const StatementResultSchema = mongoose.Schema({ + // Library the statement this result is for is in + library_name: String, + // Statement this result is for is in + statement_name: String, + // Result, processed for display, of the statement this result is for + pretty: String, + // Final, processed result of raw calculation + final: String, + // Raw result of clause calculation + raw: Mixed, + /* + * 'NA' - Not applicable. This statement is not relevant to any population calculation in this population_set. Common + * for unused library statements or statements only used for other population sets. + * + * 'FALSE' - This statement is not relevant to any of this patient's population inclusion calculations. + * + * 'TRUE' - This statement is relevant for one or more of the population inclusion calculations. + */ + relevance: { + type: String, + enum: ['NA', 'TRUE', 'FALSE'], + default: 'NA', + }, +}); + + +module.exports.StatementResultSchema = StatementResultSchema; +class StatementResult extends mongoose.Document { + constructor(object) { + super(object, StatementResultSchema); + } +} +module.exports.StatementResult = StatementResult; + +},{"mongoose/browser":219}],191:[function(require,module,exports){ +const mongoose = require('mongoose/browser'); +const Concept = require('./Concept.js'); + +const [String] = [mongoose.Schema.Types.String]; + +const ValueSetSchema = new mongoose.Schema( + { + oid: String, + display_name: String, + version: String, + + concepts: [Concept.ConceptSchema], + }, + // Options + { + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, + } +); + +module.exports.ValueSetSchema = ValueSetSchema; +class ValueSet extends mongoose.Document { + constructor(object) { + super(object, ValueSetSchema); + } +} +module.exports.ValueSet = ValueSet; + +},{"./Concept.js":183,"mongoose/browser":219}],192:[function(require,module,exports){ +module.exports = require('./AllDataElements.js'); +module.exports.CQL = require('cql-execution'); +module.exports.Result = require('./Result.js').Result; +module.exports.ResultSchema = require('./Result.js').ResultSchema; +module.exports.Measure = require('./cqm/Measure.js').Measure; +module.exports.MeasureSchema = require('./cqm/Measure.js').MeasureSchema; +module.exports.MeasurePackage = require('./cqm/MeasurePackage.js').MeasurePackage; +module.exports.MeasurePackageSchema = require('./cqm/MeasurePackage.js').MeasurePackageSchema; +module.exports.Patient = require('./cqm/Patient.js').Patient; +module.exports.PatientSchema = require('./cqm/Patient.js').PatientSchema; +module.exports.Provider = require('./cqm/Provider.js').Provider; +module.exports.ProviderSchema = require('./cqm/Provider.js').ProviderSchema; +module.exports.StatementDependency = require('./cqm/CQLStatementDependency.js').StatementDependency; +module.exports.StatementDependencySchema = require('./cqm/CQLStatementDependency.js').StatementDependencySchema; +module.exports.PopulationSet = require('./cqm/PopulationSet.js').PopulationSet; +module.exports.PopulationSetSchema = require('./cqm/PopulationSet.js').PopulationSetSchema; +module.exports.CQLLibrary = require('./cqm/CQLLibrary.js').CQLLibrary; +module.exports.CQLLibrarySchema = require('./cqm/CQLLibrary.js').CQLLibrarySchema; +module.exports.ValueSet = require('./cqm/ValueSet.js').ValueSet; +module.exports.ValueSetSchema = require('./cqm/ValueSet.js').ValueSetSchema; +module.exports.Concept = require('./cqm/Concept.js').Concept; +module.exports.ConceptSchema = require('./cqm/Concept.js').ConceptSchema; +module.exports.IndividualResult = require('./cqm/IndividualResult.js').IndividualResult; +module.exports.IndividualResultSchema = require('./cqm/IndividualResult.js').IndividualResultSchema; +module.exports.ClauseResult = require('./cqm/ClauseResult.js').ClauseResult; +module.exports.ClauseResultSchema = require('./cqm/ClauseResult.js').ClauseResultSchema; +module.exports.StatementResult = require('./cqm/StatementResult.js').StatementResult; +module.exports.StatementResultSchema = require('./cqm/StatementResult.js').StatementResultchema; + +},{"./AllDataElements.js":107,"./Result.js":156,"./cqm/CQLLibrary.js":180,"./cqm/CQLStatementDependency.js":181,"./cqm/ClauseResult.js":182,"./cqm/Concept.js":183,"./cqm/IndividualResult.js":184,"./cqm/Measure.js":185,"./cqm/MeasurePackage.js":186,"./cqm/Patient.js":187,"./cqm/PopulationSet.js":188,"./cqm/Provider.js":189,"./cqm/StatementResult.js":190,"./cqm/ValueSet.js":191,"cql-execution":54}],193:[function(require,module,exports){ +'use strict'; + +var hasPropertyDescriptors = require('has-property-descriptors')(); + +var GetIntrinsic = require('get-intrinsic'); + +var $defineProperty = hasPropertyDescriptors && GetIntrinsic('%Object.defineProperty%', true); +if ($defineProperty) { + try { + $defineProperty({}, 'a', { value: 1 }); + } catch (e) { + // IE 8 has a broken defineProperty + $defineProperty = false; + } +} + +var $SyntaxError = GetIntrinsic('%SyntaxError%'); +var $TypeError = GetIntrinsic('%TypeError%'); + +var gopd = require('gopd'); + +/** @type {(obj: Record, property: PropertyKey, value: unknown, nonEnumerable?: boolean | null, nonWritable?: boolean | null, nonConfigurable?: boolean | null, loose?: boolean) => void} */ +module.exports = function defineDataProperty( + obj, + property, + value +) { + if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) { + throw new $TypeError('`obj` must be an object or a function`'); + } + if (typeof property !== 'string' && typeof property !== 'symbol') { + throw new $TypeError('`property` must be a string or a symbol`'); + } + if (arguments.length > 3 && typeof arguments[3] !== 'boolean' && arguments[3] !== null) { + throw new $TypeError('`nonEnumerable`, if provided, must be a boolean or null'); + } + if (arguments.length > 4 && typeof arguments[4] !== 'boolean' && arguments[4] !== null) { + throw new $TypeError('`nonWritable`, if provided, must be a boolean or null'); + } + if (arguments.length > 5 && typeof arguments[5] !== 'boolean' && arguments[5] !== null) { + throw new $TypeError('`nonConfigurable`, if provided, must be a boolean or null'); + } + if (arguments.length > 6 && typeof arguments[6] !== 'boolean') { + throw new $TypeError('`loose`, if provided, must be a boolean'); + } + + var nonEnumerable = arguments.length > 3 ? arguments[3] : null; + var nonWritable = arguments.length > 4 ? arguments[4] : null; + var nonConfigurable = arguments.length > 5 ? arguments[5] : null; + var loose = arguments.length > 6 ? arguments[6] : false; + + /* @type {false | TypedPropertyDescriptor} */ + var desc = !!gopd && gopd(obj, property); + + if ($defineProperty) { + $defineProperty(obj, property, { + configurable: nonConfigurable === null && desc ? desc.configurable : !nonConfigurable, + enumerable: nonEnumerable === null && desc ? desc.enumerable : !nonEnumerable, + value: value, + writable: nonWritable === null && desc ? desc.writable : !nonWritable + }); + } else if (loose || (!nonEnumerable && !nonWritable && !nonConfigurable)) { + // must fall back to [[Set]], and was not explicitly asked to make non-enumerable, non-writable, or non-configurable + obj[property] = value; // eslint-disable-line no-param-reassign + } else { + throw new $SyntaxError('This environment does not support defining a property as non-configurable, non-writable, or non-enumerable.'); + } +}; + +},{"get-intrinsic":198,"gopd":199,"has-property-descriptors":200}],194:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +var R = typeof Reflect === 'object' ? Reflect : null +var ReflectApply = R && typeof R.apply === 'function' + ? R.apply + : function ReflectApply(target, receiver, args) { + return Function.prototype.apply.call(target, receiver, args); + } + +var ReflectOwnKeys +if (R && typeof R.ownKeys === 'function') { + ReflectOwnKeys = R.ownKeys +} else if (Object.getOwnPropertySymbols) { + ReflectOwnKeys = function ReflectOwnKeys(target) { + return Object.getOwnPropertyNames(target) + .concat(Object.getOwnPropertySymbols(target)); + }; +} else { + ReflectOwnKeys = function ReflectOwnKeys(target) { + return Object.getOwnPropertyNames(target); + }; +} + +function ProcessEmitWarning(warning) { + if (console && console.warn) console.warn(warning); +} + +var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) { + return value !== value; +} + +function EventEmitter() { + EventEmitter.init.call(this); +} +module.exports = EventEmitter; +module.exports.once = once; + +// Backwards-compat with node 0.10.x +EventEmitter.EventEmitter = EventEmitter; + +EventEmitter.prototype._events = undefined; +EventEmitter.prototype._eventsCount = 0; +EventEmitter.prototype._maxListeners = undefined; + +// By default EventEmitters will print a warning if more than 10 listeners are +// added to it. This is a useful default which helps finding memory leaks. +var defaultMaxListeners = 10; + +function checkListener(listener) { + if (typeof listener !== 'function') { + throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); + } +} + +Object.defineProperty(EventEmitter, 'defaultMaxListeners', { + enumerable: true, + get: function() { + return defaultMaxListeners; + }, + set: function(arg) { + if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) { + throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.'); + } + defaultMaxListeners = arg; + } +}); + +EventEmitter.init = function() { + + if (this._events === undefined || + this._events === Object.getPrototypeOf(this)._events) { + this._events = Object.create(null); + this._eventsCount = 0; + } + + this._maxListeners = this._maxListeners || undefined; +}; + +// Obviously not all Emitters should be limited to 10. This function allows +// that to be increased. Set to zero for unlimited. +EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { + if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { + throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.'); + } + this._maxListeners = n; + return this; +}; + +function _getMaxListeners(that) { + if (that._maxListeners === undefined) + return EventEmitter.defaultMaxListeners; + return that._maxListeners; +} + +EventEmitter.prototype.getMaxListeners = function getMaxListeners() { + return _getMaxListeners(this); +}; + +EventEmitter.prototype.emit = function emit(type) { + var args = []; + for (var i = 1; i < arguments.length; i++) args.push(arguments[i]); + var doError = (type === 'error'); + + var events = this._events; + if (events !== undefined) + doError = (doError && events.error === undefined); + else if (!doError) + return false; + + // If there is no 'error' event listener then throw. + if (doError) { + var er; + if (args.length > 0) + er = args[0]; + if (er instanceof Error) { + // Note: The comments on the `throw` lines are intentional, they show + // up in Node's output if this results in an unhandled exception. + throw er; // Unhandled 'error' event + } + // At least give some kind of context to the user + var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); + err.context = er; + throw err; // Unhandled 'error' event + } + + var handler = events[type]; + + if (handler === undefined) + return false; + + if (typeof handler === 'function') { + ReflectApply(handler, this, args); + } else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) + ReflectApply(listeners[i], this, args); + } + + return true; +}; + +function _addListener(target, type, listener, prepend) { + var m; + var events; + var existing; + + checkListener(listener); + + events = target._events; + if (events === undefined) { + events = target._events = Object.create(null); + target._eventsCount = 0; + } else { + // To avoid recursion in the case that type === "newListener"! Before + // adding it to the listeners, first emit "newListener". + if (events.newListener !== undefined) { + target.emit('newListener', type, + listener.listener ? listener.listener : listener); + + // Re-assign `events` because a newListener handler could have caused the + // this._events to be assigned to a new object + events = target._events; + } + existing = events[type]; + } + + if (existing === undefined) { + // Optimize the case of one listener. Don't need the extra array object. + existing = events[type] = listener; + ++target._eventsCount; + } else { + if (typeof existing === 'function') { + // Adding the second element, need to change to array. + existing = events[type] = + prepend ? [listener, existing] : [existing, listener]; + // If we've already got an array, just append. + } else if (prepend) { + existing.unshift(listener); + } else { + existing.push(listener); + } + + // Check for listener leak + m = _getMaxListeners(target); + if (m > 0 && existing.length > m && !existing.warned) { + existing.warned = true; + // No error code for this since it is a Warning + // eslint-disable-next-line no-restricted-syntax + var w = new Error('Possible EventEmitter memory leak detected. ' + + existing.length + ' ' + String(type) + ' listeners ' + + 'added. Use emitter.setMaxListeners() to ' + + 'increase limit'); + w.name = 'MaxListenersExceededWarning'; + w.emitter = target; + w.type = type; + w.count = existing.length; + ProcessEmitWarning(w); + } + } + + return target; +} + +EventEmitter.prototype.addListener = function addListener(type, listener) { + return _addListener(this, type, listener, false); +}; + +EventEmitter.prototype.on = EventEmitter.prototype.addListener; + +EventEmitter.prototype.prependListener = + function prependListener(type, listener) { + return _addListener(this, type, listener, true); + }; + +function onceWrapper() { + if (!this.fired) { + this.target.removeListener(this.type, this.wrapFn); + this.fired = true; + if (arguments.length === 0) + return this.listener.call(this.target); + return this.listener.apply(this.target, arguments); + } +} + +function _onceWrap(target, type, listener) { + var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener }; + var wrapped = onceWrapper.bind(state); + wrapped.listener = listener; + state.wrapFn = wrapped; + return wrapped; +} + +EventEmitter.prototype.once = function once(type, listener) { + checkListener(listener); + this.on(type, _onceWrap(this, type, listener)); + return this; +}; + +EventEmitter.prototype.prependOnceListener = + function prependOnceListener(type, listener) { + checkListener(listener); + this.prependListener(type, _onceWrap(this, type, listener)); + return this; + }; + +// Emits a 'removeListener' event if and only if the listener was removed. +EventEmitter.prototype.removeListener = + function removeListener(type, listener) { + var list, events, position, i, originalListener; + + checkListener(listener); + + events = this._events; + if (events === undefined) + return this; + + list = events[type]; + if (list === undefined) + return this; + + if (list === listener || list.listener === listener) { + if (--this._eventsCount === 0) + this._events = Object.create(null); + else { + delete events[type]; + if (events.removeListener) + this.emit('removeListener', type, list.listener || listener); + } + } else if (typeof list !== 'function') { + position = -1; + + for (i = list.length - 1; i >= 0; i--) { + if (list[i] === listener || list[i].listener === listener) { + originalListener = list[i].listener; + position = i; + break; + } + } + + if (position < 0) + return this; + + if (position === 0) + list.shift(); + else { + spliceOne(list, position); + } + + if (list.length === 1) + events[type] = list[0]; + + if (events.removeListener !== undefined) + this.emit('removeListener', type, originalListener || listener); + } + + return this; + }; + +EventEmitter.prototype.off = EventEmitter.prototype.removeListener; + +EventEmitter.prototype.removeAllListeners = + function removeAllListeners(type) { + var listeners, events, i; + + events = this._events; + if (events === undefined) + return this; + + // not listening for removeListener, no need to emit + if (events.removeListener === undefined) { + if (arguments.length === 0) { + this._events = Object.create(null); + this._eventsCount = 0; + } else if (events[type] !== undefined) { + if (--this._eventsCount === 0) + this._events = Object.create(null); + else + delete events[type]; + } + return this; + } + + // emit removeListener for all listeners on all events + if (arguments.length === 0) { + var keys = Object.keys(events); + var key; + for (i = 0; i < keys.length; ++i) { + key = keys[i]; + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); + this._events = Object.create(null); + this._eventsCount = 0; + return this; + } + + listeners = events[type]; + + if (typeof listeners === 'function') { + this.removeListener(type, listeners); + } else if (listeners !== undefined) { + // LIFO order + for (i = listeners.length - 1; i >= 0; i--) { + this.removeListener(type, listeners[i]); + } + } + + return this; + }; + +function _listeners(target, type, unwrap) { + var events = target._events; + + if (events === undefined) + return []; + + var evlistener = events[type]; + if (evlistener === undefined) + return []; + + if (typeof evlistener === 'function') + return unwrap ? [evlistener.listener || evlistener] : [evlistener]; + + return unwrap ? + unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); +} + +EventEmitter.prototype.listeners = function listeners(type) { + return _listeners(this, type, true); +}; + +EventEmitter.prototype.rawListeners = function rawListeners(type) { + return _listeners(this, type, false); +}; + +EventEmitter.listenerCount = function(emitter, type) { + if (typeof emitter.listenerCount === 'function') { + return emitter.listenerCount(type); + } else { + return listenerCount.call(emitter, type); + } +}; + +EventEmitter.prototype.listenerCount = listenerCount; +function listenerCount(type) { + var events = this._events; + + if (events !== undefined) { + var evlistener = events[type]; + + if (typeof evlistener === 'function') { + return 1; + } else if (evlistener !== undefined) { + return evlistener.length; + } + } + + return 0; +} + +EventEmitter.prototype.eventNames = function eventNames() { + return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; +}; + +function arrayClone(arr, n) { + var copy = new Array(n); + for (var i = 0; i < n; ++i) + copy[i] = arr[i]; + return copy; +} + +function spliceOne(list, index) { + for (; index + 1 < list.length; index++) + list[index] = list[index + 1]; + list.pop(); +} + +function unwrapListeners(arr) { + var ret = new Array(arr.length); + for (var i = 0; i < ret.length; ++i) { + ret[i] = arr[i].listener || arr[i]; + } + return ret; +} + +function once(emitter, name) { + return new Promise(function (resolve, reject) { + function errorListener(err) { + emitter.removeListener(name, resolver); + reject(err); + } + + function resolver() { + if (typeof emitter.removeListener === 'function') { + emitter.removeListener('error', errorListener); + } + resolve([].slice.call(arguments)); + }; + + eventTargetAgnosticAddListener(emitter, name, resolver, { once: true }); + if (name !== 'error') { + addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true }); + } + }); +} + +function addErrorHandlerIfEventEmitter(emitter, handler, flags) { + if (typeof emitter.on === 'function') { + eventTargetAgnosticAddListener(emitter, 'error', handler, flags); + } +} + +function eventTargetAgnosticAddListener(emitter, name, listener, flags) { + if (typeof emitter.on === 'function') { + if (flags.once) { + emitter.once(name, listener); + } else { + emitter.on(name, listener); + } + } else if (typeof emitter.addEventListener === 'function') { + // EventTarget does not have `error` event semantics like Node + // EventEmitters, we do not listen for `error` events here. + emitter.addEventListener(name, function wrapListener(arg) { + // IE does not have builtin `{ once: true }` support so we + // have to do it manually. + if (flags.once) { + emitter.removeEventListener(name, wrapListener); + } + listener(arg); + }); + } else { + throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter); + } +} + +},{}],195:[function(require,module,exports){ +'use strict'; + +var isCallable = require('is-callable'); + +var toStr = Object.prototype.toString; +var hasOwnProperty = Object.prototype.hasOwnProperty; + +var forEachArray = function forEachArray(array, iterator, receiver) { + for (var i = 0, len = array.length; i < len; i++) { + if (hasOwnProperty.call(array, i)) { + if (receiver == null) { + iterator(array[i], i, array); + } else { + iterator.call(receiver, array[i], i, array); + } + } + } +}; + +var forEachString = function forEachString(string, iterator, receiver) { + for (var i = 0, len = string.length; i < len; i++) { + // no such thing as a sparse string. + if (receiver == null) { + iterator(string.charAt(i), i, string); + } else { + iterator.call(receiver, string.charAt(i), i, string); + } + } +}; + +var forEachObject = function forEachObject(object, iterator, receiver) { + for (var k in object) { + if (hasOwnProperty.call(object, k)) { + if (receiver == null) { + iterator(object[k], k, object); + } else { + iterator.call(receiver, object[k], k, object); + } + } + } +}; + +var forEach = function forEach(list, iterator, thisArg) { + if (!isCallable(iterator)) { + throw new TypeError('iterator must be a function'); + } + + var receiver; + if (arguments.length >= 3) { + receiver = thisArg; + } + + if (toStr.call(list) === '[object Array]') { + forEachArray(list, iterator, receiver); + } else if (typeof list === 'string') { + forEachString(list, iterator, receiver); + } else { + forEachObject(list, iterator, receiver); + } +}; + +module.exports = forEach; + +},{"is-callable":210}],196:[function(require,module,exports){ +'use strict'; + +/* eslint no-invalid-this: 1 */ + +var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible '; +var toStr = Object.prototype.toString; +var max = Math.max; +var funcType = '[object Function]'; + +var concatty = function concatty(a, b) { + var arr = []; + + for (var i = 0; i < a.length; i += 1) { + arr[i] = a[i]; + } + for (var j = 0; j < b.length; j += 1) { + arr[j + a.length] = b[j]; + } + + return arr; +}; + +var slicy = function slicy(arrLike, offset) { + var arr = []; + for (var i = offset || 0, j = 0; i < arrLike.length; i += 1, j += 1) { + arr[j] = arrLike[i]; + } + return arr; +}; + +var joiny = function (arr, joiner) { + var str = ''; + for (var i = 0; i < arr.length; i += 1) { + str += arr[i]; + if (i + 1 < arr.length) { + str += joiner; + } + } + return str; +}; + +module.exports = function bind(that) { + var target = this; + if (typeof target !== 'function' || toStr.apply(target) !== funcType) { + throw new TypeError(ERROR_MESSAGE + target); + } + var args = slicy(arguments, 1); + + var bound; + var binder = function () { + if (this instanceof bound) { + var result = target.apply( + this, + concatty(args, arguments) + ); + if (Object(result) === result) { + return result; + } + return this; + } + return target.apply( + that, + concatty(args, arguments) + ); + + }; + + var boundLength = max(0, target.length - args.length); + var boundArgs = []; + for (var i = 0; i < boundLength; i++) { + boundArgs[i] = '$' + i; + } + + bound = Function('binder', 'return function (' + joiny(boundArgs, ',') + '){ return binder.apply(this,arguments); }')(binder); + + if (target.prototype) { + var Empty = function Empty() {}; + Empty.prototype = target.prototype; + bound.prototype = new Empty(); + Empty.prototype = null; + } + + return bound; +}; + +},{}],197:[function(require,module,exports){ +'use strict'; + +var implementation = require('./implementation'); + +module.exports = Function.prototype.bind || implementation; + +},{"./implementation":196}],198:[function(require,module,exports){ +'use strict'; + +var undefined; + +var $SyntaxError = SyntaxError; +var $Function = Function; +var $TypeError = TypeError; + +// eslint-disable-next-line consistent-return +var getEvalledConstructor = function (expressionSyntax) { + try { + return $Function('"use strict"; return (' + expressionSyntax + ').constructor;')(); + } catch (e) {} +}; + +var $gOPD = Object.getOwnPropertyDescriptor; +if ($gOPD) { + try { + $gOPD({}, ''); + } catch (e) { + $gOPD = null; // this is IE 8, which has a broken gOPD + } +} + +var throwTypeError = function () { + throw new $TypeError(); +}; +var ThrowTypeError = $gOPD + ? (function () { + try { + // eslint-disable-next-line no-unused-expressions, no-caller, no-restricted-properties + arguments.callee; // IE 8 does not throw here + return throwTypeError; + } catch (calleeThrows) { + try { + // IE 8 throws on Object.getOwnPropertyDescriptor(arguments, '') + return $gOPD(arguments, 'callee').get; + } catch (gOPDthrows) { + return throwTypeError; + } + } + }()) + : throwTypeError; + +var hasSymbols = require('has-symbols')(); +var hasProto = require('has-proto')(); + +var getProto = Object.getPrototypeOf || ( + hasProto + ? function (x) { return x.__proto__; } // eslint-disable-line no-proto + : null +); + +var needsEval = {}; + +var TypedArray = typeof Uint8Array === 'undefined' || !getProto ? undefined : getProto(Uint8Array); + +var INTRINSICS = { + '%AggregateError%': typeof AggregateError === 'undefined' ? undefined : AggregateError, + '%Array%': Array, + '%ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer, + '%ArrayIteratorPrototype%': hasSymbols && getProto ? getProto([][Symbol.iterator]()) : undefined, + '%AsyncFromSyncIteratorPrototype%': undefined, + '%AsyncFunction%': needsEval, + '%AsyncGenerator%': needsEval, + '%AsyncGeneratorFunction%': needsEval, + '%AsyncIteratorPrototype%': needsEval, + '%Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics, + '%BigInt%': typeof BigInt === 'undefined' ? undefined : BigInt, + '%BigInt64Array%': typeof BigInt64Array === 'undefined' ? undefined : BigInt64Array, + '%BigUint64Array%': typeof BigUint64Array === 'undefined' ? undefined : BigUint64Array, + '%Boolean%': Boolean, + '%DataView%': typeof DataView === 'undefined' ? undefined : DataView, + '%Date%': Date, + '%decodeURI%': decodeURI, + '%decodeURIComponent%': decodeURIComponent, + '%encodeURI%': encodeURI, + '%encodeURIComponent%': encodeURIComponent, + '%Error%': Error, + '%eval%': eval, // eslint-disable-line no-eval + '%EvalError%': EvalError, + '%Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array, + '%Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array, + '%FinalizationRegistry%': typeof FinalizationRegistry === 'undefined' ? undefined : FinalizationRegistry, + '%Function%': $Function, + '%GeneratorFunction%': needsEval, + '%Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array, + '%Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array, + '%Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array, + '%isFinite%': isFinite, + '%isNaN%': isNaN, + '%IteratorPrototype%': hasSymbols && getProto ? getProto(getProto([][Symbol.iterator]())) : undefined, + '%JSON%': typeof JSON === 'object' ? JSON : undefined, + '%Map%': typeof Map === 'undefined' ? undefined : Map, + '%MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols || !getProto ? undefined : getProto(new Map()[Symbol.iterator]()), + '%Math%': Math, + '%Number%': Number, + '%Object%': Object, + '%parseFloat%': parseFloat, + '%parseInt%': parseInt, + '%Promise%': typeof Promise === 'undefined' ? undefined : Promise, + '%Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy, + '%RangeError%': RangeError, + '%ReferenceError%': ReferenceError, + '%Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect, + '%RegExp%': RegExp, + '%Set%': typeof Set === 'undefined' ? undefined : Set, + '%SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols || !getProto ? undefined : getProto(new Set()[Symbol.iterator]()), + '%SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer, + '%String%': String, + '%StringIteratorPrototype%': hasSymbols && getProto ? getProto(''[Symbol.iterator]()) : undefined, + '%Symbol%': hasSymbols ? Symbol : undefined, + '%SyntaxError%': $SyntaxError, + '%ThrowTypeError%': ThrowTypeError, + '%TypedArray%': TypedArray, + '%TypeError%': $TypeError, + '%Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array, + '%Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray, + '%Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array, + '%Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array, + '%URIError%': URIError, + '%WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap, + '%WeakRef%': typeof WeakRef === 'undefined' ? undefined : WeakRef, + '%WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet +}; + +if (getProto) { + try { + null.error; // eslint-disable-line no-unused-expressions + } catch (e) { + // https://github.com/tc39/proposal-shadowrealm/pull/384#issuecomment-1364264229 + var errorProto = getProto(getProto(e)); + INTRINSICS['%Error.prototype%'] = errorProto; + } +} + +var doEval = function doEval(name) { + var value; + if (name === '%AsyncFunction%') { + value = getEvalledConstructor('async function () {}'); + } else if (name === '%GeneratorFunction%') { + value = getEvalledConstructor('function* () {}'); + } else if (name === '%AsyncGeneratorFunction%') { + value = getEvalledConstructor('async function* () {}'); + } else if (name === '%AsyncGenerator%') { + var fn = doEval('%AsyncGeneratorFunction%'); + if (fn) { + value = fn.prototype; + } + } else if (name === '%AsyncIteratorPrototype%') { + var gen = doEval('%AsyncGenerator%'); + if (gen && getProto) { + value = getProto(gen.prototype); + } + } + + INTRINSICS[name] = value; + + return value; +}; + +var LEGACY_ALIASES = { + '%ArrayBufferPrototype%': ['ArrayBuffer', 'prototype'], + '%ArrayPrototype%': ['Array', 'prototype'], + '%ArrayProto_entries%': ['Array', 'prototype', 'entries'], + '%ArrayProto_forEach%': ['Array', 'prototype', 'forEach'], + '%ArrayProto_keys%': ['Array', 'prototype', 'keys'], + '%ArrayProto_values%': ['Array', 'prototype', 'values'], + '%AsyncFunctionPrototype%': ['AsyncFunction', 'prototype'], + '%AsyncGenerator%': ['AsyncGeneratorFunction', 'prototype'], + '%AsyncGeneratorPrototype%': ['AsyncGeneratorFunction', 'prototype', 'prototype'], + '%BooleanPrototype%': ['Boolean', 'prototype'], + '%DataViewPrototype%': ['DataView', 'prototype'], + '%DatePrototype%': ['Date', 'prototype'], + '%ErrorPrototype%': ['Error', 'prototype'], + '%EvalErrorPrototype%': ['EvalError', 'prototype'], + '%Float32ArrayPrototype%': ['Float32Array', 'prototype'], + '%Float64ArrayPrototype%': ['Float64Array', 'prototype'], + '%FunctionPrototype%': ['Function', 'prototype'], + '%Generator%': ['GeneratorFunction', 'prototype'], + '%GeneratorPrototype%': ['GeneratorFunction', 'prototype', 'prototype'], + '%Int8ArrayPrototype%': ['Int8Array', 'prototype'], + '%Int16ArrayPrototype%': ['Int16Array', 'prototype'], + '%Int32ArrayPrototype%': ['Int32Array', 'prototype'], + '%JSONParse%': ['JSON', 'parse'], + '%JSONStringify%': ['JSON', 'stringify'], + '%MapPrototype%': ['Map', 'prototype'], + '%NumberPrototype%': ['Number', 'prototype'], + '%ObjectPrototype%': ['Object', 'prototype'], + '%ObjProto_toString%': ['Object', 'prototype', 'toString'], + '%ObjProto_valueOf%': ['Object', 'prototype', 'valueOf'], + '%PromisePrototype%': ['Promise', 'prototype'], + '%PromiseProto_then%': ['Promise', 'prototype', 'then'], + '%Promise_all%': ['Promise', 'all'], + '%Promise_reject%': ['Promise', 'reject'], + '%Promise_resolve%': ['Promise', 'resolve'], + '%RangeErrorPrototype%': ['RangeError', 'prototype'], + '%ReferenceErrorPrototype%': ['ReferenceError', 'prototype'], + '%RegExpPrototype%': ['RegExp', 'prototype'], + '%SetPrototype%': ['Set', 'prototype'], + '%SharedArrayBufferPrototype%': ['SharedArrayBuffer', 'prototype'], + '%StringPrototype%': ['String', 'prototype'], + '%SymbolPrototype%': ['Symbol', 'prototype'], + '%SyntaxErrorPrototype%': ['SyntaxError', 'prototype'], + '%TypedArrayPrototype%': ['TypedArray', 'prototype'], + '%TypeErrorPrototype%': ['TypeError', 'prototype'], + '%Uint8ArrayPrototype%': ['Uint8Array', 'prototype'], + '%Uint8ClampedArrayPrototype%': ['Uint8ClampedArray', 'prototype'], + '%Uint16ArrayPrototype%': ['Uint16Array', 'prototype'], + '%Uint32ArrayPrototype%': ['Uint32Array', 'prototype'], + '%URIErrorPrototype%': ['URIError', 'prototype'], + '%WeakMapPrototype%': ['WeakMap', 'prototype'], + '%WeakSetPrototype%': ['WeakSet', 'prototype'] +}; + +var bind = require('function-bind'); +var hasOwn = require('hasown'); +var $concat = bind.call(Function.call, Array.prototype.concat); +var $spliceApply = bind.call(Function.apply, Array.prototype.splice); +var $replace = bind.call(Function.call, String.prototype.replace); +var $strSlice = bind.call(Function.call, String.prototype.slice); +var $exec = bind.call(Function.call, RegExp.prototype.exec); + +/* adapted from https://github.com/lodash/lodash/blob/4.17.15/dist/lodash.js#L6735-L6744 */ +var rePropName = /[^%.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|%$))/g; +var reEscapeChar = /\\(\\)?/g; /** Used to match backslashes in property paths. */ +var stringToPath = function stringToPath(string) { + var first = $strSlice(string, 0, 1); + var last = $strSlice(string, -1); + if (first === '%' && last !== '%') { + throw new $SyntaxError('invalid intrinsic syntax, expected closing `%`'); + } else if (last === '%' && first !== '%') { + throw new $SyntaxError('invalid intrinsic syntax, expected opening `%`'); + } + var result = []; + $replace(string, rePropName, function (match, number, quote, subString) { + result[result.length] = quote ? $replace(subString, reEscapeChar, '$1') : number || match; + }); + return result; +}; +/* end adaptation */ + +var getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) { + var intrinsicName = name; + var alias; + if (hasOwn(LEGACY_ALIASES, intrinsicName)) { + alias = LEGACY_ALIASES[intrinsicName]; + intrinsicName = '%' + alias[0] + '%'; + } + + if (hasOwn(INTRINSICS, intrinsicName)) { + var value = INTRINSICS[intrinsicName]; + if (value === needsEval) { + value = doEval(intrinsicName); + } + if (typeof value === 'undefined' && !allowMissing) { + throw new $TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!'); + } + + return { + alias: alias, + name: intrinsicName, + value: value + }; + } + + throw new $SyntaxError('intrinsic ' + name + ' does not exist!'); +}; + +module.exports = function GetIntrinsic(name, allowMissing) { + if (typeof name !== 'string' || name.length === 0) { + throw new $TypeError('intrinsic name must be a non-empty string'); + } + if (arguments.length > 1 && typeof allowMissing !== 'boolean') { + throw new $TypeError('"allowMissing" argument must be a boolean'); + } + + if ($exec(/^%?[^%]*%?$/, name) === null) { + throw new $SyntaxError('`%` may not be present anywhere but at the beginning and end of the intrinsic name'); + } + var parts = stringToPath(name); + var intrinsicBaseName = parts.length > 0 ? parts[0] : ''; + + var intrinsic = getBaseIntrinsic('%' + intrinsicBaseName + '%', allowMissing); + var intrinsicRealName = intrinsic.name; + var value = intrinsic.value; + var skipFurtherCaching = false; + + var alias = intrinsic.alias; + if (alias) { + intrinsicBaseName = alias[0]; + $spliceApply(parts, $concat([0, 1], alias)); + } + + for (var i = 1, isOwn = true; i < parts.length; i += 1) { + var part = parts[i]; + var first = $strSlice(part, 0, 1); + var last = $strSlice(part, -1); + if ( + ( + (first === '"' || first === "'" || first === '`') + || (last === '"' || last === "'" || last === '`') + ) + && first !== last + ) { + throw new $SyntaxError('property names with quotes must have matching quotes'); + } + if (part === 'constructor' || !isOwn) { + skipFurtherCaching = true; + } + + intrinsicBaseName += '.' + part; + intrinsicRealName = '%' + intrinsicBaseName + '%'; + + if (hasOwn(INTRINSICS, intrinsicRealName)) { + value = INTRINSICS[intrinsicRealName]; + } else if (value != null) { + if (!(part in value)) { + if (!allowMissing) { + throw new $TypeError('base intrinsic for ' + name + ' exists, but the property is not available.'); + } + return void undefined; + } + if ($gOPD && (i + 1) >= parts.length) { + var desc = $gOPD(value, part); + isOwn = !!desc; + + // By convention, when a data property is converted to an accessor + // property to emulate a data property that does not suffer from + // the override mistake, that accessor's getter is marked with + // an `originalValue` property. Here, when we detect this, we + // uphold the illusion by pretending to see that original data + // property, i.e., returning the value rather than the getter + // itself. + if (isOwn && 'get' in desc && !('originalValue' in desc.get)) { + value = desc.get; + } else { + value = value[part]; + } + } else { + isOwn = hasOwn(value, part); + value = value[part]; + } + + if (isOwn && !skipFurtherCaching) { + INTRINSICS[intrinsicRealName] = value; + } + } + } + return value; +}; + +},{"function-bind":197,"has-proto":201,"has-symbols":202,"hasown":205}],199:[function(require,module,exports){ +'use strict'; + +var GetIntrinsic = require('get-intrinsic'); + +var $gOPD = GetIntrinsic('%Object.getOwnPropertyDescriptor%', true); + +if ($gOPD) { + try { + $gOPD([], 'length'); + } catch (e) { + // IE 8 has a broken gOPD + $gOPD = null; + } +} + +module.exports = $gOPD; + +},{"get-intrinsic":198}],200:[function(require,module,exports){ +'use strict'; + +var GetIntrinsic = require('get-intrinsic'); + +var $defineProperty = GetIntrinsic('%Object.defineProperty%', true); + +var hasPropertyDescriptors = function hasPropertyDescriptors() { + if ($defineProperty) { + try { + $defineProperty({}, 'a', { value: 1 }); + return true; + } catch (e) { + // IE 8 has a broken defineProperty + return false; + } + } + return false; +}; + +hasPropertyDescriptors.hasArrayLengthDefineBug = function hasArrayLengthDefineBug() { + // node v0.6 has a bug where array lengths can be Set but not Defined + if (!hasPropertyDescriptors()) { + return null; + } + try { + return $defineProperty([], 'length', { value: 1 }).length !== 1; + } catch (e) { + // In Firefox 4-22, defining length on an array throws an exception. + return true; + } +}; + +module.exports = hasPropertyDescriptors; + +},{"get-intrinsic":198}],201:[function(require,module,exports){ +'use strict'; + +var test = { + foo: {} +}; + +var $Object = Object; + +module.exports = function hasProto() { + return { __proto__: test }.foo === test.foo && !({ __proto__: null } instanceof $Object); +}; + +},{}],202:[function(require,module,exports){ +'use strict'; + +var origSymbol = typeof Symbol !== 'undefined' && Symbol; +var hasSymbolSham = require('./shams'); + +module.exports = function hasNativeSymbols() { + if (typeof origSymbol !== 'function') { return false; } + if (typeof Symbol !== 'function') { return false; } + if (typeof origSymbol('foo') !== 'symbol') { return false; } + if (typeof Symbol('bar') !== 'symbol') { return false; } + + return hasSymbolSham(); +}; + +},{"./shams":203}],203:[function(require,module,exports){ +'use strict'; + +/* eslint complexity: [2, 18], max-statements: [2, 33] */ +module.exports = function hasSymbols() { + if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; } + if (typeof Symbol.iterator === 'symbol') { return true; } + + var obj = {}; + var sym = Symbol('test'); + var symObj = Object(sym); + if (typeof sym === 'string') { return false; } + + if (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; } + if (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; } + + // temp disabled per https://github.com/ljharb/object.assign/issues/17 + // if (sym instanceof Symbol) { return false; } + // temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4 + // if (!(symObj instanceof Symbol)) { return false; } + + // if (typeof Symbol.prototype.toString !== 'function') { return false; } + // if (String(sym) !== Symbol.prototype.toString.call(sym)) { return false; } + + var symVal = 42; + obj[sym] = symVal; + for (sym in obj) { return false; } // eslint-disable-line no-restricted-syntax, no-unreachable-loop + if (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; } + + if (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; } + + var syms = Object.getOwnPropertySymbols(obj); + if (syms.length !== 1 || syms[0] !== sym) { return false; } + + if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; } + + if (typeof Object.getOwnPropertyDescriptor === 'function') { + var descriptor = Object.getOwnPropertyDescriptor(obj, sym); + if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; } + } + + return true; +}; + +},{}],204:[function(require,module,exports){ +'use strict'; + +var hasSymbols = require('has-symbols/shams'); + +module.exports = function hasToStringTagShams() { + return hasSymbols() && !!Symbol.toStringTag; +}; + +},{"has-symbols/shams":203}],205:[function(require,module,exports){ +'use strict'; + +var call = Function.prototype.call; +var $hasOwn = Object.prototype.hasOwnProperty; +var bind = require('function-bind'); + +/** @type {(o: {}, p: PropertyKey) => p is keyof o} */ +module.exports = bind.call(call, $hasOwn); + +},{"function-bind":197}],206:[function(require,module,exports){ +/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh */ +exports.read = function (buffer, offset, isLE, mLen, nBytes) { + var e, m + var eLen = (nBytes * 8) - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var nBits = -7 + var i = isLE ? (nBytes - 1) : 0 + var d = isLE ? -1 : 1 + var s = buffer[offset + i] + + i += d + + e = s & ((1 << (-nBits)) - 1) + s >>= (-nBits) + nBits += eLen + for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {} + + m = e & ((1 << (-nBits)) - 1) + e >>= (-nBits) + nBits += mLen + for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {} + + if (e === 0) { + e = 1 - eBias + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity) + } else { + m = m + Math.pow(2, mLen) + e = e - eBias + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen) +} + +exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c + var eLen = (nBytes * 8) - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) + var i = isLE ? 0 : (nBytes - 1) + var d = isLE ? 1 : -1 + var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 + + value = Math.abs(value) + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0 + e = eMax + } else { + e = Math.floor(Math.log(value) / Math.LN2) + if (value * (c = Math.pow(2, -e)) < 1) { + e-- + c *= 2 + } + if (e + eBias >= 1) { + value += rt / c + } else { + value += rt * Math.pow(2, 1 - eBias) + } + if (value * c >= 2) { + e++ + c /= 2 + } + + if (e + eBias >= eMax) { + m = 0 + e = eMax + } else if (e + eBias >= 1) { + m = ((value * c) - 1) * Math.pow(2, mLen) + e = e + eBias + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) + e = 0 + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + + e = (e << mLen) | m + eLen += mLen + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + + buffer[offset + i - d] |= s * 128 +} + +},{}],207:[function(require,module,exports){ +if (typeof Object.create === 'function') { + // implementation from standard node.js 'util' module + module.exports = function inherits(ctor, superCtor) { + if (superCtor) { + ctor.super_ = superCtor + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }) + } + }; +} else { + // old school shim for old browsers + module.exports = function inherits(ctor, superCtor) { + if (superCtor) { + ctor.super_ = superCtor + var TempCtor = function () {} + TempCtor.prototype = superCtor.prototype + ctor.prototype = new TempCtor() + ctor.prototype.constructor = ctor + } + } +} + +},{}],208:[function(require,module,exports){ +'use strict'; + +var hasToStringTag = require('has-tostringtag/shams')(); +var callBound = require('call-bind/callBound'); + +var $toString = callBound('Object.prototype.toString'); + +var isStandardArguments = function isArguments(value) { + if (hasToStringTag && value && typeof value === 'object' && Symbol.toStringTag in value) { + return false; + } + return $toString(value) === '[object Arguments]'; +}; + +var isLegacyArguments = function isArguments(value) { + if (isStandardArguments(value)) { + return true; + } + return value !== null && + typeof value === 'object' && + typeof value.length === 'number' && + value.length >= 0 && + $toString(value) !== '[object Array]' && + $toString(value.callee) === '[object Function]'; +}; + +var supportsStandardArguments = (function () { + return isStandardArguments(arguments); +}()); + +isStandardArguments.isLegacyArguments = isLegacyArguments; // for tests + +module.exports = supportsStandardArguments ? isStandardArguments : isLegacyArguments; + +},{"call-bind/callBound":50,"has-tostringtag/shams":204}],209:[function(require,module,exports){ +/*! + * Determine if an object is a Buffer + * + * @author Feross Aboukhadijeh + * @license MIT + */ + +// The _isBuffer check is for Safari 5-7 support, because it's missing +// Object.prototype.constructor. Remove this eventually +module.exports = function (obj) { + return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) +} + +function isBuffer (obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) +} + +// For Node v0.10 support. Remove this eventually. +function isSlowBuffer (obj) { + return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) +} + +},{}],210:[function(require,module,exports){ +'use strict'; + +var fnToStr = Function.prototype.toString; +var reflectApply = typeof Reflect === 'object' && Reflect !== null && Reflect.apply; +var badArrayLike; +var isCallableMarker; +if (typeof reflectApply === 'function' && typeof Object.defineProperty === 'function') { + try { + badArrayLike = Object.defineProperty({}, 'length', { + get: function () { + throw isCallableMarker; + } + }); + isCallableMarker = {}; + // eslint-disable-next-line no-throw-literal + reflectApply(function () { throw 42; }, null, badArrayLike); + } catch (_) { + if (_ !== isCallableMarker) { + reflectApply = null; + } + } +} else { + reflectApply = null; +} + +var constructorRegex = /^\s*class\b/; +var isES6ClassFn = function isES6ClassFunction(value) { + try { + var fnStr = fnToStr.call(value); + return constructorRegex.test(fnStr); + } catch (e) { + return false; // not a function + } +}; + +var tryFunctionObject = function tryFunctionToStr(value) { + try { + if (isES6ClassFn(value)) { return false; } + fnToStr.call(value); + return true; + } catch (e) { + return false; + } +}; +var toStr = Object.prototype.toString; +var objectClass = '[object Object]'; +var fnClass = '[object Function]'; +var genClass = '[object GeneratorFunction]'; +var ddaClass = '[object HTMLAllCollection]'; // IE 11 +var ddaClass2 = '[object HTML document.all class]'; +var ddaClass3 = '[object HTMLCollection]'; // IE 9-10 +var hasToStringTag = typeof Symbol === 'function' && !!Symbol.toStringTag; // better: use `has-tostringtag` + +var isIE68 = !(0 in [,]); // eslint-disable-line no-sparse-arrays, comma-spacing + +var isDDA = function isDocumentDotAll() { return false; }; +if (typeof document === 'object') { + // Firefox 3 canonicalizes DDA to undefined when it's not accessed directly + var all = document.all; + if (toStr.call(all) === toStr.call(document.all)) { + isDDA = function isDocumentDotAll(value) { + /* globals document: false */ + // in IE 6-8, typeof document.all is "object" and it's truthy + if ((isIE68 || !value) && (typeof value === 'undefined' || typeof value === 'object')) { + try { + var str = toStr.call(value); + return ( + str === ddaClass + || str === ddaClass2 + || str === ddaClass3 // opera 12.16 + || str === objectClass // IE 6-8 + ) && value('') == null; // eslint-disable-line eqeqeq + } catch (e) { /**/ } + } + return false; + }; + } +} + +module.exports = reflectApply + ? function isCallable(value) { + if (isDDA(value)) { return true; } + if (!value) { return false; } + if (typeof value !== 'function' && typeof value !== 'object') { return false; } + try { + reflectApply(value, null, badArrayLike); + } catch (e) { + if (e !== isCallableMarker) { return false; } + } + return !isES6ClassFn(value) && tryFunctionObject(value); + } + : function isCallable(value) { + if (isDDA(value)) { return true; } + if (!value) { return false; } + if (typeof value !== 'function' && typeof value !== 'object') { return false; } + if (hasToStringTag) { return tryFunctionObject(value); } + if (isES6ClassFn(value)) { return false; } + var strClass = toStr.call(value); + if (strClass !== fnClass && strClass !== genClass && !(/^\[object HTML/).test(strClass)) { return false; } + return tryFunctionObject(value); + }; + +},{}],211:[function(require,module,exports){ +'use strict'; + +module.exports = Number.isFinite || function (value) { + return !(typeof value !== 'number' || value !== value || value === Infinity || value === -Infinity); +}; + +},{}],212:[function(require,module,exports){ +'use strict'; + +var toStr = Object.prototype.toString; +var fnToStr = Function.prototype.toString; +var isFnRegex = /^\s*(?:function)?\*/; +var hasToStringTag = require('has-tostringtag/shams')(); +var getProto = Object.getPrototypeOf; +var getGeneratorFunc = function () { // eslint-disable-line consistent-return + if (!hasToStringTag) { + return false; + } + try { + return Function('return function*() {}')(); + } catch (e) { + } +}; +var GeneratorFunction; + +module.exports = function isGeneratorFunction(fn) { + if (typeof fn !== 'function') { + return false; + } + if (isFnRegex.test(fnToStr.call(fn))) { + return true; + } + if (!hasToStringTag) { + var str = toStr.call(fn); + return str === '[object GeneratorFunction]'; + } + if (!getProto) { + return false; + } + if (typeof GeneratorFunction === 'undefined') { + var generatorFunc = getGeneratorFunc(); + GeneratorFunction = generatorFunc ? getProto(generatorFunc) : false; + } + return getProto(fn) === GeneratorFunction; +}; + +},{"has-tostringtag/shams":204}],213:[function(require,module,exports){ +// https://github.com/paulmillr/es6-shim +// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isinteger +var isFinite = require("is-finite"); +module.exports = Number.isInteger || function(val) { + return typeof val === "number" && + isFinite(val) && + Math.floor(val) === val; +}; + +},{"is-finite":211}],214:[function(require,module,exports){ +'use strict'; + +var whichTypedArray = require('which-typed-array'); + +module.exports = function isTypedArray(value) { + return !!whichTypedArray(value); +}; + +},{"which-typed-array":371}],215:[function(require,module,exports){ +(function (process){(function (){ +'use strict'; + +function Kareem() { + this._pres = new Map(); + this._posts = new Map(); +} + +Kareem.prototype.execPre = function(name, context, args, callback) { + if (arguments.length === 3) { + callback = args; + args = []; + } + var pres = get(this._pres, name, []); + var numPres = pres.length; + var numAsyncPres = pres.numAsync || 0; + var currentPre = 0; + var asyncPresLeft = numAsyncPres; + var done = false; + var $args = args; + + if (!numPres) { + return process.nextTick(function() { + callback(null); + }); + } + + var next = function() { + if (currentPre >= numPres) { + return; + } + var pre = pres[currentPre]; + + if (pre.isAsync) { + var args = [ + decorateNextFn(_next), + decorateNextFn(function(error) { + if (error) { + if (done) { + return; + } + done = true; + return callback(error); + } + if (--asyncPresLeft === 0 && currentPre >= numPres) { + return callback(null); + } + }) + ]; + + callMiddlewareFunction(pre.fn, context, args, args[0]); + } else if (pre.fn.length > 0) { + var args = [decorateNextFn(_next)]; + var _args = arguments.length >= 2 ? arguments : [null].concat($args); + for (var i = 1; i < _args.length; ++i) { + args.push(_args[i]); + } + + callMiddlewareFunction(pre.fn, context, args, args[0]); + } else { + let maybePromise = null; + try { + maybePromise = pre.fn.call(context); + } catch (err) { + if (err != null) { + return callback(err); + } + } + + if (isPromise(maybePromise)) { + maybePromise.then(() => _next(), err => _next(err)); + } else { + if (++currentPre >= numPres) { + if (asyncPresLeft > 0) { + // Leave parallel hooks to run + return; + } else { + return process.nextTick(function() { + callback(null); + }); + } + } + next(); + } + } + }; + + next.apply(null, [null].concat(args)); + + function _next(error) { + if (error) { + if (done) { + return; + } + done = true; + return callback(error); + } + + if (++currentPre >= numPres) { + if (asyncPresLeft > 0) { + // Leave parallel hooks to run + return; + } else { + return callback(null); + } + } + + next.apply(context, arguments); + } +}; + +Kareem.prototype.execPreSync = function(name, context, args) { + var pres = get(this._pres, name, []); + var numPres = pres.length; + + for (var i = 0; i < numPres; ++i) { + pres[i].fn.apply(context, args || []); + } +}; + +Kareem.prototype.execPost = function(name, context, args, options, callback) { + if (arguments.length < 5) { + callback = options; + options = null; + } + var posts = get(this._posts, name, []); + var numPosts = posts.length; + var currentPost = 0; + + var firstError = null; + if (options && options.error) { + firstError = options.error; + } + + if (!numPosts) { + return process.nextTick(function() { + callback.apply(null, [firstError].concat(args)); + }); + } + + var next = function() { + var post = posts[currentPost].fn; + var numArgs = 0; + var argLength = args.length; + var newArgs = []; + for (var i = 0; i < argLength; ++i) { + numArgs += args[i] && args[i]._kareemIgnore ? 0 : 1; + if (!args[i] || !args[i]._kareemIgnore) { + newArgs.push(args[i]); + } + } + + if (firstError) { + if (post.length === numArgs + 2) { + var _cb = decorateNextFn(function(error) { + if (error) { + firstError = error; + } + if (++currentPost >= numPosts) { + return callback.call(null, firstError); + } + next(); + }); + + callMiddlewareFunction(post, context, + [firstError].concat(newArgs).concat([_cb]), _cb); + } else { + if (++currentPost >= numPosts) { + return callback.call(null, firstError); + } + next(); + } + } else { + const _cb = decorateNextFn(function(error) { + if (error) { + firstError = error; + return next(); + } + + if (++currentPost >= numPosts) { + return callback.apply(null, [null].concat(args)); + } + + next(); + }); + + if (post.length === numArgs + 2) { + // Skip error handlers if no error + if (++currentPost >= numPosts) { + return callback.apply(null, [null].concat(args)); + } + return next(); + } + if (post.length === numArgs + 1) { + callMiddlewareFunction(post, context, newArgs.concat([_cb]), _cb); + } else { + let error; + let maybePromise; + try { + maybePromise = post.apply(context, newArgs); + } catch (err) { + error = err; + firstError = err; + } + + if (isPromise(maybePromise)) { + return maybePromise.then(() => _cb(), err => _cb(err)); + } + + if (++currentPost >= numPosts) { + return callback.apply(null, [error].concat(args)); + } + + next(error); + } + } + }; + + next(); +}; + +Kareem.prototype.execPostSync = function(name, context, args) { + const posts = get(this._posts, name, []); + const numPosts = posts.length; + + for (let i = 0; i < numPosts; ++i) { + posts[i].fn.apply(context, args || []); + } +}; + +Kareem.prototype.createWrapperSync = function(name, fn) { + var kareem = this; + return function syncWrapper() { + kareem.execPreSync(name, this, arguments); + + var toReturn = fn.apply(this, arguments); + + kareem.execPostSync(name, this, [toReturn]); + + return toReturn; + }; +} + +function _handleWrapError(instance, error, name, context, args, options, callback) { + if (options.useErrorHandlers) { + var _options = { error: error }; + return instance.execPost(name, context, args, _options, function(error) { + return typeof callback === 'function' && callback(error); + }); + } else { + return typeof callback === 'function' ? + callback(error) : + undefined; + } +} + +Kareem.prototype.wrap = function(name, fn, context, args, options) { + const lastArg = (args.length > 0 ? args[args.length - 1] : null); + const argsWithoutCb = typeof lastArg === 'function' ? + args.slice(0, args.length - 1) : + args; + const _this = this; + + options = options || {}; + const checkForPromise = options.checkForPromise; + + this.execPre(name, context, args, function(error) { + if (error) { + const numCallbackParams = options.numCallbackParams || 0; + const errorArgs = options.contextParameter ? [context] : []; + for (var i = errorArgs.length; i < numCallbackParams; ++i) { + errorArgs.push(null); + } + return _handleWrapError(_this, error, name, context, errorArgs, + options, lastArg); + } + + const end = (typeof lastArg === 'function' ? args.length - 1 : args.length); + const numParameters = fn.length; + const ret = fn.apply(context, args.slice(0, end).concat(_cb)); + + if (checkForPromise) { + if (ret != null && typeof ret.then === 'function') { + // Thenable, use it + return ret.then( + res => _cb(null, res), + err => _cb(err) + ); + } + + // If `fn()` doesn't have a callback argument and doesn't return a + // promise, assume it is sync + if (numParameters < end + 1) { + return _cb(null, ret); + } + } + + function _cb() { + const args = arguments; + const argsWithoutError = Array.prototype.slice.call(arguments, 1); + if (options.nullResultByDefault && argsWithoutError.length === 0) { + argsWithoutError.push(null); + } + if (arguments[0]) { + // Assume error + return _handleWrapError(_this, arguments[0], name, context, + argsWithoutError, options, lastArg); + } else { + _this.execPost(name, context, argsWithoutError, function() { + if (arguments[0]) { + return typeof lastArg === 'function' ? + lastArg(arguments[0]) : + undefined; + } + + return typeof lastArg === 'function' ? + lastArg.apply(context, arguments) : + undefined; + }); + } + } + }); +}; + +Kareem.prototype.filter = function(fn) { + const clone = this.clone(); + + const pres = Array.from(clone._pres.keys()); + for (const name of pres) { + const hooks = this._pres.get(name). + map(h => Object.assign({}, h, { name: name })). + filter(fn); + + if (hooks.length === 0) { + clone._pres.delete(name); + continue; + } + + hooks.numAsync = hooks.filter(h => h.isAsync).length; + + clone._pres.set(name, hooks); + } + + const posts = Array.from(clone._posts.keys()); + for (const name of posts) { + const hooks = this._posts.get(name). + map(h => Object.assign({}, h, { name: name })). + filter(fn); + + if (hooks.length === 0) { + clone._posts.delete(name); + continue; + } + + clone._posts.set(name, hooks); + } + + return clone; +}; + +Kareem.prototype.hasHooks = function(name) { + return this._pres.has(name) || this._posts.has(name); +}; + +Kareem.prototype.createWrapper = function(name, fn, context, options) { + var _this = this; + if (!this.hasHooks(name)) { + // Fast path: if there's no hooks for this function, just return the + // function wrapped in a nextTick() + return function() { + process.nextTick(() => fn.apply(this, arguments)); + }; + } + return function() { + var _context = context || this; + var args = Array.prototype.slice.call(arguments); + _this.wrap(name, fn, _context, args, options); + }; +}; + +Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) { + let options = {}; + if (typeof isAsync === 'object' && isAsync != null) { + options = isAsync; + isAsync = options.isAsync; + } else if (typeof arguments[1] !== 'boolean') { + error = fn; + fn = isAsync; + isAsync = false; + } + + const pres = get(this._pres, name, []); + this._pres.set(name, pres); + + if (isAsync) { + pres.numAsync = pres.numAsync || 0; + ++pres.numAsync; + } + + if (typeof fn !== 'function') { + throw new Error('pre() requires a function, got "' + typeof fn + '"'); + } + + if (unshift) { + pres.unshift(Object.assign({}, options, { fn: fn, isAsync: isAsync })); + } else { + pres.push(Object.assign({}, options, { fn: fn, isAsync: isAsync })); + } + + return this; +}; + +Kareem.prototype.post = function(name, options, fn, unshift) { + const hooks = get(this._posts, name, []); + + if (typeof options === 'function') { + unshift = !!fn; + fn = options; + options = {}; + } + + if (typeof fn !== 'function') { + throw new Error('post() requires a function, got "' + typeof fn + '"'); + } + + if (unshift) { + hooks.unshift(Object.assign({}, options, { fn: fn })); + } else { + hooks.push(Object.assign({}, options, { fn: fn })); + } + this._posts.set(name, hooks); + return this; +}; + +Kareem.prototype.clone = function() { + const n = new Kareem(); + + for (let key of this._pres.keys()) { + const clone = this._pres.get(key).slice(); + clone.numAsync = this._pres.get(key).numAsync; + n._pres.set(key, clone); + } + for (let key of this._posts.keys()) { + n._posts.set(key, this._posts.get(key).slice()); + } + + return n; +}; + +Kareem.prototype.merge = function(other, clone) { + clone = arguments.length === 1 ? true : clone; + var ret = clone ? this.clone() : this; + + for (let key of other._pres.keys()) { + const sourcePres = get(ret._pres, key, []); + const deduplicated = other._pres.get(key). + // Deduplicate based on `fn` + filter(p => sourcePres.map(_p => _p.fn).indexOf(p.fn) === -1); + const combined = sourcePres.concat(deduplicated); + combined.numAsync = sourcePres.numAsync || 0; + combined.numAsync += deduplicated.filter(p => p.isAsync).length; + ret._pres.set(key, combined); + } + for (let key of other._posts.keys()) { + const sourcePosts = get(ret._posts, key, []); + const deduplicated = other._posts.get(key). + filter(p => sourcePosts.indexOf(p) === -1); + ret._posts.set(key, sourcePosts.concat(deduplicated)); + } + + return ret; +}; + +function get(map, key, def) { + if (map.has(key)) { + return map.get(key); + } + return def; +} + +function callMiddlewareFunction(fn, context, args, next) { + let maybePromise; + try { + maybePromise = fn.apply(context, args); + } catch (error) { + return next(error); + } + + if (isPromise(maybePromise)) { + maybePromise.then(() => next(), err => next(err)); + } +} + +function isPromise(v) { + return v != null && typeof v.then === 'function'; +} + +function decorateNextFn(fn) { + var called = false; + var _this = this; + return function() { + // Ensure this function can only be called once + if (called) { + return; + } + called = true; + // Make sure to clear the stack so try/catch doesn't catch errors + // in subsequent middleware + return process.nextTick(() => fn.apply(_this, arguments)); + }; +} + +module.exports = Kareem; + +}).call(this)}).call(this,require('_process')) +},{"_process":362}],216:[function(require,module,exports){ +(function (global){(function (){ +/** + * @license + * Lodash + * Copyright OpenJS Foundation and other contributors + * Released under MIT license + * Based on Underscore.js 1.8.3 + * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors + */ +;(function() { + + /** Used as a safe reference for `undefined` in pre-ES5 environments. */ + var undefined; + + /** Used as the semantic version number. */ + var VERSION = '4.17.21'; + + /** Used as the size to enable large array optimizations. */ + var LARGE_ARRAY_SIZE = 200; + + /** Error message constants. */ + var CORE_ERROR_TEXT = 'Unsupported core-js use. Try https://npms.io/search?q=ponyfill.', + FUNC_ERROR_TEXT = 'Expected a function', + INVALID_TEMPL_VAR_ERROR_TEXT = 'Invalid `variable` option passed into `_.template`'; + + /** Used to stand-in for `undefined` hash values. */ + var HASH_UNDEFINED = '__lodash_hash_undefined__'; + + /** Used as the maximum memoize cache size. */ + var MAX_MEMOIZE_SIZE = 500; + + /** Used as the internal argument placeholder. */ + var PLACEHOLDER = '__lodash_placeholder__'; + + /** Used to compose bitmasks for cloning. */ + var CLONE_DEEP_FLAG = 1, + CLONE_FLAT_FLAG = 2, + CLONE_SYMBOLS_FLAG = 4; + + /** Used to compose bitmasks for value comparisons. */ + var COMPARE_PARTIAL_FLAG = 1, + COMPARE_UNORDERED_FLAG = 2; + + /** Used to compose bitmasks for function metadata. */ + var WRAP_BIND_FLAG = 1, + WRAP_BIND_KEY_FLAG = 2, + WRAP_CURRY_BOUND_FLAG = 4, + WRAP_CURRY_FLAG = 8, + WRAP_CURRY_RIGHT_FLAG = 16, + WRAP_PARTIAL_FLAG = 32, + WRAP_PARTIAL_RIGHT_FLAG = 64, + WRAP_ARY_FLAG = 128, + WRAP_REARG_FLAG = 256, + WRAP_FLIP_FLAG = 512; + + /** Used as default options for `_.truncate`. */ + var DEFAULT_TRUNC_LENGTH = 30, + DEFAULT_TRUNC_OMISSION = '...'; + + /** Used to detect hot functions by number of calls within a span of milliseconds. */ + var HOT_COUNT = 800, + HOT_SPAN = 16; + + /** Used to indicate the type of lazy iteratees. */ + var LAZY_FILTER_FLAG = 1, + LAZY_MAP_FLAG = 2, + LAZY_WHILE_FLAG = 3; + + /** Used as references for various `Number` constants. */ + var INFINITY = 1 / 0, + MAX_SAFE_INTEGER = 9007199254740991, + MAX_INTEGER = 1.7976931348623157e+308, + NAN = 0 / 0; + + /** Used as references for the maximum length and index of an array. */ + var MAX_ARRAY_LENGTH = 4294967295, + MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1, + HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1; + + /** Used to associate wrap methods with their bit flags. */ + var wrapFlags = [ + ['ary', WRAP_ARY_FLAG], + ['bind', WRAP_BIND_FLAG], + ['bindKey', WRAP_BIND_KEY_FLAG], + ['curry', WRAP_CURRY_FLAG], + ['curryRight', WRAP_CURRY_RIGHT_FLAG], + ['flip', WRAP_FLIP_FLAG], + ['partial', WRAP_PARTIAL_FLAG], + ['partialRight', WRAP_PARTIAL_RIGHT_FLAG], + ['rearg', WRAP_REARG_FLAG] + ]; + + /** `Object#toString` result references. */ + var argsTag = '[object Arguments]', + arrayTag = '[object Array]', + asyncTag = '[object AsyncFunction]', + boolTag = '[object Boolean]', + dateTag = '[object Date]', + domExcTag = '[object DOMException]', + errorTag = '[object Error]', + funcTag = '[object Function]', + genTag = '[object GeneratorFunction]', + mapTag = '[object Map]', + numberTag = '[object Number]', + nullTag = '[object Null]', + objectTag = '[object Object]', + promiseTag = '[object Promise]', + proxyTag = '[object Proxy]', + regexpTag = '[object RegExp]', + setTag = '[object Set]', + stringTag = '[object String]', + symbolTag = '[object Symbol]', + undefinedTag = '[object Undefined]', + weakMapTag = '[object WeakMap]', + weakSetTag = '[object WeakSet]'; + + var arrayBufferTag = '[object ArrayBuffer]', + dataViewTag = '[object DataView]', + float32Tag = '[object Float32Array]', + float64Tag = '[object Float64Array]', + int8Tag = '[object Int8Array]', + int16Tag = '[object Int16Array]', + int32Tag = '[object Int32Array]', + uint8Tag = '[object Uint8Array]', + uint8ClampedTag = '[object Uint8ClampedArray]', + uint16Tag = '[object Uint16Array]', + uint32Tag = '[object Uint32Array]'; + + /** Used to match empty string literals in compiled template source. */ + var reEmptyStringLeading = /\b__p \+= '';/g, + reEmptyStringMiddle = /\b(__p \+=) '' \+/g, + reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g; + + /** Used to match HTML entities and HTML characters. */ + var reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g, + reUnescapedHtml = /[&<>"']/g, + reHasEscapedHtml = RegExp(reEscapedHtml.source), + reHasUnescapedHtml = RegExp(reUnescapedHtml.source); + + /** Used to match template delimiters. */ + var reEscape = /<%-([\s\S]+?)%>/g, + reEvaluate = /<%([\s\S]+?)%>/g, + reInterpolate = /<%=([\s\S]+?)%>/g; + + /** Used to match property names within property paths. */ + var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/, + reIsPlainProp = /^\w*$/, + rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g; + + /** + * Used to match `RegExp` + * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns). + */ + var reRegExpChar = /[\\^$.*+?()[\]{}|]/g, + reHasRegExpChar = RegExp(reRegExpChar.source); + + /** Used to match leading whitespace. */ + var reTrimStart = /^\s+/; + + /** Used to match a single whitespace character. */ + var reWhitespace = /\s/; + + /** Used to match wrap detail comments. */ + var reWrapComment = /\{(?:\n\/\* \[wrapped with .+\] \*\/)?\n?/, + reWrapDetails = /\{\n\/\* \[wrapped with (.+)\] \*/, + reSplitDetails = /,? & /; + + /** Used to match words composed of alphanumeric characters. */ + var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g; + + /** + * Used to validate the `validate` option in `_.template` variable. + * + * Forbids characters which could potentially change the meaning of the function argument definition: + * - "()," (modification of function parameters) + * - "=" (default value) + * - "[]{}" (destructuring of function parameters) + * - "/" (beginning of a comment) + * - whitespace + */ + var reForbiddenIdentifierChars = /[()=,{}\[\]\/\s]/; + + /** Used to match backslashes in property paths. */ + var reEscapeChar = /\\(\\)?/g; + + /** + * Used to match + * [ES template delimiters](http://ecma-international.org/ecma-262/7.0/#sec-template-literal-lexical-components). + */ + var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g; + + /** Used to match `RegExp` flags from their coerced string values. */ + var reFlags = /\w*$/; + + /** Used to detect bad signed hexadecimal string values. */ + var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; + + /** Used to detect binary string values. */ + var reIsBinary = /^0b[01]+$/i; + + /** Used to detect host constructors (Safari). */ + var reIsHostCtor = /^\[object .+?Constructor\]$/; + + /** Used to detect octal string values. */ + var reIsOctal = /^0o[0-7]+$/i; + + /** Used to detect unsigned integer values. */ + var reIsUint = /^(?:0|[1-9]\d*)$/; + + /** Used to match Latin Unicode letters (excluding mathematical operators). */ + var reLatin = /[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g; + + /** Used to ensure capturing order of template delimiters. */ + var reNoMatch = /($^)/; + + /** Used to match unescaped characters in compiled string literals. */ + var reUnescapedString = /['\n\r\u2028\u2029\\]/g; + + /** Used to compose unicode character classes. */ + var rsAstralRange = '\\ud800-\\udfff', + rsComboMarksRange = '\\u0300-\\u036f', + reComboHalfMarksRange = '\\ufe20-\\ufe2f', + rsComboSymbolsRange = '\\u20d0-\\u20ff', + rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange, + rsDingbatRange = '\\u2700-\\u27bf', + rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff', + rsMathOpRange = '\\xac\\xb1\\xd7\\xf7', + rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf', + rsPunctuationRange = '\\u2000-\\u206f', + rsSpaceRange = ' \\t\\x0b\\f\\xa0\\ufeff\\n\\r\\u2028\\u2029\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000', + rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde', + rsVarRange = '\\ufe0e\\ufe0f', + rsBreakRange = rsMathOpRange + rsNonCharRange + rsPunctuationRange + rsSpaceRange; + + /** Used to compose unicode capture groups. */ + var rsApos = "['\u2019]", + rsAstral = '[' + rsAstralRange + ']', + rsBreak = '[' + rsBreakRange + ']', + rsCombo = '[' + rsComboRange + ']', + rsDigits = '\\d+', + rsDingbat = '[' + rsDingbatRange + ']', + rsLower = '[' + rsLowerRange + ']', + rsMisc = '[^' + rsAstralRange + rsBreakRange + rsDigits + rsDingbatRange + rsLowerRange + rsUpperRange + ']', + rsFitz = '\\ud83c[\\udffb-\\udfff]', + rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')', + rsNonAstral = '[^' + rsAstralRange + ']', + rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}', + rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]', + rsUpper = '[' + rsUpperRange + ']', + rsZWJ = '\\u200d'; + + /** Used to compose unicode regexes. */ + var rsMiscLower = '(?:' + rsLower + '|' + rsMisc + ')', + rsMiscUpper = '(?:' + rsUpper + '|' + rsMisc + ')', + rsOptContrLower = '(?:' + rsApos + '(?:d|ll|m|re|s|t|ve))?', + rsOptContrUpper = '(?:' + rsApos + '(?:D|LL|M|RE|S|T|VE))?', + reOptMod = rsModifier + '?', + rsOptVar = '[' + rsVarRange + ']?', + rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*', + rsOrdLower = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])', + rsOrdUpper = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])', + rsSeq = rsOptVar + reOptMod + rsOptJoin, + rsEmoji = '(?:' + [rsDingbat, rsRegional, rsSurrPair].join('|') + ')' + rsSeq, + rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')'; + + /** Used to match apostrophes. */ + var reApos = RegExp(rsApos, 'g'); + + /** + * Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) and + * [combining diacritical marks for symbols](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_for_Symbols). + */ + var reComboMark = RegExp(rsCombo, 'g'); + + /** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */ + var reUnicode = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g'); + + /** Used to match complex or compound words. */ + var reUnicodeWord = RegExp([ + rsUpper + '?' + rsLower + '+' + rsOptContrLower + '(?=' + [rsBreak, rsUpper, '$'].join('|') + ')', + rsMiscUpper + '+' + rsOptContrUpper + '(?=' + [rsBreak, rsUpper + rsMiscLower, '$'].join('|') + ')', + rsUpper + '?' + rsMiscLower + '+' + rsOptContrLower, + rsUpper + '+' + rsOptContrUpper, + rsOrdUpper, + rsOrdLower, + rsDigits, + rsEmoji + ].join('|'), 'g'); + + /** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */ + var reHasUnicode = RegExp('[' + rsZWJ + rsAstralRange + rsComboRange + rsVarRange + ']'); + + /** Used to detect strings that need a more robust regexp to match words. */ + var reHasUnicodeWord = /[a-z][A-Z]|[A-Z]{2}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/; + + /** Used to assign default `context` object properties. */ + var contextProps = [ + 'Array', 'Buffer', 'DataView', 'Date', 'Error', 'Float32Array', 'Float64Array', + 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Map', 'Math', 'Object', + 'Promise', 'RegExp', 'Set', 'String', 'Symbol', 'TypeError', 'Uint8Array', + 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'WeakMap', + '_', 'clearTimeout', 'isFinite', 'parseInt', 'setTimeout' + ]; + + /** Used to make template sourceURLs easier to identify. */ + var templateCounter = -1; + + /** Used to identify `toStringTag` values of typed arrays. */ + var typedArrayTags = {}; + typedArrayTags[float32Tag] = typedArrayTags[float64Tag] = + typedArrayTags[int8Tag] = typedArrayTags[int16Tag] = + typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] = + typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] = + typedArrayTags[uint32Tag] = true; + typedArrayTags[argsTag] = typedArrayTags[arrayTag] = + typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] = + typedArrayTags[dataViewTag] = typedArrayTags[dateTag] = + typedArrayTags[errorTag] = typedArrayTags[funcTag] = + typedArrayTags[mapTag] = typedArrayTags[numberTag] = + typedArrayTags[objectTag] = typedArrayTags[regexpTag] = + typedArrayTags[setTag] = typedArrayTags[stringTag] = + typedArrayTags[weakMapTag] = false; + + /** Used to identify `toStringTag` values supported by `_.clone`. */ + var cloneableTags = {}; + cloneableTags[argsTag] = cloneableTags[arrayTag] = + cloneableTags[arrayBufferTag] = cloneableTags[dataViewTag] = + cloneableTags[boolTag] = cloneableTags[dateTag] = + cloneableTags[float32Tag] = cloneableTags[float64Tag] = + cloneableTags[int8Tag] = cloneableTags[int16Tag] = + cloneableTags[int32Tag] = cloneableTags[mapTag] = + cloneableTags[numberTag] = cloneableTags[objectTag] = + cloneableTags[regexpTag] = cloneableTags[setTag] = + cloneableTags[stringTag] = cloneableTags[symbolTag] = + cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] = + cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true; + cloneableTags[errorTag] = cloneableTags[funcTag] = + cloneableTags[weakMapTag] = false; + + /** Used to map Latin Unicode letters to basic Latin letters. */ + var deburredLetters = { + // Latin-1 Supplement block. + '\xc0': 'A', '\xc1': 'A', '\xc2': 'A', '\xc3': 'A', '\xc4': 'A', '\xc5': 'A', + '\xe0': 'a', '\xe1': 'a', '\xe2': 'a', '\xe3': 'a', '\xe4': 'a', '\xe5': 'a', + '\xc7': 'C', '\xe7': 'c', + '\xd0': 'D', '\xf0': 'd', + '\xc8': 'E', '\xc9': 'E', '\xca': 'E', '\xcb': 'E', + '\xe8': 'e', '\xe9': 'e', '\xea': 'e', '\xeb': 'e', + '\xcc': 'I', '\xcd': 'I', '\xce': 'I', '\xcf': 'I', + '\xec': 'i', '\xed': 'i', '\xee': 'i', '\xef': 'i', + '\xd1': 'N', '\xf1': 'n', + '\xd2': 'O', '\xd3': 'O', '\xd4': 'O', '\xd5': 'O', '\xd6': 'O', '\xd8': 'O', + '\xf2': 'o', '\xf3': 'o', '\xf4': 'o', '\xf5': 'o', '\xf6': 'o', '\xf8': 'o', + '\xd9': 'U', '\xda': 'U', '\xdb': 'U', '\xdc': 'U', + '\xf9': 'u', '\xfa': 'u', '\xfb': 'u', '\xfc': 'u', + '\xdd': 'Y', '\xfd': 'y', '\xff': 'y', + '\xc6': 'Ae', '\xe6': 'ae', + '\xde': 'Th', '\xfe': 'th', + '\xdf': 'ss', + // Latin Extended-A block. + '\u0100': 'A', '\u0102': 'A', '\u0104': 'A', + '\u0101': 'a', '\u0103': 'a', '\u0105': 'a', + '\u0106': 'C', '\u0108': 'C', '\u010a': 'C', '\u010c': 'C', + '\u0107': 'c', '\u0109': 'c', '\u010b': 'c', '\u010d': 'c', + '\u010e': 'D', '\u0110': 'D', '\u010f': 'd', '\u0111': 'd', + '\u0112': 'E', '\u0114': 'E', '\u0116': 'E', '\u0118': 'E', '\u011a': 'E', + '\u0113': 'e', '\u0115': 'e', '\u0117': 'e', '\u0119': 'e', '\u011b': 'e', + '\u011c': 'G', '\u011e': 'G', '\u0120': 'G', '\u0122': 'G', + '\u011d': 'g', '\u011f': 'g', '\u0121': 'g', '\u0123': 'g', + '\u0124': 'H', '\u0126': 'H', '\u0125': 'h', '\u0127': 'h', + '\u0128': 'I', '\u012a': 'I', '\u012c': 'I', '\u012e': 'I', '\u0130': 'I', + '\u0129': 'i', '\u012b': 'i', '\u012d': 'i', '\u012f': 'i', '\u0131': 'i', + '\u0134': 'J', '\u0135': 'j', + '\u0136': 'K', '\u0137': 'k', '\u0138': 'k', + '\u0139': 'L', '\u013b': 'L', '\u013d': 'L', '\u013f': 'L', '\u0141': 'L', + '\u013a': 'l', '\u013c': 'l', '\u013e': 'l', '\u0140': 'l', '\u0142': 'l', + '\u0143': 'N', '\u0145': 'N', '\u0147': 'N', '\u014a': 'N', + '\u0144': 'n', '\u0146': 'n', '\u0148': 'n', '\u014b': 'n', + '\u014c': 'O', '\u014e': 'O', '\u0150': 'O', + '\u014d': 'o', '\u014f': 'o', '\u0151': 'o', + '\u0154': 'R', '\u0156': 'R', '\u0158': 'R', + '\u0155': 'r', '\u0157': 'r', '\u0159': 'r', + '\u015a': 'S', '\u015c': 'S', '\u015e': 'S', '\u0160': 'S', + '\u015b': 's', '\u015d': 's', '\u015f': 's', '\u0161': 's', + '\u0162': 'T', '\u0164': 'T', '\u0166': 'T', + '\u0163': 't', '\u0165': 't', '\u0167': 't', + '\u0168': 'U', '\u016a': 'U', '\u016c': 'U', '\u016e': 'U', '\u0170': 'U', '\u0172': 'U', + '\u0169': 'u', '\u016b': 'u', '\u016d': 'u', '\u016f': 'u', '\u0171': 'u', '\u0173': 'u', + '\u0174': 'W', '\u0175': 'w', + '\u0176': 'Y', '\u0177': 'y', '\u0178': 'Y', + '\u0179': 'Z', '\u017b': 'Z', '\u017d': 'Z', + '\u017a': 'z', '\u017c': 'z', '\u017e': 'z', + '\u0132': 'IJ', '\u0133': 'ij', + '\u0152': 'Oe', '\u0153': 'oe', + '\u0149': "'n", '\u017f': 's' + }; + + /** Used to map characters to HTML entities. */ + var htmlEscapes = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' + }; + + /** Used to map HTML entities to characters. */ + var htmlUnescapes = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + ''': "'" + }; + + /** Used to escape characters for inclusion in compiled string literals. */ + var stringEscapes = { + '\\': '\\', + "'": "'", + '\n': 'n', + '\r': 'r', + '\u2028': 'u2028', + '\u2029': 'u2029' + }; + + /** Built-in method references without a dependency on `root`. */ + var freeParseFloat = parseFloat, + freeParseInt = parseInt; + + /** Detect free variable `global` from Node.js. */ + var freeGlobal = typeof global == 'object' && global && global.Object === Object && global; + + /** Detect free variable `self`. */ + var freeSelf = typeof self == 'object' && self && self.Object === Object && self; + + /** Used as a reference to the global object. */ + var root = freeGlobal || freeSelf || Function('return this')(); + + /** Detect free variable `exports`. */ + var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports; + + /** Detect free variable `module`. */ + var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module; + + /** Detect the popular CommonJS extension `module.exports`. */ + var moduleExports = freeModule && freeModule.exports === freeExports; + + /** Detect free variable `process` from Node.js. */ + var freeProcess = moduleExports && freeGlobal.process; + + /** Used to access faster Node.js helpers. */ + var nodeUtil = (function() { + try { + // Use `util.types` for Node.js 10+. + var types = freeModule && freeModule.require && freeModule.require('util').types; + + if (types) { + return types; + } + + // Legacy `process.binding('util')` for Node.js < 10. + return freeProcess && freeProcess.binding && freeProcess.binding('util'); + } catch (e) {} + }()); + + /* Node.js helper references. */ + var nodeIsArrayBuffer = nodeUtil && nodeUtil.isArrayBuffer, + nodeIsDate = nodeUtil && nodeUtil.isDate, + nodeIsMap = nodeUtil && nodeUtil.isMap, + nodeIsRegExp = nodeUtil && nodeUtil.isRegExp, + nodeIsSet = nodeUtil && nodeUtil.isSet, + nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray; + + /*--------------------------------------------------------------------------*/ + + /** + * A faster alternative to `Function#apply`, this function invokes `func` + * with the `this` binding of `thisArg` and the arguments of `args`. + * + * @private + * @param {Function} func The function to invoke. + * @param {*} thisArg The `this` binding of `func`. + * @param {Array} args The arguments to invoke `func` with. + * @returns {*} Returns the result of `func`. + */ + function apply(func, thisArg, args) { + switch (args.length) { + case 0: return func.call(thisArg); + case 1: return func.call(thisArg, args[0]); + case 2: return func.call(thisArg, args[0], args[1]); + case 3: return func.call(thisArg, args[0], args[1], args[2]); + } + return func.apply(thisArg, args); + } + + /** + * A specialized version of `baseAggregator` for arrays. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} setter The function to set `accumulator` values. + * @param {Function} iteratee The iteratee to transform keys. + * @param {Object} accumulator The initial aggregated object. + * @returns {Function} Returns `accumulator`. + */ + function arrayAggregator(array, setter, iteratee, accumulator) { + var index = -1, + length = array == null ? 0 : array.length; + + while (++index < length) { + var value = array[index]; + setter(accumulator, value, iteratee(value), array); + } + return accumulator; + } + + /** + * A specialized version of `_.forEach` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns `array`. + */ + function arrayEach(array, iteratee) { + var index = -1, + length = array == null ? 0 : array.length; + + while (++index < length) { + if (iteratee(array[index], index, array) === false) { + break; + } + } + return array; + } + + /** + * A specialized version of `_.forEachRight` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns `array`. + */ + function arrayEachRight(array, iteratee) { + var length = array == null ? 0 : array.length; + + while (length--) { + if (iteratee(array[length], length, array) === false) { + break; + } + } + return array; + } + + /** + * A specialized version of `_.every` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {boolean} Returns `true` if all elements pass the predicate check, + * else `false`. + */ + function arrayEvery(array, predicate) { + var index = -1, + length = array == null ? 0 : array.length; + + while (++index < length) { + if (!predicate(array[index], index, array)) { + return false; + } + } + return true; + } + + /** + * A specialized version of `_.filter` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {Array} Returns the new filtered array. + */ + function arrayFilter(array, predicate) { + var index = -1, + length = array == null ? 0 : array.length, + resIndex = 0, + result = []; + + while (++index < length) { + var value = array[index]; + if (predicate(value, index, array)) { + result[resIndex++] = value; + } + } + return result; + } + + /** + * A specialized version of `_.includes` for arrays without support for + * specifying an index to search from. + * + * @private + * @param {Array} [array] The array to inspect. + * @param {*} target The value to search for. + * @returns {boolean} Returns `true` if `target` is found, else `false`. + */ + function arrayIncludes(array, value) { + var length = array == null ? 0 : array.length; + return !!length && baseIndexOf(array, value, 0) > -1; + } + + /** + * This function is like `arrayIncludes` except that it accepts a comparator. + * + * @private + * @param {Array} [array] The array to inspect. + * @param {*} target The value to search for. + * @param {Function} comparator The comparator invoked per element. + * @returns {boolean} Returns `true` if `target` is found, else `false`. + */ + function arrayIncludesWith(array, value, comparator) { + var index = -1, + length = array == null ? 0 : array.length; + + while (++index < length) { + if (comparator(value, array[index])) { + return true; + } + } + return false; + } + + /** + * A specialized version of `_.map` for arrays without support for iteratee + * shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns the new mapped array. + */ + function arrayMap(array, iteratee) { + var index = -1, + length = array == null ? 0 : array.length, + result = Array(length); + + while (++index < length) { + result[index] = iteratee(array[index], index, array); + } + return result; + } + + /** + * Appends the elements of `values` to `array`. + * + * @private + * @param {Array} array The array to modify. + * @param {Array} values The values to append. + * @returns {Array} Returns `array`. + */ + function arrayPush(array, values) { + var index = -1, + length = values.length, + offset = array.length; + + while (++index < length) { + array[offset + index] = values[index]; + } + return array; + } + + /** + * A specialized version of `_.reduce` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @param {*} [accumulator] The initial value. + * @param {boolean} [initAccum] Specify using the first element of `array` as + * the initial value. + * @returns {*} Returns the accumulated value. + */ + function arrayReduce(array, iteratee, accumulator, initAccum) { + var index = -1, + length = array == null ? 0 : array.length; + + if (initAccum && length) { + accumulator = array[++index]; + } + while (++index < length) { + accumulator = iteratee(accumulator, array[index], index, array); + } + return accumulator; + } + + /** + * A specialized version of `_.reduceRight` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @param {*} [accumulator] The initial value. + * @param {boolean} [initAccum] Specify using the last element of `array` as + * the initial value. + * @returns {*} Returns the accumulated value. + */ + function arrayReduceRight(array, iteratee, accumulator, initAccum) { + var length = array == null ? 0 : array.length; + if (initAccum && length) { + accumulator = array[--length]; + } + while (length--) { + accumulator = iteratee(accumulator, array[length], length, array); + } + return accumulator; + } + + /** + * A specialized version of `_.some` for arrays without support for iteratee + * shorthands. + * + * @private + * @param {Array} [array] The array to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {boolean} Returns `true` if any element passes the predicate check, + * else `false`. + */ + function arraySome(array, predicate) { + var index = -1, + length = array == null ? 0 : array.length; + + while (++index < length) { + if (predicate(array[index], index, array)) { + return true; + } + } + return false; + } + + /** + * Gets the size of an ASCII `string`. + * + * @private + * @param {string} string The string inspect. + * @returns {number} Returns the string size. + */ + var asciiSize = baseProperty('length'); + + /** + * Converts an ASCII `string` to an array. + * + * @private + * @param {string} string The string to convert. + * @returns {Array} Returns the converted array. + */ + function asciiToArray(string) { + return string.split(''); + } + + /** + * Splits an ASCII `string` into an array of its words. + * + * @private + * @param {string} The string to inspect. + * @returns {Array} Returns the words of `string`. + */ + function asciiWords(string) { + return string.match(reAsciiWord) || []; + } + + /** + * The base implementation of methods like `_.findKey` and `_.findLastKey`, + * without support for iteratee shorthands, which iterates over `collection` + * using `eachFunc`. + * + * @private + * @param {Array|Object} collection The collection to inspect. + * @param {Function} predicate The function invoked per iteration. + * @param {Function} eachFunc The function to iterate over `collection`. + * @returns {*} Returns the found element or its key, else `undefined`. + */ + function baseFindKey(collection, predicate, eachFunc) { + var result; + eachFunc(collection, function(value, key, collection) { + if (predicate(value, key, collection)) { + result = key; + return false; + } + }); + return result; + } + + /** + * The base implementation of `_.findIndex` and `_.findLastIndex` without + * support for iteratee shorthands. + * + * @private + * @param {Array} array The array to inspect. + * @param {Function} predicate The function invoked per iteration. + * @param {number} fromIndex The index to search from. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function baseFindIndex(array, predicate, fromIndex, fromRight) { + var length = array.length, + index = fromIndex + (fromRight ? 1 : -1); + + while ((fromRight ? index-- : ++index < length)) { + if (predicate(array[index], index, array)) { + return index; + } + } + return -1; + } + + /** + * The base implementation of `_.indexOf` without `fromIndex` bounds checks. + * + * @private + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @param {number} fromIndex The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function baseIndexOf(array, value, fromIndex) { + return value === value + ? strictIndexOf(array, value, fromIndex) + : baseFindIndex(array, baseIsNaN, fromIndex); + } + + /** + * This function is like `baseIndexOf` except that it accepts a comparator. + * + * @private + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @param {number} fromIndex The index to search from. + * @param {Function} comparator The comparator invoked per element. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function baseIndexOfWith(array, value, fromIndex, comparator) { + var index = fromIndex - 1, + length = array.length; + + while (++index < length) { + if (comparator(array[index], value)) { + return index; + } + } + return -1; + } + + /** + * The base implementation of `_.isNaN` without support for number objects. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`. + */ + function baseIsNaN(value) { + return value !== value; + } + + /** + * The base implementation of `_.mean` and `_.meanBy` without support for + * iteratee shorthands. + * + * @private + * @param {Array} array The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {number} Returns the mean. + */ + function baseMean(array, iteratee) { + var length = array == null ? 0 : array.length; + return length ? (baseSum(array, iteratee) / length) : NAN; + } + + /** + * The base implementation of `_.property` without support for deep paths. + * + * @private + * @param {string} key The key of the property to get. + * @returns {Function} Returns the new accessor function. + */ + function baseProperty(key) { + return function(object) { + return object == null ? undefined : object[key]; + }; + } + + /** + * The base implementation of `_.propertyOf` without support for deep paths. + * + * @private + * @param {Object} object The object to query. + * @returns {Function} Returns the new accessor function. + */ + function basePropertyOf(object) { + return function(key) { + return object == null ? undefined : object[key]; + }; + } + + /** + * The base implementation of `_.reduce` and `_.reduceRight`, without support + * for iteratee shorthands, which iterates over `collection` using `eachFunc`. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @param {*} accumulator The initial value. + * @param {boolean} initAccum Specify using the first or last element of + * `collection` as the initial value. + * @param {Function} eachFunc The function to iterate over `collection`. + * @returns {*} Returns the accumulated value. + */ + function baseReduce(collection, iteratee, accumulator, initAccum, eachFunc) { + eachFunc(collection, function(value, index, collection) { + accumulator = initAccum + ? (initAccum = false, value) + : iteratee(accumulator, value, index, collection); + }); + return accumulator; + } + + /** + * The base implementation of `_.sortBy` which uses `comparer` to define the + * sort order of `array` and replaces criteria objects with their corresponding + * values. + * + * @private + * @param {Array} array The array to sort. + * @param {Function} comparer The function to define sort order. + * @returns {Array} Returns `array`. + */ + function baseSortBy(array, comparer) { + var length = array.length; + + array.sort(comparer); + while (length--) { + array[length] = array[length].value; + } + return array; + } + + /** + * The base implementation of `_.sum` and `_.sumBy` without support for + * iteratee shorthands. + * + * @private + * @param {Array} array The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {number} Returns the sum. + */ + function baseSum(array, iteratee) { + var result, + index = -1, + length = array.length; + + while (++index < length) { + var current = iteratee(array[index]); + if (current !== undefined) { + result = result === undefined ? current : (result + current); + } + } + return result; + } + + /** + * The base implementation of `_.times` without support for iteratee shorthands + * or max array length checks. + * + * @private + * @param {number} n The number of times to invoke `iteratee`. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns the array of results. + */ + function baseTimes(n, iteratee) { + var index = -1, + result = Array(n); + + while (++index < n) { + result[index] = iteratee(index); + } + return result; + } + + /** + * The base implementation of `_.toPairs` and `_.toPairsIn` which creates an array + * of key-value pairs for `object` corresponding to the property names of `props`. + * + * @private + * @param {Object} object The object to query. + * @param {Array} props The property names to get values for. + * @returns {Object} Returns the key-value pairs. + */ + function baseToPairs(object, props) { + return arrayMap(props, function(key) { + return [key, object[key]]; + }); + } + + /** + * The base implementation of `_.trim`. + * + * @private + * @param {string} string The string to trim. + * @returns {string} Returns the trimmed string. + */ + function baseTrim(string) { + return string + ? string.slice(0, trimmedEndIndex(string) + 1).replace(reTrimStart, '') + : string; + } + + /** + * The base implementation of `_.unary` without support for storing metadata. + * + * @private + * @param {Function} func The function to cap arguments for. + * @returns {Function} Returns the new capped function. + */ + function baseUnary(func) { + return function(value) { + return func(value); + }; + } + + /** + * The base implementation of `_.values` and `_.valuesIn` which creates an + * array of `object` property values corresponding to the property names + * of `props`. + * + * @private + * @param {Object} object The object to query. + * @param {Array} props The property names to get values for. + * @returns {Object} Returns the array of property values. + */ + function baseValues(object, props) { + return arrayMap(props, function(key) { + return object[key]; + }); + } + + /** + * Checks if a `cache` value for `key` exists. + * + * @private + * @param {Object} cache The cache to query. + * @param {string} key The key of the entry to check. + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. + */ + function cacheHas(cache, key) { + return cache.has(key); + } + + /** + * Used by `_.trim` and `_.trimStart` to get the index of the first string symbol + * that is not found in the character symbols. + * + * @private + * @param {Array} strSymbols The string symbols to inspect. + * @param {Array} chrSymbols The character symbols to find. + * @returns {number} Returns the index of the first unmatched string symbol. + */ + function charsStartIndex(strSymbols, chrSymbols) { + var index = -1, + length = strSymbols.length; + + while (++index < length && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {} + return index; + } + + /** + * Used by `_.trim` and `_.trimEnd` to get the index of the last string symbol + * that is not found in the character symbols. + * + * @private + * @param {Array} strSymbols The string symbols to inspect. + * @param {Array} chrSymbols The character symbols to find. + * @returns {number} Returns the index of the last unmatched string symbol. + */ + function charsEndIndex(strSymbols, chrSymbols) { + var index = strSymbols.length; + + while (index-- && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {} + return index; + } + + /** + * Gets the number of `placeholder` occurrences in `array`. + * + * @private + * @param {Array} array The array to inspect. + * @param {*} placeholder The placeholder to search for. + * @returns {number} Returns the placeholder count. + */ + function countHolders(array, placeholder) { + var length = array.length, + result = 0; + + while (length--) { + if (array[length] === placeholder) { + ++result; + } + } + return result; + } + + /** + * Used by `_.deburr` to convert Latin-1 Supplement and Latin Extended-A + * letters to basic Latin letters. + * + * @private + * @param {string} letter The matched letter to deburr. + * @returns {string} Returns the deburred letter. + */ + var deburrLetter = basePropertyOf(deburredLetters); + + /** + * Used by `_.escape` to convert characters to HTML entities. + * + * @private + * @param {string} chr The matched character to escape. + * @returns {string} Returns the escaped character. + */ + var escapeHtmlChar = basePropertyOf(htmlEscapes); + + /** + * Used by `_.template` to escape characters for inclusion in compiled string literals. + * + * @private + * @param {string} chr The matched character to escape. + * @returns {string} Returns the escaped character. + */ + function escapeStringChar(chr) { + return '\\' + stringEscapes[chr]; + } + + /** + * Gets the value at `key` of `object`. + * + * @private + * @param {Object} [object] The object to query. + * @param {string} key The key of the property to get. + * @returns {*} Returns the property value. + */ + function getValue(object, key) { + return object == null ? undefined : object[key]; + } + + /** + * Checks if `string` contains Unicode symbols. + * + * @private + * @param {string} string The string to inspect. + * @returns {boolean} Returns `true` if a symbol is found, else `false`. + */ + function hasUnicode(string) { + return reHasUnicode.test(string); + } + + /** + * Checks if `string` contains a word composed of Unicode symbols. + * + * @private + * @param {string} string The string to inspect. + * @returns {boolean} Returns `true` if a word is found, else `false`. + */ + function hasUnicodeWord(string) { + return reHasUnicodeWord.test(string); + } + + /** + * Converts `iterator` to an array. + * + * @private + * @param {Object} iterator The iterator to convert. + * @returns {Array} Returns the converted array. + */ + function iteratorToArray(iterator) { + var data, + result = []; + + while (!(data = iterator.next()).done) { + result.push(data.value); + } + return result; + } + + /** + * Converts `map` to its key-value pairs. + * + * @private + * @param {Object} map The map to convert. + * @returns {Array} Returns the key-value pairs. + */ + function mapToArray(map) { + var index = -1, + result = Array(map.size); + + map.forEach(function(value, key) { + result[++index] = [key, value]; + }); + return result; + } + + /** + * Creates a unary function that invokes `func` with its argument transformed. + * + * @private + * @param {Function} func The function to wrap. + * @param {Function} transform The argument transform. + * @returns {Function} Returns the new function. + */ + function overArg(func, transform) { + return function(arg) { + return func(transform(arg)); + }; + } + + /** + * Replaces all `placeholder` elements in `array` with an internal placeholder + * and returns an array of their indexes. + * + * @private + * @param {Array} array The array to modify. + * @param {*} placeholder The placeholder to replace. + * @returns {Array} Returns the new array of placeholder indexes. + */ + function replaceHolders(array, placeholder) { + var index = -1, + length = array.length, + resIndex = 0, + result = []; + + while (++index < length) { + var value = array[index]; + if (value === placeholder || value === PLACEHOLDER) { + array[index] = PLACEHOLDER; + result[resIndex++] = index; + } + } + return result; + } + + /** + * Converts `set` to an array of its values. + * + * @private + * @param {Object} set The set to convert. + * @returns {Array} Returns the values. + */ + function setToArray(set) { + var index = -1, + result = Array(set.size); + + set.forEach(function(value) { + result[++index] = value; + }); + return result; + } + + /** + * Converts `set` to its value-value pairs. + * + * @private + * @param {Object} set The set to convert. + * @returns {Array} Returns the value-value pairs. + */ + function setToPairs(set) { + var index = -1, + result = Array(set.size); + + set.forEach(function(value) { + result[++index] = [value, value]; + }); + return result; + } + + /** + * A specialized version of `_.indexOf` which performs strict equality + * comparisons of values, i.e. `===`. + * + * @private + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @param {number} fromIndex The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function strictIndexOf(array, value, fromIndex) { + var index = fromIndex - 1, + length = array.length; + + while (++index < length) { + if (array[index] === value) { + return index; + } + } + return -1; + } + + /** + * A specialized version of `_.lastIndexOf` which performs strict equality + * comparisons of values, i.e. `===`. + * + * @private + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @param {number} fromIndex The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function strictLastIndexOf(array, value, fromIndex) { + var index = fromIndex + 1; + while (index--) { + if (array[index] === value) { + return index; + } + } + return index; + } + + /** + * Gets the number of symbols in `string`. + * + * @private + * @param {string} string The string to inspect. + * @returns {number} Returns the string size. + */ + function stringSize(string) { + return hasUnicode(string) + ? unicodeSize(string) + : asciiSize(string); + } + + /** + * Converts `string` to an array. + * + * @private + * @param {string} string The string to convert. + * @returns {Array} Returns the converted array. + */ + function stringToArray(string) { + return hasUnicode(string) + ? unicodeToArray(string) + : asciiToArray(string); + } + + /** + * Used by `_.trim` and `_.trimEnd` to get the index of the last non-whitespace + * character of `string`. + * + * @private + * @param {string} string The string to inspect. + * @returns {number} Returns the index of the last non-whitespace character. + */ + function trimmedEndIndex(string) { + var index = string.length; + + while (index-- && reWhitespace.test(string.charAt(index))) {} + return index; + } + + /** + * Used by `_.unescape` to convert HTML entities to characters. + * + * @private + * @param {string} chr The matched character to unescape. + * @returns {string} Returns the unescaped character. + */ + var unescapeHtmlChar = basePropertyOf(htmlUnescapes); + + /** + * Gets the size of a Unicode `string`. + * + * @private + * @param {string} string The string inspect. + * @returns {number} Returns the string size. + */ + function unicodeSize(string) { + var result = reUnicode.lastIndex = 0; + while (reUnicode.test(string)) { + ++result; + } + return result; + } + + /** + * Converts a Unicode `string` to an array. + * + * @private + * @param {string} string The string to convert. + * @returns {Array} Returns the converted array. + */ + function unicodeToArray(string) { + return string.match(reUnicode) || []; + } + + /** + * Splits a Unicode `string` into an array of its words. + * + * @private + * @param {string} The string to inspect. + * @returns {Array} Returns the words of `string`. + */ + function unicodeWords(string) { + return string.match(reUnicodeWord) || []; + } + + /*--------------------------------------------------------------------------*/ + + /** + * Create a new pristine `lodash` function using the `context` object. + * + * @static + * @memberOf _ + * @since 1.1.0 + * @category Util + * @param {Object} [context=root] The context object. + * @returns {Function} Returns a new `lodash` function. + * @example + * + * _.mixin({ 'foo': _.constant('foo') }); + * + * var lodash = _.runInContext(); + * lodash.mixin({ 'bar': lodash.constant('bar') }); + * + * _.isFunction(_.foo); + * // => true + * _.isFunction(_.bar); + * // => false + * + * lodash.isFunction(lodash.foo); + * // => false + * lodash.isFunction(lodash.bar); + * // => true + * + * // Create a suped-up `defer` in Node.js. + * var defer = _.runInContext({ 'setTimeout': setImmediate }).defer; + */ + var runInContext = (function runInContext(context) { + context = context == null ? root : _.defaults(root.Object(), context, _.pick(root, contextProps)); + + /** Built-in constructor references. */ + var Array = context.Array, + Date = context.Date, + Error = context.Error, + Function = context.Function, + Math = context.Math, + Object = context.Object, + RegExp = context.RegExp, + String = context.String, + TypeError = context.TypeError; + + /** Used for built-in method references. */ + var arrayProto = Array.prototype, + funcProto = Function.prototype, + objectProto = Object.prototype; + + /** Used to detect overreaching core-js shims. */ + var coreJsData = context['__core-js_shared__']; + + /** Used to resolve the decompiled source of functions. */ + var funcToString = funcProto.toString; + + /** Used to check objects for own properties. */ + var hasOwnProperty = objectProto.hasOwnProperty; + + /** Used to generate unique IDs. */ + var idCounter = 0; + + /** Used to detect methods masquerading as native. */ + var maskSrcKey = (function() { + var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || ''); + return uid ? ('Symbol(src)_1.' + uid) : ''; + }()); + + /** + * Used to resolve the + * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) + * of values. + */ + var nativeObjectToString = objectProto.toString; + + /** Used to infer the `Object` constructor. */ + var objectCtorString = funcToString.call(Object); + + /** Used to restore the original `_` reference in `_.noConflict`. */ + var oldDash = root._; + + /** Used to detect if a method is native. */ + var reIsNative = RegExp('^' + + funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&') + .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$' + ); + + /** Built-in value references. */ + var Buffer = moduleExports ? context.Buffer : undefined, + Symbol = context.Symbol, + Uint8Array = context.Uint8Array, + allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined, + getPrototype = overArg(Object.getPrototypeOf, Object), + objectCreate = Object.create, + propertyIsEnumerable = objectProto.propertyIsEnumerable, + splice = arrayProto.splice, + spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined, + symIterator = Symbol ? Symbol.iterator : undefined, + symToStringTag = Symbol ? Symbol.toStringTag : undefined; + + var defineProperty = (function() { + try { + var func = getNative(Object, 'defineProperty'); + func({}, '', {}); + return func; + } catch (e) {} + }()); + + /** Mocked built-ins. */ + var ctxClearTimeout = context.clearTimeout !== root.clearTimeout && context.clearTimeout, + ctxNow = Date && Date.now !== root.Date.now && Date.now, + ctxSetTimeout = context.setTimeout !== root.setTimeout && context.setTimeout; + + /* Built-in method references for those with the same name as other `lodash` methods. */ + var nativeCeil = Math.ceil, + nativeFloor = Math.floor, + nativeGetSymbols = Object.getOwnPropertySymbols, + nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined, + nativeIsFinite = context.isFinite, + nativeJoin = arrayProto.join, + nativeKeys = overArg(Object.keys, Object), + nativeMax = Math.max, + nativeMin = Math.min, + nativeNow = Date.now, + nativeParseInt = context.parseInt, + nativeRandom = Math.random, + nativeReverse = arrayProto.reverse; + + /* Built-in method references that are verified to be native. */ + var DataView = getNative(context, 'DataView'), + Map = getNative(context, 'Map'), + Promise = getNative(context, 'Promise'), + Set = getNative(context, 'Set'), + WeakMap = getNative(context, 'WeakMap'), + nativeCreate = getNative(Object, 'create'); + + /** Used to store function metadata. */ + var metaMap = WeakMap && new WeakMap; + + /** Used to lookup unminified function names. */ + var realNames = {}; + + /** Used to detect maps, sets, and weakmaps. */ + var dataViewCtorString = toSource(DataView), + mapCtorString = toSource(Map), + promiseCtorString = toSource(Promise), + setCtorString = toSource(Set), + weakMapCtorString = toSource(WeakMap); + + /** Used to convert symbols to primitives and strings. */ + var symbolProto = Symbol ? Symbol.prototype : undefined, + symbolValueOf = symbolProto ? symbolProto.valueOf : undefined, + symbolToString = symbolProto ? symbolProto.toString : undefined; + + /*------------------------------------------------------------------------*/ + + /** + * Creates a `lodash` object which wraps `value` to enable implicit method + * chain sequences. Methods that operate on and return arrays, collections, + * and functions can be chained together. Methods that retrieve a single value + * or may return a primitive value will automatically end the chain sequence + * and return the unwrapped value. Otherwise, the value must be unwrapped + * with `_#value`. + * + * Explicit chain sequences, which must be unwrapped with `_#value`, may be + * enabled using `_.chain`. + * + * The execution of chained methods is lazy, that is, it's deferred until + * `_#value` is implicitly or explicitly called. + * + * Lazy evaluation allows several methods to support shortcut fusion. + * Shortcut fusion is an optimization to merge iteratee calls; this avoids + * the creation of intermediate arrays and can greatly reduce the number of + * iteratee executions. Sections of a chain sequence qualify for shortcut + * fusion if the section is applied to an array and iteratees accept only + * one argument. The heuristic for whether a section qualifies for shortcut + * fusion is subject to change. + * + * Chaining is supported in custom builds as long as the `_#value` method is + * directly or indirectly included in the build. + * + * In addition to lodash methods, wrappers have `Array` and `String` methods. + * + * The wrapper `Array` methods are: + * `concat`, `join`, `pop`, `push`, `shift`, `sort`, `splice`, and `unshift` + * + * The wrapper `String` methods are: + * `replace` and `split` + * + * The wrapper methods that support shortcut fusion are: + * `at`, `compact`, `drop`, `dropRight`, `dropWhile`, `filter`, `find`, + * `findLast`, `head`, `initial`, `last`, `map`, `reject`, `reverse`, `slice`, + * `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray` + * + * The chainable wrapper methods are: + * `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`, `at`, + * `before`, `bind`, `bindAll`, `bindKey`, `castArray`, `chain`, `chunk`, + * `commit`, `compact`, `concat`, `conforms`, `constant`, `countBy`, `create`, + * `curry`, `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`, + * `difference`, `differenceBy`, `differenceWith`, `drop`, `dropRight`, + * `dropRightWhile`, `dropWhile`, `extend`, `extendWith`, `fill`, `filter`, + * `flatMap`, `flatMapDeep`, `flatMapDepth`, `flatten`, `flattenDeep`, + * `flattenDepth`, `flip`, `flow`, `flowRight`, `fromPairs`, `functions`, + * `functionsIn`, `groupBy`, `initial`, `intersection`, `intersectionBy`, + * `intersectionWith`, `invert`, `invertBy`, `invokeMap`, `iteratee`, `keyBy`, + * `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`, + * `memoize`, `merge`, `mergeWith`, `method`, `methodOf`, `mixin`, `negate`, + * `nthArg`, `omit`, `omitBy`, `once`, `orderBy`, `over`, `overArgs`, + * `overEvery`, `overSome`, `partial`, `partialRight`, `partition`, `pick`, + * `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`, `pullAllBy`, + * `pullAllWith`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`, `reject`, + * `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, `shuffle`, + * `slice`, `sort`, `sortBy`, `splice`, `spread`, `tail`, `take`, `takeRight`, + * `takeRightWhile`, `takeWhile`, `tap`, `throttle`, `thru`, `toArray`, + * `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`, `transform`, `unary`, + * `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`, `uniqWith`, `unset`, + * `unshift`, `unzip`, `unzipWith`, `update`, `updateWith`, `values`, + * `valuesIn`, `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`, + * `zipObject`, `zipObjectDeep`, and `zipWith` + * + * The wrapper methods that are **not** chainable by default are: + * `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`, + * `cloneDeep`, `cloneDeepWith`, `cloneWith`, `conformsTo`, `deburr`, + * `defaultTo`, `divide`, `each`, `eachRight`, `endsWith`, `eq`, `escape`, + * `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`, `findLast`, + * `findLastIndex`, `findLastKey`, `first`, `floor`, `forEach`, `forEachRight`, + * `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `get`, `gt`, `gte`, `has`, + * `hasIn`, `head`, `identity`, `includes`, `indexOf`, `inRange`, `invoke`, + * `isArguments`, `isArray`, `isArrayBuffer`, `isArrayLike`, `isArrayLikeObject`, + * `isBoolean`, `isBuffer`, `isDate`, `isElement`, `isEmpty`, `isEqual`, + * `isEqualWith`, `isError`, `isFinite`, `isFunction`, `isInteger`, `isLength`, + * `isMap`, `isMatch`, `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`, + * `isNumber`, `isObject`, `isObjectLike`, `isPlainObject`, `isRegExp`, + * `isSafeInteger`, `isSet`, `isString`, `isUndefined`, `isTypedArray`, + * `isWeakMap`, `isWeakSet`, `join`, `kebabCase`, `last`, `lastIndexOf`, + * `lowerCase`, `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `meanBy`, + * `min`, `minBy`, `multiply`, `noConflict`, `noop`, `now`, `nth`, `pad`, + * `padEnd`, `padStart`, `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, + * `repeat`, `result`, `round`, `runInContext`, `sample`, `shift`, `size`, + * `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`, + * `sortedLastIndexBy`, `startCase`, `startsWith`, `stubArray`, `stubFalse`, + * `stubObject`, `stubString`, `stubTrue`, `subtract`, `sum`, `sumBy`, + * `template`, `times`, `toFinite`, `toInteger`, `toJSON`, `toLength`, + * `toLower`, `toNumber`, `toSafeInteger`, `toString`, `toUpper`, `trim`, + * `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`, `upperCase`, + * `upperFirst`, `value`, and `words` + * + * @name _ + * @constructor + * @category Seq + * @param {*} value The value to wrap in a `lodash` instance. + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * function square(n) { + * return n * n; + * } + * + * var wrapped = _([1, 2, 3]); + * + * // Returns an unwrapped value. + * wrapped.reduce(_.add); + * // => 6 + * + * // Returns a wrapped value. + * var squares = wrapped.map(square); + * + * _.isArray(squares); + * // => false + * + * _.isArray(squares.value()); + * // => true + */ + function lodash(value) { + if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) { + if (value instanceof LodashWrapper) { + return value; + } + if (hasOwnProperty.call(value, '__wrapped__')) { + return wrapperClone(value); + } + } + return new LodashWrapper(value); + } + + /** + * The base implementation of `_.create` without support for assigning + * properties to the created object. + * + * @private + * @param {Object} proto The object to inherit from. + * @returns {Object} Returns the new object. + */ + var baseCreate = (function() { + function object() {} + return function(proto) { + if (!isObject(proto)) { + return {}; + } + if (objectCreate) { + return objectCreate(proto); + } + object.prototype = proto; + var result = new object; + object.prototype = undefined; + return result; + }; + }()); + + /** + * The function whose prototype chain sequence wrappers inherit from. + * + * @private + */ + function baseLodash() { + // No operation performed. + } + + /** + * The base constructor for creating `lodash` wrapper objects. + * + * @private + * @param {*} value The value to wrap. + * @param {boolean} [chainAll] Enable explicit method chain sequences. + */ + function LodashWrapper(value, chainAll) { + this.__wrapped__ = value; + this.__actions__ = []; + this.__chain__ = !!chainAll; + this.__index__ = 0; + this.__values__ = undefined; + } + + /** + * By default, the template delimiters used by lodash are like those in + * embedded Ruby (ERB) as well as ES2015 template strings. Change the + * following template settings to use alternative delimiters. + * + * @static + * @memberOf _ + * @type {Object} + */ + lodash.templateSettings = { + + /** + * Used to detect `data` property values to be HTML-escaped. + * + * @memberOf _.templateSettings + * @type {RegExp} + */ + 'escape': reEscape, + + /** + * Used to detect code to be evaluated. + * + * @memberOf _.templateSettings + * @type {RegExp} + */ + 'evaluate': reEvaluate, + + /** + * Used to detect `data` property values to inject. + * + * @memberOf _.templateSettings + * @type {RegExp} + */ + 'interpolate': reInterpolate, + + /** + * Used to reference the data object in the template text. + * + * @memberOf _.templateSettings + * @type {string} + */ + 'variable': '', + + /** + * Used to import variables into the compiled template. + * + * @memberOf _.templateSettings + * @type {Object} + */ + 'imports': { + + /** + * A reference to the `lodash` function. + * + * @memberOf _.templateSettings.imports + * @type {Function} + */ + '_': lodash + } + }; + + // Ensure wrappers are instances of `baseLodash`. + lodash.prototype = baseLodash.prototype; + lodash.prototype.constructor = lodash; + + LodashWrapper.prototype = baseCreate(baseLodash.prototype); + LodashWrapper.prototype.constructor = LodashWrapper; + + /*------------------------------------------------------------------------*/ + + /** + * Creates a lazy wrapper object which wraps `value` to enable lazy evaluation. + * + * @private + * @constructor + * @param {*} value The value to wrap. + */ + function LazyWrapper(value) { + this.__wrapped__ = value; + this.__actions__ = []; + this.__dir__ = 1; + this.__filtered__ = false; + this.__iteratees__ = []; + this.__takeCount__ = MAX_ARRAY_LENGTH; + this.__views__ = []; + } + + /** + * Creates a clone of the lazy wrapper object. + * + * @private + * @name clone + * @memberOf LazyWrapper + * @returns {Object} Returns the cloned `LazyWrapper` object. + */ + function lazyClone() { + var result = new LazyWrapper(this.__wrapped__); + result.__actions__ = copyArray(this.__actions__); + result.__dir__ = this.__dir__; + result.__filtered__ = this.__filtered__; + result.__iteratees__ = copyArray(this.__iteratees__); + result.__takeCount__ = this.__takeCount__; + result.__views__ = copyArray(this.__views__); + return result; + } + + /** + * Reverses the direction of lazy iteration. + * + * @private + * @name reverse + * @memberOf LazyWrapper + * @returns {Object} Returns the new reversed `LazyWrapper` object. + */ + function lazyReverse() { + if (this.__filtered__) { + var result = new LazyWrapper(this); + result.__dir__ = -1; + result.__filtered__ = true; + } else { + result = this.clone(); + result.__dir__ *= -1; + } + return result; + } + + /** + * Extracts the unwrapped value from its lazy wrapper. + * + * @private + * @name value + * @memberOf LazyWrapper + * @returns {*} Returns the unwrapped value. + */ + function lazyValue() { + var array = this.__wrapped__.value(), + dir = this.__dir__, + isArr = isArray(array), + isRight = dir < 0, + arrLength = isArr ? array.length : 0, + view = getView(0, arrLength, this.__views__), + start = view.start, + end = view.end, + length = end - start, + index = isRight ? end : (start - 1), + iteratees = this.__iteratees__, + iterLength = iteratees.length, + resIndex = 0, + takeCount = nativeMin(length, this.__takeCount__); + + if (!isArr || (!isRight && arrLength == length && takeCount == length)) { + return baseWrapperValue(array, this.__actions__); + } + var result = []; + + outer: + while (length-- && resIndex < takeCount) { + index += dir; + + var iterIndex = -1, + value = array[index]; + + while (++iterIndex < iterLength) { + var data = iteratees[iterIndex], + iteratee = data.iteratee, + type = data.type, + computed = iteratee(value); + + if (type == LAZY_MAP_FLAG) { + value = computed; + } else if (!computed) { + if (type == LAZY_FILTER_FLAG) { + continue outer; + } else { + break outer; + } + } + } + result[resIndex++] = value; + } + return result; + } + + // Ensure `LazyWrapper` is an instance of `baseLodash`. + LazyWrapper.prototype = baseCreate(baseLodash.prototype); + LazyWrapper.prototype.constructor = LazyWrapper; + + /*------------------------------------------------------------------------*/ + + /** + * Creates a hash object. + * + * @private + * @constructor + * @param {Array} [entries] The key-value pairs to cache. + */ + function Hash(entries) { + var index = -1, + length = entries == null ? 0 : entries.length; + + this.clear(); + while (++index < length) { + var entry = entries[index]; + this.set(entry[0], entry[1]); + } + } + + /** + * Removes all key-value entries from the hash. + * + * @private + * @name clear + * @memberOf Hash + */ + function hashClear() { + this.__data__ = nativeCreate ? nativeCreate(null) : {}; + this.size = 0; + } + + /** + * Removes `key` and its value from the hash. + * + * @private + * @name delete + * @memberOf Hash + * @param {Object} hash The hash to modify. + * @param {string} key The key of the value to remove. + * @returns {boolean} Returns `true` if the entry was removed, else `false`. + */ + function hashDelete(key) { + var result = this.has(key) && delete this.__data__[key]; + this.size -= result ? 1 : 0; + return result; + } + + /** + * Gets the hash value for `key`. + * + * @private + * @name get + * @memberOf Hash + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + function hashGet(key) { + var data = this.__data__; + if (nativeCreate) { + var result = data[key]; + return result === HASH_UNDEFINED ? undefined : result; + } + return hasOwnProperty.call(data, key) ? data[key] : undefined; + } + + /** + * Checks if a hash value for `key` exists. + * + * @private + * @name has + * @memberOf Hash + * @param {string} key The key of the entry to check. + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. + */ + function hashHas(key) { + var data = this.__data__; + return nativeCreate ? (data[key] !== undefined) : hasOwnProperty.call(data, key); + } + + /** + * Sets the hash `key` to `value`. + * + * @private + * @name set + * @memberOf Hash + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the hash instance. + */ + function hashSet(key, value) { + var data = this.__data__; + this.size += this.has(key) ? 0 : 1; + data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value; + return this; + } + + // Add methods to `Hash`. + Hash.prototype.clear = hashClear; + Hash.prototype['delete'] = hashDelete; + Hash.prototype.get = hashGet; + Hash.prototype.has = hashHas; + Hash.prototype.set = hashSet; + + /*------------------------------------------------------------------------*/ + + /** + * Creates an list cache object. + * + * @private + * @constructor + * @param {Array} [entries] The key-value pairs to cache. + */ + function ListCache(entries) { + var index = -1, + length = entries == null ? 0 : entries.length; + + this.clear(); + while (++index < length) { + var entry = entries[index]; + this.set(entry[0], entry[1]); + } + } + + /** + * Removes all key-value entries from the list cache. + * + * @private + * @name clear + * @memberOf ListCache + */ + function listCacheClear() { + this.__data__ = []; + this.size = 0; + } + + /** + * Removes `key` and its value from the list cache. + * + * @private + * @name delete + * @memberOf ListCache + * @param {string} key The key of the value to remove. + * @returns {boolean} Returns `true` if the entry was removed, else `false`. + */ + function listCacheDelete(key) { + var data = this.__data__, + index = assocIndexOf(data, key); + + if (index < 0) { + return false; + } + var lastIndex = data.length - 1; + if (index == lastIndex) { + data.pop(); + } else { + splice.call(data, index, 1); + } + --this.size; + return true; + } + + /** + * Gets the list cache value for `key`. + * + * @private + * @name get + * @memberOf ListCache + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + function listCacheGet(key) { + var data = this.__data__, + index = assocIndexOf(data, key); + + return index < 0 ? undefined : data[index][1]; + } + + /** + * Checks if a list cache value for `key` exists. + * + * @private + * @name has + * @memberOf ListCache + * @param {string} key The key of the entry to check. + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. + */ + function listCacheHas(key) { + return assocIndexOf(this.__data__, key) > -1; + } + + /** + * Sets the list cache `key` to `value`. + * + * @private + * @name set + * @memberOf ListCache + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the list cache instance. + */ + function listCacheSet(key, value) { + var data = this.__data__, + index = assocIndexOf(data, key); + + if (index < 0) { + ++this.size; + data.push([key, value]); + } else { + data[index][1] = value; + } + return this; + } + + // Add methods to `ListCache`. + ListCache.prototype.clear = listCacheClear; + ListCache.prototype['delete'] = listCacheDelete; + ListCache.prototype.get = listCacheGet; + ListCache.prototype.has = listCacheHas; + ListCache.prototype.set = listCacheSet; + + /*------------------------------------------------------------------------*/ + + /** + * Creates a map cache object to store key-value pairs. + * + * @private + * @constructor + * @param {Array} [entries] The key-value pairs to cache. + */ + function MapCache(entries) { + var index = -1, + length = entries == null ? 0 : entries.length; + + this.clear(); + while (++index < length) { + var entry = entries[index]; + this.set(entry[0], entry[1]); + } + } + + /** + * Removes all key-value entries from the map. + * + * @private + * @name clear + * @memberOf MapCache + */ + function mapCacheClear() { + this.size = 0; + this.__data__ = { + 'hash': new Hash, + 'map': new (Map || ListCache), + 'string': new Hash + }; + } + + /** + * Removes `key` and its value from the map. + * + * @private + * @name delete + * @memberOf MapCache + * @param {string} key The key of the value to remove. + * @returns {boolean} Returns `true` if the entry was removed, else `false`. + */ + function mapCacheDelete(key) { + var result = getMapData(this, key)['delete'](key); + this.size -= result ? 1 : 0; + return result; + } + + /** + * Gets the map value for `key`. + * + * @private + * @name get + * @memberOf MapCache + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + function mapCacheGet(key) { + return getMapData(this, key).get(key); + } + + /** + * Checks if a map value for `key` exists. + * + * @private + * @name has + * @memberOf MapCache + * @param {string} key The key of the entry to check. + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. + */ + function mapCacheHas(key) { + return getMapData(this, key).has(key); + } + + /** + * Sets the map `key` to `value`. + * + * @private + * @name set + * @memberOf MapCache + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the map cache instance. + */ + function mapCacheSet(key, value) { + var data = getMapData(this, key), + size = data.size; + + data.set(key, value); + this.size += data.size == size ? 0 : 1; + return this; + } + + // Add methods to `MapCache`. + MapCache.prototype.clear = mapCacheClear; + MapCache.prototype['delete'] = mapCacheDelete; + MapCache.prototype.get = mapCacheGet; + MapCache.prototype.has = mapCacheHas; + MapCache.prototype.set = mapCacheSet; + + /*------------------------------------------------------------------------*/ + + /** + * + * Creates an array cache object to store unique values. + * + * @private + * @constructor + * @param {Array} [values] The values to cache. + */ + function SetCache(values) { + var index = -1, + length = values == null ? 0 : values.length; + + this.__data__ = new MapCache; + while (++index < length) { + this.add(values[index]); + } + } + + /** + * Adds `value` to the array cache. + * + * @private + * @name add + * @memberOf SetCache + * @alias push + * @param {*} value The value to cache. + * @returns {Object} Returns the cache instance. + */ + function setCacheAdd(value) { + this.__data__.set(value, HASH_UNDEFINED); + return this; + } + + /** + * Checks if `value` is in the array cache. + * + * @private + * @name has + * @memberOf SetCache + * @param {*} value The value to search for. + * @returns {number} Returns `true` if `value` is found, else `false`. + */ + function setCacheHas(value) { + return this.__data__.has(value); + } + + // Add methods to `SetCache`. + SetCache.prototype.add = SetCache.prototype.push = setCacheAdd; + SetCache.prototype.has = setCacheHas; + + /*------------------------------------------------------------------------*/ + + /** + * Creates a stack cache object to store key-value pairs. + * + * @private + * @constructor + * @param {Array} [entries] The key-value pairs to cache. + */ + function Stack(entries) { + var data = this.__data__ = new ListCache(entries); + this.size = data.size; + } + + /** + * Removes all key-value entries from the stack. + * + * @private + * @name clear + * @memberOf Stack + */ + function stackClear() { + this.__data__ = new ListCache; + this.size = 0; + } + + /** + * Removes `key` and its value from the stack. + * + * @private + * @name delete + * @memberOf Stack + * @param {string} key The key of the value to remove. + * @returns {boolean} Returns `true` if the entry was removed, else `false`. + */ + function stackDelete(key) { + var data = this.__data__, + result = data['delete'](key); + + this.size = data.size; + return result; + } + + /** + * Gets the stack value for `key`. + * + * @private + * @name get + * @memberOf Stack + * @param {string} key The key of the value to get. + * @returns {*} Returns the entry value. + */ + function stackGet(key) { + return this.__data__.get(key); + } + + /** + * Checks if a stack value for `key` exists. + * + * @private + * @name has + * @memberOf Stack + * @param {string} key The key of the entry to check. + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. + */ + function stackHas(key) { + return this.__data__.has(key); + } + + /** + * Sets the stack `key` to `value`. + * + * @private + * @name set + * @memberOf Stack + * @param {string} key The key of the value to set. + * @param {*} value The value to set. + * @returns {Object} Returns the stack cache instance. + */ + function stackSet(key, value) { + var data = this.__data__; + if (data instanceof ListCache) { + var pairs = data.__data__; + if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) { + pairs.push([key, value]); + this.size = ++data.size; + return this; + } + data = this.__data__ = new MapCache(pairs); + } + data.set(key, value); + this.size = data.size; + return this; + } + + // Add methods to `Stack`. + Stack.prototype.clear = stackClear; + Stack.prototype['delete'] = stackDelete; + Stack.prototype.get = stackGet; + Stack.prototype.has = stackHas; + Stack.prototype.set = stackSet; + + /*------------------------------------------------------------------------*/ + + /** + * Creates an array of the enumerable property names of the array-like `value`. + * + * @private + * @param {*} value The value to query. + * @param {boolean} inherited Specify returning inherited property names. + * @returns {Array} Returns the array of property names. + */ + function arrayLikeKeys(value, inherited) { + var isArr = isArray(value), + isArg = !isArr && isArguments(value), + isBuff = !isArr && !isArg && isBuffer(value), + isType = !isArr && !isArg && !isBuff && isTypedArray(value), + skipIndexes = isArr || isArg || isBuff || isType, + result = skipIndexes ? baseTimes(value.length, String) : [], + length = result.length; + + for (var key in value) { + if ((inherited || hasOwnProperty.call(value, key)) && + !(skipIndexes && ( + // Safari 9 has enumerable `arguments.length` in strict mode. + key == 'length' || + // Node.js 0.10 has enumerable non-index properties on buffers. + (isBuff && (key == 'offset' || key == 'parent')) || + // PhantomJS 2 has enumerable non-index properties on typed arrays. + (isType && (key == 'buffer' || key == 'byteLength' || key == 'byteOffset')) || + // Skip index properties. + isIndex(key, length) + ))) { + result.push(key); + } + } + return result; + } + + /** + * A specialized version of `_.sample` for arrays. + * + * @private + * @param {Array} array The array to sample. + * @returns {*} Returns the random element. + */ + function arraySample(array) { + var length = array.length; + return length ? array[baseRandom(0, length - 1)] : undefined; + } + + /** + * A specialized version of `_.sampleSize` for arrays. + * + * @private + * @param {Array} array The array to sample. + * @param {number} n The number of elements to sample. + * @returns {Array} Returns the random elements. + */ + function arraySampleSize(array, n) { + return shuffleSelf(copyArray(array), baseClamp(n, 0, array.length)); + } + + /** + * A specialized version of `_.shuffle` for arrays. + * + * @private + * @param {Array} array The array to shuffle. + * @returns {Array} Returns the new shuffled array. + */ + function arrayShuffle(array) { + return shuffleSelf(copyArray(array)); + } + + /** + * This function is like `assignValue` except that it doesn't assign + * `undefined` values. + * + * @private + * @param {Object} object The object to modify. + * @param {string} key The key of the property to assign. + * @param {*} value The value to assign. + */ + function assignMergeValue(object, key, value) { + if ((value !== undefined && !eq(object[key], value)) || + (value === undefined && !(key in object))) { + baseAssignValue(object, key, value); + } + } + + /** + * Assigns `value` to `key` of `object` if the existing value is not equivalent + * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. + * + * @private + * @param {Object} object The object to modify. + * @param {string} key The key of the property to assign. + * @param {*} value The value to assign. + */ + function assignValue(object, key, value) { + var objValue = object[key]; + if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) || + (value === undefined && !(key in object))) { + baseAssignValue(object, key, value); + } + } + + /** + * Gets the index at which the `key` is found in `array` of key-value pairs. + * + * @private + * @param {Array} array The array to inspect. + * @param {*} key The key to search for. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function assocIndexOf(array, key) { + var length = array.length; + while (length--) { + if (eq(array[length][0], key)) { + return length; + } + } + return -1; + } + + /** + * Aggregates elements of `collection` on `accumulator` with keys transformed + * by `iteratee` and values set by `setter`. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} setter The function to set `accumulator` values. + * @param {Function} iteratee The iteratee to transform keys. + * @param {Object} accumulator The initial aggregated object. + * @returns {Function} Returns `accumulator`. + */ + function baseAggregator(collection, setter, iteratee, accumulator) { + baseEach(collection, function(value, key, collection) { + setter(accumulator, value, iteratee(value), collection); + }); + return accumulator; + } + + /** + * The base implementation of `_.assign` without support for multiple sources + * or `customizer` functions. + * + * @private + * @param {Object} object The destination object. + * @param {Object} source The source object. + * @returns {Object} Returns `object`. + */ + function baseAssign(object, source) { + return object && copyObject(source, keys(source), object); + } + + /** + * The base implementation of `_.assignIn` without support for multiple sources + * or `customizer` functions. + * + * @private + * @param {Object} object The destination object. + * @param {Object} source The source object. + * @returns {Object} Returns `object`. + */ + function baseAssignIn(object, source) { + return object && copyObject(source, keysIn(source), object); + } + + /** + * The base implementation of `assignValue` and `assignMergeValue` without + * value checks. + * + * @private + * @param {Object} object The object to modify. + * @param {string} key The key of the property to assign. + * @param {*} value The value to assign. + */ + function baseAssignValue(object, key, value) { + if (key == '__proto__' && defineProperty) { + defineProperty(object, key, { + 'configurable': true, + 'enumerable': true, + 'value': value, + 'writable': true + }); + } else { + object[key] = value; + } + } + + /** + * The base implementation of `_.at` without support for individual paths. + * + * @private + * @param {Object} object The object to iterate over. + * @param {string[]} paths The property paths to pick. + * @returns {Array} Returns the picked elements. + */ + function baseAt(object, paths) { + var index = -1, + length = paths.length, + result = Array(length), + skip = object == null; + + while (++index < length) { + result[index] = skip ? undefined : get(object, paths[index]); + } + return result; + } + + /** + * The base implementation of `_.clamp` which doesn't coerce arguments. + * + * @private + * @param {number} number The number to clamp. + * @param {number} [lower] The lower bound. + * @param {number} upper The upper bound. + * @returns {number} Returns the clamped number. + */ + function baseClamp(number, lower, upper) { + if (number === number) { + if (upper !== undefined) { + number = number <= upper ? number : upper; + } + if (lower !== undefined) { + number = number >= lower ? number : lower; + } + } + return number; + } + + /** + * The base implementation of `_.clone` and `_.cloneDeep` which tracks + * traversed objects. + * + * @private + * @param {*} value The value to clone. + * @param {boolean} bitmask The bitmask flags. + * 1 - Deep clone + * 2 - Flatten inherited properties + * 4 - Clone symbols + * @param {Function} [customizer] The function to customize cloning. + * @param {string} [key] The key of `value`. + * @param {Object} [object] The parent object of `value`. + * @param {Object} [stack] Tracks traversed objects and their clone counterparts. + * @returns {*} Returns the cloned value. + */ + function baseClone(value, bitmask, customizer, key, object, stack) { + var result, + isDeep = bitmask & CLONE_DEEP_FLAG, + isFlat = bitmask & CLONE_FLAT_FLAG, + isFull = bitmask & CLONE_SYMBOLS_FLAG; + + if (customizer) { + result = object ? customizer(value, key, object, stack) : customizer(value); + } + if (result !== undefined) { + return result; + } + if (!isObject(value)) { + return value; + } + var isArr = isArray(value); + if (isArr) { + result = initCloneArray(value); + if (!isDeep) { + return copyArray(value, result); + } + } else { + var tag = getTag(value), + isFunc = tag == funcTag || tag == genTag; + + if (isBuffer(value)) { + return cloneBuffer(value, isDeep); + } + if (tag == objectTag || tag == argsTag || (isFunc && !object)) { + result = (isFlat || isFunc) ? {} : initCloneObject(value); + if (!isDeep) { + return isFlat + ? copySymbolsIn(value, baseAssignIn(result, value)) + : copySymbols(value, baseAssign(result, value)); + } + } else { + if (!cloneableTags[tag]) { + return object ? value : {}; + } + result = initCloneByTag(value, tag, isDeep); + } + } + // Check for circular references and return its corresponding clone. + stack || (stack = new Stack); + var stacked = stack.get(value); + if (stacked) { + return stacked; + } + stack.set(value, result); + + if (isSet(value)) { + value.forEach(function(subValue) { + result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack)); + }); + } else if (isMap(value)) { + value.forEach(function(subValue, key) { + result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack)); + }); + } + + var keysFunc = isFull + ? (isFlat ? getAllKeysIn : getAllKeys) + : (isFlat ? keysIn : keys); + + var props = isArr ? undefined : keysFunc(value); + arrayEach(props || value, function(subValue, key) { + if (props) { + key = subValue; + subValue = value[key]; + } + // Recursively populate clone (susceptible to call stack limits). + assignValue(result, key, baseClone(subValue, bitmask, customizer, key, value, stack)); + }); + return result; + } + + /** + * The base implementation of `_.conforms` which doesn't clone `source`. + * + * @private + * @param {Object} source The object of property predicates to conform to. + * @returns {Function} Returns the new spec function. + */ + function baseConforms(source) { + var props = keys(source); + return function(object) { + return baseConformsTo(object, source, props); + }; + } + + /** + * The base implementation of `_.conformsTo` which accepts `props` to check. + * + * @private + * @param {Object} object The object to inspect. + * @param {Object} source The object of property predicates to conform to. + * @returns {boolean} Returns `true` if `object` conforms, else `false`. + */ + function baseConformsTo(object, source, props) { + var length = props.length; + if (object == null) { + return !length; + } + object = Object(object); + while (length--) { + var key = props[length], + predicate = source[key], + value = object[key]; + + if ((value === undefined && !(key in object)) || !predicate(value)) { + return false; + } + } + return true; + } + + /** + * The base implementation of `_.delay` and `_.defer` which accepts `args` + * to provide to `func`. + * + * @private + * @param {Function} func The function to delay. + * @param {number} wait The number of milliseconds to delay invocation. + * @param {Array} args The arguments to provide to `func`. + * @returns {number|Object} Returns the timer id or timeout object. + */ + function baseDelay(func, wait, args) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + return setTimeout(function() { func.apply(undefined, args); }, wait); + } + + /** + * The base implementation of methods like `_.difference` without support + * for excluding multiple arrays or iteratee shorthands. + * + * @private + * @param {Array} array The array to inspect. + * @param {Array} values The values to exclude. + * @param {Function} [iteratee] The iteratee invoked per element. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of filtered values. + */ + function baseDifference(array, values, iteratee, comparator) { + var index = -1, + includes = arrayIncludes, + isCommon = true, + length = array.length, + result = [], + valuesLength = values.length; + + if (!length) { + return result; + } + if (iteratee) { + values = arrayMap(values, baseUnary(iteratee)); + } + if (comparator) { + includes = arrayIncludesWith; + isCommon = false; + } + else if (values.length >= LARGE_ARRAY_SIZE) { + includes = cacheHas; + isCommon = false; + values = new SetCache(values); + } + outer: + while (++index < length) { + var value = array[index], + computed = iteratee == null ? value : iteratee(value); + + value = (comparator || value !== 0) ? value : 0; + if (isCommon && computed === computed) { + var valuesIndex = valuesLength; + while (valuesIndex--) { + if (values[valuesIndex] === computed) { + continue outer; + } + } + result.push(value); + } + else if (!includes(values, computed, comparator)) { + result.push(value); + } + } + return result; + } + + /** + * The base implementation of `_.forEach` without support for iteratee shorthands. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array|Object} Returns `collection`. + */ + var baseEach = createBaseEach(baseForOwn); + + /** + * The base implementation of `_.forEachRight` without support for iteratee shorthands. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array|Object} Returns `collection`. + */ + var baseEachRight = createBaseEach(baseForOwnRight, true); + + /** + * The base implementation of `_.every` without support for iteratee shorthands. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {boolean} Returns `true` if all elements pass the predicate check, + * else `false` + */ + function baseEvery(collection, predicate) { + var result = true; + baseEach(collection, function(value, index, collection) { + result = !!predicate(value, index, collection); + return result; + }); + return result; + } + + /** + * The base implementation of methods like `_.max` and `_.min` which accepts a + * `comparator` to determine the extremum value. + * + * @private + * @param {Array} array The array to iterate over. + * @param {Function} iteratee The iteratee invoked per iteration. + * @param {Function} comparator The comparator used to compare values. + * @returns {*} Returns the extremum value. + */ + function baseExtremum(array, iteratee, comparator) { + var index = -1, + length = array.length; + + while (++index < length) { + var value = array[index], + current = iteratee(value); + + if (current != null && (computed === undefined + ? (current === current && !isSymbol(current)) + : comparator(current, computed) + )) { + var computed = current, + result = value; + } + } + return result; + } + + /** + * The base implementation of `_.fill` without an iteratee call guard. + * + * @private + * @param {Array} array The array to fill. + * @param {*} value The value to fill `array` with. + * @param {number} [start=0] The start position. + * @param {number} [end=array.length] The end position. + * @returns {Array} Returns `array`. + */ + function baseFill(array, value, start, end) { + var length = array.length; + + start = toInteger(start); + if (start < 0) { + start = -start > length ? 0 : (length + start); + } + end = (end === undefined || end > length) ? length : toInteger(end); + if (end < 0) { + end += length; + } + end = start > end ? 0 : toLength(end); + while (start < end) { + array[start++] = value; + } + return array; + } + + /** + * The base implementation of `_.filter` without support for iteratee shorthands. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {Array} Returns the new filtered array. + */ + function baseFilter(collection, predicate) { + var result = []; + baseEach(collection, function(value, index, collection) { + if (predicate(value, index, collection)) { + result.push(value); + } + }); + return result; + } + + /** + * The base implementation of `_.flatten` with support for restricting flattening. + * + * @private + * @param {Array} array The array to flatten. + * @param {number} depth The maximum recursion depth. + * @param {boolean} [predicate=isFlattenable] The function invoked per iteration. + * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks. + * @param {Array} [result=[]] The initial result value. + * @returns {Array} Returns the new flattened array. + */ + function baseFlatten(array, depth, predicate, isStrict, result) { + var index = -1, + length = array.length; + + predicate || (predicate = isFlattenable); + result || (result = []); + + while (++index < length) { + var value = array[index]; + if (depth > 0 && predicate(value)) { + if (depth > 1) { + // Recursively flatten arrays (susceptible to call stack limits). + baseFlatten(value, depth - 1, predicate, isStrict, result); + } else { + arrayPush(result, value); + } + } else if (!isStrict) { + result[result.length] = value; + } + } + return result; + } + + /** + * The base implementation of `baseForOwn` which iterates over `object` + * properties returned by `keysFunc` and invokes `iteratee` for each property. + * Iteratee functions may exit iteration early by explicitly returning `false`. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @param {Function} keysFunc The function to get the keys of `object`. + * @returns {Object} Returns `object`. + */ + var baseFor = createBaseFor(); + + /** + * This function is like `baseFor` except that it iterates over properties + * in the opposite order. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @param {Function} keysFunc The function to get the keys of `object`. + * @returns {Object} Returns `object`. + */ + var baseForRight = createBaseFor(true); + + /** + * The base implementation of `_.forOwn` without support for iteratee shorthands. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Object} Returns `object`. + */ + function baseForOwn(object, iteratee) { + return object && baseFor(object, iteratee, keys); + } + + /** + * The base implementation of `_.forOwnRight` without support for iteratee shorthands. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Object} Returns `object`. + */ + function baseForOwnRight(object, iteratee) { + return object && baseForRight(object, iteratee, keys); + } + + /** + * The base implementation of `_.functions` which creates an array of + * `object` function property names filtered from `props`. + * + * @private + * @param {Object} object The object to inspect. + * @param {Array} props The property names to filter. + * @returns {Array} Returns the function names. + */ + function baseFunctions(object, props) { + return arrayFilter(props, function(key) { + return isFunction(object[key]); + }); + } + + /** + * The base implementation of `_.get` without support for default values. + * + * @private + * @param {Object} object The object to query. + * @param {Array|string} path The path of the property to get. + * @returns {*} Returns the resolved value. + */ + function baseGet(object, path) { + path = castPath(path, object); + + var index = 0, + length = path.length; + + while (object != null && index < length) { + object = object[toKey(path[index++])]; + } + return (index && index == length) ? object : undefined; + } + + /** + * The base implementation of `getAllKeys` and `getAllKeysIn` which uses + * `keysFunc` and `symbolsFunc` to get the enumerable property names and + * symbols of `object`. + * + * @private + * @param {Object} object The object to query. + * @param {Function} keysFunc The function to get the keys of `object`. + * @param {Function} symbolsFunc The function to get the symbols of `object`. + * @returns {Array} Returns the array of property names and symbols. + */ + function baseGetAllKeys(object, keysFunc, symbolsFunc) { + var result = keysFunc(object); + return isArray(object) ? result : arrayPush(result, symbolsFunc(object)); + } + + /** + * The base implementation of `getTag` without fallbacks for buggy environments. + * + * @private + * @param {*} value The value to query. + * @returns {string} Returns the `toStringTag`. + */ + function baseGetTag(value) { + if (value == null) { + return value === undefined ? undefinedTag : nullTag; + } + return (symToStringTag && symToStringTag in Object(value)) + ? getRawTag(value) + : objectToString(value); + } + + /** + * The base implementation of `_.gt` which doesn't coerce arguments. + * + * @private + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is greater than `other`, + * else `false`. + */ + function baseGt(value, other) { + return value > other; + } + + /** + * The base implementation of `_.has` without support for deep paths. + * + * @private + * @param {Object} [object] The object to query. + * @param {Array|string} key The key to check. + * @returns {boolean} Returns `true` if `key` exists, else `false`. + */ + function baseHas(object, key) { + return object != null && hasOwnProperty.call(object, key); + } + + /** + * The base implementation of `_.hasIn` without support for deep paths. + * + * @private + * @param {Object} [object] The object to query. + * @param {Array|string} key The key to check. + * @returns {boolean} Returns `true` if `key` exists, else `false`. + */ + function baseHasIn(object, key) { + return object != null && key in Object(object); + } + + /** + * The base implementation of `_.inRange` which doesn't coerce arguments. + * + * @private + * @param {number} number The number to check. + * @param {number} start The start of the range. + * @param {number} end The end of the range. + * @returns {boolean} Returns `true` if `number` is in the range, else `false`. + */ + function baseInRange(number, start, end) { + return number >= nativeMin(start, end) && number < nativeMax(start, end); + } + + /** + * The base implementation of methods like `_.intersection`, without support + * for iteratee shorthands, that accepts an array of arrays to inspect. + * + * @private + * @param {Array} arrays The arrays to inspect. + * @param {Function} [iteratee] The iteratee invoked per element. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of shared values. + */ + function baseIntersection(arrays, iteratee, comparator) { + var includes = comparator ? arrayIncludesWith : arrayIncludes, + length = arrays[0].length, + othLength = arrays.length, + othIndex = othLength, + caches = Array(othLength), + maxLength = Infinity, + result = []; + + while (othIndex--) { + var array = arrays[othIndex]; + if (othIndex && iteratee) { + array = arrayMap(array, baseUnary(iteratee)); + } + maxLength = nativeMin(array.length, maxLength); + caches[othIndex] = !comparator && (iteratee || (length >= 120 && array.length >= 120)) + ? new SetCache(othIndex && array) + : undefined; + } + array = arrays[0]; + + var index = -1, + seen = caches[0]; + + outer: + while (++index < length && result.length < maxLength) { + var value = array[index], + computed = iteratee ? iteratee(value) : value; + + value = (comparator || value !== 0) ? value : 0; + if (!(seen + ? cacheHas(seen, computed) + : includes(result, computed, comparator) + )) { + othIndex = othLength; + while (--othIndex) { + var cache = caches[othIndex]; + if (!(cache + ? cacheHas(cache, computed) + : includes(arrays[othIndex], computed, comparator)) + ) { + continue outer; + } + } + if (seen) { + seen.push(computed); + } + result.push(value); + } + } + return result; + } + + /** + * The base implementation of `_.invert` and `_.invertBy` which inverts + * `object` with values transformed by `iteratee` and set by `setter`. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} setter The function to set `accumulator` values. + * @param {Function} iteratee The iteratee to transform values. + * @param {Object} accumulator The initial inverted object. + * @returns {Function} Returns `accumulator`. + */ + function baseInverter(object, setter, iteratee, accumulator) { + baseForOwn(object, function(value, key, object) { + setter(accumulator, iteratee(value), key, object); + }); + return accumulator; + } + + /** + * The base implementation of `_.invoke` without support for individual + * method arguments. + * + * @private + * @param {Object} object The object to query. + * @param {Array|string} path The path of the method to invoke. + * @param {Array} args The arguments to invoke the method with. + * @returns {*} Returns the result of the invoked method. + */ + function baseInvoke(object, path, args) { + path = castPath(path, object); + object = parent(object, path); + var func = object == null ? object : object[toKey(last(path))]; + return func == null ? undefined : apply(func, object, args); + } + + /** + * The base implementation of `_.isArguments`. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an `arguments` object, + */ + function baseIsArguments(value) { + return isObjectLike(value) && baseGetTag(value) == argsTag; + } + + /** + * The base implementation of `_.isArrayBuffer` without Node.js optimizations. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an array buffer, else `false`. + */ + function baseIsArrayBuffer(value) { + return isObjectLike(value) && baseGetTag(value) == arrayBufferTag; + } + + /** + * The base implementation of `_.isDate` without Node.js optimizations. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a date object, else `false`. + */ + function baseIsDate(value) { + return isObjectLike(value) && baseGetTag(value) == dateTag; + } + + /** + * The base implementation of `_.isEqual` which supports partial comparisons + * and tracks traversed objects. + * + * @private + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @param {boolean} bitmask The bitmask flags. + * 1 - Unordered comparison + * 2 - Partial comparison + * @param {Function} [customizer] The function to customize comparisons. + * @param {Object} [stack] Tracks traversed `value` and `other` objects. + * @returns {boolean} Returns `true` if the values are equivalent, else `false`. + */ + function baseIsEqual(value, other, bitmask, customizer, stack) { + if (value === other) { + return true; + } + if (value == null || other == null || (!isObjectLike(value) && !isObjectLike(other))) { + return value !== value && other !== other; + } + return baseIsEqualDeep(value, other, bitmask, customizer, baseIsEqual, stack); + } + + /** + * A specialized version of `baseIsEqual` for arrays and objects which performs + * deep comparisons and tracks traversed objects enabling objects with circular + * references to be compared. + * + * @private + * @param {Object} object The object to compare. + * @param {Object} other The other object to compare. + * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details. + * @param {Function} customizer The function to customize comparisons. + * @param {Function} equalFunc The function to determine equivalents of values. + * @param {Object} [stack] Tracks traversed `object` and `other` objects. + * @returns {boolean} Returns `true` if the objects are equivalent, else `false`. + */ + function baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) { + var objIsArr = isArray(object), + othIsArr = isArray(other), + objTag = objIsArr ? arrayTag : getTag(object), + othTag = othIsArr ? arrayTag : getTag(other); + + objTag = objTag == argsTag ? objectTag : objTag; + othTag = othTag == argsTag ? objectTag : othTag; + + var objIsObj = objTag == objectTag, + othIsObj = othTag == objectTag, + isSameTag = objTag == othTag; + + if (isSameTag && isBuffer(object)) { + if (!isBuffer(other)) { + return false; + } + objIsArr = true; + objIsObj = false; + } + if (isSameTag && !objIsObj) { + stack || (stack = new Stack); + return (objIsArr || isTypedArray(object)) + ? equalArrays(object, other, bitmask, customizer, equalFunc, stack) + : equalByTag(object, other, objTag, bitmask, customizer, equalFunc, stack); + } + if (!(bitmask & COMPARE_PARTIAL_FLAG)) { + var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'), + othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__'); + + if (objIsWrapped || othIsWrapped) { + var objUnwrapped = objIsWrapped ? object.value() : object, + othUnwrapped = othIsWrapped ? other.value() : other; + + stack || (stack = new Stack); + return equalFunc(objUnwrapped, othUnwrapped, bitmask, customizer, stack); + } + } + if (!isSameTag) { + return false; + } + stack || (stack = new Stack); + return equalObjects(object, other, bitmask, customizer, equalFunc, stack); + } + + /** + * The base implementation of `_.isMap` without Node.js optimizations. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a map, else `false`. + */ + function baseIsMap(value) { + return isObjectLike(value) && getTag(value) == mapTag; + } + + /** + * The base implementation of `_.isMatch` without support for iteratee shorthands. + * + * @private + * @param {Object} object The object to inspect. + * @param {Object} source The object of property values to match. + * @param {Array} matchData The property names, values, and compare flags to match. + * @param {Function} [customizer] The function to customize comparisons. + * @returns {boolean} Returns `true` if `object` is a match, else `false`. + */ + function baseIsMatch(object, source, matchData, customizer) { + var index = matchData.length, + length = index, + noCustomizer = !customizer; + + if (object == null) { + return !length; + } + object = Object(object); + while (index--) { + var data = matchData[index]; + if ((noCustomizer && data[2]) + ? data[1] !== object[data[0]] + : !(data[0] in object) + ) { + return false; + } + } + while (++index < length) { + data = matchData[index]; + var key = data[0], + objValue = object[key], + srcValue = data[1]; + + if (noCustomizer && data[2]) { + if (objValue === undefined && !(key in object)) { + return false; + } + } else { + var stack = new Stack; + if (customizer) { + var result = customizer(objValue, srcValue, key, object, source, stack); + } + if (!(result === undefined + ? baseIsEqual(srcValue, objValue, COMPARE_PARTIAL_FLAG | COMPARE_UNORDERED_FLAG, customizer, stack) + : result + )) { + return false; + } + } + } + return true; + } + + /** + * The base implementation of `_.isNative` without bad shim checks. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a native function, + * else `false`. + */ + function baseIsNative(value) { + if (!isObject(value) || isMasked(value)) { + return false; + } + var pattern = isFunction(value) ? reIsNative : reIsHostCtor; + return pattern.test(toSource(value)); + } + + /** + * The base implementation of `_.isRegExp` without Node.js optimizations. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a regexp, else `false`. + */ + function baseIsRegExp(value) { + return isObjectLike(value) && baseGetTag(value) == regexpTag; + } + + /** + * The base implementation of `_.isSet` without Node.js optimizations. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a set, else `false`. + */ + function baseIsSet(value) { + return isObjectLike(value) && getTag(value) == setTag; + } + + /** + * The base implementation of `_.isTypedArray` without Node.js optimizations. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a typed array, else `false`. + */ + function baseIsTypedArray(value) { + return isObjectLike(value) && + isLength(value.length) && !!typedArrayTags[baseGetTag(value)]; + } + + /** + * The base implementation of `_.iteratee`. + * + * @private + * @param {*} [value=_.identity] The value to convert to an iteratee. + * @returns {Function} Returns the iteratee. + */ + function baseIteratee(value) { + // Don't store the `typeof` result in a variable to avoid a JIT bug in Safari 9. + // See https://bugs.webkit.org/show_bug.cgi?id=156034 for more details. + if (typeof value == 'function') { + return value; + } + if (value == null) { + return identity; + } + if (typeof value == 'object') { + return isArray(value) + ? baseMatchesProperty(value[0], value[1]) + : baseMatches(value); + } + return property(value); + } + + /** + * The base implementation of `_.keys` which doesn't treat sparse arrays as dense. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. + */ + function baseKeys(object) { + if (!isPrototype(object)) { + return nativeKeys(object); + } + var result = []; + for (var key in Object(object)) { + if (hasOwnProperty.call(object, key) && key != 'constructor') { + result.push(key); + } + } + return result; + } + + /** + * The base implementation of `_.keysIn` which doesn't treat sparse arrays as dense. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. + */ + function baseKeysIn(object) { + if (!isObject(object)) { + return nativeKeysIn(object); + } + var isProto = isPrototype(object), + result = []; + + for (var key in object) { + if (!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) { + result.push(key); + } + } + return result; + } + + /** + * The base implementation of `_.lt` which doesn't coerce arguments. + * + * @private + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is less than `other`, + * else `false`. + */ + function baseLt(value, other) { + return value < other; + } + + /** + * The base implementation of `_.map` without support for iteratee shorthands. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns the new mapped array. + */ + function baseMap(collection, iteratee) { + var index = -1, + result = isArrayLike(collection) ? Array(collection.length) : []; + + baseEach(collection, function(value, key, collection) { + result[++index] = iteratee(value, key, collection); + }); + return result; + } + + /** + * The base implementation of `_.matches` which doesn't clone `source`. + * + * @private + * @param {Object} source The object of property values to match. + * @returns {Function} Returns the new spec function. + */ + function baseMatches(source) { + var matchData = getMatchData(source); + if (matchData.length == 1 && matchData[0][2]) { + return matchesStrictComparable(matchData[0][0], matchData[0][1]); + } + return function(object) { + return object === source || baseIsMatch(object, source, matchData); + }; + } + + /** + * The base implementation of `_.matchesProperty` which doesn't clone `srcValue`. + * + * @private + * @param {string} path The path of the property to get. + * @param {*} srcValue The value to match. + * @returns {Function} Returns the new spec function. + */ + function baseMatchesProperty(path, srcValue) { + if (isKey(path) && isStrictComparable(srcValue)) { + return matchesStrictComparable(toKey(path), srcValue); + } + return function(object) { + var objValue = get(object, path); + return (objValue === undefined && objValue === srcValue) + ? hasIn(object, path) + : baseIsEqual(srcValue, objValue, COMPARE_PARTIAL_FLAG | COMPARE_UNORDERED_FLAG); + }; + } + + /** + * The base implementation of `_.merge` without support for multiple sources. + * + * @private + * @param {Object} object The destination object. + * @param {Object} source The source object. + * @param {number} srcIndex The index of `source`. + * @param {Function} [customizer] The function to customize merged values. + * @param {Object} [stack] Tracks traversed source values and their merged + * counterparts. + */ + function baseMerge(object, source, srcIndex, customizer, stack) { + if (object === source) { + return; + } + baseFor(source, function(srcValue, key) { + stack || (stack = new Stack); + if (isObject(srcValue)) { + baseMergeDeep(object, source, key, srcIndex, baseMerge, customizer, stack); + } + else { + var newValue = customizer + ? customizer(safeGet(object, key), srcValue, (key + ''), object, source, stack) + : undefined; + + if (newValue === undefined) { + newValue = srcValue; + } + assignMergeValue(object, key, newValue); + } + }, keysIn); + } + + /** + * A specialized version of `baseMerge` for arrays and objects which performs + * deep merges and tracks traversed objects enabling objects with circular + * references to be merged. + * + * @private + * @param {Object} object The destination object. + * @param {Object} source The source object. + * @param {string} key The key of the value to merge. + * @param {number} srcIndex The index of `source`. + * @param {Function} mergeFunc The function to merge values. + * @param {Function} [customizer] The function to customize assigned values. + * @param {Object} [stack] Tracks traversed source values and their merged + * counterparts. + */ + function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) { + var objValue = safeGet(object, key), + srcValue = safeGet(source, key), + stacked = stack.get(srcValue); + + if (stacked) { + assignMergeValue(object, key, stacked); + return; + } + var newValue = customizer + ? customizer(objValue, srcValue, (key + ''), object, source, stack) + : undefined; + + var isCommon = newValue === undefined; + + if (isCommon) { + var isArr = isArray(srcValue), + isBuff = !isArr && isBuffer(srcValue), + isTyped = !isArr && !isBuff && isTypedArray(srcValue); + + newValue = srcValue; + if (isArr || isBuff || isTyped) { + if (isArray(objValue)) { + newValue = objValue; + } + else if (isArrayLikeObject(objValue)) { + newValue = copyArray(objValue); + } + else if (isBuff) { + isCommon = false; + newValue = cloneBuffer(srcValue, true); + } + else if (isTyped) { + isCommon = false; + newValue = cloneTypedArray(srcValue, true); + } + else { + newValue = []; + } + } + else if (isPlainObject(srcValue) || isArguments(srcValue)) { + newValue = objValue; + if (isArguments(objValue)) { + newValue = toPlainObject(objValue); + } + else if (!isObject(objValue) || isFunction(objValue)) { + newValue = initCloneObject(srcValue); + } + } + else { + isCommon = false; + } + } + if (isCommon) { + // Recursively merge objects and arrays (susceptible to call stack limits). + stack.set(srcValue, newValue); + mergeFunc(newValue, srcValue, srcIndex, customizer, stack); + stack['delete'](srcValue); + } + assignMergeValue(object, key, newValue); + } + + /** + * The base implementation of `_.nth` which doesn't coerce arguments. + * + * @private + * @param {Array} array The array to query. + * @param {number} n The index of the element to return. + * @returns {*} Returns the nth element of `array`. + */ + function baseNth(array, n) { + var length = array.length; + if (!length) { + return; + } + n += n < 0 ? length : 0; + return isIndex(n, length) ? array[n] : undefined; + } + + /** + * The base implementation of `_.orderBy` without param guards. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by. + * @param {string[]} orders The sort orders of `iteratees`. + * @returns {Array} Returns the new sorted array. + */ + function baseOrderBy(collection, iteratees, orders) { + if (iteratees.length) { + iteratees = arrayMap(iteratees, function(iteratee) { + if (isArray(iteratee)) { + return function(value) { + return baseGet(value, iteratee.length === 1 ? iteratee[0] : iteratee); + } + } + return iteratee; + }); + } else { + iteratees = [identity]; + } + + var index = -1; + iteratees = arrayMap(iteratees, baseUnary(getIteratee())); + + var result = baseMap(collection, function(value, key, collection) { + var criteria = arrayMap(iteratees, function(iteratee) { + return iteratee(value); + }); + return { 'criteria': criteria, 'index': ++index, 'value': value }; + }); + + return baseSortBy(result, function(object, other) { + return compareMultiple(object, other, orders); + }); + } + + /** + * The base implementation of `_.pick` without support for individual + * property identifiers. + * + * @private + * @param {Object} object The source object. + * @param {string[]} paths The property paths to pick. + * @returns {Object} Returns the new object. + */ + function basePick(object, paths) { + return basePickBy(object, paths, function(value, path) { + return hasIn(object, path); + }); + } + + /** + * The base implementation of `_.pickBy` without support for iteratee shorthands. + * + * @private + * @param {Object} object The source object. + * @param {string[]} paths The property paths to pick. + * @param {Function} predicate The function invoked per property. + * @returns {Object} Returns the new object. + */ + function basePickBy(object, paths, predicate) { + var index = -1, + length = paths.length, + result = {}; + + while (++index < length) { + var path = paths[index], + value = baseGet(object, path); + + if (predicate(value, path)) { + baseSet(result, castPath(path, object), value); + } + } + return result; + } + + /** + * A specialized version of `baseProperty` which supports deep paths. + * + * @private + * @param {Array|string} path The path of the property to get. + * @returns {Function} Returns the new accessor function. + */ + function basePropertyDeep(path) { + return function(object) { + return baseGet(object, path); + }; + } + + /** + * The base implementation of `_.pullAllBy` without support for iteratee + * shorthands. + * + * @private + * @param {Array} array The array to modify. + * @param {Array} values The values to remove. + * @param {Function} [iteratee] The iteratee invoked per element. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns `array`. + */ + function basePullAll(array, values, iteratee, comparator) { + var indexOf = comparator ? baseIndexOfWith : baseIndexOf, + index = -1, + length = values.length, + seen = array; + + if (array === values) { + values = copyArray(values); + } + if (iteratee) { + seen = arrayMap(array, baseUnary(iteratee)); + } + while (++index < length) { + var fromIndex = 0, + value = values[index], + computed = iteratee ? iteratee(value) : value; + + while ((fromIndex = indexOf(seen, computed, fromIndex, comparator)) > -1) { + if (seen !== array) { + splice.call(seen, fromIndex, 1); + } + splice.call(array, fromIndex, 1); + } + } + return array; + } + + /** + * The base implementation of `_.pullAt` without support for individual + * indexes or capturing the removed elements. + * + * @private + * @param {Array} array The array to modify. + * @param {number[]} indexes The indexes of elements to remove. + * @returns {Array} Returns `array`. + */ + function basePullAt(array, indexes) { + var length = array ? indexes.length : 0, + lastIndex = length - 1; + + while (length--) { + var index = indexes[length]; + if (length == lastIndex || index !== previous) { + var previous = index; + if (isIndex(index)) { + splice.call(array, index, 1); + } else { + baseUnset(array, index); + } + } + } + return array; + } + + /** + * The base implementation of `_.random` without support for returning + * floating-point numbers. + * + * @private + * @param {number} lower The lower bound. + * @param {number} upper The upper bound. + * @returns {number} Returns the random number. + */ + function baseRandom(lower, upper) { + return lower + nativeFloor(nativeRandom() * (upper - lower + 1)); + } + + /** + * The base implementation of `_.range` and `_.rangeRight` which doesn't + * coerce arguments. + * + * @private + * @param {number} start The start of the range. + * @param {number} end The end of the range. + * @param {number} step The value to increment or decrement by. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Array} Returns the range of numbers. + */ + function baseRange(start, end, step, fromRight) { + var index = -1, + length = nativeMax(nativeCeil((end - start) / (step || 1)), 0), + result = Array(length); + + while (length--) { + result[fromRight ? length : ++index] = start; + start += step; + } + return result; + } + + /** + * The base implementation of `_.repeat` which doesn't coerce arguments. + * + * @private + * @param {string} string The string to repeat. + * @param {number} n The number of times to repeat the string. + * @returns {string} Returns the repeated string. + */ + function baseRepeat(string, n) { + var result = ''; + if (!string || n < 1 || n > MAX_SAFE_INTEGER) { + return result; + } + // Leverage the exponentiation by squaring algorithm for a faster repeat. + // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details. + do { + if (n % 2) { + result += string; + } + n = nativeFloor(n / 2); + if (n) { + string += string; + } + } while (n); + + return result; + } + + /** + * The base implementation of `_.rest` which doesn't validate or coerce arguments. + * + * @private + * @param {Function} func The function to apply a rest parameter to. + * @param {number} [start=func.length-1] The start position of the rest parameter. + * @returns {Function} Returns the new function. + */ + function baseRest(func, start) { + return setToString(overRest(func, start, identity), func + ''); + } + + /** + * The base implementation of `_.sample`. + * + * @private + * @param {Array|Object} collection The collection to sample. + * @returns {*} Returns the random element. + */ + function baseSample(collection) { + return arraySample(values(collection)); + } + + /** + * The base implementation of `_.sampleSize` without param guards. + * + * @private + * @param {Array|Object} collection The collection to sample. + * @param {number} n The number of elements to sample. + * @returns {Array} Returns the random elements. + */ + function baseSampleSize(collection, n) { + var array = values(collection); + return shuffleSelf(array, baseClamp(n, 0, array.length)); + } + + /** + * The base implementation of `_.set`. + * + * @private + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {*} value The value to set. + * @param {Function} [customizer] The function to customize path creation. + * @returns {Object} Returns `object`. + */ + function baseSet(object, path, value, customizer) { + if (!isObject(object)) { + return object; + } + path = castPath(path, object); + + var index = -1, + length = path.length, + lastIndex = length - 1, + nested = object; + + while (nested != null && ++index < length) { + var key = toKey(path[index]), + newValue = value; + + if (key === '__proto__' || key === 'constructor' || key === 'prototype') { + return object; + } + + if (index != lastIndex) { + var objValue = nested[key]; + newValue = customizer ? customizer(objValue, key, nested) : undefined; + if (newValue === undefined) { + newValue = isObject(objValue) + ? objValue + : (isIndex(path[index + 1]) ? [] : {}); + } + } + assignValue(nested, key, newValue); + nested = nested[key]; + } + return object; + } + + /** + * The base implementation of `setData` without support for hot loop shorting. + * + * @private + * @param {Function} func The function to associate metadata with. + * @param {*} data The metadata. + * @returns {Function} Returns `func`. + */ + var baseSetData = !metaMap ? identity : function(func, data) { + metaMap.set(func, data); + return func; + }; + + /** + * The base implementation of `setToString` without support for hot loop shorting. + * + * @private + * @param {Function} func The function to modify. + * @param {Function} string The `toString` result. + * @returns {Function} Returns `func`. + */ + var baseSetToString = !defineProperty ? identity : function(func, string) { + return defineProperty(func, 'toString', { + 'configurable': true, + 'enumerable': false, + 'value': constant(string), + 'writable': true + }); + }; + + /** + * The base implementation of `_.shuffle`. + * + * @private + * @param {Array|Object} collection The collection to shuffle. + * @returns {Array} Returns the new shuffled array. + */ + function baseShuffle(collection) { + return shuffleSelf(values(collection)); + } + + /** + * The base implementation of `_.slice` without an iteratee call guard. + * + * @private + * @param {Array} array The array to slice. + * @param {number} [start=0] The start position. + * @param {number} [end=array.length] The end position. + * @returns {Array} Returns the slice of `array`. + */ + function baseSlice(array, start, end) { + var index = -1, + length = array.length; + + if (start < 0) { + start = -start > length ? 0 : (length + start); + } + end = end > length ? length : end; + if (end < 0) { + end += length; + } + length = start > end ? 0 : ((end - start) >>> 0); + start >>>= 0; + + var result = Array(length); + while (++index < length) { + result[index] = array[index + start]; + } + return result; + } + + /** + * The base implementation of `_.some` without support for iteratee shorthands. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {boolean} Returns `true` if any element passes the predicate check, + * else `false`. + */ + function baseSome(collection, predicate) { + var result; + + baseEach(collection, function(value, index, collection) { + result = predicate(value, index, collection); + return !result; + }); + return !!result; + } + + /** + * The base implementation of `_.sortedIndex` and `_.sortedLastIndex` which + * performs a binary search of `array` to determine the index at which `value` + * should be inserted into `array` in order to maintain its sort order. + * + * @private + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @param {boolean} [retHighest] Specify returning the highest qualified index. + * @returns {number} Returns the index at which `value` should be inserted + * into `array`. + */ + function baseSortedIndex(array, value, retHighest) { + var low = 0, + high = array == null ? low : array.length; + + if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) { + while (low < high) { + var mid = (low + high) >>> 1, + computed = array[mid]; + + if (computed !== null && !isSymbol(computed) && + (retHighest ? (computed <= value) : (computed < value))) { + low = mid + 1; + } else { + high = mid; + } + } + return high; + } + return baseSortedIndexBy(array, value, identity, retHighest); + } + + /** + * The base implementation of `_.sortedIndexBy` and `_.sortedLastIndexBy` + * which invokes `iteratee` for `value` and each element of `array` to compute + * their sort ranking. The iteratee is invoked with one argument; (value). + * + * @private + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @param {Function} iteratee The iteratee invoked per element. + * @param {boolean} [retHighest] Specify returning the highest qualified index. + * @returns {number} Returns the index at which `value` should be inserted + * into `array`. + */ + function baseSortedIndexBy(array, value, iteratee, retHighest) { + var low = 0, + high = array == null ? 0 : array.length; + if (high === 0) { + return 0; + } + + value = iteratee(value); + var valIsNaN = value !== value, + valIsNull = value === null, + valIsSymbol = isSymbol(value), + valIsUndefined = value === undefined; + + while (low < high) { + var mid = nativeFloor((low + high) / 2), + computed = iteratee(array[mid]), + othIsDefined = computed !== undefined, + othIsNull = computed === null, + othIsReflexive = computed === computed, + othIsSymbol = isSymbol(computed); + + if (valIsNaN) { + var setLow = retHighest || othIsReflexive; + } else if (valIsUndefined) { + setLow = othIsReflexive && (retHighest || othIsDefined); + } else if (valIsNull) { + setLow = othIsReflexive && othIsDefined && (retHighest || !othIsNull); + } else if (valIsSymbol) { + setLow = othIsReflexive && othIsDefined && !othIsNull && (retHighest || !othIsSymbol); + } else if (othIsNull || othIsSymbol) { + setLow = false; + } else { + setLow = retHighest ? (computed <= value) : (computed < value); + } + if (setLow) { + low = mid + 1; + } else { + high = mid; + } + } + return nativeMin(high, MAX_ARRAY_INDEX); + } + + /** + * The base implementation of `_.sortedUniq` and `_.sortedUniqBy` without + * support for iteratee shorthands. + * + * @private + * @param {Array} array The array to inspect. + * @param {Function} [iteratee] The iteratee invoked per element. + * @returns {Array} Returns the new duplicate free array. + */ + function baseSortedUniq(array, iteratee) { + var index = -1, + length = array.length, + resIndex = 0, + result = []; + + while (++index < length) { + var value = array[index], + computed = iteratee ? iteratee(value) : value; + + if (!index || !eq(computed, seen)) { + var seen = computed; + result[resIndex++] = value === 0 ? 0 : value; + } + } + return result; + } + + /** + * The base implementation of `_.toNumber` which doesn't ensure correct + * conversions of binary, hexadecimal, or octal string values. + * + * @private + * @param {*} value The value to process. + * @returns {number} Returns the number. + */ + function baseToNumber(value) { + if (typeof value == 'number') { + return value; + } + if (isSymbol(value)) { + return NAN; + } + return +value; + } + + /** + * The base implementation of `_.toString` which doesn't convert nullish + * values to empty strings. + * + * @private + * @param {*} value The value to process. + * @returns {string} Returns the string. + */ + function baseToString(value) { + // Exit early for strings to avoid a performance hit in some environments. + if (typeof value == 'string') { + return value; + } + if (isArray(value)) { + // Recursively convert values (susceptible to call stack limits). + return arrayMap(value, baseToString) + ''; + } + if (isSymbol(value)) { + return symbolToString ? symbolToString.call(value) : ''; + } + var result = (value + ''); + return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; + } + + /** + * The base implementation of `_.uniqBy` without support for iteratee shorthands. + * + * @private + * @param {Array} array The array to inspect. + * @param {Function} [iteratee] The iteratee invoked per element. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new duplicate free array. + */ + function baseUniq(array, iteratee, comparator) { + var index = -1, + includes = arrayIncludes, + length = array.length, + isCommon = true, + result = [], + seen = result; + + if (comparator) { + isCommon = false; + includes = arrayIncludesWith; + } + else if (length >= LARGE_ARRAY_SIZE) { + var set = iteratee ? null : createSet(array); + if (set) { + return setToArray(set); + } + isCommon = false; + includes = cacheHas; + seen = new SetCache; + } + else { + seen = iteratee ? [] : result; + } + outer: + while (++index < length) { + var value = array[index], + computed = iteratee ? iteratee(value) : value; + + value = (comparator || value !== 0) ? value : 0; + if (isCommon && computed === computed) { + var seenIndex = seen.length; + while (seenIndex--) { + if (seen[seenIndex] === computed) { + continue outer; + } + } + if (iteratee) { + seen.push(computed); + } + result.push(value); + } + else if (!includes(seen, computed, comparator)) { + if (seen !== result) { + seen.push(computed); + } + result.push(value); + } + } + return result; + } + + /** + * The base implementation of `_.unset`. + * + * @private + * @param {Object} object The object to modify. + * @param {Array|string} path The property path to unset. + * @returns {boolean} Returns `true` if the property is deleted, else `false`. + */ + function baseUnset(object, path) { + path = castPath(path, object); + object = parent(object, path); + return object == null || delete object[toKey(last(path))]; + } + + /** + * The base implementation of `_.update`. + * + * @private + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to update. + * @param {Function} updater The function to produce the updated value. + * @param {Function} [customizer] The function to customize path creation. + * @returns {Object} Returns `object`. + */ + function baseUpdate(object, path, updater, customizer) { + return baseSet(object, path, updater(baseGet(object, path)), customizer); + } + + /** + * The base implementation of methods like `_.dropWhile` and `_.takeWhile` + * without support for iteratee shorthands. + * + * @private + * @param {Array} array The array to query. + * @param {Function} predicate The function invoked per iteration. + * @param {boolean} [isDrop] Specify dropping elements instead of taking them. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Array} Returns the slice of `array`. + */ + function baseWhile(array, predicate, isDrop, fromRight) { + var length = array.length, + index = fromRight ? length : -1; + + while ((fromRight ? index-- : ++index < length) && + predicate(array[index], index, array)) {} + + return isDrop + ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length)) + : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index)); + } + + /** + * The base implementation of `wrapperValue` which returns the result of + * performing a sequence of actions on the unwrapped `value`, where each + * successive action is supplied the return value of the previous. + * + * @private + * @param {*} value The unwrapped value. + * @param {Array} actions Actions to perform to resolve the unwrapped value. + * @returns {*} Returns the resolved value. + */ + function baseWrapperValue(value, actions) { + var result = value; + if (result instanceof LazyWrapper) { + result = result.value(); + } + return arrayReduce(actions, function(result, action) { + return action.func.apply(action.thisArg, arrayPush([result], action.args)); + }, result); + } + + /** + * The base implementation of methods like `_.xor`, without support for + * iteratee shorthands, that accepts an array of arrays to inspect. + * + * @private + * @param {Array} arrays The arrays to inspect. + * @param {Function} [iteratee] The iteratee invoked per element. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of values. + */ + function baseXor(arrays, iteratee, comparator) { + var length = arrays.length; + if (length < 2) { + return length ? baseUniq(arrays[0]) : []; + } + var index = -1, + result = Array(length); + + while (++index < length) { + var array = arrays[index], + othIndex = -1; + + while (++othIndex < length) { + if (othIndex != index) { + result[index] = baseDifference(result[index] || array, arrays[othIndex], iteratee, comparator); + } + } + } + return baseUniq(baseFlatten(result, 1), iteratee, comparator); + } + + /** + * This base implementation of `_.zipObject` which assigns values using `assignFunc`. + * + * @private + * @param {Array} props The property identifiers. + * @param {Array} values The property values. + * @param {Function} assignFunc The function to assign values. + * @returns {Object} Returns the new object. + */ + function baseZipObject(props, values, assignFunc) { + var index = -1, + length = props.length, + valsLength = values.length, + result = {}; + + while (++index < length) { + var value = index < valsLength ? values[index] : undefined; + assignFunc(result, props[index], value); + } + return result; + } + + /** + * Casts `value` to an empty array if it's not an array like object. + * + * @private + * @param {*} value The value to inspect. + * @returns {Array|Object} Returns the cast array-like object. + */ + function castArrayLikeObject(value) { + return isArrayLikeObject(value) ? value : []; + } + + /** + * Casts `value` to `identity` if it's not a function. + * + * @private + * @param {*} value The value to inspect. + * @returns {Function} Returns cast function. + */ + function castFunction(value) { + return typeof value == 'function' ? value : identity; + } + + /** + * Casts `value` to a path array if it's not one. + * + * @private + * @param {*} value The value to inspect. + * @param {Object} [object] The object to query keys on. + * @returns {Array} Returns the cast property path array. + */ + function castPath(value, object) { + if (isArray(value)) { + return value; + } + return isKey(value, object) ? [value] : stringToPath(toString(value)); + } + + /** + * A `baseRest` alias which can be replaced with `identity` by module + * replacement plugins. + * + * @private + * @type {Function} + * @param {Function} func The function to apply a rest parameter to. + * @returns {Function} Returns the new function. + */ + var castRest = baseRest; + + /** + * Casts `array` to a slice if it's needed. + * + * @private + * @param {Array} array The array to inspect. + * @param {number} start The start position. + * @param {number} [end=array.length] The end position. + * @returns {Array} Returns the cast slice. + */ + function castSlice(array, start, end) { + var length = array.length; + end = end === undefined ? length : end; + return (!start && end >= length) ? array : baseSlice(array, start, end); + } + + /** + * A simple wrapper around the global [`clearTimeout`](https://mdn.io/clearTimeout). + * + * @private + * @param {number|Object} id The timer id or timeout object of the timer to clear. + */ + var clearTimeout = ctxClearTimeout || function(id) { + return root.clearTimeout(id); + }; + + /** + * Creates a clone of `buffer`. + * + * @private + * @param {Buffer} buffer The buffer to clone. + * @param {boolean} [isDeep] Specify a deep clone. + * @returns {Buffer} Returns the cloned buffer. + */ + function cloneBuffer(buffer, isDeep) { + if (isDeep) { + return buffer.slice(); + } + var length = buffer.length, + result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length); + + buffer.copy(result); + return result; + } + + /** + * Creates a clone of `arrayBuffer`. + * + * @private + * @param {ArrayBuffer} arrayBuffer The array buffer to clone. + * @returns {ArrayBuffer} Returns the cloned array buffer. + */ + function cloneArrayBuffer(arrayBuffer) { + var result = new arrayBuffer.constructor(arrayBuffer.byteLength); + new Uint8Array(result).set(new Uint8Array(arrayBuffer)); + return result; + } + + /** + * Creates a clone of `dataView`. + * + * @private + * @param {Object} dataView The data view to clone. + * @param {boolean} [isDeep] Specify a deep clone. + * @returns {Object} Returns the cloned data view. + */ + function cloneDataView(dataView, isDeep) { + var buffer = isDeep ? cloneArrayBuffer(dataView.buffer) : dataView.buffer; + return new dataView.constructor(buffer, dataView.byteOffset, dataView.byteLength); + } + + /** + * Creates a clone of `regexp`. + * + * @private + * @param {Object} regexp The regexp to clone. + * @returns {Object} Returns the cloned regexp. + */ + function cloneRegExp(regexp) { + var result = new regexp.constructor(regexp.source, reFlags.exec(regexp)); + result.lastIndex = regexp.lastIndex; + return result; + } + + /** + * Creates a clone of the `symbol` object. + * + * @private + * @param {Object} symbol The symbol object to clone. + * @returns {Object} Returns the cloned symbol object. + */ + function cloneSymbol(symbol) { + return symbolValueOf ? Object(symbolValueOf.call(symbol)) : {}; + } + + /** + * Creates a clone of `typedArray`. + * + * @private + * @param {Object} typedArray The typed array to clone. + * @param {boolean} [isDeep] Specify a deep clone. + * @returns {Object} Returns the cloned typed array. + */ + function cloneTypedArray(typedArray, isDeep) { + var buffer = isDeep ? cloneArrayBuffer(typedArray.buffer) : typedArray.buffer; + return new typedArray.constructor(buffer, typedArray.byteOffset, typedArray.length); + } + + /** + * Compares values to sort them in ascending order. + * + * @private + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {number} Returns the sort order indicator for `value`. + */ + function compareAscending(value, other) { + if (value !== other) { + var valIsDefined = value !== undefined, + valIsNull = value === null, + valIsReflexive = value === value, + valIsSymbol = isSymbol(value); + + var othIsDefined = other !== undefined, + othIsNull = other === null, + othIsReflexive = other === other, + othIsSymbol = isSymbol(other); + + if ((!othIsNull && !othIsSymbol && !valIsSymbol && value > other) || + (valIsSymbol && othIsDefined && othIsReflexive && !othIsNull && !othIsSymbol) || + (valIsNull && othIsDefined && othIsReflexive) || + (!valIsDefined && othIsReflexive) || + !valIsReflexive) { + return 1; + } + if ((!valIsNull && !valIsSymbol && !othIsSymbol && value < other) || + (othIsSymbol && valIsDefined && valIsReflexive && !valIsNull && !valIsSymbol) || + (othIsNull && valIsDefined && valIsReflexive) || + (!othIsDefined && valIsReflexive) || + !othIsReflexive) { + return -1; + } + } + return 0; + } + + /** + * Used by `_.orderBy` to compare multiple properties of a value to another + * and stable sort them. + * + * If `orders` is unspecified, all values are sorted in ascending order. Otherwise, + * specify an order of "desc" for descending or "asc" for ascending sort order + * of corresponding values. + * + * @private + * @param {Object} object The object to compare. + * @param {Object} other The other object to compare. + * @param {boolean[]|string[]} orders The order to sort by for each property. + * @returns {number} Returns the sort order indicator for `object`. + */ + function compareMultiple(object, other, orders) { + var index = -1, + objCriteria = object.criteria, + othCriteria = other.criteria, + length = objCriteria.length, + ordersLength = orders.length; + + while (++index < length) { + var result = compareAscending(objCriteria[index], othCriteria[index]); + if (result) { + if (index >= ordersLength) { + return result; + } + var order = orders[index]; + return result * (order == 'desc' ? -1 : 1); + } + } + // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications + // that causes it, under certain circumstances, to provide the same value for + // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247 + // for more details. + // + // This also ensures a stable sort in V8 and other engines. + // See https://bugs.chromium.org/p/v8/issues/detail?id=90 for more details. + return object.index - other.index; + } + + /** + * Creates an array that is the composition of partially applied arguments, + * placeholders, and provided arguments into a single array of arguments. + * + * @private + * @param {Array} args The provided arguments. + * @param {Array} partials The arguments to prepend to those provided. + * @param {Array} holders The `partials` placeholder indexes. + * @params {boolean} [isCurried] Specify composing for a curried function. + * @returns {Array} Returns the new array of composed arguments. + */ + function composeArgs(args, partials, holders, isCurried) { + var argsIndex = -1, + argsLength = args.length, + holdersLength = holders.length, + leftIndex = -1, + leftLength = partials.length, + rangeLength = nativeMax(argsLength - holdersLength, 0), + result = Array(leftLength + rangeLength), + isUncurried = !isCurried; + + while (++leftIndex < leftLength) { + result[leftIndex] = partials[leftIndex]; + } + while (++argsIndex < holdersLength) { + if (isUncurried || argsIndex < argsLength) { + result[holders[argsIndex]] = args[argsIndex]; + } + } + while (rangeLength--) { + result[leftIndex++] = args[argsIndex++]; + } + return result; + } + + /** + * This function is like `composeArgs` except that the arguments composition + * is tailored for `_.partialRight`. + * + * @private + * @param {Array} args The provided arguments. + * @param {Array} partials The arguments to append to those provided. + * @param {Array} holders The `partials` placeholder indexes. + * @params {boolean} [isCurried] Specify composing for a curried function. + * @returns {Array} Returns the new array of composed arguments. + */ + function composeArgsRight(args, partials, holders, isCurried) { + var argsIndex = -1, + argsLength = args.length, + holdersIndex = -1, + holdersLength = holders.length, + rightIndex = -1, + rightLength = partials.length, + rangeLength = nativeMax(argsLength - holdersLength, 0), + result = Array(rangeLength + rightLength), + isUncurried = !isCurried; + + while (++argsIndex < rangeLength) { + result[argsIndex] = args[argsIndex]; + } + var offset = argsIndex; + while (++rightIndex < rightLength) { + result[offset + rightIndex] = partials[rightIndex]; + } + while (++holdersIndex < holdersLength) { + if (isUncurried || argsIndex < argsLength) { + result[offset + holders[holdersIndex]] = args[argsIndex++]; + } + } + return result; + } + + /** + * Copies the values of `source` to `array`. + * + * @private + * @param {Array} source The array to copy values from. + * @param {Array} [array=[]] The array to copy values to. + * @returns {Array} Returns `array`. + */ + function copyArray(source, array) { + var index = -1, + length = source.length; + + array || (array = Array(length)); + while (++index < length) { + array[index] = source[index]; + } + return array; + } + + /** + * Copies properties of `source` to `object`. + * + * @private + * @param {Object} source The object to copy properties from. + * @param {Array} props The property identifiers to copy. + * @param {Object} [object={}] The object to copy properties to. + * @param {Function} [customizer] The function to customize copied values. + * @returns {Object} Returns `object`. + */ + function copyObject(source, props, object, customizer) { + var isNew = !object; + object || (object = {}); + + var index = -1, + length = props.length; + + while (++index < length) { + var key = props[index]; + + var newValue = customizer + ? customizer(object[key], source[key], key, object, source) + : undefined; + + if (newValue === undefined) { + newValue = source[key]; + } + if (isNew) { + baseAssignValue(object, key, newValue); + } else { + assignValue(object, key, newValue); + } + } + return object; + } + + /** + * Copies own symbols of `source` to `object`. + * + * @private + * @param {Object} source The object to copy symbols from. + * @param {Object} [object={}] The object to copy symbols to. + * @returns {Object} Returns `object`. + */ + function copySymbols(source, object) { + return copyObject(source, getSymbols(source), object); + } + + /** + * Copies own and inherited symbols of `source` to `object`. + * + * @private + * @param {Object} source The object to copy symbols from. + * @param {Object} [object={}] The object to copy symbols to. + * @returns {Object} Returns `object`. + */ + function copySymbolsIn(source, object) { + return copyObject(source, getSymbolsIn(source), object); + } + + /** + * Creates a function like `_.groupBy`. + * + * @private + * @param {Function} setter The function to set accumulator values. + * @param {Function} [initializer] The accumulator object initializer. + * @returns {Function} Returns the new aggregator function. + */ + function createAggregator(setter, initializer) { + return function(collection, iteratee) { + var func = isArray(collection) ? arrayAggregator : baseAggregator, + accumulator = initializer ? initializer() : {}; + + return func(collection, setter, getIteratee(iteratee, 2), accumulator); + }; + } + + /** + * Creates a function like `_.assign`. + * + * @private + * @param {Function} assigner The function to assign values. + * @returns {Function} Returns the new assigner function. + */ + function createAssigner(assigner) { + return baseRest(function(object, sources) { + var index = -1, + length = sources.length, + customizer = length > 1 ? sources[length - 1] : undefined, + guard = length > 2 ? sources[2] : undefined; + + customizer = (assigner.length > 3 && typeof customizer == 'function') + ? (length--, customizer) + : undefined; + + if (guard && isIterateeCall(sources[0], sources[1], guard)) { + customizer = length < 3 ? undefined : customizer; + length = 1; + } + object = Object(object); + while (++index < length) { + var source = sources[index]; + if (source) { + assigner(object, source, index, customizer); + } + } + return object; + }); + } + + /** + * Creates a `baseEach` or `baseEachRight` function. + * + * @private + * @param {Function} eachFunc The function to iterate over a collection. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new base function. + */ + function createBaseEach(eachFunc, fromRight) { + return function(collection, iteratee) { + if (collection == null) { + return collection; + } + if (!isArrayLike(collection)) { + return eachFunc(collection, iteratee); + } + var length = collection.length, + index = fromRight ? length : -1, + iterable = Object(collection); + + while ((fromRight ? index-- : ++index < length)) { + if (iteratee(iterable[index], index, iterable) === false) { + break; + } + } + return collection; + }; + } + + /** + * Creates a base function for methods like `_.forIn` and `_.forOwn`. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new base function. + */ + function createBaseFor(fromRight) { + return function(object, iteratee, keysFunc) { + var index = -1, + iterable = Object(object), + props = keysFunc(object), + length = props.length; + + while (length--) { + var key = props[fromRight ? length : ++index]; + if (iteratee(iterable[key], key, iterable) === false) { + break; + } + } + return object; + }; + } + + /** + * Creates a function that wraps `func` to invoke it with the optional `this` + * binding of `thisArg`. + * + * @private + * @param {Function} func The function to wrap. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @param {*} [thisArg] The `this` binding of `func`. + * @returns {Function} Returns the new wrapped function. + */ + function createBind(func, bitmask, thisArg) { + var isBind = bitmask & WRAP_BIND_FLAG, + Ctor = createCtor(func); + + function wrapper() { + var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func; + return fn.apply(isBind ? thisArg : this, arguments); + } + return wrapper; + } + + /** + * Creates a function like `_.lowerFirst`. + * + * @private + * @param {string} methodName The name of the `String` case method to use. + * @returns {Function} Returns the new case function. + */ + function createCaseFirst(methodName) { + return function(string) { + string = toString(string); + + var strSymbols = hasUnicode(string) + ? stringToArray(string) + : undefined; + + var chr = strSymbols + ? strSymbols[0] + : string.charAt(0); + + var trailing = strSymbols + ? castSlice(strSymbols, 1).join('') + : string.slice(1); + + return chr[methodName]() + trailing; + }; + } + + /** + * Creates a function like `_.camelCase`. + * + * @private + * @param {Function} callback The function to combine each word. + * @returns {Function} Returns the new compounder function. + */ + function createCompounder(callback) { + return function(string) { + return arrayReduce(words(deburr(string).replace(reApos, '')), callback, ''); + }; + } + + /** + * Creates a function that produces an instance of `Ctor` regardless of + * whether it was invoked as part of a `new` expression or by `call` or `apply`. + * + * @private + * @param {Function} Ctor The constructor to wrap. + * @returns {Function} Returns the new wrapped function. + */ + function createCtor(Ctor) { + return function() { + // Use a `switch` statement to work with class constructors. See + // http://ecma-international.org/ecma-262/7.0/#sec-ecmascript-function-objects-call-thisargument-argumentslist + // for more details. + var args = arguments; + switch (args.length) { + case 0: return new Ctor; + case 1: return new Ctor(args[0]); + case 2: return new Ctor(args[0], args[1]); + case 3: return new Ctor(args[0], args[1], args[2]); + case 4: return new Ctor(args[0], args[1], args[2], args[3]); + case 5: return new Ctor(args[0], args[1], args[2], args[3], args[4]); + case 6: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5]); + case 7: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); + } + var thisBinding = baseCreate(Ctor.prototype), + result = Ctor.apply(thisBinding, args); + + // Mimic the constructor's `return` behavior. + // See https://es5.github.io/#x13.2.2 for more details. + return isObject(result) ? result : thisBinding; + }; + } + + /** + * Creates a function that wraps `func` to enable currying. + * + * @private + * @param {Function} func The function to wrap. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @param {number} arity The arity of `func`. + * @returns {Function} Returns the new wrapped function. + */ + function createCurry(func, bitmask, arity) { + var Ctor = createCtor(func); + + function wrapper() { + var length = arguments.length, + args = Array(length), + index = length, + placeholder = getHolder(wrapper); + + while (index--) { + args[index] = arguments[index]; + } + var holders = (length < 3 && args[0] !== placeholder && args[length - 1] !== placeholder) + ? [] + : replaceHolders(args, placeholder); + + length -= holders.length; + if (length < arity) { + return createRecurry( + func, bitmask, createHybrid, wrapper.placeholder, undefined, + args, holders, undefined, undefined, arity - length); + } + var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func; + return apply(fn, this, args); + } + return wrapper; + } + + /** + * Creates a `_.find` or `_.findLast` function. + * + * @private + * @param {Function} findIndexFunc The function to find the collection index. + * @returns {Function} Returns the new find function. + */ + function createFind(findIndexFunc) { + return function(collection, predicate, fromIndex) { + var iterable = Object(collection); + if (!isArrayLike(collection)) { + var iteratee = getIteratee(predicate, 3); + collection = keys(collection); + predicate = function(key) { return iteratee(iterable[key], key, iterable); }; + } + var index = findIndexFunc(collection, predicate, fromIndex); + return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined; + }; + } + + /** + * Creates a `_.flow` or `_.flowRight` function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new flow function. + */ + function createFlow(fromRight) { + return flatRest(function(funcs) { + var length = funcs.length, + index = length, + prereq = LodashWrapper.prototype.thru; + + if (fromRight) { + funcs.reverse(); + } + while (index--) { + var func = funcs[index]; + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + if (prereq && !wrapper && getFuncName(func) == 'wrapper') { + var wrapper = new LodashWrapper([], true); + } + } + index = wrapper ? index : length; + while (++index < length) { + func = funcs[index]; + + var funcName = getFuncName(func), + data = funcName == 'wrapper' ? getData(func) : undefined; + + if (data && isLaziable(data[0]) && + data[1] == (WRAP_ARY_FLAG | WRAP_CURRY_FLAG | WRAP_PARTIAL_FLAG | WRAP_REARG_FLAG) && + !data[4].length && data[9] == 1 + ) { + wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]); + } else { + wrapper = (func.length == 1 && isLaziable(func)) + ? wrapper[funcName]() + : wrapper.thru(func); + } + } + return function() { + var args = arguments, + value = args[0]; + + if (wrapper && args.length == 1 && isArray(value)) { + return wrapper.plant(value).value(); + } + var index = 0, + result = length ? funcs[index].apply(this, args) : value; + + while (++index < length) { + result = funcs[index].call(this, result); + } + return result; + }; + }); + } + + /** + * Creates a function that wraps `func` to invoke it with optional `this` + * binding of `thisArg`, partial application, and currying. + * + * @private + * @param {Function|string} func The function or method name to wrap. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @param {*} [thisArg] The `this` binding of `func`. + * @param {Array} [partials] The arguments to prepend to those provided to + * the new function. + * @param {Array} [holders] The `partials` placeholder indexes. + * @param {Array} [partialsRight] The arguments to append to those provided + * to the new function. + * @param {Array} [holdersRight] The `partialsRight` placeholder indexes. + * @param {Array} [argPos] The argument positions of the new function. + * @param {number} [ary] The arity cap of `func`. + * @param {number} [arity] The arity of `func`. + * @returns {Function} Returns the new wrapped function. + */ + function createHybrid(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) { + var isAry = bitmask & WRAP_ARY_FLAG, + isBind = bitmask & WRAP_BIND_FLAG, + isBindKey = bitmask & WRAP_BIND_KEY_FLAG, + isCurried = bitmask & (WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG), + isFlip = bitmask & WRAP_FLIP_FLAG, + Ctor = isBindKey ? undefined : createCtor(func); + + function wrapper() { + var length = arguments.length, + args = Array(length), + index = length; + + while (index--) { + args[index] = arguments[index]; + } + if (isCurried) { + var placeholder = getHolder(wrapper), + holdersCount = countHolders(args, placeholder); + } + if (partials) { + args = composeArgs(args, partials, holders, isCurried); + } + if (partialsRight) { + args = composeArgsRight(args, partialsRight, holdersRight, isCurried); + } + length -= holdersCount; + if (isCurried && length < arity) { + var newHolders = replaceHolders(args, placeholder); + return createRecurry( + func, bitmask, createHybrid, wrapper.placeholder, thisArg, + args, newHolders, argPos, ary, arity - length + ); + } + var thisBinding = isBind ? thisArg : this, + fn = isBindKey ? thisBinding[func] : func; + + length = args.length; + if (argPos) { + args = reorder(args, argPos); + } else if (isFlip && length > 1) { + args.reverse(); + } + if (isAry && ary < length) { + args.length = ary; + } + if (this && this !== root && this instanceof wrapper) { + fn = Ctor || createCtor(fn); + } + return fn.apply(thisBinding, args); + } + return wrapper; + } + + /** + * Creates a function like `_.invertBy`. + * + * @private + * @param {Function} setter The function to set accumulator values. + * @param {Function} toIteratee The function to resolve iteratees. + * @returns {Function} Returns the new inverter function. + */ + function createInverter(setter, toIteratee) { + return function(object, iteratee) { + return baseInverter(object, setter, toIteratee(iteratee), {}); + }; + } + + /** + * Creates a function that performs a mathematical operation on two values. + * + * @private + * @param {Function} operator The function to perform the operation. + * @param {number} [defaultValue] The value used for `undefined` arguments. + * @returns {Function} Returns the new mathematical operation function. + */ + function createMathOperation(operator, defaultValue) { + return function(value, other) { + var result; + if (value === undefined && other === undefined) { + return defaultValue; + } + if (value !== undefined) { + result = value; + } + if (other !== undefined) { + if (result === undefined) { + return other; + } + if (typeof value == 'string' || typeof other == 'string') { + value = baseToString(value); + other = baseToString(other); + } else { + value = baseToNumber(value); + other = baseToNumber(other); + } + result = operator(value, other); + } + return result; + }; + } + + /** + * Creates a function like `_.over`. + * + * @private + * @param {Function} arrayFunc The function to iterate over iteratees. + * @returns {Function} Returns the new over function. + */ + function createOver(arrayFunc) { + return flatRest(function(iteratees) { + iteratees = arrayMap(iteratees, baseUnary(getIteratee())); + return baseRest(function(args) { + var thisArg = this; + return arrayFunc(iteratees, function(iteratee) { + return apply(iteratee, thisArg, args); + }); + }); + }); + } + + /** + * Creates the padding for `string` based on `length`. The `chars` string + * is truncated if the number of characters exceeds `length`. + * + * @private + * @param {number} length The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padding for `string`. + */ + function createPadding(length, chars) { + chars = chars === undefined ? ' ' : baseToString(chars); + + var charsLength = chars.length; + if (charsLength < 2) { + return charsLength ? baseRepeat(chars, length) : chars; + } + var result = baseRepeat(chars, nativeCeil(length / stringSize(chars))); + return hasUnicode(chars) + ? castSlice(stringToArray(result), 0, length).join('') + : result.slice(0, length); + } + + /** + * Creates a function that wraps `func` to invoke it with the `this` binding + * of `thisArg` and `partials` prepended to the arguments it receives. + * + * @private + * @param {Function} func The function to wrap. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @param {*} thisArg The `this` binding of `func`. + * @param {Array} partials The arguments to prepend to those provided to + * the new function. + * @returns {Function} Returns the new wrapped function. + */ + function createPartial(func, bitmask, thisArg, partials) { + var isBind = bitmask & WRAP_BIND_FLAG, + Ctor = createCtor(func); + + function wrapper() { + var argsIndex = -1, + argsLength = arguments.length, + leftIndex = -1, + leftLength = partials.length, + args = Array(leftLength + argsLength), + fn = (this && this !== root && this instanceof wrapper) ? Ctor : func; + + while (++leftIndex < leftLength) { + args[leftIndex] = partials[leftIndex]; + } + while (argsLength--) { + args[leftIndex++] = arguments[++argsIndex]; + } + return apply(fn, isBind ? thisArg : this, args); + } + return wrapper; + } + + /** + * Creates a `_.range` or `_.rangeRight` function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new range function. + */ + function createRange(fromRight) { + return function(start, end, step) { + if (step && typeof step != 'number' && isIterateeCall(start, end, step)) { + end = step = undefined; + } + // Ensure the sign of `-0` is preserved. + start = toFinite(start); + if (end === undefined) { + end = start; + start = 0; + } else { + end = toFinite(end); + } + step = step === undefined ? (start < end ? 1 : -1) : toFinite(step); + return baseRange(start, end, step, fromRight); + }; + } + + /** + * Creates a function that performs a relational operation on two values. + * + * @private + * @param {Function} operator The function to perform the operation. + * @returns {Function} Returns the new relational operation function. + */ + function createRelationalOperation(operator) { + return function(value, other) { + if (!(typeof value == 'string' && typeof other == 'string')) { + value = toNumber(value); + other = toNumber(other); + } + return operator(value, other); + }; + } + + /** + * Creates a function that wraps `func` to continue currying. + * + * @private + * @param {Function} func The function to wrap. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @param {Function} wrapFunc The function to create the `func` wrapper. + * @param {*} placeholder The placeholder value. + * @param {*} [thisArg] The `this` binding of `func`. + * @param {Array} [partials] The arguments to prepend to those provided to + * the new function. + * @param {Array} [holders] The `partials` placeholder indexes. + * @param {Array} [argPos] The argument positions of the new function. + * @param {number} [ary] The arity cap of `func`. + * @param {number} [arity] The arity of `func`. + * @returns {Function} Returns the new wrapped function. + */ + function createRecurry(func, bitmask, wrapFunc, placeholder, thisArg, partials, holders, argPos, ary, arity) { + var isCurry = bitmask & WRAP_CURRY_FLAG, + newHolders = isCurry ? holders : undefined, + newHoldersRight = isCurry ? undefined : holders, + newPartials = isCurry ? partials : undefined, + newPartialsRight = isCurry ? undefined : partials; + + bitmask |= (isCurry ? WRAP_PARTIAL_FLAG : WRAP_PARTIAL_RIGHT_FLAG); + bitmask &= ~(isCurry ? WRAP_PARTIAL_RIGHT_FLAG : WRAP_PARTIAL_FLAG); + + if (!(bitmask & WRAP_CURRY_BOUND_FLAG)) { + bitmask &= ~(WRAP_BIND_FLAG | WRAP_BIND_KEY_FLAG); + } + var newData = [ + func, bitmask, thisArg, newPartials, newHolders, newPartialsRight, + newHoldersRight, argPos, ary, arity + ]; + + var result = wrapFunc.apply(undefined, newData); + if (isLaziable(func)) { + setData(result, newData); + } + result.placeholder = placeholder; + return setWrapToString(result, func, bitmask); + } + + /** + * Creates a function like `_.round`. + * + * @private + * @param {string} methodName The name of the `Math` method to use when rounding. + * @returns {Function} Returns the new round function. + */ + function createRound(methodName) { + var func = Math[methodName]; + return function(number, precision) { + number = toNumber(number); + precision = precision == null ? 0 : nativeMin(toInteger(precision), 292); + if (precision && nativeIsFinite(number)) { + // Shift with exponential notation to avoid floating-point issues. + // See [MDN](https://mdn.io/round#Examples) for more details. + var pair = (toString(number) + 'e').split('e'), + value = func(pair[0] + 'e' + (+pair[1] + precision)); + + pair = (toString(value) + 'e').split('e'); + return +(pair[0] + 'e' + (+pair[1] - precision)); + } + return func(number); + }; + } + + /** + * Creates a set object of `values`. + * + * @private + * @param {Array} values The values to add to the set. + * @returns {Object} Returns the new set. + */ + var createSet = !(Set && (1 / setToArray(new Set([,-0]))[1]) == INFINITY) ? noop : function(values) { + return new Set(values); + }; + + /** + * Creates a `_.toPairs` or `_.toPairsIn` function. + * + * @private + * @param {Function} keysFunc The function to get the keys of a given object. + * @returns {Function} Returns the new pairs function. + */ + function createToPairs(keysFunc) { + return function(object) { + var tag = getTag(object); + if (tag == mapTag) { + return mapToArray(object); + } + if (tag == setTag) { + return setToPairs(object); + } + return baseToPairs(object, keysFunc(object)); + }; + } + + /** + * Creates a function that either curries or invokes `func` with optional + * `this` binding and partially applied arguments. + * + * @private + * @param {Function|string} func The function or method name to wrap. + * @param {number} bitmask The bitmask flags. + * 1 - `_.bind` + * 2 - `_.bindKey` + * 4 - `_.curry` or `_.curryRight` of a bound function + * 8 - `_.curry` + * 16 - `_.curryRight` + * 32 - `_.partial` + * 64 - `_.partialRight` + * 128 - `_.rearg` + * 256 - `_.ary` + * 512 - `_.flip` + * @param {*} [thisArg] The `this` binding of `func`. + * @param {Array} [partials] The arguments to be partially applied. + * @param {Array} [holders] The `partials` placeholder indexes. + * @param {Array} [argPos] The argument positions of the new function. + * @param {number} [ary] The arity cap of `func`. + * @param {number} [arity] The arity of `func`. + * @returns {Function} Returns the new wrapped function. + */ + function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) { + var isBindKey = bitmask & WRAP_BIND_KEY_FLAG; + if (!isBindKey && typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + var length = partials ? partials.length : 0; + if (!length) { + bitmask &= ~(WRAP_PARTIAL_FLAG | WRAP_PARTIAL_RIGHT_FLAG); + partials = holders = undefined; + } + ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0); + arity = arity === undefined ? arity : toInteger(arity); + length -= holders ? holders.length : 0; + + if (bitmask & WRAP_PARTIAL_RIGHT_FLAG) { + var partialsRight = partials, + holdersRight = holders; + + partials = holders = undefined; + } + var data = isBindKey ? undefined : getData(func); + + var newData = [ + func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, + argPos, ary, arity + ]; + + if (data) { + mergeData(newData, data); + } + func = newData[0]; + bitmask = newData[1]; + thisArg = newData[2]; + partials = newData[3]; + holders = newData[4]; + arity = newData[9] = newData[9] === undefined + ? (isBindKey ? 0 : func.length) + : nativeMax(newData[9] - length, 0); + + if (!arity && bitmask & (WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG)) { + bitmask &= ~(WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG); + } + if (!bitmask || bitmask == WRAP_BIND_FLAG) { + var result = createBind(func, bitmask, thisArg); + } else if (bitmask == WRAP_CURRY_FLAG || bitmask == WRAP_CURRY_RIGHT_FLAG) { + result = createCurry(func, bitmask, arity); + } else if ((bitmask == WRAP_PARTIAL_FLAG || bitmask == (WRAP_BIND_FLAG | WRAP_PARTIAL_FLAG)) && !holders.length) { + result = createPartial(func, bitmask, thisArg, partials); + } else { + result = createHybrid.apply(undefined, newData); + } + var setter = data ? baseSetData : setData; + return setWrapToString(setter(result, newData), func, bitmask); + } + + /** + * Used by `_.defaults` to customize its `_.assignIn` use to assign properties + * of source objects to the destination object for all destination properties + * that resolve to `undefined`. + * + * @private + * @param {*} objValue The destination value. + * @param {*} srcValue The source value. + * @param {string} key The key of the property to assign. + * @param {Object} object The parent object of `objValue`. + * @returns {*} Returns the value to assign. + */ + function customDefaultsAssignIn(objValue, srcValue, key, object) { + if (objValue === undefined || + (eq(objValue, objectProto[key]) && !hasOwnProperty.call(object, key))) { + return srcValue; + } + return objValue; + } + + /** + * Used by `_.defaultsDeep` to customize its `_.merge` use to merge source + * objects into destination objects that are passed thru. + * + * @private + * @param {*} objValue The destination value. + * @param {*} srcValue The source value. + * @param {string} key The key of the property to merge. + * @param {Object} object The parent object of `objValue`. + * @param {Object} source The parent object of `srcValue`. + * @param {Object} [stack] Tracks traversed source values and their merged + * counterparts. + * @returns {*} Returns the value to assign. + */ + function customDefaultsMerge(objValue, srcValue, key, object, source, stack) { + if (isObject(objValue) && isObject(srcValue)) { + // Recursively merge objects and arrays (susceptible to call stack limits). + stack.set(srcValue, objValue); + baseMerge(objValue, srcValue, undefined, customDefaultsMerge, stack); + stack['delete'](srcValue); + } + return objValue; + } + + /** + * Used by `_.omit` to customize its `_.cloneDeep` use to only clone plain + * objects. + * + * @private + * @param {*} value The value to inspect. + * @param {string} key The key of the property to inspect. + * @returns {*} Returns the uncloned value or `undefined` to defer cloning to `_.cloneDeep`. + */ + function customOmitClone(value) { + return isPlainObject(value) ? undefined : value; + } + + /** + * A specialized version of `baseIsEqualDeep` for arrays with support for + * partial deep comparisons. + * + * @private + * @param {Array} array The array to compare. + * @param {Array} other The other array to compare. + * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details. + * @param {Function} customizer The function to customize comparisons. + * @param {Function} equalFunc The function to determine equivalents of values. + * @param {Object} stack Tracks traversed `array` and `other` objects. + * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`. + */ + function equalArrays(array, other, bitmask, customizer, equalFunc, stack) { + var isPartial = bitmask & COMPARE_PARTIAL_FLAG, + arrLength = array.length, + othLength = other.length; + + if (arrLength != othLength && !(isPartial && othLength > arrLength)) { + return false; + } + // Check that cyclic values are equal. + var arrStacked = stack.get(array); + var othStacked = stack.get(other); + if (arrStacked && othStacked) { + return arrStacked == other && othStacked == array; + } + var index = -1, + result = true, + seen = (bitmask & COMPARE_UNORDERED_FLAG) ? new SetCache : undefined; + + stack.set(array, other); + stack.set(other, array); + + // Ignore non-index properties. + while (++index < arrLength) { + var arrValue = array[index], + othValue = other[index]; + + if (customizer) { + var compared = isPartial + ? customizer(othValue, arrValue, index, other, array, stack) + : customizer(arrValue, othValue, index, array, other, stack); + } + if (compared !== undefined) { + if (compared) { + continue; + } + result = false; + break; + } + // Recursively compare arrays (susceptible to call stack limits). + if (seen) { + if (!arraySome(other, function(othValue, othIndex) { + if (!cacheHas(seen, othIndex) && + (arrValue === othValue || equalFunc(arrValue, othValue, bitmask, customizer, stack))) { + return seen.push(othIndex); + } + })) { + result = false; + break; + } + } else if (!( + arrValue === othValue || + equalFunc(arrValue, othValue, bitmask, customizer, stack) + )) { + result = false; + break; + } + } + stack['delete'](array); + stack['delete'](other); + return result; + } + + /** + * A specialized version of `baseIsEqualDeep` for comparing objects of + * the same `toStringTag`. + * + * **Note:** This function only supports comparing values with tags of + * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`. + * + * @private + * @param {Object} object The object to compare. + * @param {Object} other The other object to compare. + * @param {string} tag The `toStringTag` of the objects to compare. + * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details. + * @param {Function} customizer The function to customize comparisons. + * @param {Function} equalFunc The function to determine equivalents of values. + * @param {Object} stack Tracks traversed `object` and `other` objects. + * @returns {boolean} Returns `true` if the objects are equivalent, else `false`. + */ + function equalByTag(object, other, tag, bitmask, customizer, equalFunc, stack) { + switch (tag) { + case dataViewTag: + if ((object.byteLength != other.byteLength) || + (object.byteOffset != other.byteOffset)) { + return false; + } + object = object.buffer; + other = other.buffer; + + case arrayBufferTag: + if ((object.byteLength != other.byteLength) || + !equalFunc(new Uint8Array(object), new Uint8Array(other))) { + return false; + } + return true; + + case boolTag: + case dateTag: + case numberTag: + // Coerce booleans to `1` or `0` and dates to milliseconds. + // Invalid dates are coerced to `NaN`. + return eq(+object, +other); + + case errorTag: + return object.name == other.name && object.message == other.message; + + case regexpTag: + case stringTag: + // Coerce regexes to strings and treat strings, primitives and objects, + // as equal. See http://www.ecma-international.org/ecma-262/7.0/#sec-regexp.prototype.tostring + // for more details. + return object == (other + ''); + + case mapTag: + var convert = mapToArray; + + case setTag: + var isPartial = bitmask & COMPARE_PARTIAL_FLAG; + convert || (convert = setToArray); + + if (object.size != other.size && !isPartial) { + return false; + } + // Assume cyclic values are equal. + var stacked = stack.get(object); + if (stacked) { + return stacked == other; + } + bitmask |= COMPARE_UNORDERED_FLAG; + + // Recursively compare objects (susceptible to call stack limits). + stack.set(object, other); + var result = equalArrays(convert(object), convert(other), bitmask, customizer, equalFunc, stack); + stack['delete'](object); + return result; + + case symbolTag: + if (symbolValueOf) { + return symbolValueOf.call(object) == symbolValueOf.call(other); + } + } + return false; + } + + /** + * A specialized version of `baseIsEqualDeep` for objects with support for + * partial deep comparisons. + * + * @private + * @param {Object} object The object to compare. + * @param {Object} other The other object to compare. + * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details. + * @param {Function} customizer The function to customize comparisons. + * @param {Function} equalFunc The function to determine equivalents of values. + * @param {Object} stack Tracks traversed `object` and `other` objects. + * @returns {boolean} Returns `true` if the objects are equivalent, else `false`. + */ + function equalObjects(object, other, bitmask, customizer, equalFunc, stack) { + var isPartial = bitmask & COMPARE_PARTIAL_FLAG, + objProps = getAllKeys(object), + objLength = objProps.length, + othProps = getAllKeys(other), + othLength = othProps.length; + + if (objLength != othLength && !isPartial) { + return false; + } + var index = objLength; + while (index--) { + var key = objProps[index]; + if (!(isPartial ? key in other : hasOwnProperty.call(other, key))) { + return false; + } + } + // Check that cyclic values are equal. + var objStacked = stack.get(object); + var othStacked = stack.get(other); + if (objStacked && othStacked) { + return objStacked == other && othStacked == object; + } + var result = true; + stack.set(object, other); + stack.set(other, object); + + var skipCtor = isPartial; + while (++index < objLength) { + key = objProps[index]; + var objValue = object[key], + othValue = other[key]; + + if (customizer) { + var compared = isPartial + ? customizer(othValue, objValue, key, other, object, stack) + : customizer(objValue, othValue, key, object, other, stack); + } + // Recursively compare objects (susceptible to call stack limits). + if (!(compared === undefined + ? (objValue === othValue || equalFunc(objValue, othValue, bitmask, customizer, stack)) + : compared + )) { + result = false; + break; + } + skipCtor || (skipCtor = key == 'constructor'); + } + if (result && !skipCtor) { + var objCtor = object.constructor, + othCtor = other.constructor; + + // Non `Object` object instances with different constructors are not equal. + if (objCtor != othCtor && + ('constructor' in object && 'constructor' in other) && + !(typeof objCtor == 'function' && objCtor instanceof objCtor && + typeof othCtor == 'function' && othCtor instanceof othCtor)) { + result = false; + } + } + stack['delete'](object); + stack['delete'](other); + return result; + } + + /** + * A specialized version of `baseRest` which flattens the rest array. + * + * @private + * @param {Function} func The function to apply a rest parameter to. + * @returns {Function} Returns the new function. + */ + function flatRest(func) { + return setToString(overRest(func, undefined, flatten), func + ''); + } + + /** + * Creates an array of own enumerable property names and symbols of `object`. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names and symbols. + */ + function getAllKeys(object) { + return baseGetAllKeys(object, keys, getSymbols); + } + + /** + * Creates an array of own and inherited enumerable property names and + * symbols of `object`. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names and symbols. + */ + function getAllKeysIn(object) { + return baseGetAllKeys(object, keysIn, getSymbolsIn); + } + + /** + * Gets metadata for `func`. + * + * @private + * @param {Function} func The function to query. + * @returns {*} Returns the metadata for `func`. + */ + var getData = !metaMap ? noop : function(func) { + return metaMap.get(func); + }; + + /** + * Gets the name of `func`. + * + * @private + * @param {Function} func The function to query. + * @returns {string} Returns the function name. + */ + function getFuncName(func) { + var result = (func.name + ''), + array = realNames[result], + length = hasOwnProperty.call(realNames, result) ? array.length : 0; + + while (length--) { + var data = array[length], + otherFunc = data.func; + if (otherFunc == null || otherFunc == func) { + return data.name; + } + } + return result; + } + + /** + * Gets the argument placeholder value for `func`. + * + * @private + * @param {Function} func The function to inspect. + * @returns {*} Returns the placeholder value. + */ + function getHolder(func) { + var object = hasOwnProperty.call(lodash, 'placeholder') ? lodash : func; + return object.placeholder; + } + + /** + * Gets the appropriate "iteratee" function. If `_.iteratee` is customized, + * this function returns the custom method, otherwise it returns `baseIteratee`. + * If arguments are provided, the chosen function is invoked with them and + * its result is returned. + * + * @private + * @param {*} [value] The value to convert to an iteratee. + * @param {number} [arity] The arity of the created iteratee. + * @returns {Function} Returns the chosen function or its result. + */ + function getIteratee() { + var result = lodash.iteratee || iteratee; + result = result === iteratee ? baseIteratee : result; + return arguments.length ? result(arguments[0], arguments[1]) : result; + } + + /** + * Gets the data for `map`. + * + * @private + * @param {Object} map The map to query. + * @param {string} key The reference key. + * @returns {*} Returns the map data. + */ + function getMapData(map, key) { + var data = map.__data__; + return isKeyable(key) + ? data[typeof key == 'string' ? 'string' : 'hash'] + : data.map; + } + + /** + * Gets the property names, values, and compare flags of `object`. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the match data of `object`. + */ + function getMatchData(object) { + var result = keys(object), + length = result.length; + + while (length--) { + var key = result[length], + value = object[key]; + + result[length] = [key, value, isStrictComparable(value)]; + } + return result; + } + + /** + * Gets the native function at `key` of `object`. + * + * @private + * @param {Object} object The object to query. + * @param {string} key The key of the method to get. + * @returns {*} Returns the function if it's native, else `undefined`. + */ + function getNative(object, key) { + var value = getValue(object, key); + return baseIsNative(value) ? value : undefined; + } + + /** + * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values. + * + * @private + * @param {*} value The value to query. + * @returns {string} Returns the raw `toStringTag`. + */ + function getRawTag(value) { + var isOwn = hasOwnProperty.call(value, symToStringTag), + tag = value[symToStringTag]; + + try { + value[symToStringTag] = undefined; + var unmasked = true; + } catch (e) {} + + var result = nativeObjectToString.call(value); + if (unmasked) { + if (isOwn) { + value[symToStringTag] = tag; + } else { + delete value[symToStringTag]; + } + } + return result; + } + + /** + * Creates an array of the own enumerable symbols of `object`. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of symbols. + */ + var getSymbols = !nativeGetSymbols ? stubArray : function(object) { + if (object == null) { + return []; + } + object = Object(object); + return arrayFilter(nativeGetSymbols(object), function(symbol) { + return propertyIsEnumerable.call(object, symbol); + }); + }; + + /** + * Creates an array of the own and inherited enumerable symbols of `object`. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of symbols. + */ + var getSymbolsIn = !nativeGetSymbols ? stubArray : function(object) { + var result = []; + while (object) { + arrayPush(result, getSymbols(object)); + object = getPrototype(object); + } + return result; + }; + + /** + * Gets the `toStringTag` of `value`. + * + * @private + * @param {*} value The value to query. + * @returns {string} Returns the `toStringTag`. + */ + var getTag = baseGetTag; + + // Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6. + if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) || + (Map && getTag(new Map) != mapTag) || + (Promise && getTag(Promise.resolve()) != promiseTag) || + (Set && getTag(new Set) != setTag) || + (WeakMap && getTag(new WeakMap) != weakMapTag)) { + getTag = function(value) { + var result = baseGetTag(value), + Ctor = result == objectTag ? value.constructor : undefined, + ctorString = Ctor ? toSource(Ctor) : ''; + + if (ctorString) { + switch (ctorString) { + case dataViewCtorString: return dataViewTag; + case mapCtorString: return mapTag; + case promiseCtorString: return promiseTag; + case setCtorString: return setTag; + case weakMapCtorString: return weakMapTag; + } + } + return result; + }; + } + + /** + * Gets the view, applying any `transforms` to the `start` and `end` positions. + * + * @private + * @param {number} start The start of the view. + * @param {number} end The end of the view. + * @param {Array} transforms The transformations to apply to the view. + * @returns {Object} Returns an object containing the `start` and `end` + * positions of the view. + */ + function getView(start, end, transforms) { + var index = -1, + length = transforms.length; + + while (++index < length) { + var data = transforms[index], + size = data.size; + + switch (data.type) { + case 'drop': start += size; break; + case 'dropRight': end -= size; break; + case 'take': end = nativeMin(end, start + size); break; + case 'takeRight': start = nativeMax(start, end - size); break; + } + } + return { 'start': start, 'end': end }; + } + + /** + * Extracts wrapper details from the `source` body comment. + * + * @private + * @param {string} source The source to inspect. + * @returns {Array} Returns the wrapper details. + */ + function getWrapDetails(source) { + var match = source.match(reWrapDetails); + return match ? match[1].split(reSplitDetails) : []; + } + + /** + * Checks if `path` exists on `object`. + * + * @private + * @param {Object} object The object to query. + * @param {Array|string} path The path to check. + * @param {Function} hasFunc The function to check properties. + * @returns {boolean} Returns `true` if `path` exists, else `false`. + */ + function hasPath(object, path, hasFunc) { + path = castPath(path, object); + + var index = -1, + length = path.length, + result = false; + + while (++index < length) { + var key = toKey(path[index]); + if (!(result = object != null && hasFunc(object, key))) { + break; + } + object = object[key]; + } + if (result || ++index != length) { + return result; + } + length = object == null ? 0 : object.length; + return !!length && isLength(length) && isIndex(key, length) && + (isArray(object) || isArguments(object)); + } + + /** + * Initializes an array clone. + * + * @private + * @param {Array} array The array to clone. + * @returns {Array} Returns the initialized clone. + */ + function initCloneArray(array) { + var length = array.length, + result = new array.constructor(length); + + // Add properties assigned by `RegExp#exec`. + if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) { + result.index = array.index; + result.input = array.input; + } + return result; + } + + /** + * Initializes an object clone. + * + * @private + * @param {Object} object The object to clone. + * @returns {Object} Returns the initialized clone. + */ + function initCloneObject(object) { + return (typeof object.constructor == 'function' && !isPrototype(object)) + ? baseCreate(getPrototype(object)) + : {}; + } + + /** + * Initializes an object clone based on its `toStringTag`. + * + * **Note:** This function only supports cloning values with tags of + * `Boolean`, `Date`, `Error`, `Map`, `Number`, `RegExp`, `Set`, or `String`. + * + * @private + * @param {Object} object The object to clone. + * @param {string} tag The `toStringTag` of the object to clone. + * @param {boolean} [isDeep] Specify a deep clone. + * @returns {Object} Returns the initialized clone. + */ + function initCloneByTag(object, tag, isDeep) { + var Ctor = object.constructor; + switch (tag) { + case arrayBufferTag: + return cloneArrayBuffer(object); + + case boolTag: + case dateTag: + return new Ctor(+object); + + case dataViewTag: + return cloneDataView(object, isDeep); + + case float32Tag: case float64Tag: + case int8Tag: case int16Tag: case int32Tag: + case uint8Tag: case uint8ClampedTag: case uint16Tag: case uint32Tag: + return cloneTypedArray(object, isDeep); + + case mapTag: + return new Ctor; + + case numberTag: + case stringTag: + return new Ctor(object); + + case regexpTag: + return cloneRegExp(object); + + case setTag: + return new Ctor; + + case symbolTag: + return cloneSymbol(object); + } + } + + /** + * Inserts wrapper `details` in a comment at the top of the `source` body. + * + * @private + * @param {string} source The source to modify. + * @returns {Array} details The details to insert. + * @returns {string} Returns the modified source. + */ + function insertWrapDetails(source, details) { + var length = details.length; + if (!length) { + return source; + } + var lastIndex = length - 1; + details[lastIndex] = (length > 1 ? '& ' : '') + details[lastIndex]; + details = details.join(length > 2 ? ', ' : ' '); + return source.replace(reWrapComment, '{\n/* [wrapped with ' + details + '] */\n'); + } + + /** + * Checks if `value` is a flattenable `arguments` object or array. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is flattenable, else `false`. + */ + function isFlattenable(value) { + return isArray(value) || isArguments(value) || + !!(spreadableSymbol && value && value[spreadableSymbol]); + } + + /** + * Checks if `value` is a valid array-like index. + * + * @private + * @param {*} value The value to check. + * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index. + * @returns {boolean} Returns `true` if `value` is a valid index, else `false`. + */ + function isIndex(value, length) { + var type = typeof value; + length = length == null ? MAX_SAFE_INTEGER : length; + + return !!length && + (type == 'number' || + (type != 'symbol' && reIsUint.test(value))) && + (value > -1 && value % 1 == 0 && value < length); + } + + /** + * Checks if the given arguments are from an iteratee call. + * + * @private + * @param {*} value The potential iteratee value argument. + * @param {*} index The potential iteratee index or key argument. + * @param {*} object The potential iteratee object argument. + * @returns {boolean} Returns `true` if the arguments are from an iteratee call, + * else `false`. + */ + function isIterateeCall(value, index, object) { + if (!isObject(object)) { + return false; + } + var type = typeof index; + if (type == 'number' + ? (isArrayLike(object) && isIndex(index, object.length)) + : (type == 'string' && index in object) + ) { + return eq(object[index], value); + } + return false; + } + + /** + * Checks if `value` is a property name and not a property path. + * + * @private + * @param {*} value The value to check. + * @param {Object} [object] The object to query keys on. + * @returns {boolean} Returns `true` if `value` is a property name, else `false`. + */ + function isKey(value, object) { + if (isArray(value)) { + return false; + } + var type = typeof value; + if (type == 'number' || type == 'symbol' || type == 'boolean' || + value == null || isSymbol(value)) { + return true; + } + return reIsPlainProp.test(value) || !reIsDeepProp.test(value) || + (object != null && value in Object(object)); + } + + /** + * Checks if `value` is suitable for use as unique object key. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is suitable, else `false`. + */ + function isKeyable(value) { + var type = typeof value; + return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean') + ? (value !== '__proto__') + : (value === null); + } + + /** + * Checks if `func` has a lazy counterpart. + * + * @private + * @param {Function} func The function to check. + * @returns {boolean} Returns `true` if `func` has a lazy counterpart, + * else `false`. + */ + function isLaziable(func) { + var funcName = getFuncName(func), + other = lodash[funcName]; + + if (typeof other != 'function' || !(funcName in LazyWrapper.prototype)) { + return false; + } + if (func === other) { + return true; + } + var data = getData(other); + return !!data && func === data[0]; + } + + /** + * Checks if `func` has its source masked. + * + * @private + * @param {Function} func The function to check. + * @returns {boolean} Returns `true` if `func` is masked, else `false`. + */ + function isMasked(func) { + return !!maskSrcKey && (maskSrcKey in func); + } + + /** + * Checks if `func` is capable of being masked. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `func` is maskable, else `false`. + */ + var isMaskable = coreJsData ? isFunction : stubFalse; + + /** + * Checks if `value` is likely a prototype object. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a prototype, else `false`. + */ + function isPrototype(value) { + var Ctor = value && value.constructor, + proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto; + + return value === proto; + } + + /** + * Checks if `value` is suitable for strict equality comparisons, i.e. `===`. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` if suitable for strict + * equality comparisons, else `false`. + */ + function isStrictComparable(value) { + return value === value && !isObject(value); + } + + /** + * A specialized version of `matchesProperty` for source values suitable + * for strict equality comparisons, i.e. `===`. + * + * @private + * @param {string} key The key of the property to get. + * @param {*} srcValue The value to match. + * @returns {Function} Returns the new spec function. + */ + function matchesStrictComparable(key, srcValue) { + return function(object) { + if (object == null) { + return false; + } + return object[key] === srcValue && + (srcValue !== undefined || (key in Object(object))); + }; + } + + /** + * A specialized version of `_.memoize` which clears the memoized function's + * cache when it exceeds `MAX_MEMOIZE_SIZE`. + * + * @private + * @param {Function} func The function to have its output memoized. + * @returns {Function} Returns the new memoized function. + */ + function memoizeCapped(func) { + var result = memoize(func, function(key) { + if (cache.size === MAX_MEMOIZE_SIZE) { + cache.clear(); + } + return key; + }); + + var cache = result.cache; + return result; + } + + /** + * Merges the function metadata of `source` into `data`. + * + * Merging metadata reduces the number of wrappers used to invoke a function. + * This is possible because methods like `_.bind`, `_.curry`, and `_.partial` + * may be applied regardless of execution order. Methods like `_.ary` and + * `_.rearg` modify function arguments, making the order in which they are + * executed important, preventing the merging of metadata. However, we make + * an exception for a safe combined case where curried functions have `_.ary` + * and or `_.rearg` applied. + * + * @private + * @param {Array} data The destination metadata. + * @param {Array} source The source metadata. + * @returns {Array} Returns `data`. + */ + function mergeData(data, source) { + var bitmask = data[1], + srcBitmask = source[1], + newBitmask = bitmask | srcBitmask, + isCommon = newBitmask < (WRAP_BIND_FLAG | WRAP_BIND_KEY_FLAG | WRAP_ARY_FLAG); + + var isCombo = + ((srcBitmask == WRAP_ARY_FLAG) && (bitmask == WRAP_CURRY_FLAG)) || + ((srcBitmask == WRAP_ARY_FLAG) && (bitmask == WRAP_REARG_FLAG) && (data[7].length <= source[8])) || + ((srcBitmask == (WRAP_ARY_FLAG | WRAP_REARG_FLAG)) && (source[7].length <= source[8]) && (bitmask == WRAP_CURRY_FLAG)); + + // Exit early if metadata can't be merged. + if (!(isCommon || isCombo)) { + return data; + } + // Use source `thisArg` if available. + if (srcBitmask & WRAP_BIND_FLAG) { + data[2] = source[2]; + // Set when currying a bound function. + newBitmask |= bitmask & WRAP_BIND_FLAG ? 0 : WRAP_CURRY_BOUND_FLAG; + } + // Compose partial arguments. + var value = source[3]; + if (value) { + var partials = data[3]; + data[3] = partials ? composeArgs(partials, value, source[4]) : value; + data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : source[4]; + } + // Compose partial right arguments. + value = source[5]; + if (value) { + partials = data[5]; + data[5] = partials ? composeArgsRight(partials, value, source[6]) : value; + data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : source[6]; + } + // Use source `argPos` if available. + value = source[7]; + if (value) { + data[7] = value; + } + // Use source `ary` if it's smaller. + if (srcBitmask & WRAP_ARY_FLAG) { + data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]); + } + // Use source `arity` if one is not provided. + if (data[9] == null) { + data[9] = source[9]; + } + // Use source `func` and merge bitmasks. + data[0] = source[0]; + data[1] = newBitmask; + + return data; + } + + /** + * This function is like + * [`Object.keys`](http://ecma-international.org/ecma-262/7.0/#sec-object.keys) + * except that it includes inherited enumerable properties. + * + * @private + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. + */ + function nativeKeysIn(object) { + var result = []; + if (object != null) { + for (var key in Object(object)) { + result.push(key); + } + } + return result; + } + + /** + * Converts `value` to a string using `Object.prototype.toString`. + * + * @private + * @param {*} value The value to convert. + * @returns {string} Returns the converted string. + */ + function objectToString(value) { + return nativeObjectToString.call(value); + } + + /** + * A specialized version of `baseRest` which transforms the rest array. + * + * @private + * @param {Function} func The function to apply a rest parameter to. + * @param {number} [start=func.length-1] The start position of the rest parameter. + * @param {Function} transform The rest array transform. + * @returns {Function} Returns the new function. + */ + function overRest(func, start, transform) { + start = nativeMax(start === undefined ? (func.length - 1) : start, 0); + return function() { + var args = arguments, + index = -1, + length = nativeMax(args.length - start, 0), + array = Array(length); + + while (++index < length) { + array[index] = args[start + index]; + } + index = -1; + var otherArgs = Array(start + 1); + while (++index < start) { + otherArgs[index] = args[index]; + } + otherArgs[start] = transform(array); + return apply(func, this, otherArgs); + }; + } + + /** + * Gets the parent value at `path` of `object`. + * + * @private + * @param {Object} object The object to query. + * @param {Array} path The path to get the parent value of. + * @returns {*} Returns the parent value. + */ + function parent(object, path) { + return path.length < 2 ? object : baseGet(object, baseSlice(path, 0, -1)); + } + + /** + * Reorder `array` according to the specified indexes where the element at + * the first index is assigned as the first element, the element at + * the second index is assigned as the second element, and so on. + * + * @private + * @param {Array} array The array to reorder. + * @param {Array} indexes The arranged array indexes. + * @returns {Array} Returns `array`. + */ + function reorder(array, indexes) { + var arrLength = array.length, + length = nativeMin(indexes.length, arrLength), + oldArray = copyArray(array); + + while (length--) { + var index = indexes[length]; + array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined; + } + return array; + } + + /** + * Gets the value at `key`, unless `key` is "__proto__" or "constructor". + * + * @private + * @param {Object} object The object to query. + * @param {string} key The key of the property to get. + * @returns {*} Returns the property value. + */ + function safeGet(object, key) { + if (key === 'constructor' && typeof object[key] === 'function') { + return; + } + + if (key == '__proto__') { + return; + } + + return object[key]; + } + + /** + * Sets metadata for `func`. + * + * **Note:** If this function becomes hot, i.e. is invoked a lot in a short + * period of time, it will trip its breaker and transition to an identity + * function to avoid garbage collection pauses in V8. See + * [V8 issue 2070](https://bugs.chromium.org/p/v8/issues/detail?id=2070) + * for more details. + * + * @private + * @param {Function} func The function to associate metadata with. + * @param {*} data The metadata. + * @returns {Function} Returns `func`. + */ + var setData = shortOut(baseSetData); + + /** + * A simple wrapper around the global [`setTimeout`](https://mdn.io/setTimeout). + * + * @private + * @param {Function} func The function to delay. + * @param {number} wait The number of milliseconds to delay invocation. + * @returns {number|Object} Returns the timer id or timeout object. + */ + var setTimeout = ctxSetTimeout || function(func, wait) { + return root.setTimeout(func, wait); + }; + + /** + * Sets the `toString` method of `func` to return `string`. + * + * @private + * @param {Function} func The function to modify. + * @param {Function} string The `toString` result. + * @returns {Function} Returns `func`. + */ + var setToString = shortOut(baseSetToString); + + /** + * Sets the `toString` method of `wrapper` to mimic the source of `reference` + * with wrapper details in a comment at the top of the source body. + * + * @private + * @param {Function} wrapper The function to modify. + * @param {Function} reference The reference function. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @returns {Function} Returns `wrapper`. + */ + function setWrapToString(wrapper, reference, bitmask) { + var source = (reference + ''); + return setToString(wrapper, insertWrapDetails(source, updateWrapDetails(getWrapDetails(source), bitmask))); + } + + /** + * Creates a function that'll short out and invoke `identity` instead + * of `func` when it's called `HOT_COUNT` or more times in `HOT_SPAN` + * milliseconds. + * + * @private + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new shortable function. + */ + function shortOut(func) { + var count = 0, + lastCalled = 0; + + return function() { + var stamp = nativeNow(), + remaining = HOT_SPAN - (stamp - lastCalled); + + lastCalled = stamp; + if (remaining > 0) { + if (++count >= HOT_COUNT) { + return arguments[0]; + } + } else { + count = 0; + } + return func.apply(undefined, arguments); + }; + } + + /** + * A specialized version of `_.shuffle` which mutates and sets the size of `array`. + * + * @private + * @param {Array} array The array to shuffle. + * @param {number} [size=array.length] The size of `array`. + * @returns {Array} Returns `array`. + */ + function shuffleSelf(array, size) { + var index = -1, + length = array.length, + lastIndex = length - 1; + + size = size === undefined ? length : size; + while (++index < size) { + var rand = baseRandom(index, lastIndex), + value = array[rand]; + + array[rand] = array[index]; + array[index] = value; + } + array.length = size; + return array; + } + + /** + * Converts `string` to a property path array. + * + * @private + * @param {string} string The string to convert. + * @returns {Array} Returns the property path array. + */ + var stringToPath = memoizeCapped(function(string) { + var result = []; + if (string.charCodeAt(0) === 46 /* . */) { + result.push(''); + } + string.replace(rePropName, function(match, number, quote, subString) { + result.push(quote ? subString.replace(reEscapeChar, '$1') : (number || match)); + }); + return result; + }); + + /** + * Converts `value` to a string key if it's not a string or symbol. + * + * @private + * @param {*} value The value to inspect. + * @returns {string|symbol} Returns the key. + */ + function toKey(value) { + if (typeof value == 'string' || isSymbol(value)) { + return value; + } + var result = (value + ''); + return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; + } + + /** + * Converts `func` to its source code. + * + * @private + * @param {Function} func The function to convert. + * @returns {string} Returns the source code. + */ + function toSource(func) { + if (func != null) { + try { + return funcToString.call(func); + } catch (e) {} + try { + return (func + ''); + } catch (e) {} + } + return ''; + } + + /** + * Updates wrapper `details` based on `bitmask` flags. + * + * @private + * @returns {Array} details The details to modify. + * @param {number} bitmask The bitmask flags. See `createWrap` for more details. + * @returns {Array} Returns `details`. + */ + function updateWrapDetails(details, bitmask) { + arrayEach(wrapFlags, function(pair) { + var value = '_.' + pair[0]; + if ((bitmask & pair[1]) && !arrayIncludes(details, value)) { + details.push(value); + } + }); + return details.sort(); + } + + /** + * Creates a clone of `wrapper`. + * + * @private + * @param {Object} wrapper The wrapper to clone. + * @returns {Object} Returns the cloned wrapper. + */ + function wrapperClone(wrapper) { + if (wrapper instanceof LazyWrapper) { + return wrapper.clone(); + } + var result = new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__); + result.__actions__ = copyArray(wrapper.__actions__); + result.__index__ = wrapper.__index__; + result.__values__ = wrapper.__values__; + return result; + } + + /*------------------------------------------------------------------------*/ + + /** + * Creates an array of elements split into groups the length of `size`. + * If `array` can't be split evenly, the final chunk will be the remaining + * elements. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to process. + * @param {number} [size=1] The length of each chunk + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Array} Returns the new array of chunks. + * @example + * + * _.chunk(['a', 'b', 'c', 'd'], 2); + * // => [['a', 'b'], ['c', 'd']] + * + * _.chunk(['a', 'b', 'c', 'd'], 3); + * // => [['a', 'b', 'c'], ['d']] + */ + function chunk(array, size, guard) { + if ((guard ? isIterateeCall(array, size, guard) : size === undefined)) { + size = 1; + } else { + size = nativeMax(toInteger(size), 0); + } + var length = array == null ? 0 : array.length; + if (!length || size < 1) { + return []; + } + var index = 0, + resIndex = 0, + result = Array(nativeCeil(length / size)); + + while (index < length) { + result[resIndex++] = baseSlice(array, index, (index += size)); + } + return result; + } + + /** + * Creates an array with all falsey values removed. The values `false`, `null`, + * `0`, `""`, `undefined`, and `NaN` are falsey. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to compact. + * @returns {Array} Returns the new array of filtered values. + * @example + * + * _.compact([0, 1, false, 2, '', 3]); + * // => [1, 2, 3] + */ + function compact(array) { + var index = -1, + length = array == null ? 0 : array.length, + resIndex = 0, + result = []; + + while (++index < length) { + var value = array[index]; + if (value) { + result[resIndex++] = value; + } + } + return result; + } + + /** + * Creates a new array concatenating `array` with any additional arrays + * and/or values. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to concatenate. + * @param {...*} [values] The values to concatenate. + * @returns {Array} Returns the new concatenated array. + * @example + * + * var array = [1]; + * var other = _.concat(array, 2, [3], [[4]]); + * + * console.log(other); + * // => [1, 2, 3, [4]] + * + * console.log(array); + * // => [1] + */ + function concat() { + var length = arguments.length; + if (!length) { + return []; + } + var args = Array(length - 1), + array = arguments[0], + index = length; + + while (index--) { + args[index - 1] = arguments[index]; + } + return arrayPush(isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1)); + } + + /** + * Creates an array of `array` values not included in the other given arrays + * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. The order and references of result values are + * determined by the first array. + * + * **Note:** Unlike `_.pullAll`, this method returns a new array. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {...Array} [values] The values to exclude. + * @returns {Array} Returns the new array of filtered values. + * @see _.without, _.xor + * @example + * + * _.difference([2, 1], [2, 3]); + * // => [1] + */ + var difference = baseRest(function(array, values) { + return isArrayLikeObject(array) + ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true)) + : []; + }); + + /** + * This method is like `_.difference` except that it accepts `iteratee` which + * is invoked for each element of `array` and `values` to generate the criterion + * by which they're compared. The order and references of result values are + * determined by the first array. The iteratee is invoked with one argument: + * (value). + * + * **Note:** Unlike `_.pullAllBy`, this method returns a new array. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {...Array} [values] The values to exclude. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Array} Returns the new array of filtered values. + * @example + * + * _.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); + * // => [1.2] + * + * // The `_.property` iteratee shorthand. + * _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x'); + * // => [{ 'x': 2 }] + */ + var differenceBy = baseRest(function(array, values) { + var iteratee = last(values); + if (isArrayLikeObject(iteratee)) { + iteratee = undefined; + } + return isArrayLikeObject(array) + ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), getIteratee(iteratee, 2)) + : []; + }); + + /** + * This method is like `_.difference` except that it accepts `comparator` + * which is invoked to compare elements of `array` to `values`. The order and + * references of result values are determined by the first array. The comparator + * is invoked with two arguments: (arrVal, othVal). + * + * **Note:** Unlike `_.pullAllWith`, this method returns a new array. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {...Array} [values] The values to exclude. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of filtered values. + * @example + * + * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; + * + * _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual); + * // => [{ 'x': 2, 'y': 1 }] + */ + var differenceWith = baseRest(function(array, values) { + var comparator = last(values); + if (isArrayLikeObject(comparator)) { + comparator = undefined; + } + return isArrayLikeObject(array) + ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), undefined, comparator) + : []; + }); + + /** + * Creates a slice of `array` with `n` elements dropped from the beginning. + * + * @static + * @memberOf _ + * @since 0.5.0 + * @category Array + * @param {Array} array The array to query. + * @param {number} [n=1] The number of elements to drop. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Array} Returns the slice of `array`. + * @example + * + * _.drop([1, 2, 3]); + * // => [2, 3] + * + * _.drop([1, 2, 3], 2); + * // => [3] + * + * _.drop([1, 2, 3], 5); + * // => [] + * + * _.drop([1, 2, 3], 0); + * // => [1, 2, 3] + */ + function drop(array, n, guard) { + var length = array == null ? 0 : array.length; + if (!length) { + return []; + } + n = (guard || n === undefined) ? 1 : toInteger(n); + return baseSlice(array, n < 0 ? 0 : n, length); + } + + /** + * Creates a slice of `array` with `n` elements dropped from the end. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to query. + * @param {number} [n=1] The number of elements to drop. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Array} Returns the slice of `array`. + * @example + * + * _.dropRight([1, 2, 3]); + * // => [1, 2] + * + * _.dropRight([1, 2, 3], 2); + * // => [1] + * + * _.dropRight([1, 2, 3], 5); + * // => [] + * + * _.dropRight([1, 2, 3], 0); + * // => [1, 2, 3] + */ + function dropRight(array, n, guard) { + var length = array == null ? 0 : array.length; + if (!length) { + return []; + } + n = (guard || n === undefined) ? 1 : toInteger(n); + n = length - n; + return baseSlice(array, 0, n < 0 ? 0 : n); + } + + /** + * Creates a slice of `array` excluding elements dropped from the end. + * Elements are dropped until `predicate` returns falsey. The predicate is + * invoked with three arguments: (value, index, array). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to query. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the slice of `array`. + * @example + * + * var users = [ + * { 'user': 'barney', 'active': true }, + * { 'user': 'fred', 'active': false }, + * { 'user': 'pebbles', 'active': false } + * ]; + * + * _.dropRightWhile(users, function(o) { return !o.active; }); + * // => objects for ['barney'] + * + * // The `_.matches` iteratee shorthand. + * _.dropRightWhile(users, { 'user': 'pebbles', 'active': false }); + * // => objects for ['barney', 'fred'] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.dropRightWhile(users, ['active', false]); + * // => objects for ['barney'] + * + * // The `_.property` iteratee shorthand. + * _.dropRightWhile(users, 'active'); + * // => objects for ['barney', 'fred', 'pebbles'] + */ + function dropRightWhile(array, predicate) { + return (array && array.length) + ? baseWhile(array, getIteratee(predicate, 3), true, true) + : []; + } + + /** + * Creates a slice of `array` excluding elements dropped from the beginning. + * Elements are dropped until `predicate` returns falsey. The predicate is + * invoked with three arguments: (value, index, array). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to query. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the slice of `array`. + * @example + * + * var users = [ + * { 'user': 'barney', 'active': false }, + * { 'user': 'fred', 'active': false }, + * { 'user': 'pebbles', 'active': true } + * ]; + * + * _.dropWhile(users, function(o) { return !o.active; }); + * // => objects for ['pebbles'] + * + * // The `_.matches` iteratee shorthand. + * _.dropWhile(users, { 'user': 'barney', 'active': false }); + * // => objects for ['fred', 'pebbles'] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.dropWhile(users, ['active', false]); + * // => objects for ['pebbles'] + * + * // The `_.property` iteratee shorthand. + * _.dropWhile(users, 'active'); + * // => objects for ['barney', 'fred', 'pebbles'] + */ + function dropWhile(array, predicate) { + return (array && array.length) + ? baseWhile(array, getIteratee(predicate, 3), true) + : []; + } + + /** + * Fills elements of `array` with `value` from `start` up to, but not + * including, `end`. + * + * **Note:** This method mutates `array`. + * + * @static + * @memberOf _ + * @since 3.2.0 + * @category Array + * @param {Array} array The array to fill. + * @param {*} value The value to fill `array` with. + * @param {number} [start=0] The start position. + * @param {number} [end=array.length] The end position. + * @returns {Array} Returns `array`. + * @example + * + * var array = [1, 2, 3]; + * + * _.fill(array, 'a'); + * console.log(array); + * // => ['a', 'a', 'a'] + * + * _.fill(Array(3), 2); + * // => [2, 2, 2] + * + * _.fill([4, 6, 8, 10], '*', 1, 3); + * // => [4, '*', '*', 10] + */ + function fill(array, value, start, end) { + var length = array == null ? 0 : array.length; + if (!length) { + return []; + } + if (start && typeof start != 'number' && isIterateeCall(array, value, start)) { + start = 0; + end = length; + } + return baseFill(array, value, start, end); + } + + /** + * This method is like `_.find` except that it returns the index of the first + * element `predicate` returns truthy for instead of the element itself. + * + * @static + * @memberOf _ + * @since 1.1.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @param {number} [fromIndex=0] The index to search from. + * @returns {number} Returns the index of the found element, else `-1`. + * @example + * + * var users = [ + * { 'user': 'barney', 'active': false }, + * { 'user': 'fred', 'active': false }, + * { 'user': 'pebbles', 'active': true } + * ]; + * + * _.findIndex(users, function(o) { return o.user == 'barney'; }); + * // => 0 + * + * // The `_.matches` iteratee shorthand. + * _.findIndex(users, { 'user': 'fred', 'active': false }); + * // => 1 + * + * // The `_.matchesProperty` iteratee shorthand. + * _.findIndex(users, ['active', false]); + * // => 0 + * + * // The `_.property` iteratee shorthand. + * _.findIndex(users, 'active'); + * // => 2 + */ + function findIndex(array, predicate, fromIndex) { + var length = array == null ? 0 : array.length; + if (!length) { + return -1; + } + var index = fromIndex == null ? 0 : toInteger(fromIndex); + if (index < 0) { + index = nativeMax(length + index, 0); + } + return baseFindIndex(array, getIteratee(predicate, 3), index); + } + + /** + * This method is like `_.findIndex` except that it iterates over elements + * of `collection` from right to left. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @param {number} [fromIndex=array.length-1] The index to search from. + * @returns {number} Returns the index of the found element, else `-1`. + * @example + * + * var users = [ + * { 'user': 'barney', 'active': true }, + * { 'user': 'fred', 'active': false }, + * { 'user': 'pebbles', 'active': false } + * ]; + * + * _.findLastIndex(users, function(o) { return o.user == 'pebbles'; }); + * // => 2 + * + * // The `_.matches` iteratee shorthand. + * _.findLastIndex(users, { 'user': 'barney', 'active': true }); + * // => 0 + * + * // The `_.matchesProperty` iteratee shorthand. + * _.findLastIndex(users, ['active', false]); + * // => 2 + * + * // The `_.property` iteratee shorthand. + * _.findLastIndex(users, 'active'); + * // => 0 + */ + function findLastIndex(array, predicate, fromIndex) { + var length = array == null ? 0 : array.length; + if (!length) { + return -1; + } + var index = length - 1; + if (fromIndex !== undefined) { + index = toInteger(fromIndex); + index = fromIndex < 0 + ? nativeMax(length + index, 0) + : nativeMin(index, length - 1); + } + return baseFindIndex(array, getIteratee(predicate, 3), index, true); + } + + /** + * Flattens `array` a single level deep. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to flatten. + * @returns {Array} Returns the new flattened array. + * @example + * + * _.flatten([1, [2, [3, [4]], 5]]); + * // => [1, 2, [3, [4]], 5] + */ + function flatten(array) { + var length = array == null ? 0 : array.length; + return length ? baseFlatten(array, 1) : []; + } + + /** + * Recursively flattens `array`. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to flatten. + * @returns {Array} Returns the new flattened array. + * @example + * + * _.flattenDeep([1, [2, [3, [4]], 5]]); + * // => [1, 2, 3, 4, 5] + */ + function flattenDeep(array) { + var length = array == null ? 0 : array.length; + return length ? baseFlatten(array, INFINITY) : []; + } + + /** + * Recursively flatten `array` up to `depth` times. + * + * @static + * @memberOf _ + * @since 4.4.0 + * @category Array + * @param {Array} array The array to flatten. + * @param {number} [depth=1] The maximum recursion depth. + * @returns {Array} Returns the new flattened array. + * @example + * + * var array = [1, [2, [3, [4]], 5]]; + * + * _.flattenDepth(array, 1); + * // => [1, 2, [3, [4]], 5] + * + * _.flattenDepth(array, 2); + * // => [1, 2, 3, [4], 5] + */ + function flattenDepth(array, depth) { + var length = array == null ? 0 : array.length; + if (!length) { + return []; + } + depth = depth === undefined ? 1 : toInteger(depth); + return baseFlatten(array, depth); + } + + /** + * The inverse of `_.toPairs`; this method returns an object composed + * from key-value `pairs`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} pairs The key-value pairs. + * @returns {Object} Returns the new object. + * @example + * + * _.fromPairs([['a', 1], ['b', 2]]); + * // => { 'a': 1, 'b': 2 } + */ + function fromPairs(pairs) { + var index = -1, + length = pairs == null ? 0 : pairs.length, + result = {}; + + while (++index < length) { + var pair = pairs[index]; + result[pair[0]] = pair[1]; + } + return result; + } + + /** + * Gets the first element of `array`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @alias first + * @category Array + * @param {Array} array The array to query. + * @returns {*} Returns the first element of `array`. + * @example + * + * _.head([1, 2, 3]); + * // => 1 + * + * _.head([]); + * // => undefined + */ + function head(array) { + return (array && array.length) ? array[0] : undefined; + } + + /** + * Gets the index at which the first occurrence of `value` is found in `array` + * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. If `fromIndex` is negative, it's used as the + * offset from the end of `array`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @param {number} [fromIndex=0] The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + * @example + * + * _.indexOf([1, 2, 1, 2], 2); + * // => 1 + * + * // Search from the `fromIndex`. + * _.indexOf([1, 2, 1, 2], 2, 2); + * // => 3 + */ + function indexOf(array, value, fromIndex) { + var length = array == null ? 0 : array.length; + if (!length) { + return -1; + } + var index = fromIndex == null ? 0 : toInteger(fromIndex); + if (index < 0) { + index = nativeMax(length + index, 0); + } + return baseIndexOf(array, value, index); + } + + /** + * Gets all but the last element of `array`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to query. + * @returns {Array} Returns the slice of `array`. + * @example + * + * _.initial([1, 2, 3]); + * // => [1, 2] + */ + function initial(array) { + var length = array == null ? 0 : array.length; + return length ? baseSlice(array, 0, -1) : []; + } + + /** + * Creates an array of unique values that are included in all given arrays + * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. The order and references of result values are + * determined by the first array. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @returns {Array} Returns the new array of intersecting values. + * @example + * + * _.intersection([2, 1], [2, 3]); + * // => [2] + */ + var intersection = baseRest(function(arrays) { + var mapped = arrayMap(arrays, castArrayLikeObject); + return (mapped.length && mapped[0] === arrays[0]) + ? baseIntersection(mapped) + : []; + }); + + /** + * This method is like `_.intersection` except that it accepts `iteratee` + * which is invoked for each element of each `arrays` to generate the criterion + * by which they're compared. The order and references of result values are + * determined by the first array. The iteratee is invoked with one argument: + * (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Array} Returns the new array of intersecting values. + * @example + * + * _.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); + * // => [2.1] + * + * // The `_.property` iteratee shorthand. + * _.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); + * // => [{ 'x': 1 }] + */ + var intersectionBy = baseRest(function(arrays) { + var iteratee = last(arrays), + mapped = arrayMap(arrays, castArrayLikeObject); + + if (iteratee === last(mapped)) { + iteratee = undefined; + } else { + mapped.pop(); + } + return (mapped.length && mapped[0] === arrays[0]) + ? baseIntersection(mapped, getIteratee(iteratee, 2)) + : []; + }); + + /** + * This method is like `_.intersection` except that it accepts `comparator` + * which is invoked to compare elements of `arrays`. The order and references + * of result values are determined by the first array. The comparator is + * invoked with two arguments: (arrVal, othVal). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of intersecting values. + * @example + * + * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; + * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]; + * + * _.intersectionWith(objects, others, _.isEqual); + * // => [{ 'x': 1, 'y': 2 }] + */ + var intersectionWith = baseRest(function(arrays) { + var comparator = last(arrays), + mapped = arrayMap(arrays, castArrayLikeObject); + + comparator = typeof comparator == 'function' ? comparator : undefined; + if (comparator) { + mapped.pop(); + } + return (mapped.length && mapped[0] === arrays[0]) + ? baseIntersection(mapped, undefined, comparator) + : []; + }); + + /** + * Converts all elements in `array` into a string separated by `separator`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to convert. + * @param {string} [separator=','] The element separator. + * @returns {string} Returns the joined string. + * @example + * + * _.join(['a', 'b', 'c'], '~'); + * // => 'a~b~c' + */ + function join(array, separator) { + return array == null ? '' : nativeJoin.call(array, separator); + } + + /** + * Gets the last element of `array`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to query. + * @returns {*} Returns the last element of `array`. + * @example + * + * _.last([1, 2, 3]); + * // => 3 + */ + function last(array) { + var length = array == null ? 0 : array.length; + return length ? array[length - 1] : undefined; + } + + /** + * This method is like `_.indexOf` except that it iterates over elements of + * `array` from right to left. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @param {number} [fromIndex=array.length-1] The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + * @example + * + * _.lastIndexOf([1, 2, 1, 2], 2); + * // => 3 + * + * // Search from the `fromIndex`. + * _.lastIndexOf([1, 2, 1, 2], 2, 2); + * // => 1 + */ + function lastIndexOf(array, value, fromIndex) { + var length = array == null ? 0 : array.length; + if (!length) { + return -1; + } + var index = length; + if (fromIndex !== undefined) { + index = toInteger(fromIndex); + index = index < 0 ? nativeMax(length + index, 0) : nativeMin(index, length - 1); + } + return value === value + ? strictLastIndexOf(array, value, index) + : baseFindIndex(array, baseIsNaN, index, true); + } + + /** + * Gets the element at index `n` of `array`. If `n` is negative, the nth + * element from the end is returned. + * + * @static + * @memberOf _ + * @since 4.11.0 + * @category Array + * @param {Array} array The array to query. + * @param {number} [n=0] The index of the element to return. + * @returns {*} Returns the nth element of `array`. + * @example + * + * var array = ['a', 'b', 'c', 'd']; + * + * _.nth(array, 1); + * // => 'b' + * + * _.nth(array, -2); + * // => 'c'; + */ + function nth(array, n) { + return (array && array.length) ? baseNth(array, toInteger(n)) : undefined; + } + + /** + * Removes all given values from `array` using + * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. + * + * **Note:** Unlike `_.without`, this method mutates `array`. Use `_.remove` + * to remove elements from an array by predicate. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Array + * @param {Array} array The array to modify. + * @param {...*} [values] The values to remove. + * @returns {Array} Returns `array`. + * @example + * + * var array = ['a', 'b', 'c', 'a', 'b', 'c']; + * + * _.pull(array, 'a', 'c'); + * console.log(array); + * // => ['b', 'b'] + */ + var pull = baseRest(pullAll); + + /** + * This method is like `_.pull` except that it accepts an array of values to remove. + * + * **Note:** Unlike `_.difference`, this method mutates `array`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to modify. + * @param {Array} values The values to remove. + * @returns {Array} Returns `array`. + * @example + * + * var array = ['a', 'b', 'c', 'a', 'b', 'c']; + * + * _.pullAll(array, ['a', 'c']); + * console.log(array); + * // => ['b', 'b'] + */ + function pullAll(array, values) { + return (array && array.length && values && values.length) + ? basePullAll(array, values) + : array; + } + + /** + * This method is like `_.pullAll` except that it accepts `iteratee` which is + * invoked for each element of `array` and `values` to generate the criterion + * by which they're compared. The iteratee is invoked with one argument: (value). + * + * **Note:** Unlike `_.differenceBy`, this method mutates `array`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to modify. + * @param {Array} values The values to remove. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Array} Returns `array`. + * @example + * + * var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }]; + * + * _.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x'); + * console.log(array); + * // => [{ 'x': 2 }] + */ + function pullAllBy(array, values, iteratee) { + return (array && array.length && values && values.length) + ? basePullAll(array, values, getIteratee(iteratee, 2)) + : array; + } + + /** + * This method is like `_.pullAll` except that it accepts `comparator` which + * is invoked to compare elements of `array` to `values`. The comparator is + * invoked with two arguments: (arrVal, othVal). + * + * **Note:** Unlike `_.differenceWith`, this method mutates `array`. + * + * @static + * @memberOf _ + * @since 4.6.0 + * @category Array + * @param {Array} array The array to modify. + * @param {Array} values The values to remove. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns `array`. + * @example + * + * var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }]; + * + * _.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual); + * console.log(array); + * // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }] + */ + function pullAllWith(array, values, comparator) { + return (array && array.length && values && values.length) + ? basePullAll(array, values, undefined, comparator) + : array; + } + + /** + * Removes elements from `array` corresponding to `indexes` and returns an + * array of removed elements. + * + * **Note:** Unlike `_.at`, this method mutates `array`. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to modify. + * @param {...(number|number[])} [indexes] The indexes of elements to remove. + * @returns {Array} Returns the new array of removed elements. + * @example + * + * var array = ['a', 'b', 'c', 'd']; + * var pulled = _.pullAt(array, [1, 3]); + * + * console.log(array); + * // => ['a', 'c'] + * + * console.log(pulled); + * // => ['b', 'd'] + */ + var pullAt = flatRest(function(array, indexes) { + var length = array == null ? 0 : array.length, + result = baseAt(array, indexes); + + basePullAt(array, arrayMap(indexes, function(index) { + return isIndex(index, length) ? +index : index; + }).sort(compareAscending)); + + return result; + }); + + /** + * Removes all elements from `array` that `predicate` returns truthy for + * and returns an array of the removed elements. The predicate is invoked + * with three arguments: (value, index, array). + * + * **Note:** Unlike `_.filter`, this method mutates `array`. Use `_.pull` + * to pull elements from an array by value. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Array + * @param {Array} array The array to modify. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the new array of removed elements. + * @example + * + * var array = [1, 2, 3, 4]; + * var evens = _.remove(array, function(n) { + * return n % 2 == 0; + * }); + * + * console.log(array); + * // => [1, 3] + * + * console.log(evens); + * // => [2, 4] + */ + function remove(array, predicate) { + var result = []; + if (!(array && array.length)) { + return result; + } + var index = -1, + indexes = [], + length = array.length; + + predicate = getIteratee(predicate, 3); + while (++index < length) { + var value = array[index]; + if (predicate(value, index, array)) { + result.push(value); + indexes.push(index); + } + } + basePullAt(array, indexes); + return result; + } + + /** + * Reverses `array` so that the first element becomes the last, the second + * element becomes the second to last, and so on. + * + * **Note:** This method mutates `array` and is based on + * [`Array#reverse`](https://mdn.io/Array/reverse). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to modify. + * @returns {Array} Returns `array`. + * @example + * + * var array = [1, 2, 3]; + * + * _.reverse(array); + * // => [3, 2, 1] + * + * console.log(array); + * // => [3, 2, 1] + */ + function reverse(array) { + return array == null ? array : nativeReverse.call(array); + } + + /** + * Creates a slice of `array` from `start` up to, but not including, `end`. + * + * **Note:** This method is used instead of + * [`Array#slice`](https://mdn.io/Array/slice) to ensure dense arrays are + * returned. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to slice. + * @param {number} [start=0] The start position. + * @param {number} [end=array.length] The end position. + * @returns {Array} Returns the slice of `array`. + */ + function slice(array, start, end) { + var length = array == null ? 0 : array.length; + if (!length) { + return []; + } + if (end && typeof end != 'number' && isIterateeCall(array, start, end)) { + start = 0; + end = length; + } + else { + start = start == null ? 0 : toInteger(start); + end = end === undefined ? length : toInteger(end); + } + return baseSlice(array, start, end); + } + + /** + * Uses a binary search to determine the lowest index at which `value` + * should be inserted into `array` in order to maintain its sort order. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @returns {number} Returns the index at which `value` should be inserted + * into `array`. + * @example + * + * _.sortedIndex([30, 50], 40); + * // => 1 + */ + function sortedIndex(array, value) { + return baseSortedIndex(array, value); + } + + /** + * This method is like `_.sortedIndex` except that it accepts `iteratee` + * which is invoked for `value` and each element of `array` to compute their + * sort ranking. The iteratee is invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {number} Returns the index at which `value` should be inserted + * into `array`. + * @example + * + * var objects = [{ 'x': 4 }, { 'x': 5 }]; + * + * _.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; }); + * // => 0 + * + * // The `_.property` iteratee shorthand. + * _.sortedIndexBy(objects, { 'x': 4 }, 'x'); + * // => 0 + */ + function sortedIndexBy(array, value, iteratee) { + return baseSortedIndexBy(array, value, getIteratee(iteratee, 2)); + } + + /** + * This method is like `_.indexOf` except that it performs a binary + * search on a sorted `array`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @returns {number} Returns the index of the matched value, else `-1`. + * @example + * + * _.sortedIndexOf([4, 5, 5, 5, 6], 5); + * // => 1 + */ + function sortedIndexOf(array, value) { + var length = array == null ? 0 : array.length; + if (length) { + var index = baseSortedIndex(array, value); + if (index < length && eq(array[index], value)) { + return index; + } + } + return -1; + } + + /** + * This method is like `_.sortedIndex` except that it returns the highest + * index at which `value` should be inserted into `array` in order to + * maintain its sort order. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @returns {number} Returns the index at which `value` should be inserted + * into `array`. + * @example + * + * _.sortedLastIndex([4, 5, 5, 5, 6], 5); + * // => 4 + */ + function sortedLastIndex(array, value) { + return baseSortedIndex(array, value, true); + } + + /** + * This method is like `_.sortedLastIndex` except that it accepts `iteratee` + * which is invoked for `value` and each element of `array` to compute their + * sort ranking. The iteratee is invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {number} Returns the index at which `value` should be inserted + * into `array`. + * @example + * + * var objects = [{ 'x': 4 }, { 'x': 5 }]; + * + * _.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; }); + * // => 1 + * + * // The `_.property` iteratee shorthand. + * _.sortedLastIndexBy(objects, { 'x': 4 }, 'x'); + * // => 1 + */ + function sortedLastIndexBy(array, value, iteratee) { + return baseSortedIndexBy(array, value, getIteratee(iteratee, 2), true); + } + + /** + * This method is like `_.lastIndexOf` except that it performs a binary + * search on a sorted `array`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {*} value The value to search for. + * @returns {number} Returns the index of the matched value, else `-1`. + * @example + * + * _.sortedLastIndexOf([4, 5, 5, 5, 6], 5); + * // => 3 + */ + function sortedLastIndexOf(array, value) { + var length = array == null ? 0 : array.length; + if (length) { + var index = baseSortedIndex(array, value, true) - 1; + if (eq(array[index], value)) { + return index; + } + } + return -1; + } + + /** + * This method is like `_.uniq` except that it's designed and optimized + * for sorted arrays. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @returns {Array} Returns the new duplicate free array. + * @example + * + * _.sortedUniq([1, 1, 2]); + * // => [1, 2] + */ + function sortedUniq(array) { + return (array && array.length) + ? baseSortedUniq(array) + : []; + } + + /** + * This method is like `_.uniqBy` except that it's designed and optimized + * for sorted arrays. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {Function} [iteratee] The iteratee invoked per element. + * @returns {Array} Returns the new duplicate free array. + * @example + * + * _.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor); + * // => [1.1, 2.3] + */ + function sortedUniqBy(array, iteratee) { + return (array && array.length) + ? baseSortedUniq(array, getIteratee(iteratee, 2)) + : []; + } + + /** + * Gets all but the first element of `array`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to query. + * @returns {Array} Returns the slice of `array`. + * @example + * + * _.tail([1, 2, 3]); + * // => [2, 3] + */ + function tail(array) { + var length = array == null ? 0 : array.length; + return length ? baseSlice(array, 1, length) : []; + } + + /** + * Creates a slice of `array` with `n` elements taken from the beginning. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to query. + * @param {number} [n=1] The number of elements to take. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Array} Returns the slice of `array`. + * @example + * + * _.take([1, 2, 3]); + * // => [1] + * + * _.take([1, 2, 3], 2); + * // => [1, 2] + * + * _.take([1, 2, 3], 5); + * // => [1, 2, 3] + * + * _.take([1, 2, 3], 0); + * // => [] + */ + function take(array, n, guard) { + if (!(array && array.length)) { + return []; + } + n = (guard || n === undefined) ? 1 : toInteger(n); + return baseSlice(array, 0, n < 0 ? 0 : n); + } + + /** + * Creates a slice of `array` with `n` elements taken from the end. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to query. + * @param {number} [n=1] The number of elements to take. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Array} Returns the slice of `array`. + * @example + * + * _.takeRight([1, 2, 3]); + * // => [3] + * + * _.takeRight([1, 2, 3], 2); + * // => [2, 3] + * + * _.takeRight([1, 2, 3], 5); + * // => [1, 2, 3] + * + * _.takeRight([1, 2, 3], 0); + * // => [] + */ + function takeRight(array, n, guard) { + var length = array == null ? 0 : array.length; + if (!length) { + return []; + } + n = (guard || n === undefined) ? 1 : toInteger(n); + n = length - n; + return baseSlice(array, n < 0 ? 0 : n, length); + } + + /** + * Creates a slice of `array` with elements taken from the end. Elements are + * taken until `predicate` returns falsey. The predicate is invoked with + * three arguments: (value, index, array). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to query. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the slice of `array`. + * @example + * + * var users = [ + * { 'user': 'barney', 'active': true }, + * { 'user': 'fred', 'active': false }, + * { 'user': 'pebbles', 'active': false } + * ]; + * + * _.takeRightWhile(users, function(o) { return !o.active; }); + * // => objects for ['fred', 'pebbles'] + * + * // The `_.matches` iteratee shorthand. + * _.takeRightWhile(users, { 'user': 'pebbles', 'active': false }); + * // => objects for ['pebbles'] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.takeRightWhile(users, ['active', false]); + * // => objects for ['fred', 'pebbles'] + * + * // The `_.property` iteratee shorthand. + * _.takeRightWhile(users, 'active'); + * // => [] + */ + function takeRightWhile(array, predicate) { + return (array && array.length) + ? baseWhile(array, getIteratee(predicate, 3), false, true) + : []; + } + + /** + * Creates a slice of `array` with elements taken from the beginning. Elements + * are taken until `predicate` returns falsey. The predicate is invoked with + * three arguments: (value, index, array). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Array + * @param {Array} array The array to query. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the slice of `array`. + * @example + * + * var users = [ + * { 'user': 'barney', 'active': false }, + * { 'user': 'fred', 'active': false }, + * { 'user': 'pebbles', 'active': true } + * ]; + * + * _.takeWhile(users, function(o) { return !o.active; }); + * // => objects for ['barney', 'fred'] + * + * // The `_.matches` iteratee shorthand. + * _.takeWhile(users, { 'user': 'barney', 'active': false }); + * // => objects for ['barney'] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.takeWhile(users, ['active', false]); + * // => objects for ['barney', 'fred'] + * + * // The `_.property` iteratee shorthand. + * _.takeWhile(users, 'active'); + * // => [] + */ + function takeWhile(array, predicate) { + return (array && array.length) + ? baseWhile(array, getIteratee(predicate, 3)) + : []; + } + + /** + * Creates an array of unique values, in order, from all given arrays using + * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @returns {Array} Returns the new array of combined values. + * @example + * + * _.union([2], [1, 2]); + * // => [2, 1] + */ + var union = baseRest(function(arrays) { + return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true)); + }); + + /** + * This method is like `_.union` except that it accepts `iteratee` which is + * invoked for each element of each `arrays` to generate the criterion by + * which uniqueness is computed. Result values are chosen from the first + * array in which the value occurs. The iteratee is invoked with one argument: + * (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Array} Returns the new array of combined values. + * @example + * + * _.unionBy([2.1], [1.2, 2.3], Math.floor); + * // => [2.1, 1.2] + * + * // The `_.property` iteratee shorthand. + * _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); + * // => [{ 'x': 1 }, { 'x': 2 }] + */ + var unionBy = baseRest(function(arrays) { + var iteratee = last(arrays); + if (isArrayLikeObject(iteratee)) { + iteratee = undefined; + } + return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), getIteratee(iteratee, 2)); + }); + + /** + * This method is like `_.union` except that it accepts `comparator` which + * is invoked to compare elements of `arrays`. Result values are chosen from + * the first array in which the value occurs. The comparator is invoked + * with two arguments: (arrVal, othVal). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of combined values. + * @example + * + * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; + * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]; + * + * _.unionWith(objects, others, _.isEqual); + * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }] + */ + var unionWith = baseRest(function(arrays) { + var comparator = last(arrays); + comparator = typeof comparator == 'function' ? comparator : undefined; + return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator); + }); + + /** + * Creates a duplicate-free version of an array, using + * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons, in which only the first occurrence of each element + * is kept. The order of result values is determined by the order they occur + * in the array. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to inspect. + * @returns {Array} Returns the new duplicate free array. + * @example + * + * _.uniq([2, 1, 2]); + * // => [2, 1] + */ + function uniq(array) { + return (array && array.length) ? baseUniq(array) : []; + } + + /** + * This method is like `_.uniq` except that it accepts `iteratee` which is + * invoked for each element in `array` to generate the criterion by which + * uniqueness is computed. The order of result values is determined by the + * order they occur in the array. The iteratee is invoked with one argument: + * (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Array} Returns the new duplicate free array. + * @example + * + * _.uniqBy([2.1, 1.2, 2.3], Math.floor); + * // => [2.1, 1.2] + * + * // The `_.property` iteratee shorthand. + * _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); + * // => [{ 'x': 1 }, { 'x': 2 }] + */ + function uniqBy(array, iteratee) { + return (array && array.length) ? baseUniq(array, getIteratee(iteratee, 2)) : []; + } + + /** + * This method is like `_.uniq` except that it accepts `comparator` which + * is invoked to compare elements of `array`. The order of result values is + * determined by the order they occur in the array.The comparator is invoked + * with two arguments: (arrVal, othVal). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new duplicate free array. + * @example + * + * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; + * + * _.uniqWith(objects, _.isEqual); + * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }] + */ + function uniqWith(array, comparator) { + comparator = typeof comparator == 'function' ? comparator : undefined; + return (array && array.length) ? baseUniq(array, undefined, comparator) : []; + } + + /** + * This method is like `_.zip` except that it accepts an array of grouped + * elements and creates an array regrouping the elements to their pre-zip + * configuration. + * + * @static + * @memberOf _ + * @since 1.2.0 + * @category Array + * @param {Array} array The array of grouped elements to process. + * @returns {Array} Returns the new array of regrouped elements. + * @example + * + * var zipped = _.zip(['a', 'b'], [1, 2], [true, false]); + * // => [['a', 1, true], ['b', 2, false]] + * + * _.unzip(zipped); + * // => [['a', 'b'], [1, 2], [true, false]] + */ + function unzip(array) { + if (!(array && array.length)) { + return []; + } + var length = 0; + array = arrayFilter(array, function(group) { + if (isArrayLikeObject(group)) { + length = nativeMax(group.length, length); + return true; + } + }); + return baseTimes(length, function(index) { + return arrayMap(array, baseProperty(index)); + }); + } + + /** + * This method is like `_.unzip` except that it accepts `iteratee` to specify + * how regrouped values should be combined. The iteratee is invoked with the + * elements of each group: (...group). + * + * @static + * @memberOf _ + * @since 3.8.0 + * @category Array + * @param {Array} array The array of grouped elements to process. + * @param {Function} [iteratee=_.identity] The function to combine + * regrouped values. + * @returns {Array} Returns the new array of regrouped elements. + * @example + * + * var zipped = _.zip([1, 2], [10, 20], [100, 200]); + * // => [[1, 10, 100], [2, 20, 200]] + * + * _.unzipWith(zipped, _.add); + * // => [3, 30, 300] + */ + function unzipWith(array, iteratee) { + if (!(array && array.length)) { + return []; + } + var result = unzip(array); + if (iteratee == null) { + return result; + } + return arrayMap(result, function(group) { + return apply(iteratee, undefined, group); + }); + } + + /** + * Creates an array excluding all given values using + * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * for equality comparisons. + * + * **Note:** Unlike `_.pull`, this method returns a new array. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {Array} array The array to inspect. + * @param {...*} [values] The values to exclude. + * @returns {Array} Returns the new array of filtered values. + * @see _.difference, _.xor + * @example + * + * _.without([2, 1, 2, 3], 1, 2); + * // => [3] + */ + var without = baseRest(function(array, values) { + return isArrayLikeObject(array) + ? baseDifference(array, values) + : []; + }); + + /** + * Creates an array of unique values that is the + * [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference) + * of the given arrays. The order of result values is determined by the order + * they occur in the arrays. + * + * @static + * @memberOf _ + * @since 2.4.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @returns {Array} Returns the new array of filtered values. + * @see _.difference, _.without + * @example + * + * _.xor([2, 1], [2, 3]); + * // => [1, 3] + */ + var xor = baseRest(function(arrays) { + return baseXor(arrayFilter(arrays, isArrayLikeObject)); + }); + + /** + * This method is like `_.xor` except that it accepts `iteratee` which is + * invoked for each element of each `arrays` to generate the criterion by + * which by which they're compared. The order of result values is determined + * by the order they occur in the arrays. The iteratee is invoked with one + * argument: (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Array} Returns the new array of filtered values. + * @example + * + * _.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor); + * // => [1.2, 3.4] + * + * // The `_.property` iteratee shorthand. + * _.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); + * // => [{ 'x': 2 }] + */ + var xorBy = baseRest(function(arrays) { + var iteratee = last(arrays); + if (isArrayLikeObject(iteratee)) { + iteratee = undefined; + } + return baseXor(arrayFilter(arrays, isArrayLikeObject), getIteratee(iteratee, 2)); + }); + + /** + * This method is like `_.xor` except that it accepts `comparator` which is + * invoked to compare elements of `arrays`. The order of result values is + * determined by the order they occur in the arrays. The comparator is invoked + * with two arguments: (arrVal, othVal). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Array + * @param {...Array} [arrays] The arrays to inspect. + * @param {Function} [comparator] The comparator invoked per element. + * @returns {Array} Returns the new array of filtered values. + * @example + * + * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; + * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]; + * + * _.xorWith(objects, others, _.isEqual); + * // => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }] + */ + var xorWith = baseRest(function(arrays) { + var comparator = last(arrays); + comparator = typeof comparator == 'function' ? comparator : undefined; + return baseXor(arrayFilter(arrays, isArrayLikeObject), undefined, comparator); + }); + + /** + * Creates an array of grouped elements, the first of which contains the + * first elements of the given arrays, the second of which contains the + * second elements of the given arrays, and so on. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Array + * @param {...Array} [arrays] The arrays to process. + * @returns {Array} Returns the new array of grouped elements. + * @example + * + * _.zip(['a', 'b'], [1, 2], [true, false]); + * // => [['a', 1, true], ['b', 2, false]] + */ + var zip = baseRest(unzip); + + /** + * This method is like `_.fromPairs` except that it accepts two arrays, + * one of property identifiers and one of corresponding values. + * + * @static + * @memberOf _ + * @since 0.4.0 + * @category Array + * @param {Array} [props=[]] The property identifiers. + * @param {Array} [values=[]] The property values. + * @returns {Object} Returns the new object. + * @example + * + * _.zipObject(['a', 'b'], [1, 2]); + * // => { 'a': 1, 'b': 2 } + */ + function zipObject(props, values) { + return baseZipObject(props || [], values || [], assignValue); + } + + /** + * This method is like `_.zipObject` except that it supports property paths. + * + * @static + * @memberOf _ + * @since 4.1.0 + * @category Array + * @param {Array} [props=[]] The property identifiers. + * @param {Array} [values=[]] The property values. + * @returns {Object} Returns the new object. + * @example + * + * _.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]); + * // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } } + */ + function zipObjectDeep(props, values) { + return baseZipObject(props || [], values || [], baseSet); + } + + /** + * This method is like `_.zip` except that it accepts `iteratee` to specify + * how grouped values should be combined. The iteratee is invoked with the + * elements of each group: (...group). + * + * @static + * @memberOf _ + * @since 3.8.0 + * @category Array + * @param {...Array} [arrays] The arrays to process. + * @param {Function} [iteratee=_.identity] The function to combine + * grouped values. + * @returns {Array} Returns the new array of grouped elements. + * @example + * + * _.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) { + * return a + b + c; + * }); + * // => [111, 222] + */ + var zipWith = baseRest(function(arrays) { + var length = arrays.length, + iteratee = length > 1 ? arrays[length - 1] : undefined; + + iteratee = typeof iteratee == 'function' ? (arrays.pop(), iteratee) : undefined; + return unzipWith(arrays, iteratee); + }); + + /*------------------------------------------------------------------------*/ + + /** + * Creates a `lodash` wrapper instance that wraps `value` with explicit method + * chain sequences enabled. The result of such sequences must be unwrapped + * with `_#value`. + * + * @static + * @memberOf _ + * @since 1.3.0 + * @category Seq + * @param {*} value The value to wrap. + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 }, + * { 'user': 'pebbles', 'age': 1 } + * ]; + * + * var youngest = _ + * .chain(users) + * .sortBy('age') + * .map(function(o) { + * return o.user + ' is ' + o.age; + * }) + * .head() + * .value(); + * // => 'pebbles is 1' + */ + function chain(value) { + var result = lodash(value); + result.__chain__ = true; + return result; + } + + /** + * This method invokes `interceptor` and returns `value`. The interceptor + * is invoked with one argument; (value). The purpose of this method is to + * "tap into" a method chain sequence in order to modify intermediate results. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Seq + * @param {*} value The value to provide to `interceptor`. + * @param {Function} interceptor The function to invoke. + * @returns {*} Returns `value`. + * @example + * + * _([1, 2, 3]) + * .tap(function(array) { + * // Mutate input array. + * array.pop(); + * }) + * .reverse() + * .value(); + * // => [2, 1] + */ + function tap(value, interceptor) { + interceptor(value); + return value; + } + + /** + * This method is like `_.tap` except that it returns the result of `interceptor`. + * The purpose of this method is to "pass thru" values replacing intermediate + * results in a method chain sequence. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Seq + * @param {*} value The value to provide to `interceptor`. + * @param {Function} interceptor The function to invoke. + * @returns {*} Returns the result of `interceptor`. + * @example + * + * _(' abc ') + * .chain() + * .trim() + * .thru(function(value) { + * return [value]; + * }) + * .value(); + * // => ['abc'] + */ + function thru(value, interceptor) { + return interceptor(value); + } + + /** + * This method is the wrapper version of `_.at`. + * + * @name at + * @memberOf _ + * @since 1.0.0 + * @category Seq + * @param {...(string|string[])} [paths] The property paths to pick. + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 3 } }, 4] }; + * + * _(object).at(['a[0].b.c', 'a[1]']).value(); + * // => [3, 4] + */ + var wrapperAt = flatRest(function(paths) { + var length = paths.length, + start = length ? paths[0] : 0, + value = this.__wrapped__, + interceptor = function(object) { return baseAt(object, paths); }; + + if (length > 1 || this.__actions__.length || + !(value instanceof LazyWrapper) || !isIndex(start)) { + return this.thru(interceptor); + } + value = value.slice(start, +start + (length ? 1 : 0)); + value.__actions__.push({ + 'func': thru, + 'args': [interceptor], + 'thisArg': undefined + }); + return new LodashWrapper(value, this.__chain__).thru(function(array) { + if (length && !array.length) { + array.push(undefined); + } + return array; + }); + }); + + /** + * Creates a `lodash` wrapper instance with explicit method chain sequences enabled. + * + * @name chain + * @memberOf _ + * @since 0.1.0 + * @category Seq + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 } + * ]; + * + * // A sequence without explicit chaining. + * _(users).head(); + * // => { 'user': 'barney', 'age': 36 } + * + * // A sequence with explicit chaining. + * _(users) + * .chain() + * .head() + * .pick('user') + * .value(); + * // => { 'user': 'barney' } + */ + function wrapperChain() { + return chain(this); + } + + /** + * Executes the chain sequence and returns the wrapped result. + * + * @name commit + * @memberOf _ + * @since 3.2.0 + * @category Seq + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * var array = [1, 2]; + * var wrapped = _(array).push(3); + * + * console.log(array); + * // => [1, 2] + * + * wrapped = wrapped.commit(); + * console.log(array); + * // => [1, 2, 3] + * + * wrapped.last(); + * // => 3 + * + * console.log(array); + * // => [1, 2, 3] + */ + function wrapperCommit() { + return new LodashWrapper(this.value(), this.__chain__); + } + + /** + * Gets the next value on a wrapped object following the + * [iterator protocol](https://mdn.io/iteration_protocols#iterator). + * + * @name next + * @memberOf _ + * @since 4.0.0 + * @category Seq + * @returns {Object} Returns the next iterator value. + * @example + * + * var wrapped = _([1, 2]); + * + * wrapped.next(); + * // => { 'done': false, 'value': 1 } + * + * wrapped.next(); + * // => { 'done': false, 'value': 2 } + * + * wrapped.next(); + * // => { 'done': true, 'value': undefined } + */ + function wrapperNext() { + if (this.__values__ === undefined) { + this.__values__ = toArray(this.value()); + } + var done = this.__index__ >= this.__values__.length, + value = done ? undefined : this.__values__[this.__index__++]; + + return { 'done': done, 'value': value }; + } + + /** + * Enables the wrapper to be iterable. + * + * @name Symbol.iterator + * @memberOf _ + * @since 4.0.0 + * @category Seq + * @returns {Object} Returns the wrapper object. + * @example + * + * var wrapped = _([1, 2]); + * + * wrapped[Symbol.iterator]() === wrapped; + * // => true + * + * Array.from(wrapped); + * // => [1, 2] + */ + function wrapperToIterator() { + return this; + } + + /** + * Creates a clone of the chain sequence planting `value` as the wrapped value. + * + * @name plant + * @memberOf _ + * @since 3.2.0 + * @category Seq + * @param {*} value The value to plant. + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * function square(n) { + * return n * n; + * } + * + * var wrapped = _([1, 2]).map(square); + * var other = wrapped.plant([3, 4]); + * + * other.value(); + * // => [9, 16] + * + * wrapped.value(); + * // => [1, 4] + */ + function wrapperPlant(value) { + var result, + parent = this; + + while (parent instanceof baseLodash) { + var clone = wrapperClone(parent); + clone.__index__ = 0; + clone.__values__ = undefined; + if (result) { + previous.__wrapped__ = clone; + } else { + result = clone; + } + var previous = clone; + parent = parent.__wrapped__; + } + previous.__wrapped__ = value; + return result; + } + + /** + * This method is the wrapper version of `_.reverse`. + * + * **Note:** This method mutates the wrapped array. + * + * @name reverse + * @memberOf _ + * @since 0.1.0 + * @category Seq + * @returns {Object} Returns the new `lodash` wrapper instance. + * @example + * + * var array = [1, 2, 3]; + * + * _(array).reverse().value() + * // => [3, 2, 1] + * + * console.log(array); + * // => [3, 2, 1] + */ + function wrapperReverse() { + var value = this.__wrapped__; + if (value instanceof LazyWrapper) { + var wrapped = value; + if (this.__actions__.length) { + wrapped = new LazyWrapper(this); + } + wrapped = wrapped.reverse(); + wrapped.__actions__.push({ + 'func': thru, + 'args': [reverse], + 'thisArg': undefined + }); + return new LodashWrapper(wrapped, this.__chain__); + } + return this.thru(reverse); + } + + /** + * Executes the chain sequence to resolve the unwrapped value. + * + * @name value + * @memberOf _ + * @since 0.1.0 + * @alias toJSON, valueOf + * @category Seq + * @returns {*} Returns the resolved unwrapped value. + * @example + * + * _([1, 2, 3]).value(); + * // => [1, 2, 3] + */ + function wrapperValue() { + return baseWrapperValue(this.__wrapped__, this.__actions__); + } + + /*------------------------------------------------------------------------*/ + + /** + * Creates an object composed of keys generated from the results of running + * each element of `collection` thru `iteratee`. The corresponding value of + * each key is the number of times the key was returned by `iteratee`. The + * iteratee is invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 0.5.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The iteratee to transform keys. + * @returns {Object} Returns the composed aggregate object. + * @example + * + * _.countBy([6.1, 4.2, 6.3], Math.floor); + * // => { '4': 1, '6': 2 } + * + * // The `_.property` iteratee shorthand. + * _.countBy(['one', 'two', 'three'], 'length'); + * // => { '3': 2, '5': 1 } + */ + var countBy = createAggregator(function(result, value, key) { + if (hasOwnProperty.call(result, key)) { + ++result[key]; + } else { + baseAssignValue(result, key, 1); + } + }); + + /** + * Checks if `predicate` returns truthy for **all** elements of `collection`. + * Iteration is stopped once `predicate` returns falsey. The predicate is + * invoked with three arguments: (value, index|key, collection). + * + * **Note:** This method returns `true` for + * [empty collections](https://en.wikipedia.org/wiki/Empty_set) because + * [everything is true](https://en.wikipedia.org/wiki/Vacuous_truth) of + * elements of empty collections. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {boolean} Returns `true` if all elements pass the predicate check, + * else `false`. + * @example + * + * _.every([true, 1, null, 'yes'], Boolean); + * // => false + * + * var users = [ + * { 'user': 'barney', 'age': 36, 'active': false }, + * { 'user': 'fred', 'age': 40, 'active': false } + * ]; + * + * // The `_.matches` iteratee shorthand. + * _.every(users, { 'user': 'barney', 'active': false }); + * // => false + * + * // The `_.matchesProperty` iteratee shorthand. + * _.every(users, ['active', false]); + * // => true + * + * // The `_.property` iteratee shorthand. + * _.every(users, 'active'); + * // => false + */ + function every(collection, predicate, guard) { + var func = isArray(collection) ? arrayEvery : baseEvery; + if (guard && isIterateeCall(collection, predicate, guard)) { + predicate = undefined; + } + return func(collection, getIteratee(predicate, 3)); + } + + /** + * Iterates over elements of `collection`, returning an array of all elements + * `predicate` returns truthy for. The predicate is invoked with three + * arguments: (value, index|key, collection). + * + * **Note:** Unlike `_.remove`, this method returns a new array. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the new filtered array. + * @see _.reject + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36, 'active': true }, + * { 'user': 'fred', 'age': 40, 'active': false } + * ]; + * + * _.filter(users, function(o) { return !o.active; }); + * // => objects for ['fred'] + * + * // The `_.matches` iteratee shorthand. + * _.filter(users, { 'age': 36, 'active': true }); + * // => objects for ['barney'] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.filter(users, ['active', false]); + * // => objects for ['fred'] + * + * // The `_.property` iteratee shorthand. + * _.filter(users, 'active'); + * // => objects for ['barney'] + * + * // Combining several predicates using `_.overEvery` or `_.overSome`. + * _.filter(users, _.overSome([{ 'age': 36 }, ['age', 40]])); + * // => objects for ['fred', 'barney'] + */ + function filter(collection, predicate) { + var func = isArray(collection) ? arrayFilter : baseFilter; + return func(collection, getIteratee(predicate, 3)); + } + + /** + * Iterates over elements of `collection`, returning the first element + * `predicate` returns truthy for. The predicate is invoked with three + * arguments: (value, index|key, collection). + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to inspect. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @param {number} [fromIndex=0] The index to search from. + * @returns {*} Returns the matched element, else `undefined`. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36, 'active': true }, + * { 'user': 'fred', 'age': 40, 'active': false }, + * { 'user': 'pebbles', 'age': 1, 'active': true } + * ]; + * + * _.find(users, function(o) { return o.age < 40; }); + * // => object for 'barney' + * + * // The `_.matches` iteratee shorthand. + * _.find(users, { 'age': 1, 'active': true }); + * // => object for 'pebbles' + * + * // The `_.matchesProperty` iteratee shorthand. + * _.find(users, ['active', false]); + * // => object for 'fred' + * + * // The `_.property` iteratee shorthand. + * _.find(users, 'active'); + * // => object for 'barney' + */ + var find = createFind(findIndex); + + /** + * This method is like `_.find` except that it iterates over elements of + * `collection` from right to left. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Collection + * @param {Array|Object} collection The collection to inspect. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @param {number} [fromIndex=collection.length-1] The index to search from. + * @returns {*} Returns the matched element, else `undefined`. + * @example + * + * _.findLast([1, 2, 3, 4], function(n) { + * return n % 2 == 1; + * }); + * // => 3 + */ + var findLast = createFind(findLastIndex); + + /** + * Creates a flattened array of values by running each element in `collection` + * thru `iteratee` and flattening the mapped results. The iteratee is invoked + * with three arguments: (value, index|key, collection). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Array} Returns the new flattened array. + * @example + * + * function duplicate(n) { + * return [n, n]; + * } + * + * _.flatMap([1, 2], duplicate); + * // => [1, 1, 2, 2] + */ + function flatMap(collection, iteratee) { + return baseFlatten(map(collection, iteratee), 1); + } + + /** + * This method is like `_.flatMap` except that it recursively flattens the + * mapped results. + * + * @static + * @memberOf _ + * @since 4.7.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Array} Returns the new flattened array. + * @example + * + * function duplicate(n) { + * return [[[n, n]]]; + * } + * + * _.flatMapDeep([1, 2], duplicate); + * // => [1, 1, 2, 2] + */ + function flatMapDeep(collection, iteratee) { + return baseFlatten(map(collection, iteratee), INFINITY); + } + + /** + * This method is like `_.flatMap` except that it recursively flattens the + * mapped results up to `depth` times. + * + * @static + * @memberOf _ + * @since 4.7.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {number} [depth=1] The maximum recursion depth. + * @returns {Array} Returns the new flattened array. + * @example + * + * function duplicate(n) { + * return [[[n, n]]]; + * } + * + * _.flatMapDepth([1, 2], duplicate, 2); + * // => [[1, 1], [2, 2]] + */ + function flatMapDepth(collection, iteratee, depth) { + depth = depth === undefined ? 1 : toInteger(depth); + return baseFlatten(map(collection, iteratee), depth); + } + + /** + * Iterates over elements of `collection` and invokes `iteratee` for each element. + * The iteratee is invoked with three arguments: (value, index|key, collection). + * Iteratee functions may exit iteration early by explicitly returning `false`. + * + * **Note:** As with other "Collections" methods, objects with a "length" + * property are iterated like arrays. To avoid this behavior use `_.forIn` + * or `_.forOwn` for object iteration. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @alias each + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Array|Object} Returns `collection`. + * @see _.forEachRight + * @example + * + * _.forEach([1, 2], function(value) { + * console.log(value); + * }); + * // => Logs `1` then `2`. + * + * _.forEach({ 'a': 1, 'b': 2 }, function(value, key) { + * console.log(key); + * }); + * // => Logs 'a' then 'b' (iteration order is not guaranteed). + */ + function forEach(collection, iteratee) { + var func = isArray(collection) ? arrayEach : baseEach; + return func(collection, getIteratee(iteratee, 3)); + } + + /** + * This method is like `_.forEach` except that it iterates over elements of + * `collection` from right to left. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @alias eachRight + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Array|Object} Returns `collection`. + * @see _.forEach + * @example + * + * _.forEachRight([1, 2], function(value) { + * console.log(value); + * }); + * // => Logs `2` then `1`. + */ + function forEachRight(collection, iteratee) { + var func = isArray(collection) ? arrayEachRight : baseEachRight; + return func(collection, getIteratee(iteratee, 3)); + } + + /** + * Creates an object composed of keys generated from the results of running + * each element of `collection` thru `iteratee`. The order of grouped values + * is determined by the order they occur in `collection`. The corresponding + * value of each key is an array of elements responsible for generating the + * key. The iteratee is invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The iteratee to transform keys. + * @returns {Object} Returns the composed aggregate object. + * @example + * + * _.groupBy([6.1, 4.2, 6.3], Math.floor); + * // => { '4': [4.2], '6': [6.1, 6.3] } + * + * // The `_.property` iteratee shorthand. + * _.groupBy(['one', 'two', 'three'], 'length'); + * // => { '3': ['one', 'two'], '5': ['three'] } + */ + var groupBy = createAggregator(function(result, value, key) { + if (hasOwnProperty.call(result, key)) { + result[key].push(value); + } else { + baseAssignValue(result, key, [value]); + } + }); + + /** + * Checks if `value` is in `collection`. If `collection` is a string, it's + * checked for a substring of `value`, otherwise + * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * is used for equality comparisons. If `fromIndex` is negative, it's used as + * the offset from the end of `collection`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object|string} collection The collection to inspect. + * @param {*} value The value to search for. + * @param {number} [fromIndex=0] The index to search from. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.reduce`. + * @returns {boolean} Returns `true` if `value` is found, else `false`. + * @example + * + * _.includes([1, 2, 3], 1); + * // => true + * + * _.includes([1, 2, 3], 1, 2); + * // => false + * + * _.includes({ 'a': 1, 'b': 2 }, 1); + * // => true + * + * _.includes('abcd', 'bc'); + * // => true + */ + function includes(collection, value, fromIndex, guard) { + collection = isArrayLike(collection) ? collection : values(collection); + fromIndex = (fromIndex && !guard) ? toInteger(fromIndex) : 0; + + var length = collection.length; + if (fromIndex < 0) { + fromIndex = nativeMax(length + fromIndex, 0); + } + return isString(collection) + ? (fromIndex <= length && collection.indexOf(value, fromIndex) > -1) + : (!!length && baseIndexOf(collection, value, fromIndex) > -1); + } + + /** + * Invokes the method at `path` of each element in `collection`, returning + * an array of the results of each invoked method. Any additional arguments + * are provided to each invoked method. If `path` is a function, it's invoked + * for, and `this` bound to, each element in `collection`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Array|Function|string} path The path of the method to invoke or + * the function invoked per iteration. + * @param {...*} [args] The arguments to invoke each method with. + * @returns {Array} Returns the array of results. + * @example + * + * _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort'); + * // => [[1, 5, 7], [1, 2, 3]] + * + * _.invokeMap([123, 456], String.prototype.split, ''); + * // => [['1', '2', '3'], ['4', '5', '6']] + */ + var invokeMap = baseRest(function(collection, path, args) { + var index = -1, + isFunc = typeof path == 'function', + result = isArrayLike(collection) ? Array(collection.length) : []; + + baseEach(collection, function(value) { + result[++index] = isFunc ? apply(path, value, args) : baseInvoke(value, path, args); + }); + return result; + }); + + /** + * Creates an object composed of keys generated from the results of running + * each element of `collection` thru `iteratee`. The corresponding value of + * each key is the last element responsible for generating the key. The + * iteratee is invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The iteratee to transform keys. + * @returns {Object} Returns the composed aggregate object. + * @example + * + * var array = [ + * { 'dir': 'left', 'code': 97 }, + * { 'dir': 'right', 'code': 100 } + * ]; + * + * _.keyBy(array, function(o) { + * return String.fromCharCode(o.code); + * }); + * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } + * + * _.keyBy(array, 'dir'); + * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } + */ + var keyBy = createAggregator(function(result, value, key) { + baseAssignValue(result, key, value); + }); + + /** + * Creates an array of values by running each element in `collection` thru + * `iteratee`. The iteratee is invoked with three arguments: + * (value, index|key, collection). + * + * Many lodash methods are guarded to work as iteratees for methods like + * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`. + * + * The guarded methods are: + * `ary`, `chunk`, `curry`, `curryRight`, `drop`, `dropRight`, `every`, + * `fill`, `invert`, `parseInt`, `random`, `range`, `rangeRight`, `repeat`, + * `sampleSize`, `slice`, `some`, `sortBy`, `split`, `take`, `takeRight`, + * `template`, `trim`, `trimEnd`, `trimStart`, and `words` + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Array} Returns the new mapped array. + * @example + * + * function square(n) { + * return n * n; + * } + * + * _.map([4, 8], square); + * // => [16, 64] + * + * _.map({ 'a': 4, 'b': 8 }, square); + * // => [16, 64] (iteration order is not guaranteed) + * + * var users = [ + * { 'user': 'barney' }, + * { 'user': 'fred' } + * ]; + * + * // The `_.property` iteratee shorthand. + * _.map(users, 'user'); + * // => ['barney', 'fred'] + */ + function map(collection, iteratee) { + var func = isArray(collection) ? arrayMap : baseMap; + return func(collection, getIteratee(iteratee, 3)); + } + + /** + * This method is like `_.sortBy` except that it allows specifying the sort + * orders of the iteratees to sort by. If `orders` is unspecified, all values + * are sorted in ascending order. Otherwise, specify an order of "desc" for + * descending or "asc" for ascending sort order of corresponding values. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Array[]|Function[]|Object[]|string[]} [iteratees=[_.identity]] + * The iteratees to sort by. + * @param {string[]} [orders] The sort orders of `iteratees`. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.reduce`. + * @returns {Array} Returns the new sorted array. + * @example + * + * var users = [ + * { 'user': 'fred', 'age': 48 }, + * { 'user': 'barney', 'age': 34 }, + * { 'user': 'fred', 'age': 40 }, + * { 'user': 'barney', 'age': 36 } + * ]; + * + * // Sort by `user` in ascending order and by `age` in descending order. + * _.orderBy(users, ['user', 'age'], ['asc', 'desc']); + * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]] + */ + function orderBy(collection, iteratees, orders, guard) { + if (collection == null) { + return []; + } + if (!isArray(iteratees)) { + iteratees = iteratees == null ? [] : [iteratees]; + } + orders = guard ? undefined : orders; + if (!isArray(orders)) { + orders = orders == null ? [] : [orders]; + } + return baseOrderBy(collection, iteratees, orders); + } + + /** + * Creates an array of elements split into two groups, the first of which + * contains elements `predicate` returns truthy for, the second of which + * contains elements `predicate` returns falsey for. The predicate is + * invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the array of grouped elements. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36, 'active': false }, + * { 'user': 'fred', 'age': 40, 'active': true }, + * { 'user': 'pebbles', 'age': 1, 'active': false } + * ]; + * + * _.partition(users, function(o) { return o.active; }); + * // => objects for [['fred'], ['barney', 'pebbles']] + * + * // The `_.matches` iteratee shorthand. + * _.partition(users, { 'age': 1, 'active': false }); + * // => objects for [['pebbles'], ['barney', 'fred']] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.partition(users, ['active', false]); + * // => objects for [['barney', 'pebbles'], ['fred']] + * + * // The `_.property` iteratee shorthand. + * _.partition(users, 'active'); + * // => objects for [['fred'], ['barney', 'pebbles']] + */ + var partition = createAggregator(function(result, value, key) { + result[key ? 0 : 1].push(value); + }, function() { return [[], []]; }); + + /** + * Reduces `collection` to a value which is the accumulated result of running + * each element in `collection` thru `iteratee`, where each successive + * invocation is supplied the return value of the previous. If `accumulator` + * is not given, the first element of `collection` is used as the initial + * value. The iteratee is invoked with four arguments: + * (accumulator, value, index|key, collection). + * + * Many lodash methods are guarded to work as iteratees for methods like + * `_.reduce`, `_.reduceRight`, and `_.transform`. + * + * The guarded methods are: + * `assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `orderBy`, + * and `sortBy` + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [accumulator] The initial value. + * @returns {*} Returns the accumulated value. + * @see _.reduceRight + * @example + * + * _.reduce([1, 2], function(sum, n) { + * return sum + n; + * }, 0); + * // => 3 + * + * _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) { + * (result[value] || (result[value] = [])).push(key); + * return result; + * }, {}); + * // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed) + */ + function reduce(collection, iteratee, accumulator) { + var func = isArray(collection) ? arrayReduce : baseReduce, + initAccum = arguments.length < 3; + + return func(collection, getIteratee(iteratee, 4), accumulator, initAccum, baseEach); + } + + /** + * This method is like `_.reduce` except that it iterates over elements of + * `collection` from right to left. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [accumulator] The initial value. + * @returns {*} Returns the accumulated value. + * @see _.reduce + * @example + * + * var array = [[0, 1], [2, 3], [4, 5]]; + * + * _.reduceRight(array, function(flattened, other) { + * return flattened.concat(other); + * }, []); + * // => [4, 5, 2, 3, 0, 1] + */ + function reduceRight(collection, iteratee, accumulator) { + var func = isArray(collection) ? arrayReduceRight : baseReduce, + initAccum = arguments.length < 3; + + return func(collection, getIteratee(iteratee, 4), accumulator, initAccum, baseEachRight); + } + + /** + * The opposite of `_.filter`; this method returns the elements of `collection` + * that `predicate` does **not** return truthy for. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {Array} Returns the new filtered array. + * @see _.filter + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36, 'active': false }, + * { 'user': 'fred', 'age': 40, 'active': true } + * ]; + * + * _.reject(users, function(o) { return !o.active; }); + * // => objects for ['fred'] + * + * // The `_.matches` iteratee shorthand. + * _.reject(users, { 'age': 40, 'active': true }); + * // => objects for ['barney'] + * + * // The `_.matchesProperty` iteratee shorthand. + * _.reject(users, ['active', false]); + * // => objects for ['fred'] + * + * // The `_.property` iteratee shorthand. + * _.reject(users, 'active'); + * // => objects for ['barney'] + */ + function reject(collection, predicate) { + var func = isArray(collection) ? arrayFilter : baseFilter; + return func(collection, negate(getIteratee(predicate, 3))); + } + + /** + * Gets a random element from `collection`. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Collection + * @param {Array|Object} collection The collection to sample. + * @returns {*} Returns the random element. + * @example + * + * _.sample([1, 2, 3, 4]); + * // => 2 + */ + function sample(collection) { + var func = isArray(collection) ? arraySample : baseSample; + return func(collection); + } + + /** + * Gets `n` random elements at unique keys from `collection` up to the + * size of `collection`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Collection + * @param {Array|Object} collection The collection to sample. + * @param {number} [n=1] The number of elements to sample. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Array} Returns the random elements. + * @example + * + * _.sampleSize([1, 2, 3], 2); + * // => [3, 1] + * + * _.sampleSize([1, 2, 3], 4); + * // => [2, 3, 1] + */ + function sampleSize(collection, n, guard) { + if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) { + n = 1; + } else { + n = toInteger(n); + } + var func = isArray(collection) ? arraySampleSize : baseSampleSize; + return func(collection, n); + } + + /** + * Creates an array of shuffled values, using a version of the + * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle). + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to shuffle. + * @returns {Array} Returns the new shuffled array. + * @example + * + * _.shuffle([1, 2, 3, 4]); + * // => [4, 1, 3, 2] + */ + function shuffle(collection) { + var func = isArray(collection) ? arrayShuffle : baseShuffle; + return func(collection); + } + + /** + * Gets the size of `collection` by returning its length for array-like + * values or the number of own enumerable string keyed properties for objects. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object|string} collection The collection to inspect. + * @returns {number} Returns the collection size. + * @example + * + * _.size([1, 2, 3]); + * // => 3 + * + * _.size({ 'a': 1, 'b': 2 }); + * // => 2 + * + * _.size('pebbles'); + * // => 7 + */ + function size(collection) { + if (collection == null) { + return 0; + } + if (isArrayLike(collection)) { + return isString(collection) ? stringSize(collection) : collection.length; + } + var tag = getTag(collection); + if (tag == mapTag || tag == setTag) { + return collection.size; + } + return baseKeys(collection).length; + } + + /** + * Checks if `predicate` returns truthy for **any** element of `collection`. + * Iteration is stopped once `predicate` returns truthy. The predicate is + * invoked with three arguments: (value, index|key, collection). + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {boolean} Returns `true` if any element passes the predicate check, + * else `false`. + * @example + * + * _.some([null, 0, 'yes', false], Boolean); + * // => true + * + * var users = [ + * { 'user': 'barney', 'active': true }, + * { 'user': 'fred', 'active': false } + * ]; + * + * // The `_.matches` iteratee shorthand. + * _.some(users, { 'user': 'barney', 'active': false }); + * // => false + * + * // The `_.matchesProperty` iteratee shorthand. + * _.some(users, ['active', false]); + * // => true + * + * // The `_.property` iteratee shorthand. + * _.some(users, 'active'); + * // => true + */ + function some(collection, predicate, guard) { + var func = isArray(collection) ? arraySome : baseSome; + if (guard && isIterateeCall(collection, predicate, guard)) { + predicate = undefined; + } + return func(collection, getIteratee(predicate, 3)); + } + + /** + * Creates an array of elements, sorted in ascending order by the results of + * running each element in a collection thru each iteratee. This method + * performs a stable sort, that is, it preserves the original sort order of + * equal elements. The iteratees are invoked with one argument: (value). + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {...(Function|Function[])} [iteratees=[_.identity]] + * The iteratees to sort by. + * @returns {Array} Returns the new sorted array. + * @example + * + * var users = [ + * { 'user': 'fred', 'age': 48 }, + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 30 }, + * { 'user': 'barney', 'age': 34 } + * ]; + * + * _.sortBy(users, [function(o) { return o.user; }]); + * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 30]] + * + * _.sortBy(users, ['user', 'age']); + * // => objects for [['barney', 34], ['barney', 36], ['fred', 30], ['fred', 48]] + */ + var sortBy = baseRest(function(collection, iteratees) { + if (collection == null) { + return []; + } + var length = iteratees.length; + if (length > 1 && isIterateeCall(collection, iteratees[0], iteratees[1])) { + iteratees = []; + } else if (length > 2 && isIterateeCall(iteratees[0], iteratees[1], iteratees[2])) { + iteratees = [iteratees[0]]; + } + return baseOrderBy(collection, baseFlatten(iteratees, 1), []); + }); + + /*------------------------------------------------------------------------*/ + + /** + * Gets the timestamp of the number of milliseconds that have elapsed since + * the Unix epoch (1 January 1970 00:00:00 UTC). + * + * @static + * @memberOf _ + * @since 2.4.0 + * @category Date + * @returns {number} Returns the timestamp. + * @example + * + * _.defer(function(stamp) { + * console.log(_.now() - stamp); + * }, _.now()); + * // => Logs the number of milliseconds it took for the deferred invocation. + */ + var now = ctxNow || function() { + return root.Date.now(); + }; + + /*------------------------------------------------------------------------*/ + + /** + * The opposite of `_.before`; this method creates a function that invokes + * `func` once it's called `n` or more times. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {number} n The number of calls before `func` is invoked. + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new restricted function. + * @example + * + * var saves = ['profile', 'settings']; + * + * var done = _.after(saves.length, function() { + * console.log('done saving!'); + * }); + * + * _.forEach(saves, function(type) { + * asyncSave({ 'type': type, 'complete': done }); + * }); + * // => Logs 'done saving!' after the two async saves have completed. + */ + function after(n, func) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + n = toInteger(n); + return function() { + if (--n < 1) { + return func.apply(this, arguments); + } + }; + } + + /** + * Creates a function that invokes `func`, with up to `n` arguments, + * ignoring any additional arguments. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Function + * @param {Function} func The function to cap arguments for. + * @param {number} [n=func.length] The arity cap. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Function} Returns the new capped function. + * @example + * + * _.map(['6', '8', '10'], _.ary(parseInt, 1)); + * // => [6, 8, 10] + */ + function ary(func, n, guard) { + n = guard ? undefined : n; + n = (func && n == null) ? func.length : n; + return createWrap(func, WRAP_ARY_FLAG, undefined, undefined, undefined, undefined, n); + } + + /** + * Creates a function that invokes `func`, with the `this` binding and arguments + * of the created function, while it's called less than `n` times. Subsequent + * calls to the created function return the result of the last `func` invocation. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Function + * @param {number} n The number of calls at which `func` is no longer invoked. + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new restricted function. + * @example + * + * jQuery(element).on('click', _.before(5, addContactToList)); + * // => Allows adding up to 4 contacts to the list. + */ + function before(n, func) { + var result; + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + n = toInteger(n); + return function() { + if (--n > 0) { + result = func.apply(this, arguments); + } + if (n <= 1) { + func = undefined; + } + return result; + }; + } + + /** + * Creates a function that invokes `func` with the `this` binding of `thisArg` + * and `partials` prepended to the arguments it receives. + * + * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds, + * may be used as a placeholder for partially applied arguments. + * + * **Note:** Unlike native `Function#bind`, this method doesn't set the "length" + * property of bound functions. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to bind. + * @param {*} thisArg The `this` binding of `func`. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new bound function. + * @example + * + * function greet(greeting, punctuation) { + * return greeting + ' ' + this.user + punctuation; + * } + * + * var object = { 'user': 'fred' }; + * + * var bound = _.bind(greet, object, 'hi'); + * bound('!'); + * // => 'hi fred!' + * + * // Bound with placeholders. + * var bound = _.bind(greet, object, _, '!'); + * bound('hi'); + * // => 'hi fred!' + */ + var bind = baseRest(function(func, thisArg, partials) { + var bitmask = WRAP_BIND_FLAG; + if (partials.length) { + var holders = replaceHolders(partials, getHolder(bind)); + bitmask |= WRAP_PARTIAL_FLAG; + } + return createWrap(func, bitmask, thisArg, partials, holders); + }); + + /** + * Creates a function that invokes the method at `object[key]` with `partials` + * prepended to the arguments it receives. + * + * This method differs from `_.bind` by allowing bound functions to reference + * methods that may be redefined or don't yet exist. See + * [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern) + * for more details. + * + * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for partially applied arguments. + * + * @static + * @memberOf _ + * @since 0.10.0 + * @category Function + * @param {Object} object The object to invoke the method on. + * @param {string} key The key of the method. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new bound function. + * @example + * + * var object = { + * 'user': 'fred', + * 'greet': function(greeting, punctuation) { + * return greeting + ' ' + this.user + punctuation; + * } + * }; + * + * var bound = _.bindKey(object, 'greet', 'hi'); + * bound('!'); + * // => 'hi fred!' + * + * object.greet = function(greeting, punctuation) { + * return greeting + 'ya ' + this.user + punctuation; + * }; + * + * bound('!'); + * // => 'hiya fred!' + * + * // Bound with placeholders. + * var bound = _.bindKey(object, 'greet', _, '!'); + * bound('hi'); + * // => 'hiya fred!' + */ + var bindKey = baseRest(function(object, key, partials) { + var bitmask = WRAP_BIND_FLAG | WRAP_BIND_KEY_FLAG; + if (partials.length) { + var holders = replaceHolders(partials, getHolder(bindKey)); + bitmask |= WRAP_PARTIAL_FLAG; + } + return createWrap(key, bitmask, object, partials, holders); + }); + + /** + * Creates a function that accepts arguments of `func` and either invokes + * `func` returning its result, if at least `arity` number of arguments have + * been provided, or returns a function that accepts the remaining `func` + * arguments, and so on. The arity of `func` may be specified if `func.length` + * is not sufficient. + * + * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds, + * may be used as a placeholder for provided arguments. + * + * **Note:** This method doesn't set the "length" property of curried functions. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Function + * @param {Function} func The function to curry. + * @param {number} [arity=func.length] The arity of `func`. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Function} Returns the new curried function. + * @example + * + * var abc = function(a, b, c) { + * return [a, b, c]; + * }; + * + * var curried = _.curry(abc); + * + * curried(1)(2)(3); + * // => [1, 2, 3] + * + * curried(1, 2)(3); + * // => [1, 2, 3] + * + * curried(1, 2, 3); + * // => [1, 2, 3] + * + * // Curried with placeholders. + * curried(1)(_, 3)(2); + * // => [1, 2, 3] + */ + function curry(func, arity, guard) { + arity = guard ? undefined : arity; + var result = createWrap(func, WRAP_CURRY_FLAG, undefined, undefined, undefined, undefined, undefined, arity); + result.placeholder = curry.placeholder; + return result; + } + + /** + * This method is like `_.curry` except that arguments are applied to `func` + * in the manner of `_.partialRight` instead of `_.partial`. + * + * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for provided arguments. + * + * **Note:** This method doesn't set the "length" property of curried functions. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Function + * @param {Function} func The function to curry. + * @param {number} [arity=func.length] The arity of `func`. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Function} Returns the new curried function. + * @example + * + * var abc = function(a, b, c) { + * return [a, b, c]; + * }; + * + * var curried = _.curryRight(abc); + * + * curried(3)(2)(1); + * // => [1, 2, 3] + * + * curried(2, 3)(1); + * // => [1, 2, 3] + * + * curried(1, 2, 3); + * // => [1, 2, 3] + * + * // Curried with placeholders. + * curried(3)(1, _)(2); + * // => [1, 2, 3] + */ + function curryRight(func, arity, guard) { + arity = guard ? undefined : arity; + var result = createWrap(func, WRAP_CURRY_RIGHT_FLAG, undefined, undefined, undefined, undefined, undefined, arity); + result.placeholder = curryRight.placeholder; + return result; + } + + /** + * Creates a debounced function that delays invoking `func` until after `wait` + * milliseconds have elapsed since the last time the debounced function was + * invoked. The debounced function comes with a `cancel` method to cancel + * delayed `func` invocations and a `flush` method to immediately invoke them. + * Provide `options` to indicate whether `func` should be invoked on the + * leading and/or trailing edge of the `wait` timeout. The `func` is invoked + * with the last arguments provided to the debounced function. Subsequent + * calls to the debounced function return the result of the last `func` + * invocation. + * + * **Note:** If `leading` and `trailing` options are `true`, `func` is + * invoked on the trailing edge of the timeout only if the debounced function + * is invoked more than once during the `wait` timeout. + * + * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred + * until to the next tick, similar to `setTimeout` with a timeout of `0`. + * + * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) + * for details over the differences between `_.debounce` and `_.throttle`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to debounce. + * @param {number} [wait=0] The number of milliseconds to delay. + * @param {Object} [options={}] The options object. + * @param {boolean} [options.leading=false] + * Specify invoking on the leading edge of the timeout. + * @param {number} [options.maxWait] + * The maximum time `func` is allowed to be delayed before it's invoked. + * @param {boolean} [options.trailing=true] + * Specify invoking on the trailing edge of the timeout. + * @returns {Function} Returns the new debounced function. + * @example + * + * // Avoid costly calculations while the window size is in flux. + * jQuery(window).on('resize', _.debounce(calculateLayout, 150)); + * + * // Invoke `sendMail` when clicked, debouncing subsequent calls. + * jQuery(element).on('click', _.debounce(sendMail, 300, { + * 'leading': true, + * 'trailing': false + * })); + * + * // Ensure `batchLog` is invoked once after 1 second of debounced calls. + * var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); + * var source = new EventSource('/stream'); + * jQuery(source).on('message', debounced); + * + * // Cancel the trailing debounced invocation. + * jQuery(window).on('popstate', debounced.cancel); + */ + function debounce(func, wait, options) { + var lastArgs, + lastThis, + maxWait, + result, + timerId, + lastCallTime, + lastInvokeTime = 0, + leading = false, + maxing = false, + trailing = true; + + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + wait = toNumber(wait) || 0; + if (isObject(options)) { + leading = !!options.leading; + maxing = 'maxWait' in options; + maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait; + trailing = 'trailing' in options ? !!options.trailing : trailing; + } + + function invokeFunc(time) { + var args = lastArgs, + thisArg = lastThis; + + lastArgs = lastThis = undefined; + lastInvokeTime = time; + result = func.apply(thisArg, args); + return result; + } + + function leadingEdge(time) { + // Reset any `maxWait` timer. + lastInvokeTime = time; + // Start the timer for the trailing edge. + timerId = setTimeout(timerExpired, wait); + // Invoke the leading edge. + return leading ? invokeFunc(time) : result; + } + + function remainingWait(time) { + var timeSinceLastCall = time - lastCallTime, + timeSinceLastInvoke = time - lastInvokeTime, + timeWaiting = wait - timeSinceLastCall; + + return maxing + ? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke) + : timeWaiting; + } + + function shouldInvoke(time) { + var timeSinceLastCall = time - lastCallTime, + timeSinceLastInvoke = time - lastInvokeTime; + + // Either this is the first call, activity has stopped and we're at the + // trailing edge, the system time has gone backwards and we're treating + // it as the trailing edge, or we've hit the `maxWait` limit. + return (lastCallTime === undefined || (timeSinceLastCall >= wait) || + (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)); + } + + function timerExpired() { + var time = now(); + if (shouldInvoke(time)) { + return trailingEdge(time); + } + // Restart the timer. + timerId = setTimeout(timerExpired, remainingWait(time)); + } + + function trailingEdge(time) { + timerId = undefined; + + // Only invoke if we have `lastArgs` which means `func` has been + // debounced at least once. + if (trailing && lastArgs) { + return invokeFunc(time); + } + lastArgs = lastThis = undefined; + return result; + } + + function cancel() { + if (timerId !== undefined) { + clearTimeout(timerId); + } + lastInvokeTime = 0; + lastArgs = lastCallTime = lastThis = timerId = undefined; + } + + function flush() { + return timerId === undefined ? result : trailingEdge(now()); + } + + function debounced() { + var time = now(), + isInvoking = shouldInvoke(time); + + lastArgs = arguments; + lastThis = this; + lastCallTime = time; + + if (isInvoking) { + if (timerId === undefined) { + return leadingEdge(lastCallTime); + } + if (maxing) { + // Handle invocations in a tight loop. + clearTimeout(timerId); + timerId = setTimeout(timerExpired, wait); + return invokeFunc(lastCallTime); + } + } + if (timerId === undefined) { + timerId = setTimeout(timerExpired, wait); + } + return result; + } + debounced.cancel = cancel; + debounced.flush = flush; + return debounced; + } + + /** + * Defers invoking the `func` until the current call stack has cleared. Any + * additional arguments are provided to `func` when it's invoked. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to defer. + * @param {...*} [args] The arguments to invoke `func` with. + * @returns {number} Returns the timer id. + * @example + * + * _.defer(function(text) { + * console.log(text); + * }, 'deferred'); + * // => Logs 'deferred' after one millisecond. + */ + var defer = baseRest(function(func, args) { + return baseDelay(func, 1, args); + }); + + /** + * Invokes `func` after `wait` milliseconds. Any additional arguments are + * provided to `func` when it's invoked. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to delay. + * @param {number} wait The number of milliseconds to delay invocation. + * @param {...*} [args] The arguments to invoke `func` with. + * @returns {number} Returns the timer id. + * @example + * + * _.delay(function(text) { + * console.log(text); + * }, 1000, 'later'); + * // => Logs 'later' after one second. + */ + var delay = baseRest(function(func, wait, args) { + return baseDelay(func, toNumber(wait) || 0, args); + }); + + /** + * Creates a function that invokes `func` with arguments reversed. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Function + * @param {Function} func The function to flip arguments for. + * @returns {Function} Returns the new flipped function. + * @example + * + * var flipped = _.flip(function() { + * return _.toArray(arguments); + * }); + * + * flipped('a', 'b', 'c', 'd'); + * // => ['d', 'c', 'b', 'a'] + */ + function flip(func) { + return createWrap(func, WRAP_FLIP_FLAG); + } + + /** + * Creates a function that memoizes the result of `func`. If `resolver` is + * provided, it determines the cache key for storing the result based on the + * arguments provided to the memoized function. By default, the first argument + * provided to the memoized function is used as the map cache key. The `func` + * is invoked with the `this` binding of the memoized function. + * + * **Note:** The cache is exposed as the `cache` property on the memoized + * function. Its creation may be customized by replacing the `_.memoize.Cache` + * constructor with one whose instances implement the + * [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object) + * method interface of `clear`, `delete`, `get`, `has`, and `set`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to have its output memoized. + * @param {Function} [resolver] The function to resolve the cache key. + * @returns {Function} Returns the new memoized function. + * @example + * + * var object = { 'a': 1, 'b': 2 }; + * var other = { 'c': 3, 'd': 4 }; + * + * var values = _.memoize(_.values); + * values(object); + * // => [1, 2] + * + * values(other); + * // => [3, 4] + * + * object.a = 2; + * values(object); + * // => [1, 2] + * + * // Modify the result cache. + * values.cache.set(object, ['a', 'b']); + * values(object); + * // => ['a', 'b'] + * + * // Replace `_.memoize.Cache`. + * _.memoize.Cache = WeakMap; + */ + function memoize(func, resolver) { + if (typeof func != 'function' || (resolver != null && typeof resolver != 'function')) { + throw new TypeError(FUNC_ERROR_TEXT); + } + var memoized = function() { + var args = arguments, + key = resolver ? resolver.apply(this, args) : args[0], + cache = memoized.cache; + + if (cache.has(key)) { + return cache.get(key); + } + var result = func.apply(this, args); + memoized.cache = cache.set(key, result) || cache; + return result; + }; + memoized.cache = new (memoize.Cache || MapCache); + return memoized; + } + + // Expose `MapCache`. + memoize.Cache = MapCache; + + /** + * Creates a function that negates the result of the predicate `func`. The + * `func` predicate is invoked with the `this` binding and arguments of the + * created function. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Function + * @param {Function} predicate The predicate to negate. + * @returns {Function} Returns the new negated function. + * @example + * + * function isEven(n) { + * return n % 2 == 0; + * } + * + * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven)); + * // => [1, 3, 5] + */ + function negate(predicate) { + if (typeof predicate != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + return function() { + var args = arguments; + switch (args.length) { + case 0: return !predicate.call(this); + case 1: return !predicate.call(this, args[0]); + case 2: return !predicate.call(this, args[0], args[1]); + case 3: return !predicate.call(this, args[0], args[1], args[2]); + } + return !predicate.apply(this, args); + }; + } + + /** + * Creates a function that is restricted to invoking `func` once. Repeat calls + * to the function return the value of the first invocation. The `func` is + * invoked with the `this` binding and arguments of the created function. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to restrict. + * @returns {Function} Returns the new restricted function. + * @example + * + * var initialize = _.once(createApplication); + * initialize(); + * initialize(); + * // => `createApplication` is invoked once + */ + function once(func) { + return before(2, func); + } + + /** + * Creates a function that invokes `func` with its arguments transformed. + * + * @static + * @since 4.0.0 + * @memberOf _ + * @category Function + * @param {Function} func The function to wrap. + * @param {...(Function|Function[])} [transforms=[_.identity]] + * The argument transforms. + * @returns {Function} Returns the new function. + * @example + * + * function doubled(n) { + * return n * 2; + * } + * + * function square(n) { + * return n * n; + * } + * + * var func = _.overArgs(function(x, y) { + * return [x, y]; + * }, [square, doubled]); + * + * func(9, 3); + * // => [81, 6] + * + * func(10, 5); + * // => [100, 10] + */ + var overArgs = castRest(function(func, transforms) { + transforms = (transforms.length == 1 && isArray(transforms[0])) + ? arrayMap(transforms[0], baseUnary(getIteratee())) + : arrayMap(baseFlatten(transforms, 1), baseUnary(getIteratee())); + + var funcsLength = transforms.length; + return baseRest(function(args) { + var index = -1, + length = nativeMin(args.length, funcsLength); + + while (++index < length) { + args[index] = transforms[index].call(this, args[index]); + } + return apply(func, this, args); + }); + }); + + /** + * Creates a function that invokes `func` with `partials` prepended to the + * arguments it receives. This method is like `_.bind` except it does **not** + * alter the `this` binding. + * + * The `_.partial.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for partially applied arguments. + * + * **Note:** This method doesn't set the "length" property of partially + * applied functions. + * + * @static + * @memberOf _ + * @since 0.2.0 + * @category Function + * @param {Function} func The function to partially apply arguments to. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new partially applied function. + * @example + * + * function greet(greeting, name) { + * return greeting + ' ' + name; + * } + * + * var sayHelloTo = _.partial(greet, 'hello'); + * sayHelloTo('fred'); + * // => 'hello fred' + * + * // Partially applied with placeholders. + * var greetFred = _.partial(greet, _, 'fred'); + * greetFred('hi'); + * // => 'hi fred' + */ + var partial = baseRest(function(func, partials) { + var holders = replaceHolders(partials, getHolder(partial)); + return createWrap(func, WRAP_PARTIAL_FLAG, undefined, partials, holders); + }); + + /** + * This method is like `_.partial` except that partially applied arguments + * are appended to the arguments it receives. + * + * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic + * builds, may be used as a placeholder for partially applied arguments. + * + * **Note:** This method doesn't set the "length" property of partially + * applied functions. + * + * @static + * @memberOf _ + * @since 1.0.0 + * @category Function + * @param {Function} func The function to partially apply arguments to. + * @param {...*} [partials] The arguments to be partially applied. + * @returns {Function} Returns the new partially applied function. + * @example + * + * function greet(greeting, name) { + * return greeting + ' ' + name; + * } + * + * var greetFred = _.partialRight(greet, 'fred'); + * greetFred('hi'); + * // => 'hi fred' + * + * // Partially applied with placeholders. + * var sayHelloTo = _.partialRight(greet, 'hello', _); + * sayHelloTo('fred'); + * // => 'hello fred' + */ + var partialRight = baseRest(function(func, partials) { + var holders = replaceHolders(partials, getHolder(partialRight)); + return createWrap(func, WRAP_PARTIAL_RIGHT_FLAG, undefined, partials, holders); + }); + + /** + * Creates a function that invokes `func` with arguments arranged according + * to the specified `indexes` where the argument value at the first index is + * provided as the first argument, the argument value at the second index is + * provided as the second argument, and so on. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Function + * @param {Function} func The function to rearrange arguments for. + * @param {...(number|number[])} indexes The arranged argument indexes. + * @returns {Function} Returns the new function. + * @example + * + * var rearged = _.rearg(function(a, b, c) { + * return [a, b, c]; + * }, [2, 0, 1]); + * + * rearged('b', 'c', 'a') + * // => ['a', 'b', 'c'] + */ + var rearg = flatRest(function(func, indexes) { + return createWrap(func, WRAP_REARG_FLAG, undefined, undefined, undefined, indexes); + }); + + /** + * Creates a function that invokes `func` with the `this` binding of the + * created function and arguments from `start` and beyond provided as + * an array. + * + * **Note:** This method is based on the + * [rest parameter](https://mdn.io/rest_parameters). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Function + * @param {Function} func The function to apply a rest parameter to. + * @param {number} [start=func.length-1] The start position of the rest parameter. + * @returns {Function} Returns the new function. + * @example + * + * var say = _.rest(function(what, names) { + * return what + ' ' + _.initial(names).join(', ') + + * (_.size(names) > 1 ? ', & ' : '') + _.last(names); + * }); + * + * say('hello', 'fred', 'barney', 'pebbles'); + * // => 'hello fred, barney, & pebbles' + */ + function rest(func, start) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + start = start === undefined ? start : toInteger(start); + return baseRest(func, start); + } + + /** + * Creates a function that invokes `func` with the `this` binding of the + * create function and an array of arguments much like + * [`Function#apply`](http://www.ecma-international.org/ecma-262/7.0/#sec-function.prototype.apply). + * + * **Note:** This method is based on the + * [spread operator](https://mdn.io/spread_operator). + * + * @static + * @memberOf _ + * @since 3.2.0 + * @category Function + * @param {Function} func The function to spread arguments over. + * @param {number} [start=0] The start position of the spread. + * @returns {Function} Returns the new function. + * @example + * + * var say = _.spread(function(who, what) { + * return who + ' says ' + what; + * }); + * + * say(['fred', 'hello']); + * // => 'fred says hello' + * + * var numbers = Promise.all([ + * Promise.resolve(40), + * Promise.resolve(36) + * ]); + * + * numbers.then(_.spread(function(x, y) { + * return x + y; + * })); + * // => a Promise of 76 + */ + function spread(func, start) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + start = start == null ? 0 : nativeMax(toInteger(start), 0); + return baseRest(function(args) { + var array = args[start], + otherArgs = castSlice(args, 0, start); + + if (array) { + arrayPush(otherArgs, array); + } + return apply(func, this, otherArgs); + }); + } + + /** + * Creates a throttled function that only invokes `func` at most once per + * every `wait` milliseconds. The throttled function comes with a `cancel` + * method to cancel delayed `func` invocations and a `flush` method to + * immediately invoke them. Provide `options` to indicate whether `func` + * should be invoked on the leading and/or trailing edge of the `wait` + * timeout. The `func` is invoked with the last arguments provided to the + * throttled function. Subsequent calls to the throttled function return the + * result of the last `func` invocation. + * + * **Note:** If `leading` and `trailing` options are `true`, `func` is + * invoked on the trailing edge of the timeout only if the throttled function + * is invoked more than once during the `wait` timeout. + * + * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred + * until to the next tick, similar to `setTimeout` with a timeout of `0`. + * + * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) + * for details over the differences between `_.throttle` and `_.debounce`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {Function} func The function to throttle. + * @param {number} [wait=0] The number of milliseconds to throttle invocations to. + * @param {Object} [options={}] The options object. + * @param {boolean} [options.leading=true] + * Specify invoking on the leading edge of the timeout. + * @param {boolean} [options.trailing=true] + * Specify invoking on the trailing edge of the timeout. + * @returns {Function} Returns the new throttled function. + * @example + * + * // Avoid excessively updating the position while scrolling. + * jQuery(window).on('scroll', _.throttle(updatePosition, 100)); + * + * // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes. + * var throttled = _.throttle(renewToken, 300000, { 'trailing': false }); + * jQuery(element).on('click', throttled); + * + * // Cancel the trailing throttled invocation. + * jQuery(window).on('popstate', throttled.cancel); + */ + function throttle(func, wait, options) { + var leading = true, + trailing = true; + + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + if (isObject(options)) { + leading = 'leading' in options ? !!options.leading : leading; + trailing = 'trailing' in options ? !!options.trailing : trailing; + } + return debounce(func, wait, { + 'leading': leading, + 'maxWait': wait, + 'trailing': trailing + }); + } + + /** + * Creates a function that accepts up to one argument, ignoring any + * additional arguments. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Function + * @param {Function} func The function to cap arguments for. + * @returns {Function} Returns the new capped function. + * @example + * + * _.map(['6', '8', '10'], _.unary(parseInt)); + * // => [6, 8, 10] + */ + function unary(func) { + return ary(func, 1); + } + + /** + * Creates a function that provides `value` to `wrapper` as its first + * argument. Any additional arguments provided to the function are appended + * to those provided to the `wrapper`. The wrapper is invoked with the `this` + * binding of the created function. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Function + * @param {*} value The value to wrap. + * @param {Function} [wrapper=identity] The wrapper function. + * @returns {Function} Returns the new function. + * @example + * + * var p = _.wrap(_.escape, function(func, text) { + * return '

' + func(text) + '

'; + * }); + * + * p('fred, barney, & pebbles'); + * // => '

fred, barney, & pebbles

' + */ + function wrap(value, wrapper) { + return partial(castFunction(wrapper), value); + } + + /*------------------------------------------------------------------------*/ + + /** + * Casts `value` as an array if it's not one. + * + * @static + * @memberOf _ + * @since 4.4.0 + * @category Lang + * @param {*} value The value to inspect. + * @returns {Array} Returns the cast array. + * @example + * + * _.castArray(1); + * // => [1] + * + * _.castArray({ 'a': 1 }); + * // => [{ 'a': 1 }] + * + * _.castArray('abc'); + * // => ['abc'] + * + * _.castArray(null); + * // => [null] + * + * _.castArray(undefined); + * // => [undefined] + * + * _.castArray(); + * // => [] + * + * var array = [1, 2, 3]; + * console.log(_.castArray(array) === array); + * // => true + */ + function castArray() { + if (!arguments.length) { + return []; + } + var value = arguments[0]; + return isArray(value) ? value : [value]; + } + + /** + * Creates a shallow clone of `value`. + * + * **Note:** This method is loosely based on the + * [structured clone algorithm](https://mdn.io/Structured_clone_algorithm) + * and supports cloning arrays, array buffers, booleans, date objects, maps, + * numbers, `Object` objects, regexes, sets, strings, symbols, and typed + * arrays. The own enumerable properties of `arguments` objects are cloned + * as plain objects. An empty object is returned for uncloneable values such + * as error objects, functions, DOM nodes, and WeakMaps. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to clone. + * @returns {*} Returns the cloned value. + * @see _.cloneDeep + * @example + * + * var objects = [{ 'a': 1 }, { 'b': 2 }]; + * + * var shallow = _.clone(objects); + * console.log(shallow[0] === objects[0]); + * // => true + */ + function clone(value) { + return baseClone(value, CLONE_SYMBOLS_FLAG); + } + + /** + * This method is like `_.clone` except that it accepts `customizer` which + * is invoked to produce the cloned value. If `customizer` returns `undefined`, + * cloning is handled by the method instead. The `customizer` is invoked with + * up to four arguments; (value [, index|key, object, stack]). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to clone. + * @param {Function} [customizer] The function to customize cloning. + * @returns {*} Returns the cloned value. + * @see _.cloneDeepWith + * @example + * + * function customizer(value) { + * if (_.isElement(value)) { + * return value.cloneNode(false); + * } + * } + * + * var el = _.cloneWith(document.body, customizer); + * + * console.log(el === document.body); + * // => false + * console.log(el.nodeName); + * // => 'BODY' + * console.log(el.childNodes.length); + * // => 0 + */ + function cloneWith(value, customizer) { + customizer = typeof customizer == 'function' ? customizer : undefined; + return baseClone(value, CLONE_SYMBOLS_FLAG, customizer); + } + + /** + * This method is like `_.clone` except that it recursively clones `value`. + * + * @static + * @memberOf _ + * @since 1.0.0 + * @category Lang + * @param {*} value The value to recursively clone. + * @returns {*} Returns the deep cloned value. + * @see _.clone + * @example + * + * var objects = [{ 'a': 1 }, { 'b': 2 }]; + * + * var deep = _.cloneDeep(objects); + * console.log(deep[0] === objects[0]); + * // => false + */ + function cloneDeep(value) { + return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG); + } + + /** + * This method is like `_.cloneWith` except that it recursively clones `value`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to recursively clone. + * @param {Function} [customizer] The function to customize cloning. + * @returns {*} Returns the deep cloned value. + * @see _.cloneWith + * @example + * + * function customizer(value) { + * if (_.isElement(value)) { + * return value.cloneNode(true); + * } + * } + * + * var el = _.cloneDeepWith(document.body, customizer); + * + * console.log(el === document.body); + * // => false + * console.log(el.nodeName); + * // => 'BODY' + * console.log(el.childNodes.length); + * // => 20 + */ + function cloneDeepWith(value, customizer) { + customizer = typeof customizer == 'function' ? customizer : undefined; + return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG, customizer); + } + + /** + * Checks if `object` conforms to `source` by invoking the predicate + * properties of `source` with the corresponding property values of `object`. + * + * **Note:** This method is equivalent to `_.conforms` when `source` is + * partially applied. + * + * @static + * @memberOf _ + * @since 4.14.0 + * @category Lang + * @param {Object} object The object to inspect. + * @param {Object} source The object of property predicates to conform to. + * @returns {boolean} Returns `true` if `object` conforms, else `false`. + * @example + * + * var object = { 'a': 1, 'b': 2 }; + * + * _.conformsTo(object, { 'b': function(n) { return n > 1; } }); + * // => true + * + * _.conformsTo(object, { 'b': function(n) { return n > 2; } }); + * // => false + */ + function conformsTo(object, source) { + return source == null || baseConformsTo(object, source, keys(source)); + } + + /** + * Performs a + * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) + * comparison between two values to determine if they are equivalent. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if the values are equivalent, else `false`. + * @example + * + * var object = { 'a': 1 }; + * var other = { 'a': 1 }; + * + * _.eq(object, object); + * // => true + * + * _.eq(object, other); + * // => false + * + * _.eq('a', 'a'); + * // => true + * + * _.eq('a', Object('a')); + * // => false + * + * _.eq(NaN, NaN); + * // => true + */ + function eq(value, other) { + return value === other || (value !== value && other !== other); + } + + /** + * Checks if `value` is greater than `other`. + * + * @static + * @memberOf _ + * @since 3.9.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is greater than `other`, + * else `false`. + * @see _.lt + * @example + * + * _.gt(3, 1); + * // => true + * + * _.gt(3, 3); + * // => false + * + * _.gt(1, 3); + * // => false + */ + var gt = createRelationalOperation(baseGt); + + /** + * Checks if `value` is greater than or equal to `other`. + * + * @static + * @memberOf _ + * @since 3.9.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is greater than or equal to + * `other`, else `false`. + * @see _.lte + * @example + * + * _.gte(3, 1); + * // => true + * + * _.gte(3, 3); + * // => true + * + * _.gte(1, 3); + * // => false + */ + var gte = createRelationalOperation(function(value, other) { + return value >= other; + }); + + /** + * Checks if `value` is likely an `arguments` object. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an `arguments` object, + * else `false`. + * @example + * + * _.isArguments(function() { return arguments; }()); + * // => true + * + * _.isArguments([1, 2, 3]); + * // => false + */ + var isArguments = baseIsArguments(function() { return arguments; }()) ? baseIsArguments : function(value) { + return isObjectLike(value) && hasOwnProperty.call(value, 'callee') && + !propertyIsEnumerable.call(value, 'callee'); + }; + + /** + * Checks if `value` is classified as an `Array` object. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an array, else `false`. + * @example + * + * _.isArray([1, 2, 3]); + * // => true + * + * _.isArray(document.body.children); + * // => false + * + * _.isArray('abc'); + * // => false + * + * _.isArray(_.noop); + * // => false + */ + var isArray = Array.isArray; + + /** + * Checks if `value` is classified as an `ArrayBuffer` object. + * + * @static + * @memberOf _ + * @since 4.3.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an array buffer, else `false`. + * @example + * + * _.isArrayBuffer(new ArrayBuffer(2)); + * // => true + * + * _.isArrayBuffer(new Array(2)); + * // => false + */ + var isArrayBuffer = nodeIsArrayBuffer ? baseUnary(nodeIsArrayBuffer) : baseIsArrayBuffer; + + /** + * Checks if `value` is array-like. A value is considered array-like if it's + * not a function and has a `value.length` that's an integer greater than or + * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is array-like, else `false`. + * @example + * + * _.isArrayLike([1, 2, 3]); + * // => true + * + * _.isArrayLike(document.body.children); + * // => true + * + * _.isArrayLike('abc'); + * // => true + * + * _.isArrayLike(_.noop); + * // => false + */ + function isArrayLike(value) { + return value != null && isLength(value.length) && !isFunction(value); + } + + /** + * This method is like `_.isArrayLike` except that it also checks if `value` + * is an object. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an array-like object, + * else `false`. + * @example + * + * _.isArrayLikeObject([1, 2, 3]); + * // => true + * + * _.isArrayLikeObject(document.body.children); + * // => true + * + * _.isArrayLikeObject('abc'); + * // => false + * + * _.isArrayLikeObject(_.noop); + * // => false + */ + function isArrayLikeObject(value) { + return isObjectLike(value) && isArrayLike(value); + } + + /** + * Checks if `value` is classified as a boolean primitive or object. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a boolean, else `false`. + * @example + * + * _.isBoolean(false); + * // => true + * + * _.isBoolean(null); + * // => false + */ + function isBoolean(value) { + return value === true || value === false || + (isObjectLike(value) && baseGetTag(value) == boolTag); + } + + /** + * Checks if `value` is a buffer. + * + * @static + * @memberOf _ + * @since 4.3.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a buffer, else `false`. + * @example + * + * _.isBuffer(new Buffer(2)); + * // => true + * + * _.isBuffer(new Uint8Array(2)); + * // => false + */ + var isBuffer = nativeIsBuffer || stubFalse; + + /** + * Checks if `value` is classified as a `Date` object. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a date object, else `false`. + * @example + * + * _.isDate(new Date); + * // => true + * + * _.isDate('Mon April 23 2012'); + * // => false + */ + var isDate = nodeIsDate ? baseUnary(nodeIsDate) : baseIsDate; + + /** + * Checks if `value` is likely a DOM element. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a DOM element, else `false`. + * @example + * + * _.isElement(document.body); + * // => true + * + * _.isElement(''); + * // => false + */ + function isElement(value) { + return isObjectLike(value) && value.nodeType === 1 && !isPlainObject(value); + } + + /** + * Checks if `value` is an empty object, collection, map, or set. + * + * Objects are considered empty if they have no own enumerable string keyed + * properties. + * + * Array-like values such as `arguments` objects, arrays, buffers, strings, or + * jQuery-like collections are considered empty if they have a `length` of `0`. + * Similarly, maps and sets are considered empty if they have a `size` of `0`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is empty, else `false`. + * @example + * + * _.isEmpty(null); + * // => true + * + * _.isEmpty(true); + * // => true + * + * _.isEmpty(1); + * // => true + * + * _.isEmpty([1, 2, 3]); + * // => false + * + * _.isEmpty({ 'a': 1 }); + * // => false + */ + function isEmpty(value) { + if (value == null) { + return true; + } + if (isArrayLike(value) && + (isArray(value) || typeof value == 'string' || typeof value.splice == 'function' || + isBuffer(value) || isTypedArray(value) || isArguments(value))) { + return !value.length; + } + var tag = getTag(value); + if (tag == mapTag || tag == setTag) { + return !value.size; + } + if (isPrototype(value)) { + return !baseKeys(value).length; + } + for (var key in value) { + if (hasOwnProperty.call(value, key)) { + return false; + } + } + return true; + } + + /** + * Performs a deep comparison between two values to determine if they are + * equivalent. + * + * **Note:** This method supports comparing arrays, array buffers, booleans, + * date objects, error objects, maps, numbers, `Object` objects, regexes, + * sets, strings, symbols, and typed arrays. `Object` objects are compared + * by their own, not inherited, enumerable properties. Functions and DOM + * nodes are compared by strict equality, i.e. `===`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if the values are equivalent, else `false`. + * @example + * + * var object = { 'a': 1 }; + * var other = { 'a': 1 }; + * + * _.isEqual(object, other); + * // => true + * + * object === other; + * // => false + */ + function isEqual(value, other) { + return baseIsEqual(value, other); + } + + /** + * This method is like `_.isEqual` except that it accepts `customizer` which + * is invoked to compare values. If `customizer` returns `undefined`, comparisons + * are handled by the method instead. The `customizer` is invoked with up to + * six arguments: (objValue, othValue [, index|key, object, other, stack]). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @param {Function} [customizer] The function to customize comparisons. + * @returns {boolean} Returns `true` if the values are equivalent, else `false`. + * @example + * + * function isGreeting(value) { + * return /^h(?:i|ello)$/.test(value); + * } + * + * function customizer(objValue, othValue) { + * if (isGreeting(objValue) && isGreeting(othValue)) { + * return true; + * } + * } + * + * var array = ['hello', 'goodbye']; + * var other = ['hi', 'goodbye']; + * + * _.isEqualWith(array, other, customizer); + * // => true + */ + function isEqualWith(value, other, customizer) { + customizer = typeof customizer == 'function' ? customizer : undefined; + var result = customizer ? customizer(value, other) : undefined; + return result === undefined ? baseIsEqual(value, other, undefined, customizer) : !!result; + } + + /** + * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`, + * `SyntaxError`, `TypeError`, or `URIError` object. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an error object, else `false`. + * @example + * + * _.isError(new Error); + * // => true + * + * _.isError(Error); + * // => false + */ + function isError(value) { + if (!isObjectLike(value)) { + return false; + } + var tag = baseGetTag(value); + return tag == errorTag || tag == domExcTag || + (typeof value.message == 'string' && typeof value.name == 'string' && !isPlainObject(value)); + } + + /** + * Checks if `value` is a finite primitive number. + * + * **Note:** This method is based on + * [`Number.isFinite`](https://mdn.io/Number/isFinite). + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a finite number, else `false`. + * @example + * + * _.isFinite(3); + * // => true + * + * _.isFinite(Number.MIN_VALUE); + * // => true + * + * _.isFinite(Infinity); + * // => false + * + * _.isFinite('3'); + * // => false + */ + function isFinite(value) { + return typeof value == 'number' && nativeIsFinite(value); + } + + /** + * Checks if `value` is classified as a `Function` object. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a function, else `false`. + * @example + * + * _.isFunction(_); + * // => true + * + * _.isFunction(/abc/); + * // => false + */ + function isFunction(value) { + if (!isObject(value)) { + return false; + } + // The use of `Object#toString` avoids issues with the `typeof` operator + // in Safari 9 which returns 'object' for typed arrays and other constructors. + var tag = baseGetTag(value); + return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag; + } + + /** + * Checks if `value` is an integer. + * + * **Note:** This method is based on + * [`Number.isInteger`](https://mdn.io/Number/isInteger). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an integer, else `false`. + * @example + * + * _.isInteger(3); + * // => true + * + * _.isInteger(Number.MIN_VALUE); + * // => false + * + * _.isInteger(Infinity); + * // => false + * + * _.isInteger('3'); + * // => false + */ + function isInteger(value) { + return typeof value == 'number' && value == toInteger(value); + } + + /** + * Checks if `value` is a valid array-like length. + * + * **Note:** This method is loosely based on + * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a valid length, else `false`. + * @example + * + * _.isLength(3); + * // => true + * + * _.isLength(Number.MIN_VALUE); + * // => false + * + * _.isLength(Infinity); + * // => false + * + * _.isLength('3'); + * // => false + */ + function isLength(value) { + return typeof value == 'number' && + value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER; + } + + /** + * Checks if `value` is the + * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) + * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an object, else `false`. + * @example + * + * _.isObject({}); + * // => true + * + * _.isObject([1, 2, 3]); + * // => true + * + * _.isObject(_.noop); + * // => true + * + * _.isObject(null); + * // => false + */ + function isObject(value) { + var type = typeof value; + return value != null && (type == 'object' || type == 'function'); + } + + /** + * Checks if `value` is object-like. A value is object-like if it's not `null` + * and has a `typeof` result of "object". + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is object-like, else `false`. + * @example + * + * _.isObjectLike({}); + * // => true + * + * _.isObjectLike([1, 2, 3]); + * // => true + * + * _.isObjectLike(_.noop); + * // => false + * + * _.isObjectLike(null); + * // => false + */ + function isObjectLike(value) { + return value != null && typeof value == 'object'; + } + + /** + * Checks if `value` is classified as a `Map` object. + * + * @static + * @memberOf _ + * @since 4.3.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a map, else `false`. + * @example + * + * _.isMap(new Map); + * // => true + * + * _.isMap(new WeakMap); + * // => false + */ + var isMap = nodeIsMap ? baseUnary(nodeIsMap) : baseIsMap; + + /** + * Performs a partial deep comparison between `object` and `source` to + * determine if `object` contains equivalent property values. + * + * **Note:** This method is equivalent to `_.matches` when `source` is + * partially applied. + * + * Partial comparisons will match empty array and empty object `source` + * values against any array or object value, respectively. See `_.isEqual` + * for a list of supported value comparisons. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Lang + * @param {Object} object The object to inspect. + * @param {Object} source The object of property values to match. + * @returns {boolean} Returns `true` if `object` is a match, else `false`. + * @example + * + * var object = { 'a': 1, 'b': 2 }; + * + * _.isMatch(object, { 'b': 2 }); + * // => true + * + * _.isMatch(object, { 'b': 1 }); + * // => false + */ + function isMatch(object, source) { + return object === source || baseIsMatch(object, source, getMatchData(source)); + } + + /** + * This method is like `_.isMatch` except that it accepts `customizer` which + * is invoked to compare values. If `customizer` returns `undefined`, comparisons + * are handled by the method instead. The `customizer` is invoked with five + * arguments: (objValue, srcValue, index|key, object, source). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {Object} object The object to inspect. + * @param {Object} source The object of property values to match. + * @param {Function} [customizer] The function to customize comparisons. + * @returns {boolean} Returns `true` if `object` is a match, else `false`. + * @example + * + * function isGreeting(value) { + * return /^h(?:i|ello)$/.test(value); + * } + * + * function customizer(objValue, srcValue) { + * if (isGreeting(objValue) && isGreeting(srcValue)) { + * return true; + * } + * } + * + * var object = { 'greeting': 'hello' }; + * var source = { 'greeting': 'hi' }; + * + * _.isMatchWith(object, source, customizer); + * // => true + */ + function isMatchWith(object, source, customizer) { + customizer = typeof customizer == 'function' ? customizer : undefined; + return baseIsMatch(object, source, getMatchData(source), customizer); + } + + /** + * Checks if `value` is `NaN`. + * + * **Note:** This method is based on + * [`Number.isNaN`](https://mdn.io/Number/isNaN) and is not the same as + * global [`isNaN`](https://mdn.io/isNaN) which returns `true` for + * `undefined` and other non-number values. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`. + * @example + * + * _.isNaN(NaN); + * // => true + * + * _.isNaN(new Number(NaN)); + * // => true + * + * isNaN(undefined); + * // => true + * + * _.isNaN(undefined); + * // => false + */ + function isNaN(value) { + // An `NaN` primitive is the only value that is not equal to itself. + // Perform the `toStringTag` check first to avoid errors with some + // ActiveX objects in IE. + return isNumber(value) && value != +value; + } + + /** + * Checks if `value` is a pristine native function. + * + * **Note:** This method can't reliably detect native functions in the presence + * of the core-js package because core-js circumvents this kind of detection. + * Despite multiple requests, the core-js maintainer has made it clear: any + * attempt to fix the detection will be obstructed. As a result, we're left + * with little choice but to throw an error. Unfortunately, this also affects + * packages, like [babel-polyfill](https://www.npmjs.com/package/babel-polyfill), + * which rely on core-js. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a native function, + * else `false`. + * @example + * + * _.isNative(Array.prototype.push); + * // => true + * + * _.isNative(_); + * // => false + */ + function isNative(value) { + if (isMaskable(value)) { + throw new Error(CORE_ERROR_TEXT); + } + return baseIsNative(value); + } + + /** + * Checks if `value` is `null`. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `null`, else `false`. + * @example + * + * _.isNull(null); + * // => true + * + * _.isNull(void 0); + * // => false + */ + function isNull(value) { + return value === null; + } + + /** + * Checks if `value` is `null` or `undefined`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is nullish, else `false`. + * @example + * + * _.isNil(null); + * // => true + * + * _.isNil(void 0); + * // => true + * + * _.isNil(NaN); + * // => false + */ + function isNil(value) { + return value == null; + } + + /** + * Checks if `value` is classified as a `Number` primitive or object. + * + * **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are + * classified as numbers, use the `_.isFinite` method. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a number, else `false`. + * @example + * + * _.isNumber(3); + * // => true + * + * _.isNumber(Number.MIN_VALUE); + * // => true + * + * _.isNumber(Infinity); + * // => true + * + * _.isNumber('3'); + * // => false + */ + function isNumber(value) { + return typeof value == 'number' || + (isObjectLike(value) && baseGetTag(value) == numberTag); + } + + /** + * Checks if `value` is a plain object, that is, an object created by the + * `Object` constructor or one with a `[[Prototype]]` of `null`. + * + * @static + * @memberOf _ + * @since 0.8.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. + * @example + * + * function Foo() { + * this.a = 1; + * } + * + * _.isPlainObject(new Foo); + * // => false + * + * _.isPlainObject([1, 2, 3]); + * // => false + * + * _.isPlainObject({ 'x': 0, 'y': 0 }); + * // => true + * + * _.isPlainObject(Object.create(null)); + * // => true + */ + function isPlainObject(value) { + if (!isObjectLike(value) || baseGetTag(value) != objectTag) { + return false; + } + var proto = getPrototype(value); + if (proto === null) { + return true; + } + var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor; + return typeof Ctor == 'function' && Ctor instanceof Ctor && + funcToString.call(Ctor) == objectCtorString; + } + + /** + * Checks if `value` is classified as a `RegExp` object. + * + * @static + * @memberOf _ + * @since 0.1.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a regexp, else `false`. + * @example + * + * _.isRegExp(/abc/); + * // => true + * + * _.isRegExp('/abc/'); + * // => false + */ + var isRegExp = nodeIsRegExp ? baseUnary(nodeIsRegExp) : baseIsRegExp; + + /** + * Checks if `value` is a safe integer. An integer is safe if it's an IEEE-754 + * double precision number which isn't the result of a rounded unsafe integer. + * + * **Note:** This method is based on + * [`Number.isSafeInteger`](https://mdn.io/Number/isSafeInteger). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a safe integer, else `false`. + * @example + * + * _.isSafeInteger(3); + * // => true + * + * _.isSafeInteger(Number.MIN_VALUE); + * // => false + * + * _.isSafeInteger(Infinity); + * // => false + * + * _.isSafeInteger('3'); + * // => false + */ + function isSafeInteger(value) { + return isInteger(value) && value >= -MAX_SAFE_INTEGER && value <= MAX_SAFE_INTEGER; + } + + /** + * Checks if `value` is classified as a `Set` object. + * + * @static + * @memberOf _ + * @since 4.3.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a set, else `false`. + * @example + * + * _.isSet(new Set); + * // => true + * + * _.isSet(new WeakSet); + * // => false + */ + var isSet = nodeIsSet ? baseUnary(nodeIsSet) : baseIsSet; + + /** + * Checks if `value` is classified as a `String` primitive or object. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a string, else `false`. + * @example + * + * _.isString('abc'); + * // => true + * + * _.isString(1); + * // => false + */ + function isString(value) { + return typeof value == 'string' || + (!isArray(value) && isObjectLike(value) && baseGetTag(value) == stringTag); + } + + /** + * Checks if `value` is classified as a `Symbol` primitive or object. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a symbol, else `false`. + * @example + * + * _.isSymbol(Symbol.iterator); + * // => true + * + * _.isSymbol('abc'); + * // => false + */ + function isSymbol(value) { + return typeof value == 'symbol' || + (isObjectLike(value) && baseGetTag(value) == symbolTag); + } + + /** + * Checks if `value` is classified as a typed array. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a typed array, else `false`. + * @example + * + * _.isTypedArray(new Uint8Array); + * // => true + * + * _.isTypedArray([]); + * // => false + */ + var isTypedArray = nodeIsTypedArray ? baseUnary(nodeIsTypedArray) : baseIsTypedArray; + + /** + * Checks if `value` is `undefined`. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`. + * @example + * + * _.isUndefined(void 0); + * // => true + * + * _.isUndefined(null); + * // => false + */ + function isUndefined(value) { + return value === undefined; + } + + /** + * Checks if `value` is classified as a `WeakMap` object. + * + * @static + * @memberOf _ + * @since 4.3.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a weak map, else `false`. + * @example + * + * _.isWeakMap(new WeakMap); + * // => true + * + * _.isWeakMap(new Map); + * // => false + */ + function isWeakMap(value) { + return isObjectLike(value) && getTag(value) == weakMapTag; + } + + /** + * Checks if `value` is classified as a `WeakSet` object. + * + * @static + * @memberOf _ + * @since 4.3.0 + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is a weak set, else `false`. + * @example + * + * _.isWeakSet(new WeakSet); + * // => true + * + * _.isWeakSet(new Set); + * // => false + */ + function isWeakSet(value) { + return isObjectLike(value) && baseGetTag(value) == weakSetTag; + } + + /** + * Checks if `value` is less than `other`. + * + * @static + * @memberOf _ + * @since 3.9.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is less than `other`, + * else `false`. + * @see _.gt + * @example + * + * _.lt(1, 3); + * // => true + * + * _.lt(3, 3); + * // => false + * + * _.lt(3, 1); + * // => false + */ + var lt = createRelationalOperation(baseLt); + + /** + * Checks if `value` is less than or equal to `other`. + * + * @static + * @memberOf _ + * @since 3.9.0 + * @category Lang + * @param {*} value The value to compare. + * @param {*} other The other value to compare. + * @returns {boolean} Returns `true` if `value` is less than or equal to + * `other`, else `false`. + * @see _.gte + * @example + * + * _.lte(1, 3); + * // => true + * + * _.lte(3, 3); + * // => true + * + * _.lte(3, 1); + * // => false + */ + var lte = createRelationalOperation(function(value, other) { + return value <= other; + }); + + /** + * Converts `value` to an array. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Lang + * @param {*} value The value to convert. + * @returns {Array} Returns the converted array. + * @example + * + * _.toArray({ 'a': 1, 'b': 2 }); + * // => [1, 2] + * + * _.toArray('abc'); + * // => ['a', 'b', 'c'] + * + * _.toArray(1); + * // => [] + * + * _.toArray(null); + * // => [] + */ + function toArray(value) { + if (!value) { + return []; + } + if (isArrayLike(value)) { + return isString(value) ? stringToArray(value) : copyArray(value); + } + if (symIterator && value[symIterator]) { + return iteratorToArray(value[symIterator]()); + } + var tag = getTag(value), + func = tag == mapTag ? mapToArray : (tag == setTag ? setToArray : values); + + return func(value); + } + + /** + * Converts `value` to a finite number. + * + * @static + * @memberOf _ + * @since 4.12.0 + * @category Lang + * @param {*} value The value to convert. + * @returns {number} Returns the converted number. + * @example + * + * _.toFinite(3.2); + * // => 3.2 + * + * _.toFinite(Number.MIN_VALUE); + * // => 5e-324 + * + * _.toFinite(Infinity); + * // => 1.7976931348623157e+308 + * + * _.toFinite('3.2'); + * // => 3.2 + */ + function toFinite(value) { + if (!value) { + return value === 0 ? value : 0; + } + value = toNumber(value); + if (value === INFINITY || value === -INFINITY) { + var sign = (value < 0 ? -1 : 1); + return sign * MAX_INTEGER; + } + return value === value ? value : 0; + } + + /** + * Converts `value` to an integer. + * + * **Note:** This method is loosely based on + * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to convert. + * @returns {number} Returns the converted integer. + * @example + * + * _.toInteger(3.2); + * // => 3 + * + * _.toInteger(Number.MIN_VALUE); + * // => 0 + * + * _.toInteger(Infinity); + * // => 1.7976931348623157e+308 + * + * _.toInteger('3.2'); + * // => 3 + */ + function toInteger(value) { + var result = toFinite(value), + remainder = result % 1; + + return result === result ? (remainder ? result - remainder : result) : 0; + } + + /** + * Converts `value` to an integer suitable for use as the length of an + * array-like object. + * + * **Note:** This method is based on + * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to convert. + * @returns {number} Returns the converted integer. + * @example + * + * _.toLength(3.2); + * // => 3 + * + * _.toLength(Number.MIN_VALUE); + * // => 0 + * + * _.toLength(Infinity); + * // => 4294967295 + * + * _.toLength('3.2'); + * // => 3 + */ + function toLength(value) { + return value ? baseClamp(toInteger(value), 0, MAX_ARRAY_LENGTH) : 0; + } + + /** + * Converts `value` to a number. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to process. + * @returns {number} Returns the number. + * @example + * + * _.toNumber(3.2); + * // => 3.2 + * + * _.toNumber(Number.MIN_VALUE); + * // => 5e-324 + * + * _.toNumber(Infinity); + * // => Infinity + * + * _.toNumber('3.2'); + * // => 3.2 + */ + function toNumber(value) { + if (typeof value == 'number') { + return value; + } + if (isSymbol(value)) { + return NAN; + } + if (isObject(value)) { + var other = typeof value.valueOf == 'function' ? value.valueOf() : value; + value = isObject(other) ? (other + '') : other; + } + if (typeof value != 'string') { + return value === 0 ? value : +value; + } + value = baseTrim(value); + var isBinary = reIsBinary.test(value); + return (isBinary || reIsOctal.test(value)) + ? freeParseInt(value.slice(2), isBinary ? 2 : 8) + : (reIsBadHex.test(value) ? NAN : +value); + } + + /** + * Converts `value` to a plain object flattening inherited enumerable string + * keyed properties of `value` to own properties of the plain object. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Lang + * @param {*} value The value to convert. + * @returns {Object} Returns the converted plain object. + * @example + * + * function Foo() { + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.assign({ 'a': 1 }, new Foo); + * // => { 'a': 1, 'b': 2 } + * + * _.assign({ 'a': 1 }, _.toPlainObject(new Foo)); + * // => { 'a': 1, 'b': 2, 'c': 3 } + */ + function toPlainObject(value) { + return copyObject(value, keysIn(value)); + } + + /** + * Converts `value` to a safe integer. A safe integer can be compared and + * represented correctly. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to convert. + * @returns {number} Returns the converted integer. + * @example + * + * _.toSafeInteger(3.2); + * // => 3 + * + * _.toSafeInteger(Number.MIN_VALUE); + * // => 0 + * + * _.toSafeInteger(Infinity); + * // => 9007199254740991 + * + * _.toSafeInteger('3.2'); + * // => 3 + */ + function toSafeInteger(value) { + return value + ? baseClamp(toInteger(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER) + : (value === 0 ? value : 0); + } + + /** + * Converts `value` to a string. An empty string is returned for `null` + * and `undefined` values. The sign of `-0` is preserved. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Lang + * @param {*} value The value to convert. + * @returns {string} Returns the converted string. + * @example + * + * _.toString(null); + * // => '' + * + * _.toString(-0); + * // => '-0' + * + * _.toString([1, 2, 3]); + * // => '1,2,3' + */ + function toString(value) { + return value == null ? '' : baseToString(value); + } + + /*------------------------------------------------------------------------*/ + + /** + * Assigns own enumerable string keyed properties of source objects to the + * destination object. Source objects are applied from left to right. + * Subsequent sources overwrite property assignments of previous sources. + * + * **Note:** This method mutates `object` and is loosely based on + * [`Object.assign`](https://mdn.io/Object/assign). + * + * @static + * @memberOf _ + * @since 0.10.0 + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. + * @see _.assignIn + * @example + * + * function Foo() { + * this.a = 1; + * } + * + * function Bar() { + * this.c = 3; + * } + * + * Foo.prototype.b = 2; + * Bar.prototype.d = 4; + * + * _.assign({ 'a': 0 }, new Foo, new Bar); + * // => { 'a': 1, 'c': 3 } + */ + var assign = createAssigner(function(object, source) { + if (isPrototype(source) || isArrayLike(source)) { + copyObject(source, keys(source), object); + return; + } + for (var key in source) { + if (hasOwnProperty.call(source, key)) { + assignValue(object, key, source[key]); + } + } + }); + + /** + * This method is like `_.assign` except that it iterates over own and + * inherited source properties. + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @alias extend + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. + * @see _.assign + * @example + * + * function Foo() { + * this.a = 1; + * } + * + * function Bar() { + * this.c = 3; + * } + * + * Foo.prototype.b = 2; + * Bar.prototype.d = 4; + * + * _.assignIn({ 'a': 0 }, new Foo, new Bar); + * // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 } + */ + var assignIn = createAssigner(function(object, source) { + copyObject(source, keysIn(source), object); + }); + + /** + * This method is like `_.assignIn` except that it accepts `customizer` + * which is invoked to produce the assigned values. If `customizer` returns + * `undefined`, assignment is handled by the method instead. The `customizer` + * is invoked with five arguments: (objValue, srcValue, key, object, source). + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @alias extendWith + * @category Object + * @param {Object} object The destination object. + * @param {...Object} sources The source objects. + * @param {Function} [customizer] The function to customize assigned values. + * @returns {Object} Returns `object`. + * @see _.assignWith + * @example + * + * function customizer(objValue, srcValue) { + * return _.isUndefined(objValue) ? srcValue : objValue; + * } + * + * var defaults = _.partialRight(_.assignInWith, customizer); + * + * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); + * // => { 'a': 1, 'b': 2 } + */ + var assignInWith = createAssigner(function(object, source, srcIndex, customizer) { + copyObject(source, keysIn(source), object, customizer); + }); + + /** + * This method is like `_.assign` except that it accepts `customizer` + * which is invoked to produce the assigned values. If `customizer` returns + * `undefined`, assignment is handled by the method instead. The `customizer` + * is invoked with five arguments: (objValue, srcValue, key, object, source). + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The destination object. + * @param {...Object} sources The source objects. + * @param {Function} [customizer] The function to customize assigned values. + * @returns {Object} Returns `object`. + * @see _.assignInWith + * @example + * + * function customizer(objValue, srcValue) { + * return _.isUndefined(objValue) ? srcValue : objValue; + * } + * + * var defaults = _.partialRight(_.assignWith, customizer); + * + * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); + * // => { 'a': 1, 'b': 2 } + */ + var assignWith = createAssigner(function(object, source, srcIndex, customizer) { + copyObject(source, keys(source), object, customizer); + }); + + /** + * Creates an array of values corresponding to `paths` of `object`. + * + * @static + * @memberOf _ + * @since 1.0.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {...(string|string[])} [paths] The property paths to pick. + * @returns {Array} Returns the picked values. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 3 } }, 4] }; + * + * _.at(object, ['a[0].b.c', 'a[1]']); + * // => [3, 4] + */ + var at = flatRest(baseAt); + + /** + * Creates an object that inherits from the `prototype` object. If a + * `properties` object is given, its own enumerable string keyed properties + * are assigned to the created object. + * + * @static + * @memberOf _ + * @since 2.3.0 + * @category Object + * @param {Object} prototype The object to inherit from. + * @param {Object} [properties] The properties to assign to the object. + * @returns {Object} Returns the new object. + * @example + * + * function Shape() { + * this.x = 0; + * this.y = 0; + * } + * + * function Circle() { + * Shape.call(this); + * } + * + * Circle.prototype = _.create(Shape.prototype, { + * 'constructor': Circle + * }); + * + * var circle = new Circle; + * circle instanceof Circle; + * // => true + * + * circle instanceof Shape; + * // => true + */ + function create(prototype, properties) { + var result = baseCreate(prototype); + return properties == null ? result : baseAssign(result, properties); + } + + /** + * Assigns own and inherited enumerable string keyed properties of source + * objects to the destination object for all destination properties that + * resolve to `undefined`. Source objects are applied from left to right. + * Once a property is set, additional values of the same property are ignored. + * + * **Note:** This method mutates `object`. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. + * @see _.defaultsDeep + * @example + * + * _.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); + * // => { 'a': 1, 'b': 2 } + */ + var defaults = baseRest(function(object, sources) { + object = Object(object); + + var index = -1; + var length = sources.length; + var guard = length > 2 ? sources[2] : undefined; + + if (guard && isIterateeCall(sources[0], sources[1], guard)) { + length = 1; + } + + while (++index < length) { + var source = sources[index]; + var props = keysIn(source); + var propsIndex = -1; + var propsLength = props.length; + + while (++propsIndex < propsLength) { + var key = props[propsIndex]; + var value = object[key]; + + if (value === undefined || + (eq(value, objectProto[key]) && !hasOwnProperty.call(object, key))) { + object[key] = source[key]; + } + } + } + + return object; + }); + + /** + * This method is like `_.defaults` except that it recursively assigns + * default properties. + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 3.10.0 + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. + * @see _.defaults + * @example + * + * _.defaultsDeep({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }); + * // => { 'a': { 'b': 2, 'c': 3 } } + */ + var defaultsDeep = baseRest(function(args) { + args.push(undefined, customDefaultsMerge); + return apply(mergeWith, undefined, args); + }); + + /** + * This method is like `_.find` except that it returns the key of the first + * element `predicate` returns truthy for instead of the element itself. + * + * @static + * @memberOf _ + * @since 1.1.0 + * @category Object + * @param {Object} object The object to inspect. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {string|undefined} Returns the key of the matched element, + * else `undefined`. + * @example + * + * var users = { + * 'barney': { 'age': 36, 'active': true }, + * 'fred': { 'age': 40, 'active': false }, + * 'pebbles': { 'age': 1, 'active': true } + * }; + * + * _.findKey(users, function(o) { return o.age < 40; }); + * // => 'barney' (iteration order is not guaranteed) + * + * // The `_.matches` iteratee shorthand. + * _.findKey(users, { 'age': 1, 'active': true }); + * // => 'pebbles' + * + * // The `_.matchesProperty` iteratee shorthand. + * _.findKey(users, ['active', false]); + * // => 'fred' + * + * // The `_.property` iteratee shorthand. + * _.findKey(users, 'active'); + * // => 'barney' + */ + function findKey(object, predicate) { + return baseFindKey(object, getIteratee(predicate, 3), baseForOwn); + } + + /** + * This method is like `_.findKey` except that it iterates over elements of + * a collection in the opposite order. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Object + * @param {Object} object The object to inspect. + * @param {Function} [predicate=_.identity] The function invoked per iteration. + * @returns {string|undefined} Returns the key of the matched element, + * else `undefined`. + * @example + * + * var users = { + * 'barney': { 'age': 36, 'active': true }, + * 'fred': { 'age': 40, 'active': false }, + * 'pebbles': { 'age': 1, 'active': true } + * }; + * + * _.findLastKey(users, function(o) { return o.age < 40; }); + * // => returns 'pebbles' assuming `_.findKey` returns 'barney' + * + * // The `_.matches` iteratee shorthand. + * _.findLastKey(users, { 'age': 36, 'active': true }); + * // => 'barney' + * + * // The `_.matchesProperty` iteratee shorthand. + * _.findLastKey(users, ['active', false]); + * // => 'fred' + * + * // The `_.property` iteratee shorthand. + * _.findLastKey(users, 'active'); + * // => 'pebbles' + */ + function findLastKey(object, predicate) { + return baseFindKey(object, getIteratee(predicate, 3), baseForOwnRight); + } + + /** + * Iterates over own and inherited enumerable string keyed properties of an + * object and invokes `iteratee` for each property. The iteratee is invoked + * with three arguments: (value, key, object). Iteratee functions may exit + * iteration early by explicitly returning `false`. + * + * @static + * @memberOf _ + * @since 0.3.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns `object`. + * @see _.forInRight + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.forIn(new Foo, function(value, key) { + * console.log(key); + * }); + * // => Logs 'a', 'b', then 'c' (iteration order is not guaranteed). + */ + function forIn(object, iteratee) { + return object == null + ? object + : baseFor(object, getIteratee(iteratee, 3), keysIn); + } + + /** + * This method is like `_.forIn` except that it iterates over properties of + * `object` in the opposite order. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns `object`. + * @see _.forIn + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.forInRight(new Foo, function(value, key) { + * console.log(key); + * }); + * // => Logs 'c', 'b', then 'a' assuming `_.forIn` logs 'a', 'b', then 'c'. + */ + function forInRight(object, iteratee) { + return object == null + ? object + : baseForRight(object, getIteratee(iteratee, 3), keysIn); + } + + /** + * Iterates over own enumerable string keyed properties of an object and + * invokes `iteratee` for each property. The iteratee is invoked with three + * arguments: (value, key, object). Iteratee functions may exit iteration + * early by explicitly returning `false`. + * + * @static + * @memberOf _ + * @since 0.3.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns `object`. + * @see _.forOwnRight + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.forOwn(new Foo, function(value, key) { + * console.log(key); + * }); + * // => Logs 'a' then 'b' (iteration order is not guaranteed). + */ + function forOwn(object, iteratee) { + return object && baseForOwn(object, getIteratee(iteratee, 3)); + } + + /** + * This method is like `_.forOwn` except that it iterates over properties of + * `object` in the opposite order. + * + * @static + * @memberOf _ + * @since 2.0.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns `object`. + * @see _.forOwn + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.forOwnRight(new Foo, function(value, key) { + * console.log(key); + * }); + * // => Logs 'b' then 'a' assuming `_.forOwn` logs 'a' then 'b'. + */ + function forOwnRight(object, iteratee) { + return object && baseForOwnRight(object, getIteratee(iteratee, 3)); + } + + /** + * Creates an array of function property names from own enumerable properties + * of `object`. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The object to inspect. + * @returns {Array} Returns the function names. + * @see _.functionsIn + * @example + * + * function Foo() { + * this.a = _.constant('a'); + * this.b = _.constant('b'); + * } + * + * Foo.prototype.c = _.constant('c'); + * + * _.functions(new Foo); + * // => ['a', 'b'] + */ + function functions(object) { + return object == null ? [] : baseFunctions(object, keys(object)); + } + + /** + * Creates an array of function property names from own and inherited + * enumerable properties of `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The object to inspect. + * @returns {Array} Returns the function names. + * @see _.functions + * @example + * + * function Foo() { + * this.a = _.constant('a'); + * this.b = _.constant('b'); + * } + * + * Foo.prototype.c = _.constant('c'); + * + * _.functionsIn(new Foo); + * // => ['a', 'b', 'c'] + */ + function functionsIn(object) { + return object == null ? [] : baseFunctions(object, keysIn(object)); + } + + /** + * Gets the value at `path` of `object`. If the resolved value is + * `undefined`, the `defaultValue` is returned in its place. + * + * @static + * @memberOf _ + * @since 3.7.0 + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path of the property to get. + * @param {*} [defaultValue] The value returned for `undefined` resolved values. + * @returns {*} Returns the resolved value. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 3 } }] }; + * + * _.get(object, 'a[0].b.c'); + * // => 3 + * + * _.get(object, ['a', '0', 'b', 'c']); + * // => 3 + * + * _.get(object, 'a.b.c', 'default'); + * // => 'default' + */ + function get(object, path, defaultValue) { + var result = object == null ? undefined : baseGet(object, path); + return result === undefined ? defaultValue : result; + } + + /** + * Checks if `path` is a direct property of `object`. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path to check. + * @returns {boolean} Returns `true` if `path` exists, else `false`. + * @example + * + * var object = { 'a': { 'b': 2 } }; + * var other = _.create({ 'a': _.create({ 'b': 2 }) }); + * + * _.has(object, 'a'); + * // => true + * + * _.has(object, 'a.b'); + * // => true + * + * _.has(object, ['a', 'b']); + * // => true + * + * _.has(other, 'a'); + * // => false + */ + function has(object, path) { + return object != null && hasPath(object, path, baseHas); + } + + /** + * Checks if `path` is a direct or inherited property of `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path to check. + * @returns {boolean} Returns `true` if `path` exists, else `false`. + * @example + * + * var object = _.create({ 'a': _.create({ 'b': 2 }) }); + * + * _.hasIn(object, 'a'); + * // => true + * + * _.hasIn(object, 'a.b'); + * // => true + * + * _.hasIn(object, ['a', 'b']); + * // => true + * + * _.hasIn(object, 'b'); + * // => false + */ + function hasIn(object, path) { + return object != null && hasPath(object, path, baseHasIn); + } + + /** + * Creates an object composed of the inverted keys and values of `object`. + * If `object` contains duplicate values, subsequent values overwrite + * property assignments of previous values. + * + * @static + * @memberOf _ + * @since 0.7.0 + * @category Object + * @param {Object} object The object to invert. + * @returns {Object} Returns the new inverted object. + * @example + * + * var object = { 'a': 1, 'b': 2, 'c': 1 }; + * + * _.invert(object); + * // => { '1': 'c', '2': 'b' } + */ + var invert = createInverter(function(result, value, key) { + if (value != null && + typeof value.toString != 'function') { + value = nativeObjectToString.call(value); + } + + result[value] = key; + }, constant(identity)); + + /** + * This method is like `_.invert` except that the inverted object is generated + * from the results of running each element of `object` thru `iteratee`. The + * corresponding inverted value of each inverted key is an array of keys + * responsible for generating the inverted value. The iteratee is invoked + * with one argument: (value). + * + * @static + * @memberOf _ + * @since 4.1.0 + * @category Object + * @param {Object} object The object to invert. + * @param {Function} [iteratee=_.identity] The iteratee invoked per element. + * @returns {Object} Returns the new inverted object. + * @example + * + * var object = { 'a': 1, 'b': 2, 'c': 1 }; + * + * _.invertBy(object); + * // => { '1': ['a', 'c'], '2': ['b'] } + * + * _.invertBy(object, function(value) { + * return 'group' + value; + * }); + * // => { 'group1': ['a', 'c'], 'group2': ['b'] } + */ + var invertBy = createInverter(function(result, value, key) { + if (value != null && + typeof value.toString != 'function') { + value = nativeObjectToString.call(value); + } + + if (hasOwnProperty.call(result, value)) { + result[value].push(key); + } else { + result[value] = [key]; + } + }, getIteratee); + + /** + * Invokes the method at `path` of `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path of the method to invoke. + * @param {...*} [args] The arguments to invoke the method with. + * @returns {*} Returns the result of the invoked method. + * @example + * + * var object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] }; + * + * _.invoke(object, 'a[0].b.c.slice', 1, 3); + * // => [2, 3] + */ + var invoke = baseRest(baseInvoke); + + /** + * Creates an array of the own enumerable property names of `object`. + * + * **Note:** Non-object values are coerced to objects. See the + * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys) + * for more details. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.keys(new Foo); + * // => ['a', 'b'] (iteration order is not guaranteed) + * + * _.keys('hi'); + * // => ['0', '1'] + */ + function keys(object) { + return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object); + } + + /** + * Creates an array of the own and inherited enumerable property names of `object`. + * + * **Note:** Non-object values are coerced to objects. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property names. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.keysIn(new Foo); + * // => ['a', 'b', 'c'] (iteration order is not guaranteed) + */ + function keysIn(object) { + return isArrayLike(object) ? arrayLikeKeys(object, true) : baseKeysIn(object); + } + + /** + * The opposite of `_.mapValues`; this method creates an object with the + * same values as `object` and keys generated by running each own enumerable + * string keyed property of `object` thru `iteratee`. The iteratee is invoked + * with three arguments: (value, key, object). + * + * @static + * @memberOf _ + * @since 3.8.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns the new mapped object. + * @see _.mapValues + * @example + * + * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { + * return key + value; + * }); + * // => { 'a1': 1, 'b2': 2 } + */ + function mapKeys(object, iteratee) { + var result = {}; + iteratee = getIteratee(iteratee, 3); + + baseForOwn(object, function(value, key, object) { + baseAssignValue(result, iteratee(value, key, object), value); + }); + return result; + } + + /** + * Creates an object with the same keys as `object` and values generated + * by running each own enumerable string keyed property of `object` thru + * `iteratee`. The iteratee is invoked with three arguments: + * (value, key, object). + * + * @static + * @memberOf _ + * @since 2.4.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns the new mapped object. + * @see _.mapKeys + * @example + * + * var users = { + * 'fred': { 'user': 'fred', 'age': 40 }, + * 'pebbles': { 'user': 'pebbles', 'age': 1 } + * }; + * + * _.mapValues(users, function(o) { return o.age; }); + * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed) + * + * // The `_.property` iteratee shorthand. + * _.mapValues(users, 'age'); + * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed) + */ + function mapValues(object, iteratee) { + var result = {}; + iteratee = getIteratee(iteratee, 3); + + baseForOwn(object, function(value, key, object) { + baseAssignValue(result, key, iteratee(value, key, object)); + }); + return result; + } + + /** + * This method is like `_.assign` except that it recursively merges own and + * inherited enumerable string keyed properties of source objects into the + * destination object. Source properties that resolve to `undefined` are + * skipped if a destination value exists. Array and plain object properties + * are merged recursively. Other objects and value types are overridden by + * assignment. Source objects are applied from left to right. Subsequent + * sources overwrite property assignments of previous sources. + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 0.5.0 + * @category Object + * @param {Object} object The destination object. + * @param {...Object} [sources] The source objects. + * @returns {Object} Returns `object`. + * @example + * + * var object = { + * 'a': [{ 'b': 2 }, { 'd': 4 }] + * }; + * + * var other = { + * 'a': [{ 'c': 3 }, { 'e': 5 }] + * }; + * + * _.merge(object, other); + * // => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] } + */ + var merge = createAssigner(function(object, source, srcIndex) { + baseMerge(object, source, srcIndex); + }); + + /** + * This method is like `_.merge` except that it accepts `customizer` which + * is invoked to produce the merged values of the destination and source + * properties. If `customizer` returns `undefined`, merging is handled by the + * method instead. The `customizer` is invoked with six arguments: + * (objValue, srcValue, key, object, source, stack). + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The destination object. + * @param {...Object} sources The source objects. + * @param {Function} customizer The function to customize assigned values. + * @returns {Object} Returns `object`. + * @example + * + * function customizer(objValue, srcValue) { + * if (_.isArray(objValue)) { + * return objValue.concat(srcValue); + * } + * } + * + * var object = { 'a': [1], 'b': [2] }; + * var other = { 'a': [3], 'b': [4] }; + * + * _.mergeWith(object, other, customizer); + * // => { 'a': [1, 3], 'b': [2, 4] } + */ + var mergeWith = createAssigner(function(object, source, srcIndex, customizer) { + baseMerge(object, source, srcIndex, customizer); + }); + + /** + * The opposite of `_.pick`; this method creates an object composed of the + * own and inherited enumerable property paths of `object` that are not omitted. + * + * **Note:** This method is considerably slower than `_.pick`. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The source object. + * @param {...(string|string[])} [paths] The property paths to omit. + * @returns {Object} Returns the new object. + * @example + * + * var object = { 'a': 1, 'b': '2', 'c': 3 }; + * + * _.omit(object, ['a', 'c']); + * // => { 'b': '2' } + */ + var omit = flatRest(function(object, paths) { + var result = {}; + if (object == null) { + return result; + } + var isDeep = false; + paths = arrayMap(paths, function(path) { + path = castPath(path, object); + isDeep || (isDeep = path.length > 1); + return path; + }); + copyObject(object, getAllKeysIn(object), result); + if (isDeep) { + result = baseClone(result, CLONE_DEEP_FLAG | CLONE_FLAT_FLAG | CLONE_SYMBOLS_FLAG, customOmitClone); + } + var length = paths.length; + while (length--) { + baseUnset(result, paths[length]); + } + return result; + }); + + /** + * The opposite of `_.pickBy`; this method creates an object composed of + * the own and inherited enumerable string keyed properties of `object` that + * `predicate` doesn't return truthy for. The predicate is invoked with two + * arguments: (value, key). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The source object. + * @param {Function} [predicate=_.identity] The function invoked per property. + * @returns {Object} Returns the new object. + * @example + * + * var object = { 'a': 1, 'b': '2', 'c': 3 }; + * + * _.omitBy(object, _.isNumber); + * // => { 'b': '2' } + */ + function omitBy(object, predicate) { + return pickBy(object, negate(getIteratee(predicate))); + } + + /** + * Creates an object composed of the picked `object` properties. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The source object. + * @param {...(string|string[])} [paths] The property paths to pick. + * @returns {Object} Returns the new object. + * @example + * + * var object = { 'a': 1, 'b': '2', 'c': 3 }; + * + * _.pick(object, ['a', 'c']); + * // => { 'a': 1, 'c': 3 } + */ + var pick = flatRest(function(object, paths) { + return object == null ? {} : basePick(object, paths); + }); + + /** + * Creates an object composed of the `object` properties `predicate` returns + * truthy for. The predicate is invoked with two arguments: (value, key). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The source object. + * @param {Function} [predicate=_.identity] The function invoked per property. + * @returns {Object} Returns the new object. + * @example + * + * var object = { 'a': 1, 'b': '2', 'c': 3 }; + * + * _.pickBy(object, _.isNumber); + * // => { 'a': 1, 'c': 3 } + */ + function pickBy(object, predicate) { + if (object == null) { + return {}; + } + var props = arrayMap(getAllKeysIn(object), function(prop) { + return [prop]; + }); + predicate = getIteratee(predicate); + return basePickBy(object, props, function(value, path) { + return predicate(value, path[0]); + }); + } + + /** + * This method is like `_.get` except that if the resolved value is a + * function it's invoked with the `this` binding of its parent object and + * its result is returned. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The object to query. + * @param {Array|string} path The path of the property to resolve. + * @param {*} [defaultValue] The value returned for `undefined` resolved values. + * @returns {*} Returns the resolved value. + * @example + * + * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] }; + * + * _.result(object, 'a[0].b.c1'); + * // => 3 + * + * _.result(object, 'a[0].b.c2'); + * // => 4 + * + * _.result(object, 'a[0].b.c3', 'default'); + * // => 'default' + * + * _.result(object, 'a[0].b.c3', _.constant('default')); + * // => 'default' + */ + function result(object, path, defaultValue) { + path = castPath(path, object); + + var index = -1, + length = path.length; + + // Ensure the loop is entered when path is empty. + if (!length) { + length = 1; + object = undefined; + } + while (++index < length) { + var value = object == null ? undefined : object[toKey(path[index])]; + if (value === undefined) { + index = length; + value = defaultValue; + } + object = isFunction(value) ? value.call(object) : value; + } + return object; + } + + /** + * Sets the value at `path` of `object`. If a portion of `path` doesn't exist, + * it's created. Arrays are created for missing index properties while objects + * are created for all other missing properties. Use `_.setWith` to customize + * `path` creation. + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 3.7.0 + * @category Object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {*} value The value to set. + * @returns {Object} Returns `object`. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 3 } }] }; + * + * _.set(object, 'a[0].b.c', 4); + * console.log(object.a[0].b.c); + * // => 4 + * + * _.set(object, ['x', '0', 'y', 'z'], 5); + * console.log(object.x[0].y.z); + * // => 5 + */ + function set(object, path, value) { + return object == null ? object : baseSet(object, path, value); + } + + /** + * This method is like `_.set` except that it accepts `customizer` which is + * invoked to produce the objects of `path`. If `customizer` returns `undefined` + * path creation is handled by the method instead. The `customizer` is invoked + * with three arguments: (nsValue, key, nsObject). + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {*} value The value to set. + * @param {Function} [customizer] The function to customize assigned values. + * @returns {Object} Returns `object`. + * @example + * + * var object = {}; + * + * _.setWith(object, '[0][1]', 'a', Object); + * // => { '0': { '1': 'a' } } + */ + function setWith(object, path, value, customizer) { + customizer = typeof customizer == 'function' ? customizer : undefined; + return object == null ? object : baseSet(object, path, value, customizer); + } + + /** + * Creates an array of own enumerable string keyed-value pairs for `object` + * which can be consumed by `_.fromPairs`. If `object` is a map or set, its + * entries are returned. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @alias entries + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the key-value pairs. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.toPairs(new Foo); + * // => [['a', 1], ['b', 2]] (iteration order is not guaranteed) + */ + var toPairs = createToPairs(keys); + + /** + * Creates an array of own and inherited enumerable string keyed-value pairs + * for `object` which can be consumed by `_.fromPairs`. If `object` is a map + * or set, its entries are returned. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @alias entriesIn + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the key-value pairs. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.toPairsIn(new Foo); + * // => [['a', 1], ['b', 2], ['c', 3]] (iteration order is not guaranteed) + */ + var toPairsIn = createToPairs(keysIn); + + /** + * An alternative to `_.reduce`; this method transforms `object` to a new + * `accumulator` object which is the result of running each of its own + * enumerable string keyed properties thru `iteratee`, with each invocation + * potentially mutating the `accumulator` object. If `accumulator` is not + * provided, a new object with the same `[[Prototype]]` will be used. The + * iteratee is invoked with four arguments: (accumulator, value, key, object). + * Iteratee functions may exit iteration early by explicitly returning `false`. + * + * @static + * @memberOf _ + * @since 1.3.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @param {*} [accumulator] The custom accumulator value. + * @returns {*} Returns the accumulated value. + * @example + * + * _.transform([2, 3, 4], function(result, n) { + * result.push(n *= n); + * return n % 2 == 0; + * }, []); + * // => [4, 9] + * + * _.transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) { + * (result[value] || (result[value] = [])).push(key); + * }, {}); + * // => { '1': ['a', 'c'], '2': ['b'] } + */ + function transform(object, iteratee, accumulator) { + var isArr = isArray(object), + isArrLike = isArr || isBuffer(object) || isTypedArray(object); + + iteratee = getIteratee(iteratee, 4); + if (accumulator == null) { + var Ctor = object && object.constructor; + if (isArrLike) { + accumulator = isArr ? new Ctor : []; + } + else if (isObject(object)) { + accumulator = isFunction(Ctor) ? baseCreate(getPrototype(object)) : {}; + } + else { + accumulator = {}; + } + } + (isArrLike ? arrayEach : baseForOwn)(object, function(value, index, object) { + return iteratee(accumulator, value, index, object); + }); + return accumulator; + } + + /** + * Removes the property at `path` of `object`. + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to unset. + * @returns {boolean} Returns `true` if the property is deleted, else `false`. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 7 } }] }; + * _.unset(object, 'a[0].b.c'); + * // => true + * + * console.log(object); + * // => { 'a': [{ 'b': {} }] }; + * + * _.unset(object, ['a', '0', 'b', 'c']); + * // => true + * + * console.log(object); + * // => { 'a': [{ 'b': {} }] }; + */ + function unset(object, path) { + return object == null ? true : baseUnset(object, path); + } + + /** + * This method is like `_.set` except that accepts `updater` to produce the + * value to set. Use `_.updateWith` to customize `path` creation. The `updater` + * is invoked with one argument: (value). + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.6.0 + * @category Object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {Function} updater The function to produce the updated value. + * @returns {Object} Returns `object`. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 3 } }] }; + * + * _.update(object, 'a[0].b.c', function(n) { return n * n; }); + * console.log(object.a[0].b.c); + * // => 9 + * + * _.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; }); + * console.log(object.x[0].y.z); + * // => 0 + */ + function update(object, path, updater) { + return object == null ? object : baseUpdate(object, path, castFunction(updater)); + } + + /** + * This method is like `_.update` except that it accepts `customizer` which is + * invoked to produce the objects of `path`. If `customizer` returns `undefined` + * path creation is handled by the method instead. The `customizer` is invoked + * with three arguments: (nsValue, key, nsObject). + * + * **Note:** This method mutates `object`. + * + * @static + * @memberOf _ + * @since 4.6.0 + * @category Object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {Function} updater The function to produce the updated value. + * @param {Function} [customizer] The function to customize assigned values. + * @returns {Object} Returns `object`. + * @example + * + * var object = {}; + * + * _.updateWith(object, '[0][1]', _.constant('a'), Object); + * // => { '0': { '1': 'a' } } + */ + function updateWith(object, path, updater, customizer) { + customizer = typeof customizer == 'function' ? customizer : undefined; + return object == null ? object : baseUpdate(object, path, castFunction(updater), customizer); + } + + /** + * Creates an array of the own enumerable string keyed property values of `object`. + * + * **Note:** Non-object values are coerced to objects. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property values. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.values(new Foo); + * // => [1, 2] (iteration order is not guaranteed) + * + * _.values('hi'); + * // => ['h', 'i'] + */ + function values(object) { + return object == null ? [] : baseValues(object, keys(object)); + } + + /** + * Creates an array of the own and inherited enumerable string keyed property + * values of `object`. + * + * **Note:** Non-object values are coerced to objects. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category Object + * @param {Object} object The object to query. + * @returns {Array} Returns the array of property values. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.valuesIn(new Foo); + * // => [1, 2, 3] (iteration order is not guaranteed) + */ + function valuesIn(object) { + return object == null ? [] : baseValues(object, keysIn(object)); + } + + /*------------------------------------------------------------------------*/ + + /** + * Clamps `number` within the inclusive `lower` and `upper` bounds. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category Number + * @param {number} number The number to clamp. + * @param {number} [lower] The lower bound. + * @param {number} upper The upper bound. + * @returns {number} Returns the clamped number. + * @example + * + * _.clamp(-10, -5, 5); + * // => -5 + * + * _.clamp(10, -5, 5); + * // => 5 + */ + function clamp(number, lower, upper) { + if (upper === undefined) { + upper = lower; + lower = undefined; + } + if (upper !== undefined) { + upper = toNumber(upper); + upper = upper === upper ? upper : 0; + } + if (lower !== undefined) { + lower = toNumber(lower); + lower = lower === lower ? lower : 0; + } + return baseClamp(toNumber(number), lower, upper); + } + + /** + * Checks if `n` is between `start` and up to, but not including, `end`. If + * `end` is not specified, it's set to `start` with `start` then set to `0`. + * If `start` is greater than `end` the params are swapped to support + * negative ranges. + * + * @static + * @memberOf _ + * @since 3.3.0 + * @category Number + * @param {number} number The number to check. + * @param {number} [start=0] The start of the range. + * @param {number} end The end of the range. + * @returns {boolean} Returns `true` if `number` is in the range, else `false`. + * @see _.range, _.rangeRight + * @example + * + * _.inRange(3, 2, 4); + * // => true + * + * _.inRange(4, 8); + * // => true + * + * _.inRange(4, 2); + * // => false + * + * _.inRange(2, 2); + * // => false + * + * _.inRange(1.2, 2); + * // => true + * + * _.inRange(5.2, 4); + * // => false + * + * _.inRange(-3, -2, -6); + * // => true + */ + function inRange(number, start, end) { + start = toFinite(start); + if (end === undefined) { + end = start; + start = 0; + } else { + end = toFinite(end); + } + number = toNumber(number); + return baseInRange(number, start, end); + } + + /** + * Produces a random number between the inclusive `lower` and `upper` bounds. + * If only one argument is provided a number between `0` and the given number + * is returned. If `floating` is `true`, or either `lower` or `upper` are + * floats, a floating-point number is returned instead of an integer. + * + * **Note:** JavaScript follows the IEEE-754 standard for resolving + * floating-point values which can produce unexpected results. + * + * @static + * @memberOf _ + * @since 0.7.0 + * @category Number + * @param {number} [lower=0] The lower bound. + * @param {number} [upper=1] The upper bound. + * @param {boolean} [floating] Specify returning a floating-point number. + * @returns {number} Returns the random number. + * @example + * + * _.random(0, 5); + * // => an integer between 0 and 5 + * + * _.random(5); + * // => also an integer between 0 and 5 + * + * _.random(5, true); + * // => a floating-point number between 0 and 5 + * + * _.random(1.2, 5.2); + * // => a floating-point number between 1.2 and 5.2 + */ + function random(lower, upper, floating) { + if (floating && typeof floating != 'boolean' && isIterateeCall(lower, upper, floating)) { + upper = floating = undefined; + } + if (floating === undefined) { + if (typeof upper == 'boolean') { + floating = upper; + upper = undefined; + } + else if (typeof lower == 'boolean') { + floating = lower; + lower = undefined; + } + } + if (lower === undefined && upper === undefined) { + lower = 0; + upper = 1; + } + else { + lower = toFinite(lower); + if (upper === undefined) { + upper = lower; + lower = 0; + } else { + upper = toFinite(upper); + } + } + if (lower > upper) { + var temp = lower; + lower = upper; + upper = temp; + } + if (floating || lower % 1 || upper % 1) { + var rand = nativeRandom(); + return nativeMin(lower + (rand * (upper - lower + freeParseFloat('1e-' + ((rand + '').length - 1)))), upper); + } + return baseRandom(lower, upper); + } + + /*------------------------------------------------------------------------*/ + + /** + * Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the camel cased string. + * @example + * + * _.camelCase('Foo Bar'); + * // => 'fooBar' + * + * _.camelCase('--foo-bar--'); + * // => 'fooBar' + * + * _.camelCase('__FOO_BAR__'); + * // => 'fooBar' + */ + var camelCase = createCompounder(function(result, word, index) { + word = word.toLowerCase(); + return result + (index ? capitalize(word) : word); + }); + + /** + * Converts the first character of `string` to upper case and the remaining + * to lower case. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to capitalize. + * @returns {string} Returns the capitalized string. + * @example + * + * _.capitalize('FRED'); + * // => 'Fred' + */ + function capitalize(string) { + return upperFirst(toString(string).toLowerCase()); + } + + /** + * Deburrs `string` by converting + * [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table) + * and [Latin Extended-A](https://en.wikipedia.org/wiki/Latin_Extended-A) + * letters to basic Latin letters and removing + * [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to deburr. + * @returns {string} Returns the deburred string. + * @example + * + * _.deburr('déjà vu'); + * // => 'deja vu' + */ + function deburr(string) { + string = toString(string); + return string && string.replace(reLatin, deburrLetter).replace(reComboMark, ''); + } + + /** + * Checks if `string` ends with the given target string. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to inspect. + * @param {string} [target] The string to search for. + * @param {number} [position=string.length] The position to search up to. + * @returns {boolean} Returns `true` if `string` ends with `target`, + * else `false`. + * @example + * + * _.endsWith('abc', 'c'); + * // => true + * + * _.endsWith('abc', 'b'); + * // => false + * + * _.endsWith('abc', 'b', 2); + * // => true + */ + function endsWith(string, target, position) { + string = toString(string); + target = baseToString(target); + + var length = string.length; + position = position === undefined + ? length + : baseClamp(toInteger(position), 0, length); + + var end = position; + position -= target.length; + return position >= 0 && string.slice(position, end) == target; + } + + /** + * Converts the characters "&", "<", ">", '"', and "'" in `string` to their + * corresponding HTML entities. + * + * **Note:** No other characters are escaped. To escape additional + * characters use a third-party library like [_he_](https://mths.be/he). + * + * Though the ">" character is escaped for symmetry, characters like + * ">" and "/" don't need escaping in HTML and have no special meaning + * unless they're part of a tag or unquoted attribute value. See + * [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands) + * (under "semi-related fun fact") for more details. + * + * When working with HTML you should always + * [quote attribute values](http://wonko.com/post/html-escaping) to reduce + * XSS vectors. + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category String + * @param {string} [string=''] The string to escape. + * @returns {string} Returns the escaped string. + * @example + * + * _.escape('fred, barney, & pebbles'); + * // => 'fred, barney, & pebbles' + */ + function escape(string) { + string = toString(string); + return (string && reHasUnescapedHtml.test(string)) + ? string.replace(reUnescapedHtml, escapeHtmlChar) + : string; + } + + /** + * Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+", + * "?", "(", ")", "[", "]", "{", "}", and "|" in `string`. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to escape. + * @returns {string} Returns the escaped string. + * @example + * + * _.escapeRegExp('[lodash](https://lodash.com/)'); + * // => '\[lodash\]\(https://lodash\.com/\)' + */ + function escapeRegExp(string) { + string = toString(string); + return (string && reHasRegExpChar.test(string)) + ? string.replace(reRegExpChar, '\\$&') + : string; + } + + /** + * Converts `string` to + * [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the kebab cased string. + * @example + * + * _.kebabCase('Foo Bar'); + * // => 'foo-bar' + * + * _.kebabCase('fooBar'); + * // => 'foo-bar' + * + * _.kebabCase('__FOO_BAR__'); + * // => 'foo-bar' + */ + var kebabCase = createCompounder(function(result, word, index) { + return result + (index ? '-' : '') + word.toLowerCase(); + }); + + /** + * Converts `string`, as space separated words, to lower case. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the lower cased string. + * @example + * + * _.lowerCase('--Foo-Bar--'); + * // => 'foo bar' + * + * _.lowerCase('fooBar'); + * // => 'foo bar' + * + * _.lowerCase('__FOO_BAR__'); + * // => 'foo bar' + */ + var lowerCase = createCompounder(function(result, word, index) { + return result + (index ? ' ' : '') + word.toLowerCase(); + }); + + /** + * Converts the first character of `string` to lower case. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the converted string. + * @example + * + * _.lowerFirst('Fred'); + * // => 'fred' + * + * _.lowerFirst('FRED'); + * // => 'fRED' + */ + var lowerFirst = createCaseFirst('toLowerCase'); + + /** + * Pads `string` on the left and right sides if it's shorter than `length`. + * Padding characters are truncated if they can't be evenly divided by `length`. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to pad. + * @param {number} [length=0] The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padded string. + * @example + * + * _.pad('abc', 8); + * // => ' abc ' + * + * _.pad('abc', 8, '_-'); + * // => '_-abc_-_' + * + * _.pad('abc', 3); + * // => 'abc' + */ + function pad(string, length, chars) { + string = toString(string); + length = toInteger(length); + + var strLength = length ? stringSize(string) : 0; + if (!length || strLength >= length) { + return string; + } + var mid = (length - strLength) / 2; + return ( + createPadding(nativeFloor(mid), chars) + + string + + createPadding(nativeCeil(mid), chars) + ); + } + + /** + * Pads `string` on the right side if it's shorter than `length`. Padding + * characters are truncated if they exceed `length`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category String + * @param {string} [string=''] The string to pad. + * @param {number} [length=0] The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padded string. + * @example + * + * _.padEnd('abc', 6); + * // => 'abc ' + * + * _.padEnd('abc', 6, '_-'); + * // => 'abc_-_' + * + * _.padEnd('abc', 3); + * // => 'abc' + */ + function padEnd(string, length, chars) { + string = toString(string); + length = toInteger(length); + + var strLength = length ? stringSize(string) : 0; + return (length && strLength < length) + ? (string + createPadding(length - strLength, chars)) + : string; + } + + /** + * Pads `string` on the left side if it's shorter than `length`. Padding + * characters are truncated if they exceed `length`. + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category String + * @param {string} [string=''] The string to pad. + * @param {number} [length=0] The padding length. + * @param {string} [chars=' '] The string used as padding. + * @returns {string} Returns the padded string. + * @example + * + * _.padStart('abc', 6); + * // => ' abc' + * + * _.padStart('abc', 6, '_-'); + * // => '_-_abc' + * + * _.padStart('abc', 3); + * // => 'abc' + */ + function padStart(string, length, chars) { + string = toString(string); + length = toInteger(length); + + var strLength = length ? stringSize(string) : 0; + return (length && strLength < length) + ? (createPadding(length - strLength, chars) + string) + : string; + } + + /** + * Converts `string` to an integer of the specified radix. If `radix` is + * `undefined` or `0`, a `radix` of `10` is used unless `value` is a + * hexadecimal, in which case a `radix` of `16` is used. + * + * **Note:** This method aligns with the + * [ES5 implementation](https://es5.github.io/#x15.1.2.2) of `parseInt`. + * + * @static + * @memberOf _ + * @since 1.1.0 + * @category String + * @param {string} string The string to convert. + * @param {number} [radix=10] The radix to interpret `value` by. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {number} Returns the converted integer. + * @example + * + * _.parseInt('08'); + * // => 8 + * + * _.map(['6', '08', '10'], _.parseInt); + * // => [6, 8, 10] + */ + function parseInt(string, radix, guard) { + if (guard || radix == null) { + radix = 0; + } else if (radix) { + radix = +radix; + } + return nativeParseInt(toString(string).replace(reTrimStart, ''), radix || 0); + } + + /** + * Repeats the given string `n` times. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to repeat. + * @param {number} [n=1] The number of times to repeat the string. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {string} Returns the repeated string. + * @example + * + * _.repeat('*', 3); + * // => '***' + * + * _.repeat('abc', 2); + * // => 'abcabc' + * + * _.repeat('abc', 0); + * // => '' + */ + function repeat(string, n, guard) { + if ((guard ? isIterateeCall(string, n, guard) : n === undefined)) { + n = 1; + } else { + n = toInteger(n); + } + return baseRepeat(toString(string), n); + } + + /** + * Replaces matches for `pattern` in `string` with `replacement`. + * + * **Note:** This method is based on + * [`String#replace`](https://mdn.io/String/replace). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category String + * @param {string} [string=''] The string to modify. + * @param {RegExp|string} pattern The pattern to replace. + * @param {Function|string} replacement The match replacement. + * @returns {string} Returns the modified string. + * @example + * + * _.replace('Hi Fred', 'Fred', 'Barney'); + * // => 'Hi Barney' + */ + function replace() { + var args = arguments, + string = toString(args[0]); + + return args.length < 3 ? string : string.replace(args[1], args[2]); + } + + /** + * Converts `string` to + * [snake case](https://en.wikipedia.org/wiki/Snake_case). + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the snake cased string. + * @example + * + * _.snakeCase('Foo Bar'); + * // => 'foo_bar' + * + * _.snakeCase('fooBar'); + * // => 'foo_bar' + * + * _.snakeCase('--FOO-BAR--'); + * // => 'foo_bar' + */ + var snakeCase = createCompounder(function(result, word, index) { + return result + (index ? '_' : '') + word.toLowerCase(); + }); + + /** + * Splits `string` by `separator`. + * + * **Note:** This method is based on + * [`String#split`](https://mdn.io/String/split). + * + * @static + * @memberOf _ + * @since 4.0.0 + * @category String + * @param {string} [string=''] The string to split. + * @param {RegExp|string} separator The separator pattern to split by. + * @param {number} [limit] The length to truncate results to. + * @returns {Array} Returns the string segments. + * @example + * + * _.split('a-b-c', '-', 2); + * // => ['a', 'b'] + */ + function split(string, separator, limit) { + if (limit && typeof limit != 'number' && isIterateeCall(string, separator, limit)) { + separator = limit = undefined; + } + limit = limit === undefined ? MAX_ARRAY_LENGTH : limit >>> 0; + if (!limit) { + return []; + } + string = toString(string); + if (string && ( + typeof separator == 'string' || + (separator != null && !isRegExp(separator)) + )) { + separator = baseToString(separator); + if (!separator && hasUnicode(string)) { + return castSlice(stringToArray(string), 0, limit); + } + } + return string.split(separator, limit); + } + + /** + * Converts `string` to + * [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage). + * + * @static + * @memberOf _ + * @since 3.1.0 + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the start cased string. + * @example + * + * _.startCase('--foo-bar--'); + * // => 'Foo Bar' + * + * _.startCase('fooBar'); + * // => 'Foo Bar' + * + * _.startCase('__FOO_BAR__'); + * // => 'FOO BAR' + */ + var startCase = createCompounder(function(result, word, index) { + return result + (index ? ' ' : '') + upperFirst(word); + }); + + /** + * Checks if `string` starts with the given target string. + * + * @static + * @memberOf _ + * @since 3.0.0 + * @category String + * @param {string} [string=''] The string to inspect. + * @param {string} [target] The string to search for. + * @param {number} [position=0] The position to search from. + * @returns {boolean} Returns `true` if `string` starts with `target`, + * else `false`. + * @example + * + * _.startsWith('abc', 'a'); + * // => true + * + * _.startsWith('abc', 'b'); + * // => false + * + * _.startsWith('abc', 'b', 1); + * // => true + */ + function startsWith(string, target, position) { + string = toString(string); + position = position == null + ? 0 + : baseClamp(toInteger(position), 0, string.length); + + target = baseToString(target); + return string.slice(position, position + target.length) == target; + } + + /** + * Creates a compiled template function that can interpolate data properties + * in "interpolate" delimiters, HTML-escape interpolated data properties in + * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data + * properties may be accessed as free variables in the template. If a setting + * object is given, it takes precedence over `_.templateSettings` values. + * + * **Note:** In the development build `_.template` utilizes + * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl) + * for easier debugging. + * + * For more information on precompiling templates see + * [lodash's custom builds documentation](https://lodash.com/custom-builds). + * + * For more information on Chrome extension sandboxes see + * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval). + * + * @static + * @since 0.1.0 + * @memberOf _ + * @category String + * @param {string} [string=''] The template string. + * @param {Object} [options={}] The options object. + * @param {RegExp} [options.escape=_.templateSettings.escape] + * The HTML "escape" delimiter. + * @param {RegExp} [options.evaluate=_.templateSettings.evaluate] + * The "evaluate" delimiter. + * @param {Object} [options.imports=_.templateSettings.imports] + * An object to import into the template as free variables. + * @param {RegExp} [options.interpolate=_.templateSettings.interpolate] + * The "interpolate" delimiter. + * @param {string} [options.sourceURL='lodash.templateSources[n]'] + * The sourceURL of the compiled template. + * @param {string} [options.variable='obj'] + * The data object variable name. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. + * @returns {Function} Returns the compiled template function. + * @example + * + * // Use the "interpolate" delimiter to create a compiled template. + * var compiled = _.template('hello <%= user %>!'); + * compiled({ 'user': 'fred' }); + * // => 'hello fred!' + * + * // Use the HTML "escape" delimiter to escape data property values. + * var compiled = _.template('<%- value %>'); + * compiled({ 'value': '