Skip to content

Commit

Permalink
Merge pull request #1867 from kujirahand/add_migitorimu
Browse files Browse the repository at this point in the history
「右トリム」「末尾空白除去」命令を追加 #1866
  • Loading branch information
kujirahand authored Dec 19, 2024
2 parents ddb3c24 + 9a064ea commit 02250f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/src/nako_csv.mts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export function parse (txt: string, delimiter: string|undefined = undefined): st
return res
}

// convert 2D array to CSV string
export function stringify (ary: string[][], delimiter: string|undefined = undefined, eol: string|undefined = undefined): string {
// check arguments
if (delimiter === undefined) {
Expand Down
21 changes: 20 additions & 1 deletion core/src/plugin_system.mts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export default {
}
}
// Propアクセス支援
sys.__registPropAccessor = (f: Function, getProp: (prop: string|string[], sys: NakoSystem) => any, setProp: (prop: string|string[], value: object, sys: NakoSystem) => any, sys?: NakoSystem) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
sys.__registPropAccessor = (f: Function, getProp: (prop: string|string[], sys: NakoSystem) => any, setProp: (prop: string|string[], value: object, sys: NakoSystem) => any) => {
system.__propAccessor.push(
{
target: f,
Expand Down Expand Up @@ -1309,6 +1310,24 @@ export default {
return s
}
},
'右トリム': { // @文字列Sの末尾にある空白を削除する // @みぎとりむ
type: 'func',
josi: [['の', 'を']],
pure: true,
fn: function (s: string): string {
s = String(s).replace(/\s+$/, '')
return s
}
},
'末尾空白除去': { // @文字列Sの末尾にある空白を削除する // @まつびくうはくじょきょ
type: 'func',
josi: [['の', 'を']],
pure: true,
fn: function (s: string): string {
s = String(s).replace(/\s+$/, '')
return s
}
},

// @文字変換
'大文字変換': { // @アルファベットの文字列Sを大文字に変換 // @おおもじへんかん
Expand Down
15 changes: 9 additions & 6 deletions core/test/plugin_system_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('plugin_system_test', async () => {
const g = await nako.runAsync(code, 'main.nako3')
assert.strictEqual(g.log, res)
}
const cmpex = async (/** @type {string} */ code, /** @type { name: string, message: string } */ exinfo) => {
const cmpex = async (/** @type {string} */ code, /** @type {name: string, message: string} */ exinfo) => {
const nako = new NakoCompiler()
nako.getLogger().debug('code=' + code)
try {
Expand Down Expand Up @@ -484,8 +484,8 @@ describe('plugin_system_test', async () => {
it('「ナデシコ」が空白行を出力してしまう問題の修正', async () => {
let lineCount = 0
const nako = new NakoCompiler()
nako.logger.addListener('stdout', (_data) => { lineCount++ })
nako.run('「a=1+2」をナデシコ')
nako.getLogger().addListener('stdout', (_data) => { lineCount++ })
await nako.runAsync('「a=1+2」をナデシコ', 'main.nako')
assert.strictEqual(lineCount, 0)
})
it('JSメソッド実行 #854', async () => {
Expand All @@ -498,13 +498,10 @@ describe('plugin_system_test', async () => {
globalScope = global
globalName = 'global'
}
// @ts-ignore
globalScope.jstest = () => { return 777 }
await cmp('「' + globalName + '」の「jstest」を[]でJSメソッド実行して表示。', '777')
// @ts-ignore
globalScope.jstest_x2 = (/** @type {number} */ a) => { return a * 2 }
await cmp('「' + globalName + '」の「jstest_x2」を30でJSメソッド実行して表示。', '60')
// @ts-ignore
globalScope.jstest_mul = (/** @type {number} */ a, /** @type {number} */ b) => { return a * b }
await cmp('「' + globalName + '」の「jstest_mul」を[30,30]でJSメソッド実行して表示。', '900')
})
Expand Down Expand Up @@ -704,4 +701,10 @@ describe('plugin_system_test', async () => {
await cmp('[97,98,99]のCHRを「」で配列結合して表示', 'abc') // 配列なら全てのCHR
await cmp('97のCHRを表示', 'a') // 数値
})
it('右トリム/末尾空白除去 #1866', async () => {
await cmp('「 abc 」を右トリムして表示', ' abc')
await cmp('「 abc 」を末尾空白除去して表示', ' abc')
await cmp('「 abc 」を右トリムして表示', ' abc')
await cmp('「 abc 」を末尾空白除去して表示', ' abc')
})
})

0 comments on commit 02250f6

Please sign in to comment.