Skip to content

Commit c35a801

Browse files
dvd101xDavid Contrerasjosdejong
authored
Print one based in the parser (#3009)
* broadcasting * Simplified broadcasting * Updated for broadcasting * Changed to camel case * Camel case and auto formating * Added comments * Skip if matrices have the same size * Fixed issue with undefined variable missing dot in `A._size` * Implemented broadcasting in all functions * Added helper functions * Added function to check for broadcasting rules * Tests for broadcasted arithmetic * Fixed issue with matrix the size of a vector * Documented and updated broadcasting * Included broadcast.test * Included math to syntax when missing * Added print transform and tests * Simplify conditional * Included regex in an util --------- Co-authored-by: David Contreras <[email protected]> Co-authored-by: Jos de Jong <[email protected]>
1 parent 08bf93b commit c35a801

File tree

7 files changed

+56
-2
lines changed

7 files changed

+56
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Evan Miller <[email protected]>
217217
218218
Ari Markowitz <[email protected]>
219219
220+
David Contreras <[email protected]
220221
Jaeu Jeong <[email protected]>
221222
cyavictor88 <[email protected]>
222223
David Contreras <[email protected]>

src/expression/embeddedDocs/function/utils/print.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const printDocs = {
1010
'print("Lucy is $age years old", {age: 5})',
1111
'print("The value of pi is $pi", {pi: pi}, 3)',
1212
'print("Hello, $user.name!", {user: {name: "John"}})',
13-
'print("Values: $0, $1, $2", [6, 9, 4])'
13+
'print("Values: $1, $2, $3", [6, 9, 4])'
1414
],
1515
seealso: ['format']
1616
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createPrint } from '../../function/string/print.js'
2+
import { factory } from '../../utils/factory.js'
3+
import { printTemplate } from '../../utils/print.js'
4+
5+
const name = 'print'
6+
const dependencies = ['typed', 'matrix', 'zeros', 'add']
7+
8+
export const createPrintTransform = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, zeros, add }) => {
9+
const print = createPrint({ typed, matrix, zeros, add })
10+
return typed(name, {
11+
'string, Object | Array': function (template, values) { return print(_convertTemplateToZeroBasedIndex(template), values) },
12+
'string, Object | Array, number | Object': function (template, values, options) { return print(_convertTemplateToZeroBasedIndex(template), values, options) }
13+
})
14+
15+
function _convertTemplateToZeroBasedIndex (template) {
16+
return template.replace(printTemplate, (x) => {
17+
const parts = x.slice(1).split('.')
18+
const result = parts.map(function (part) {
19+
if (!isNaN(part) && part.length > 0) {
20+
return parseInt(part) - 1
21+
} else {
22+
return part
23+
}
24+
})
25+
return '$' + result.join('.')
26+
})
27+
}
28+
}, { isTransformFunction: true })

src/factoriesAny.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,4 @@ export { createSumTransform } from './expression/transform/sum.transform.js'
357357
export { createQuantileSeqTransform } from './expression/transform/quantileSeq.transform.js'
358358
export { createCumSumTransform } from './expression/transform/cumsum.transform.js'
359359
export { createVarianceTransform } from './expression/transform/variance.transform.js'
360+
export { createPrintTransform } from './expression/transform/print.transform.js'

src/function/string/print.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { format } from '../../utils/string.js'
22
import { isString } from '../../utils/is.js'
33
import { factory } from '../../utils/factory.js'
4+
import { printTemplate } from '../../utils/print.js'
45

56
const name = 'print'
67
const dependencies = ['typed']
@@ -66,9 +67,12 @@ export const createPrint = /* #__PURE__ */ factory(name, dependencies, ({ typed
6667
* @private
6768
*/
6869
function _print (template, values, options) {
69-
return template.replace(/\$([\w.]+)/g, function (original, key) {
70+
return template.replace(printTemplate, function (original, key) {
7071
const keys = key.split('.')
7172
let value = values[keys.shift()]
73+
if (value !== undefined && value.isMatrix) {
74+
value = value.toArray()
75+
}
7276
while (keys.length && value !== undefined) {
7377
const k = keys.shift()
7478
value = k ? value[k] : value + '.'

src/utils/print.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const printTemplate = /\$([\w.]+)/g

test/unit-tests/function/string/print.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ describe('print', function () {
2727
}), 'hello, first last!')
2828
})
2929

30+
it('should interpolate values from a nested object in a template (mixed object/matrix template)', function () {
31+
assert.strictEqual(math.print('hello$separator.0 $name.first $name.last!', {
32+
name: {
33+
first: 'first',
34+
last: 'last'
35+
},
36+
separator: math.matrix([','])
37+
}), 'hello, first last!')
38+
})
39+
3040
it('should round interpolate values with provided precision (object template)', function () {
3141
assert.strictEqual(math.print('pi=$pi', { pi: math.pi }, 3), 'pi=3.14')
3242
})
@@ -104,4 +114,13 @@ describe('print', function () {
104114
const expression = math.parse('print(template,values)')
105115
assert.strictEqual(expression.toTex(), '\\mathrm{print}\\left( template, values\\right)')
106116
})
117+
118+
it('should work one indexed in the parser for all previous combinations', function () {
119+
assert.deepStrictEqual(math.evaluate("print('I like $food', {food:'pizza'})"), 'I like pizza')
120+
assert.deepStrictEqual(math.evaluate("print('I like $1', ['pizza'])"), 'I like pizza')
121+
assert.deepStrictEqual(math.evaluate("print('I like $food.1', {food:['pizza']})"), 'I like pizza')
122+
assert.deepStrictEqual(math.evaluate("print('I like $1.food', [{food:'pizza'}])"), 'I like pizza')
123+
assert.deepStrictEqual(math.evaluate("print('I like $food.1 and $food.2', {food:['pizza','tacos']})"), 'I like pizza and tacos')
124+
assert.deepStrictEqual(math.evaluate("print('Values: $1, $2, $3', [6, 9, 4])"), 'Values: 6, 9, 4')
125+
})
107126
})

0 commit comments

Comments
 (0)