formatted_file 字段与方法
字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。
字段
来自 KScript.Core
#formatted_file->find(text: string) → number
首个字面匹配的从零开始的源偏移量,若无匹配则为 -1。
示例
<let name="source_file" value="```ps1
Write-Output 'before'
```" />
<echo>{ $source_file->find('before') }</echo>
$source_file = ```ps1
Write-Output 'before'
```
echo $source_file->find('before')
#formatted_file->insert(content, at?: int) → formatted_file
返回一个副本,在从零开始的偏移位置插入相同格式的文本或源码。未指定偏移时则追加到末尾。
| 参数 | 接受 |
|---|
content 必填 | 任意值 |
at 可选 | int |
参数可以使用命名方式:->insert(…, at: …)。
示例
<let name="source_file" value="```ps1
Write-Output 'before'
```" />
<echo>{ $source_file->insert('# Example ', at: 0).text }</echo>
$source_file = ```ps1
Write-Output 'before'
```
echo $source_file->insert('# Example ', at: 0).text
#formatted_file->replace(search: string, replacement: string) → formatted_file
返回一个副本,其中每个字面匹配项都被替换。
参数可以使用命名方式:->replace(…, replacement: …)。
示例
<let name="source_file" value="```ps1
Write-Output 'before'
```" />
<echo>{ $source_file->replace('before', 'after').text }</echo>
$source_file = ```ps1
Write-Output 'before'
```
echo $source_file->replace('before', 'after').text
#formatted_file->save(path: string)
将源码写入文件。若路径没有扩展名,则添加 fence 扩展名。
示例
<let name="source_file" value="```ps1
Write-Output 'before'
```" />
<do>$source_file->save('example.ps1')</do>
$source_file = ```ps1
Write-Output 'before'
```
$source_file->save('example.ps1')
来自 any 的方法