UNPKG

quiz-interactions

Version:

A React UI component Library for quiz interaction types.

85 lines (63 loc) 3.03 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.substituteVars = substituteVars; exports.mathjsIsLoaded = exports.loadMathjs = exports.mathjsLoadingWrapper = void 0; var _interopRequireWildcard2 = _interopRequireDefault(require("@babel/runtime/helpers/interopRequireWildcard")); 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")); var openingQuote = '`'; var closingQuote = '`'; function formatNumber(value, locale, mathquill) { return (0, _quizScientificNotation.isScientificNotation)(value) ? formatScientificNotation(value, mathquill) : (0, _quizI18n.formatPlain)(value, locale); } 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) { return text // Replace mathquill variables first so that we can apply special // formatting for numbers in scientific notation: .replace( // eslint-disable-next-line no-useless-escape new RegExp("(\\(.*?)".concat(openingQuote).concat((0, _escapeRegExp.default)(v.name)).concat(closingQuote, "(.*?\\))"), 'g'), "$1".concat(formatNumber(v.value, locale, true), "$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)); }, input); } var promiseToGetMathjs; var mathjsLoadingWrapper = { mathjs: null }; exports.mathjsLoadingWrapper = mathjsLoadingWrapper; var loadMathjs = function loadMathjs() { if (!promiseToGetMathjs) { promiseToGetMathjs = Promise.resolve().then(function () { return (0, _interopRequireWildcard2.default)(require('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; }; exports.loadMathjs = loadMathjs; var mathjsIsLoaded = function mathjsIsLoaded() { return mathjsLoadingWrapper.mathjs !== null; }; exports.mathjsIsLoaded = mathjsIsLoaded;