28 lines
1.2 KiB
JavaScript
28 lines
1.2 KiB
JavaScript
// webAPP 标签状态纯函数:不可变风格,Node 可 require、浏览器挂 window.tabsStore
|
|
// webapp tab state pure functions: immutable style, Node-requireable, browser exposes window.tabsStore
|
|
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) module.exports = factory();
|
|
else root.tabsStore = factory();
|
|
})(typeof self !== 'undefined' ? self : this, function () {
|
|
// 新增/激活标签:id 不存在才 push,返回激活 id 的新数组,不修改原数组
|
|
// Add/activate a tab: push only if id missing, return a new array with id active
|
|
function add(tabs, id, name) {
|
|
const base = tabs.some(t => t.id === id) ? tabs : tabs.concat({ id, name });
|
|
return base.map(t => ({ ...t, active: t.id === id }));
|
|
}
|
|
|
|
// 删除标签:过滤掉 id,返回新数组,不修改原数组
|
|
// Remove a tab: filter out id, return a new array
|
|
function remove(tabs, id) {
|
|
return tabs.filter(t => t.id !== id);
|
|
}
|
|
|
|
// 切换激活标签:返回激活 id 的新数组,不修改原数组
|
|
// Switch active tab: return a new array with id active
|
|
function switchTo(tabs, id) {
|
|
return tabs.map(t => ({ ...t, active: t.id === id }));
|
|
}
|
|
|
|
return { add, remove, switchTo };
|
|
});
|