refactor: remove getNonNullValues, replace with _.ominBy and _.isNull (lodash)

ref: https://stackoverflow.com/a/70554734/11408375
This commit is contained in:
HD
2022-06-28 10:31:19 +08:00
parent ab5f9e6c07
commit 6bf64e38fc
3 changed files with 7 additions and 13 deletions
+3 -2
View File
@@ -1,6 +1,6 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { isNull, omitBy } from "lodash";
import BASE_URL from "../config";
import { getNonNullValues } from "../../common/utils";
import { Channel, UpdateChannelDTO, UpdatePinnedMessageDTO } from "../../types/channel";
interface StoredChannel extends Channel {
@@ -70,7 +70,8 @@ const channelsSlice = createSlice({
break;
}
default:
state.byId[gid] = { ...state.byId[gid]!, ...getNonNullValues(rest) };
// old code: state.byId[gid] = { ...state.byId[gid]!, ...getNonNullValues(rest) };
state.byId[gid] = { ...state.byId[gid]!, ...omitBy(rest, isNull) };
break;
}
},
+3 -2
View File
@@ -1,5 +1,5 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { getNonNullValues } from "../../common/utils";
import { isNull, omitBy } from "lodash";
import BASE_URL from "../config";
import { User } from "../../types/auth";
import { UserLog, UserState } from "../../types/sse";
@@ -46,9 +46,10 @@ const contactsSlice = createSlice({
changeLogs.forEach(({ action, uid, ...rest }) => {
switch (action) {
case "update": {
const vals = getNonNullValues(rest);
const vals = omitBy(rest, isNull);
if (state.byId[uid]) {
Object.keys(vals).forEach((k) => {
// @ts-ignore
state.byId[uid]![k] = vals[k];
if (k == "avatar_updated_at") {
state.byId[uid]!.avatar = `${BASE_URL}/resource/avatar?uid=${uid}&t=${vals[k]}`;
+1 -9
View File
@@ -20,15 +20,6 @@ export const isTreatAsImage = (file: File) => {
return false;
};
export const getNonNullValues = (obj, whiteList = ["log_id"]) => {
const tmp = {};
Object.keys(obj).forEach((k) => {
if (!whiteList.includes(k) && obj[k] !== null) {
tmp[k] = obj[k];
}
});
return tmp;
};
export function getDefaultSize(size = null, min = 480) {
if (!size) return { width: 0, height: 0 };
const { width: oWidth, height: oHeight } = size;
@@ -48,6 +39,7 @@ export function getDefaultSize(size = null, min = 480) {
}
return { width: dWidth, height: dHeight };
}
export function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return "0 Bytes";