@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
72 lines (71 loc) • 4.38 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mathjsLoadingWrapper = exports.mathjsIsLoaded = exports.loadMathjs = void 0;
exports.substituteVars = substituteVars;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _escapeRegExp = _interopRequireDefault(require("lodash/escapeRegExp"));
var _quizI18n = require("@instructure/quiz-i18n");
var _quizScientificNotation = require("@instructure/quiz-scientific-notation");
var _additional_functions = _interopRequireDefault(require("./additional_functions"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
var openingQuote = '`';
var closingQuote = '`';
function formatNumber(value, locale, mathquill) {
var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
return (0, _quizScientificNotation.isScientificNotation)(value) ? formatScientificNotation(value, mathquill) : (0, _quizI18n.formatPlain)(value, locale, opts);
}
function formatScientificNotation(value, mathquill) {
if (!mathquill) return value;
var _parseScientificNotat = (0, _quizScientificNotation.parseScientificNotation)(value),
_parseScientificNotat2 = (0, _slicedToArray2["default"])(_parseScientificNotat, 2),
mantissa = _parseScientificNotat2[0],
exponent = _parseScientificNotat2[1];
return "".concat(mantissa, "*10^{").concat(exponent, "}");
}
function substituteVars(input, vars, locale) {
return vars.reduce(function (text, v) {
var fractionDigits = v.value.toString().split('.')[1];
var numFractionDigits = fractionDigits && fractionDigits.length;
return text
// Replace mathquill variables first so that we can apply special
// formatting for numbers in scientific notation:
.replace(new RegExp(// eslint-disable-next-line no-useless-escape
"(\\(.*?)".concat(openingQuote).concat((0, _escapeRegExp["default"])(v.name)).concat(closingQuote, "(.*?\\))"), 'g'), "$1".concat(formatNumber(v.value, locale, true, {
minimumFractionDigits: numFractionDigits
}), "$2"))
// Then substitute non-mathquill variables:
.replace(new RegExp("".concat(openingQuote).concat((0, _escapeRegExp["default"])(v.name)).concat(closingQuote), 'g'), formatNumber(v.value, locale, false, {
minimumFractionDigits: numFractionDigits,
useGrouping: true
}));
}, input);
}
var promiseToGetMathjs;
var mathjsLoadingWrapper = exports.mathjsLoadingWrapper = {
mathjs: null
};
var loadMathjs = exports.loadMathjs = function loadMathjs() {
if (!promiseToGetMathjs) {
promiseToGetMathjs = Promise.resolve().then(function () {
return _interopRequireWildcard(require(/* webpackChunkName: "mathjs" */'mathjs'));
}).then(function (_mathjs) {
// import all our additional factory functions
_mathjs["import"](_additional_functions["default"], {
override: true
});
_mathjs.config({
angles: 'deg'
});
mathjsLoadingWrapper.mathjs = _mathjs;
return mathjsLoadingWrapper.mathjs;
});
}
return promiseToGetMathjs;
};
var mathjsIsLoaded = exports.mathjsIsLoaded = function mathjsIsLoaded() {
return mathjsLoadingWrapper.mathjs !== null;
};