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

ASC/CHR関数を配列対応させる #1853 #1854

Merged
merged 1 commit into from
Dec 2, 2024
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
42 changes: 34 additions & 8 deletions core/src/plugin_system.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,22 +1007,38 @@ export default {
return String(s).indexOf(a) + 1
}
},
'CHR': { // @文字コードから文字を返す // @CHR
'CHR': { // @文字コードV(あるいは文字列配列)から文字を返す // @CHR
type: 'func',
josi: [['の']],
pure: true,
fn: function (v: any) {
if (!String.fromCodePoint) { return String.fromCharCode(v) }
return String.fromCodePoint(v)
fn: function (v: number|number[]): string|string[] {
if (typeof v === 'number') {
if (!String.fromCodePoint) { return String.fromCharCode(v) }
return String.fromCodePoint(v)
}
const res: string[] = []
for (const s of v) {
if (!String.fromCodePoint) { res.push(String.fromCharCode(s)) }
res.push(String.fromCodePoint(s))
}
return res
}
},
'ASC': { // @文字列Vの最初の文字の文字コードを返す // @ASC
'ASC': { // @文字列V(あるいは文字列配列)の最初の文字の文字コードを返す // @ASC
type: 'func',
josi: [['の']],
pure: true,
fn: function (v: any) {
if (!String.prototype.codePointAt) { return String(v).charCodeAt(0) }
return String(v).codePointAt(0)
fn: function (v: string|string[]): number|number[] {
if (typeof v === 'string') {
if (!String.prototype.codePointAt) { return String(v).charCodeAt(0) }
return String(v).codePointAt(0) || 0
}
const res: number[] = []
for (const s of v) {
if (!String.prototype.codePointAt) { res.push(String(s).charCodeAt(0)) }
res.push(String(s).codePointAt(0) || 0)
}
return res
}
},
'文字挿入': { // @文字列SのI文字目に文字列Aを挿入する // @もじそうにゅう
Expand Down Expand Up @@ -1627,6 +1643,16 @@ export default {
return a2.join('' + s)
}
},
'配列只結合': { // @配列Aの要素をただ結合して文字列で返す。(「」で配列結合と同じ) // @はいれつただけつごう
type: 'func',
josi: [['を']],
pure: true,
fn: function (a: any): string {
if (a instanceof Array) { return a.join('') }
const a2 = String(a).split('\n') // 配列でなければ無理矢理改行で区切ってみる
return a2.join('')
}
},
'配列検索': { // @配列Aから文字列Sを探してインデックス番号(0起点)を返す。見つからなければ-1を返す。 // @はいれつけんさく
type: 'func',
josi: [['の', 'から'], ['を']],
Expand Down
6 changes: 6 additions & 0 deletions core/test/plugin_system_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -698,4 +698,10 @@ describe('plugin_system_test', async () => {
await cmp('??1+2*3', '7')
await cmp('??(1+2)*3', '9')
})
it('ASC/CHRの配列 #1853', async () => {
await cmp('["a","b","c"]のASCをJSON_Eして表示', '[97,98,99]') // 配列なら全ての文字のASC
await cmp('「abc」のASCをJSON_Eして表示', '97') // 文字列なら最初の文字のみ
await cmp('[97,98,99]のCHRを「」で配列結合して表示', 'abc') // 配列なら全てのCHR
await cmp('97のCHRを表示', 'a') // 数値
})
})