实现说明
演示用的数据放在这几个模块级绑定中:stages、patched、renamedTail。
使用要点
- 没有锁文件条目,没有版本钉死——除了运行时,文件什么都不需要。
- 每一步打印都带着自己的标签:
patched:、source :、tail :。
常见问题
不安装任何包也能运行吗?
可以,开箱即跑——没有引用任何内建 API 之外的东西。 文件的状态由几个模块级绑定持有:stages、patched、renamedTail。
核心语句
const stages = ['lint', 'test', 'package', 'deploy'];
const renamedTail = patched.with(-1, 'publish');
const patched = stages.with(2, 'bundle');
代码示例
const stages = ['lint', 'test', 'package', 'deploy'];
// Replace at a positive index without mutating `stages`
const patched = stages.with(2, 'bundle');
console.log('patched:', JSON.stringify(patched));
console.log('source :', JSON.stringify(stages));
// Negative indices count backward from the tail
const renamedTail = patched.with(-1, 'publish');
console.log('tail :', JSON.stringify(renamedTail));
// Unlike `stages[10] = 'x'` (which creates sparse holes), .with() throws RangeError
try {
stages.with(10, 'overflow');
} catch (err) {
console.log('out-of-bounds:', err.name, err.message.includes('Invalid index'));
}