Skip to content

Commit

Permalink
fix: xApplyFunctionStr did not accept functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Oct 27, 2022
1 parent 9538864 commit 8b6dca3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/x/__tests__/xApplyFunctionStr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ test('xApplyFunctionStr', () => {
expect(
xApplyFunctionStr([3, 4], { fctString: 'y*2', variableLabel: 'y' }),
).toStrictEqual(Float64Array.from([6, 8]));
expect(
xApplyFunctionStr([100, 10], { fctString: 'log10(x)', variableLabel: 'x' }),
).toStrictEqual(Float64Array.from([2, 1]));
expect(
xApplyFunctionStr([100.12, 10.23], {
fctString: 'floor(y)',
variableLabel: 'y',
}),
).toStrictEqual(Float64Array.from([100, 10]));
});
2 changes: 1 addition & 1 deletion src/x/xApplyFunctionStr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function xApplyFunctionStr(
const fct = new Function(
variableLabel,
`return Number(${fctString
.replace(/(?:^|\W)(?:[a-z]{2,})/g, '$1Math.$2')
.replace(/(?<before>^|\W)(?<after>[a-z0-9]{2,}\()/g, '$1Math.$2')

This comment has been minimized.

Copy link
@targos

targos Oct 28, 2022

Member

btw you can use $<before> and $<after> in the replacement string

This comment has been minimized.

Copy link
@lpatiny

lpatiny Oct 28, 2022

Author Member

Thanks !!! Indeed otherwise it just make no sense to name capture groups.

.replace(/Math\.Math/g, 'Math')})`,
);
const toReturn = Float64Array.from(array);
Expand Down

0 comments on commit 8b6dca3

Please sign in to comment.