Skip to content

feat: support random function range arguments#657

Merged
MakinoharaShoko merged 1 commit into
OpenWebGAL:devfrom
xiaoxustudio:feat-random-arguments
Apr 1, 2025
Merged

feat: support random function range arguments#657
MakinoharaShoko merged 1 commit into
OpenWebGAL:devfrom
xiaoxustudio:feat-random-arguments

Conversation

@xiaoxustudio
Copy link
Copy Markdown
Member

@xiaoxustudio xiaoxustudio commented Mar 19, 2025

random 提供了参数选项,与lodash相同的参数:

random();
// => a floating-point number

random(0, 5);
// => an integer between 0 and 5
 
random(5);
// => also an integer between 0 and 5
 
random(5, true);
// => a floating-point number between 0 and 5
 
random(1.2, 5.2);
// => a floating-point number between 1.2 and 5.2

issue : #654

const valExp = sentence.content.split(/\s*=\s*/)[1];
if (valExp === 'random()') {
webgalStore.dispatch(targetReducerFunction({ key, value: Math.random() }));
if (/^\s*[a-zA-Z_$][\w$]*\s*\(.*\)\s*$/.test(valExp)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该明确判断是和 random 相关联

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该明确判断是和 random 相关联

这个主要是匹配xxx(...xxx)这种函数调用,因为想到以后的可扩展性

const instance = expression.compile(val);
return instance({
random: (...args: any[]) => {
return args.length ? random(...args) : Math.random();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lodash 的 random 不能不传参数吗

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lodash 的 random 不能不传参数吗

它不传参只返回0和1,不符合 预期的不传参为浮点数

@MakinoharaShoko MakinoharaShoko merged commit b3bc69d into OpenWebGAL:dev Apr 1, 2025
KonshinHaoshin pushed a commit to KonshinHaoshin/WebGAL_Eastmount that referenced this pull request Nov 7, 2025
…ents

feat: support random function range arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants