mirror of
https://github.com/wassname/talk.git
synced 2026-07-19 11:28:50 +08:00
fix: addresses issue with creating stories (#2442)
This commit is contained in:
committed by
Kim Gardner
parent
fdc6afec42
commit
4e548e8fbf
@@ -1,4 +1,11 @@
|
||||
import { isArray, isNull, isNumber, isPlainObject, isString } from "lodash";
|
||||
import {
|
||||
isArray,
|
||||
isNull,
|
||||
isNumber,
|
||||
isPlainObject,
|
||||
isString,
|
||||
isUndefined,
|
||||
} from "lodash";
|
||||
|
||||
function isObject(obj: any): obj is Record<string, any> {
|
||||
return isPlainObject(obj);
|
||||
@@ -12,13 +19,23 @@ function deriveKey(property: string, prefix?: string) {
|
||||
return property;
|
||||
}
|
||||
|
||||
function reduce(
|
||||
result: Record<string, any>,
|
||||
obj: Record<string, any> | number | null | string,
|
||||
ignoreArrays: boolean,
|
||||
embedArrays: boolean,
|
||||
prefix?: string
|
||||
) {
|
||||
interface ReduceOptions {
|
||||
result: Record<string, any>;
|
||||
obj: Record<string, any> | number | null | string;
|
||||
ignoreArrays: boolean;
|
||||
embedArrays: boolean;
|
||||
includeUndefined: boolean;
|
||||
prefix?: string;
|
||||
}
|
||||
|
||||
function reduce({
|
||||
result,
|
||||
obj,
|
||||
ignoreArrays,
|
||||
embedArrays,
|
||||
includeUndefined,
|
||||
prefix,
|
||||
}: ReduceOptions) {
|
||||
if (prefix) {
|
||||
if (isNumber(obj) || isString(obj) || isNull(obj)) {
|
||||
result[prefix] = obj;
|
||||
@@ -36,24 +53,38 @@ function reduce(
|
||||
const key = deriveKey(property, prefix);
|
||||
|
||||
if (isPlainObject(value)) {
|
||||
reduce(result, value, ignoreArrays, embedArrays, key);
|
||||
reduce({
|
||||
result,
|
||||
obj: value,
|
||||
ignoreArrays,
|
||||
embedArrays,
|
||||
includeUndefined,
|
||||
prefix: key,
|
||||
});
|
||||
} else if (isArray(value)) {
|
||||
if (!ignoreArrays) {
|
||||
if (embedArrays) {
|
||||
result[key] = value;
|
||||
} else {
|
||||
value.forEach((item, index) => {
|
||||
reduce(
|
||||
reduce({
|
||||
result,
|
||||
item,
|
||||
obj: item,
|
||||
ignoreArrays,
|
||||
embedArrays,
|
||||
`${key}[${index}]`
|
||||
);
|
||||
includeUndefined,
|
||||
prefix: `${key}[${index}]`,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If the underlying value is undefined and we aren't including
|
||||
// undefined elements, then continue.
|
||||
if (!includeUndefined && isUndefined(value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
@@ -74,9 +105,19 @@ export interface DotizeOptions {
|
||||
* without recusing the dotize algorithm to it.
|
||||
*/
|
||||
embedArrays?: boolean;
|
||||
|
||||
/**
|
||||
* includeUndefined if true, will include undefined values in the result.
|
||||
*/
|
||||
includeUndefined?: boolean;
|
||||
}
|
||||
|
||||
export const dotize = (
|
||||
obj: Record<string, any>,
|
||||
{ ignoreArrays = false, embedArrays = false }: DotizeOptions = {}
|
||||
): Record<string, any> => reduce({}, obj, ignoreArrays, embedArrays);
|
||||
{
|
||||
ignoreArrays = false,
|
||||
embedArrays = false,
|
||||
includeUndefined = false,
|
||||
}: DotizeOptions = {}
|
||||
): Record<string, any> =>
|
||||
reduce({ result: {}, obj, ignoreArrays, embedArrays, includeUndefined });
|
||||
|
||||
Reference in New Issue
Block a user