LoginSignup
2
0

More than 3 years have passed since last update.

proposal-collection-normalization

Last updated at Posted at 2019-10-08
1 / 9

Profile


・simochee (しもちぃって読みます)
・フロントエンドエンジニア
Qiita (simochee)
Twitter


proposal-collection-normalization

Stage 2

Map/SetへKeyとValueを正規化する
coerceKeycoerceValueの追加


※以降、実際に動かしてはいません


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;
  },
});

References

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0