Skip to content

Instantly share code, notes, and snippets.

@renatobenks-zz
Created May 12, 2018 10:46
Show Gist options
  • Save renatobenks-zz/9d84d8fe2f023eb46edc8398bd581d85 to your computer and use it in GitHub Desktop.
Save renatobenks-zz/9d84d8fe2f023eb46edc8398bd581d85 to your computer and use it in GitHub Desktop.
A functional based normalize with legacy code written in Javascript totally typed-safe
// @flow
const FOLLOWING_ENUM = {
S: true,
N: false,
};
type FollowingEnum = $Values<typeof FOLLOWING_ENUM>;
const SANITIZED_KEYS = {
USER_ID: 'codigo',
USER_NICK: 'apelido',
USER_BIO: 'bio',
USER_NAME: 'nome',
CREATED_AT: 'datacadastro',
FOLLOWING: 'following',
};
type SanitizedKeys = $Values<typeof SANITIZE_KEYS> | $Keys<UserType>;
const WRONG_KEYS_PREFIX = 'usuario_';
const WRONG_KEYS = {
USER_ID: WRONG_KEYS_PREFIX + SANITIZED_KEYS.USER_ID,
USER_NICK: WRONG_KEYS_PREFIX + SANITIZED_KEYS.USER_NICK,
USER_BIO: WRONG_KEYS_PREFIX + SANITIZED_KEYS.USER_BIO,
USER_NAME: WRONG_KEYS_PREFIX + SANITIZED_KEYS.USER_NAME,
};
const SANITIZE_KEYS = {
...WRONG_KEYS,
CREATED_AT: 'usuario_cadastrado_em',
FOLLOWING: 'ja_seguindo',
};
type SanitizeKeys = $Values<typeof SANITIZE_KEYS>;
function sanitizeUserKey(key: SanitizeKeys): SanitizedKeys {
switch (key) {
case SANITIZE_KEYS.USER_ID:
case SANITIZE_KEYS.USER_NICK:
case SANITIZE_KEYS.USER_BIO:
case SANITIZE_KEYS.USER_NAME: {
return key.replace(WRONG_KEYS_PREFIX, '');
}
case SANITIZE_KEYS.CREATED_AT: {
return SANITIZED_KEYS.CREATED_AT;
}
case SANITIZE_KEYS.FOLLOWING: {
return SANITIZED_KEYS.FOLLOWING;
}
default: {
return key;
}
}
}
type SanitizedUserValue = | FollowingEnum | null;
function sanitizeUserValue(key: SanitizeKeys, value: $Values<User>): SanitizedUserValue {
switch (key) {
case SANITIZE_KEYS.FOLLOWING: {
return FOLLOWING_ENUM[value.toUpperCase()] || false;
}
default: {
return null;
}
}
}
type UserType = {
ja_seguindo: string,
livro_categoria: string,
livro_data_publicacao: string,
livro_situacao: string,
livro_status: string,
livro_titulo: string,
total_seguidores: string,
total_visualizacoes: string,
usuario_avatar_codigo: string,
usuario_avatar_extensao: string,
usuario_avatar_hash: string,
usuario_avatar_nome: string,
usuario_bio: string,
};
type User = UserType & {
usuario_codigo: string,
usuario_apelido: string,
usuario_bio: string,
usuario_nome: string,
usuario_cadastrado_em: string,
};
type UserNormalized = UserType & {
codigo: $PropertyType<User, 'usuario_codigo'>,
apelido: $PropertyType<User, 'usuario_apelido'>,
bio: $PropertyType<User, 'usuario_bio'>,
nome: $PropertyType<User, 'usuario_nome'>,
datacadastro: $PropertyType<User, 'usuario_cadastrado_em'>,
following: boolean,
};
const normalizeToUser = (user: User): UserNormalized => {
return Object.keys(user).reduce((obj, key) => {
const item = user[key];
return {
...obj,
[sanitizeUserKey(key)]: sanitizeUserValue(key, item) || item,
};
}, {});
};
export { normalizeToUser };
@renatobenks-zz
Copy link
Author

renatobenks-zz commented May 12, 2018

@sibelius take a quick look at here! 🎆 Give me suggestions about it (:

@renatobenks-zz
Copy link
Author

renatobenks-zz commented May 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment