Skip to content

Commit

Permalink
str.pad_endを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
salano-ym committed May 9, 2024
1 parent 427a6d8 commit e1b3a22
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 未リリース分
- `Date:year`系の関数に0を渡すと現在時刻になる問題を修正
- シンタックスエラーなどの位置情報を修正
- `str.pad_start`を追加
- `str.pad_start`,`str.pad_end`を追加

# 0.18.0
- `Core:abort`でプログラムを緊急停止できるように
Expand Down
5 changes: 5 additions & 0 deletions docs/primitive-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ _fromIndex_が負値の時は末尾からの位置(文字列の長さ+_fromInd
_pad_ を省略した場合、空白`' '`で埋められます。\
_pad_ が長すぎる場合、_pad_ の末尾が切り捨てられます。

### @(_v_: str).pad_end(_width_: num, _pad_?: str): str
文字列の長さがが _width_ になるように、末尾を _pad_ の繰り返しで埋めた新しい文字列を返します。\
_pad_ を省略した場合、空白`' '`で埋められます。\
_pad_ が長すぎる場合、_pad_ の末尾が切り捨てられます。

### @(_v_: str).trim(): str
文字列の前後の空白を取り除いたものを返します。

Expand Down
7 changes: 7 additions & 0 deletions src/interpreter/primitive-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ const PRIMITIVE_PROPS: {

return STR(target.value.padStart(width.value, s));
}),

pad_end: (target: VStr): VFn => FN_NATIVE(([width, pad], _) => {
assertNumber(width);
const s = (pad) ? (assertString(pad), pad.value) : ' ';

return STR(target.value.padEnd(width.value, s));
}),
},

arr: {
Expand Down
16 changes: 16 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,22 @@ describe('primitive props', () => {
STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("0abc"), STR("01abc"),
]));
});

test.concurrent("pad_end", async () => {
const res = await exe(`
let str = "abc"
<: [
str.pad_end(0), str.pad_end(1), str.pad_end(2), str.pad_end(3), str.pad_end(4), str.pad_end(5),
str.pad_end(0, "0"), str.pad_end(1, "0"), str.pad_end(2, "0"), str.pad_end(3, "0"), str.pad_end(4, "0"), str.pad_end(5, "0"),
str.pad_end(0, "01"), str.pad_end(1, "01"), str.pad_end(2, "01"), str.pad_end(3, "01"), str.pad_end(4, "01"), str.pad_end(5, "01"),
]
`);
eq(res, ARR([
STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("abc "), STR("abc "),
STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("abc0"), STR("abc00"),
STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("abc0"), STR("abc01"),
]));
});
});

describe('arr', () => {
Expand Down

0 comments on commit e1b3a22

Please sign in to comment.