Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

「MAX」「最大値」「MIN」「最小値」「CLAMP」を追加 #1892

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions core/src/plugin_system.mts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,56 @@ export default {
return a.reduce((p: any, c: any) => p + c)
}
},
'MAX': { // @2個以上の数値のうち最大値を返す。// @MAX
type: 'func',
josi: [['の'], ['と']],
isVariableJosi: true,
pure: true,
fn: function (b: number, ...a: any): number {
const sys = a.pop()
return sys.__exec('最大値', [b, ...a, sys])
}
},
'最大値': { // @2個以上の数値のうち最大値を返す。// @さいだいち
type: 'func',
josi: [['の'], ['と']],
isVariableJosi: true,
pure: true,
fn: function (b: number, ...a: any): number {
a.pop() // 必ず末尾に sys があるので、末尾のシステム変数を除外
a.push(b)
return a.reduce((p: number, c: number) => Math.max(p, c))
}
},
'MIN': { // @2個以上の数値のうち最小値を返す。// @MIN
type: 'func',
josi: [['の'], ['と']],
isVariableJosi: true,
pure: true,
fn: function (b: number, ...a: any): number {
const sys = a.pop()
return sys.__exec('最小値', [b, ...a, sys])
}
},
'最小値': { // @2個以上の数値のうち最小値を返す。// @さいしょうち
type: 'func',
josi: [['の'], ['と']],
isVariableJosi: true,
pure: true,
fn: function (b: number, ...a: any): number {
a.pop() // 必ず末尾に sys があるので、末尾のシステム変数を除外
a.push(b)
return a.reduce((p: number, c: number) => Math.min(p, c))
}
},
'CLAMP': { // @数値を下限から上限の範囲内に収めた値を返す。// @CLAMP
type: 'func',
josi: [['の', 'を'], ['から'], ['までの', 'で']],
pure: true,
fn: function (x: number, a: number, b: number): number {
return Math.min(Math.max(x, a), b)
}
},

// @敬語
'ください': { // @敬語対応のため // @ください
Expand Down
28 changes: 28 additions & 0 deletions core/test/calc_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ describe('calc_test.js', async () => {
it('連続演算:て-3に5を掛けて表示', async () => {
await cmp('3に5を掛けて表示', '15')
})
it('MAX', async () => {
await cmp('10と20のMAXを表示', '20')
await cmp('MAX(10, 20)を表示', '20')
await cmp('MAX(10, 20, 30)を表示', '30')
})
it('最大値', async () => {
await cmp('10と20の最大値を表示', '20')
await cmp('10と20と30の最大値を表示', '30')
await cmp('10と10の最大値を表示', '10')
})
it('MIN', async () => {
await cmp('10と20のMINを表示', '10')
await cmp('MIN(10, 20)を表示', '10')
await cmp('MIN(5, 10, 20, 30)を表示', '5')
})
it('最小値', async () => {
await cmp('10と20の最小値を表示', '10')
await cmp('5と10と20と30の最小値を表示', '5')
await cmp('10と10の最小値を表示', '10')
})
it('CLAMP', async () => {
await cmp('10の20から30までのCLAMPを表示', '20')
await cmp('40を20から30でCLAMPして表示', '30')
await cmp('25の20から30までのCLAMPを表示', '25')
await cmp('CLAMP(10, 20, 30)を表示', '20')
await cmp('CLAMP(20, 20, 30)を表示', '20')
await cmp('CLAMP(30, 20, 30)を表示', '30')
})
it('配列', async () => {
await cmp('a=[];a[1]=30;a[1]を表示', '30')
await cmp('a=[];a【1】=30;a[1]を表示', '30')
Expand Down
Loading