Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 165 additions & 50 deletions CHANGELOG.md

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions base/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export function* keysOf<T extends Record<string, unknown>>(
obj: T,
): Generator<string> {
for (const k in obj) {
// deno-lint-ignore no-prototype-builtins
if (obj.hasOwnProperty(k)) {
if (Object.hasOwn(obj, k)) {
yield k;
}
}
Expand Down Expand Up @@ -130,10 +129,6 @@ export function* unionIter<T>(...args: Iterable<T>[]): Iterable<T> {
}
}

export function newInstance<T = any>(instance: any, ...args: any[]): T {
return new (instance.constructor as any)(...args) as T;
}

/**
* Maps values from an iterable to new values using a mapping function.
*
Expand Down
2 changes: 1 addition & 1 deletion base/comparisons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export function isFunction(v: any): v is Function {

export function isEmptyObject(obj: any): boolean {
for (const k in obj) {
if (obj.hasOwnProperty(k)) {
if (Object.hasOwn(obj, k)) {
return false;
}
}
Expand Down
139 changes: 11 additions & 128 deletions base/core-types/clone.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,23 @@
import { notReached } from '../error.ts';
import { Dictionary } from '../collections/dict.ts';
import {
CoreKey,
CoreObject,
CoreType,
CoreValue,
CoreValueCloneOpts,
ReadonlyCoreArray,
} from './base.ts';
import { getCoreType, isClonable } from './utils.ts';
import type { CoreValue, CoreValueCloneOpts } from './base.ts';
import { getCoreType } from './utils.ts';
import { kTypeOps } from './type-ops.ts';

export function coreValueClone<T extends CoreValue>(
e: T,
opts?: CoreValueCloneOpts,
): T {
return internalCoreValueClone(e, undefined, opts);
// One shared context per top-level clone; the Object entry allocates
// `objectsCache` into it so shared refs stay aliased (see type-ops.ts).
return kTypeOps[getCoreType(e)].clone(e, {}, opts) as T;
}

export function coreObjectClone<T extends Object>(
obj: T,
opts?: CoreValueCloneOpts,
) {
return internalCoreValueClone(obj as any, undefined, opts);
}

function internalCoreValueClone<T extends CoreValue>(
e: T,
objectsCache: Map<CoreObject, CoreObject> | undefined,
opts?: CoreValueCloneOpts,
): T {
switch (getCoreType(e)) {
case CoreType.String:
case CoreType.Number:
case CoreType.Boolean:
case CoreType.Null:
case CoreType.Undefined:
return e;

case CoreType.Date:
return new Date((e as Date).getTime()) as T;

case CoreType.Array: {
let arr = e as ReadonlyCoreArray;
if (opts !== undefined && opts.iterableFilter !== undefined) {
arr = arr.filter(opts.iterableFilter);
}
return arr.map((v) => internalCoreValueClone(v, objectsCache, opts)) as T;
}

case CoreType.Set: {
const set = e as Set<CoreValue>;
const result = new Set<CoreValue>();
for (const v of set) {
if (opts !== undefined && opts.iterableFilter !== undefined) {
if (!opts.iterableFilter(v)) continue;
}
result.add(internalCoreValueClone(v, objectsCache, opts));
}
return result as T;
}

case CoreType.Object: {
if (objectsCache === undefined) {
objectsCache = new Map<CoreObject, CoreObject>();
}
const obj = e as { [key: string]: CoreValue };
const cached = objectsCache.get(obj);
if (cached !== undefined) return cached as T;

if (opts && opts.objectOverride) {
const [result, newObj] = opts.objectOverride(obj);
if (result) {
objectsCache.set(obj, newObj as CoreObject);
return newObj as T;
}
}

const result: { [key: string]: CoreValue } = {};
for (const k in obj) {
if (
obj.hasOwnProperty(k) &&
(opts === undefined ||
opts.objectFilterFields === undefined ||
opts.objectFilterFields(k, obj))
) {
if (opts !== undefined && opts.fieldCloneOverride !== undefined) {
result[k] = opts.fieldCloneOverride(obj, k, opts);
} else {
result[k] = internalCoreValueClone(obj[k], objectsCache, opts);
}
}
}

objectsCache.set(obj, result);

return result as T;
}

case CoreType.Dictionary: {
const newMap = new Map<CoreKey, CoreValue>();
for (const [k, v] of e as Dictionary<CoreKey, CoreValue>) {
if (opts !== undefined && opts.fieldCloneOverride !== undefined) {
newMap.set(k, opts.fieldCloneOverride(e as Dictionary, k, opts));
} else {
newMap.set(k, internalCoreValueClone(v, objectsCache, opts));
}
}
return newMap as unknown as T;
}

case CoreType.Generator: {
const arr: CoreValue[] = [];
for (const item of e as Generator<CoreValue>) {
if (opts !== undefined && opts.iterableFilter !== undefined) {
if (opts.iterableFilter(item)) {
arr.push(internalCoreValueClone(item, objectsCache, opts));
}
} else {
arr.push(internalCoreValueClone(item, objectsCache, opts));
}
}

return arr as T;
}

case CoreType.ClassObject:
if (isClonable(e)) {
return e.clone(opts) as T;
}
if (opts && opts.notClonableExt) {
const fRes = opts.notClonableExt(e as Object);
if (fRes !== undefined) return fRes as T;
}
throw new Error(`${e?.constructor.name} must be Clonable`);

default:
notReached('clone failed');
}
return kTypeOps[getCoreType(obj as unknown as CoreValue)].clone(
obj,
{},
opts,
) as T;
}
3 changes: 1 addition & 2 deletions base/core-types/comparable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { notReached } from '../error.ts';
import { compareStrings } from '../string.ts';
import { CoreOptions, CoreType, CoreValue } from './base.ts';
import { type CoreOptions, CoreType, type CoreValue } from './base.ts';
import { encodableValueHash } from './encoding/hash.ts';
import { Comparable } from './index.ts';
import { getCoreType, isComparable, isEquatable } from './utils.ts';

export const MinComparableValue = {};
Expand Down
Loading
Loading