mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(_util): remove deepAssign
(#2847)
This commit is contained in:
parent
5c8bd0b6f6
commit
60466e8618
@ -1,55 +0,0 @@
|
||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
import { assert } from "./assert.ts";
|
||||
|
||||
export function deepAssign<T, U>(target: T, source: U): T & U;
|
||||
export function deepAssign<T, U, V>(
|
||||
target: T,
|
||||
source1: U,
|
||||
source2: V,
|
||||
): T & U & V;
|
||||
export function deepAssign<T, U, V, W>(
|
||||
target: T,
|
||||
source1: U,
|
||||
source2: V,
|
||||
source3: W,
|
||||
): T & U & V & W;
|
||||
export function deepAssign(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
target: Record<string, any>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
...sources: any[]
|
||||
): // deno-lint-ignore ban-types
|
||||
object | undefined {
|
||||
for (let i = 0; i < sources.length; i++) {
|
||||
const source = sources[i];
|
||||
if (!source || typeof source !== `object`) {
|
||||
return;
|
||||
}
|
||||
Object.entries(source).forEach(([key, value]) => {
|
||||
if (value instanceof Date) {
|
||||
target[key] = new Date(value);
|
||||
return;
|
||||
}
|
||||
if (value instanceof RegExp) {
|
||||
target[key] = new RegExp(value);
|
||||
return;
|
||||
}
|
||||
if (!value || typeof value !== `object`) {
|
||||
target[key] = value;
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
target[key] = [];
|
||||
}
|
||||
// value is an Object
|
||||
if (typeof target[key] !== `object` || !target[key]) {
|
||||
target[key] = {};
|
||||
}
|
||||
assert(value);
|
||||
deepAssign(target[key] as Record<string, unknown>, value);
|
||||
});
|
||||
}
|
||||
return target;
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
import { assert, assertEquals } from "../testing/asserts.ts";
|
||||
import { deepAssign } from "./deep_assign.ts";
|
||||
|
||||
Deno.test("deepAssignTest", function () {
|
||||
const date = new Date("1979-05-27T07:32:00Z");
|
||||
const reg = RegExp(/DENOWOWO/);
|
||||
const obj1 = { deno: { bar: { deno: ["is", "not", "node"] } } };
|
||||
const obj2 = { foo: { deno: date } };
|
||||
const obj3 = { foo: { bar: "deno" }, reg: reg };
|
||||
const actual = deepAssign(obj1, obj2, obj3);
|
||||
const expected = {
|
||||
foo: {
|
||||
deno: new Date("1979-05-27T07:32:00Z"),
|
||||
bar: "deno",
|
||||
},
|
||||
deno: { bar: { deno: ["is", "not", "node"] } },
|
||||
reg: RegExp(/DENOWOWO/),
|
||||
};
|
||||
assert(date !== expected.foo.deno);
|
||||
assert(reg !== expected.reg);
|
||||
assertEquals(actual, expected);
|
||||
});
|
@ -1,7 +1,7 @@
|
||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
import { deepAssign } from "../../_util/deep_assign.ts";
|
||||
import { deepMerge } from "../../collections/deep_merge.ts";
|
||||
|
||||
// ---------------------------
|
||||
// Interfaces and base classes
|
||||
@ -276,10 +276,11 @@ function merge(
|
||||
if (!result.ok) {
|
||||
return failure();
|
||||
}
|
||||
const body = {};
|
||||
let body = {};
|
||||
for (const record of result.body) {
|
||||
if (typeof body === "object" && body !== null) {
|
||||
deepAssign(body, record);
|
||||
// deno-lint-ignore no-explicit-any
|
||||
body = deepMerge(body, record as Record<any, any>);
|
||||
}
|
||||
}
|
||||
return success(body);
|
||||
@ -748,9 +749,9 @@ export function InlineTable(
|
||||
if (!pairs.ok) {
|
||||
return failure();
|
||||
}
|
||||
const table = {};
|
||||
let table = {};
|
||||
for (const pair of pairs.body) {
|
||||
deepAssign(table, pair);
|
||||
table = deepMerge(table, pair);
|
||||
}
|
||||
return success(table);
|
||||
}
|
||||
@ -835,11 +836,11 @@ export function Toml(
|
||||
if (!blocks.ok) {
|
||||
return failure();
|
||||
}
|
||||
const body = {};
|
||||
let body = {};
|
||||
for (const block of blocks.body) {
|
||||
switch (block.type) {
|
||||
case "Block": {
|
||||
deepAssign(body, block.value);
|
||||
body = deepMerge(body, block.value);
|
||||
break;
|
||||
}
|
||||
case "Table": {
|
||||
|
Loading…
Reference in New Issue
Block a user