simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

print.js (2957B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createPrint = void 0;
      7 
      8 var _string = require("../../utils/string.js");
      9 
     10 var _is = require("../../utils/is.js");
     11 
     12 var _factory = require("../../utils/factory.js");
     13 
     14 var name = 'print';
     15 var dependencies = ['typed'];
     16 var createPrint = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed;
     18 
     19   /**
     20    * Interpolate values into a string template.
     21    *
     22    * Syntax:
     23    *
     24    *     math.print(template, values)
     25    *     math.print(template, values, precision)
     26    *     math.print(template, values, options)
     27    *
     28    * Example usage:
     29    *
     30    *     // the following outputs: 'Lucy is 5 years old'
     31    *     math.print('Lucy is $age years old', {age: 5})
     32    *
     33    *     // the following outputs: 'The value of pi is 3.141592654'
     34    *     math.print('The value of pi is $pi', {pi: math.pi}, 10)
     35    *
     36    *     // the following outputs: 'hello Mary! The date is 2013-03-23'
     37    *     math.print('Hello $user.name! The date is $date', {
     38    *       user: {
     39    *         name: 'Mary',
     40    *       },
     41    *       date: new Date(2013, 2, 23).toISOString().substring(0, 10)
     42    *     })
     43    *
     44    *     // the following outputs: 'My favorite fruits are apples and bananas !'
     45    *     math.print('My favorite fruits are $0 and $1 !', [
     46    *       'apples',
     47    *       'bananas'
     48    *     ])
     49    *
     50    * See also:
     51    *
     52    *     format
     53    *
     54    * @param {string} template           A string containing variable placeholders.
     55    * @param {Object | Array | Matrix}   values An object or array containing variables
     56    *                                    which will be filled in in the template.
     57    * @param {number | Object} [options] Formatting options,
     58    *                                    or the number of digits to format numbers.
     59    *                                    See function math.format for a description
     60    *                                    of all options.
     61    * @return {string} Interpolated string
     62    */
     63   return typed(name, {
     64     // note: Matrix will be converted automatically to an Array
     65     'string, Object | Array': _print,
     66     'string, Object | Array, number | Object': _print
     67   });
     68 });
     69 /**
     70  * Interpolate values into a string template.
     71  * @param {string} template
     72  * @param {Object} values
     73  * @param {number | Object} [options]
     74  * @returns {string} Interpolated string
     75  * @private
     76  */
     77 
     78 exports.createPrint = createPrint;
     79 
     80 function _print(template, values, options) {
     81   return template.replace(/\$([\w.]+)/g, function (original, key) {
     82     var keys = key.split('.');
     83     var value = values[keys.shift()];
     84 
     85     while (keys.length && value !== undefined) {
     86       var k = keys.shift();
     87       value = k ? value[k] : value + '.';
     88     }
     89 
     90     if (value !== undefined) {
     91       if (!(0, _is.isString)(value)) {
     92         return (0, _string.format)(value, options);
     93       } else {
     94         return value;
     95       }
     96     }
     97 
     98     return original;
     99   });
    100 }