在用户目录采用这段生成代码前,对它进行审查。
按 id 存储档案,允许把 save 注册为回调,校验边界输入,不返回令牌或内部可变引用,并支持频繁读取排序摘要。
JavaScript
class ProfileStore {
#profiles = new Map();
save(profile) {
this.#profiles.set(profile.id, profile);
return profile;
}
find(id) {
return this.#profiles.get(id);
}
summaries() {
return [...this.#profiles.values()]
.sort((left, right) => left.name.localeCompare(right.name))
.map(({ token, ...profile }) => profile);
}
}
const store = new ProfileStore();
const save = store.save;
[{ id: 'a', name: 'Ada', token: 'secret', roles: ['admin'] }]
.forEach(save);
console.log(store.summaries());
生成代码仅作示例,不代表任何特定模型