Profile
proposal-collection-normalization
Stage 2
Map/SetへKeyとValueを正規化するcoerceKey
とcoerceValue
の追加
※以降、実際に動かしてはいません
Keyの正規化①
オブジェクトのKeyをKeyとして設定する
const profile = { email: 'simochee@example.com' };
const pointMap = new Map(undefined, {
coerceKey: ({ email }) => email,
});
pointMap.set(profile, 1200);
pointMap.get('simochee@example.com'); // 1200
Keyの正規化②
Keyを文字列化させる
const map = new Map(undefined, {
coerceKey: String,
});
map.set(1, 'simochee');
map.has(1); // true
// map.has('1') // true? false?
[...map.entries()]; // [['1', 'simochee']]
Valueの正規化①
データをインスタンス化させる
const map = new Map(undefined, {
coerceValue: (user) => {
return user instanceof UserModel
? user
: new UserModel(user);
},
});
Valueの正規化②
不正なインスタンスの場合エラーにする
const map = new Map(undefined, {
coerceValue: (user) => {
if (!(user instanceof UserModel)) {
throw new TypeError('データの方が不正です');
}
return user;
},
});