src/vs/base/common/objects.ts
286 LOC · 270 covered · 16 uncovered · 82 ranges · 10161 concepts · 42 introducers · 5181 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
objects.ts ×13
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isTypedArray, isObject, isUndefinedOrNull } from './types.js';
export function deepClone<T>(obj: T): T {
}
}
Object.entries(obj).forEach(([key, value]) => {
return result;
}
export function deepFreeze<T>(obj: T): T {
return obj;
}
while (stack.length > 0) {
const obj = stack.shift();
Object.freeze(obj);
for (const key in obj) {
if (_hasOwnProperty.call(obj, key)) {
const prop = obj[key];
if (typeof prop === 'object' && !Object.isFrozen(prop) && !isTypedArray(prop)) {
}
}
}
return obj;
}
const _hasOwnProperty = Object.prototype.hasOwnProperty;
export function cloneAndChange(obj: any, changer: (orig: any) => any): any {
}
function _cloneAndChange(obj: any, changer: (orig: any) => any, seen: Set<any>): any {
objects.ts ×6
if (isUndefinedOrNull(obj)) {
}
const changed = changer(obj);
if (typeof changed !== 'undefined') {
}
if (Array.isArray(obj)) {
for (const e of obj) {
r1.push(_cloneAndChange(e, changer, seen));
}
return r1;
}
if (isObject(obj)) {
if (seen.has(obj)) {
throw new Error('Cannot clone recursive data-structure');
}
const r2: Record<string, unknown> = {};
for (const i2 in obj) {
if (_hasOwnProperty.call(obj, i2)) {
r2[i2] = _cloneAndChange(obj[i2], changer, seen);
}
}
seen.delete(obj);
return r2;
}
return obj;
}
/**
* Copies all properties of source into destination. The optional parameter "overwrite" allows to control
* if existing properties on the destination should be overwritten or not. Defaults to true (overwrite).
*/
export function mixin(destination: any, source: any, overwrite: boolean = true): any {
return source;
}
if (isObject(source)) {
Object.keys(source).forEach(key => {
if (key in destination) {
if (isObject(destination[key]) && isObject(source[key])) {
mixin(destination[key], source[key], overwrite);
} else {
destination[key] = source[key];
}
}
}
}
return destination;
}
export function equals(one: any, other: any): boolean {
}
if (one === null || one === undefined || other === null || other === undefined) {
objects.ts ×2
}
}
}
}
let i: number;
let key: string;
if (Array.isArray(one)) {
if (one.length !== other.length) {
}
}
for (key in one) {
}
const otherKeys: string[] = [];
for (key in other) {
}
if (!equals(oneKeys, otherKeys)) {
}
}
}
/**
* Calls `JSON.Stringify` with a replacer to break apart any circular references.
* This prevents `JSON`.stringify` from throwing the exception
* "Uncaught TypeError: Converting circular structure to JSON"
*/
export function safeStringify(obj: any): string {
return JSON.stringify(obj, (key, value) => {
if (isObject(value) || Array.isArray(value)) {
if (seen.has(value)) {
seen.add(value);
}
}
if (typeof value === 'bigint') {
}
});
}
/**
* Like `JSON.stringify`, but with deterministic ordering of object keys so that
* structurally equal inputs always produce the same string. Useful for cache
* keys derived from arbitrary object payloads.
*
* - Object keys are sorted at every level of nesting.
* - Properties whose value is `undefined` are omitted (matching `JSON.stringify`).
* - Circular references are replaced with the string `"[Circular]"` to avoid
* throwing.
* - A top-level `undefined` returns the string `'undefined'`; any other
* stringification failure returns the empty string.
*/
export function stableStringify(value: unknown): string {
return 'undefined';
}
try {
return _stableStringify(value, new WeakSet());
} catch {
return '';
}
if (value === null || typeof value !== 'object') {
return JSON.stringify(value) ?? 'null';
}
if (seen.has(value as object)) {
return '"[Circular]"';
}
seen.add(value as object);
if (Array.isArray(value)) {
return '[' + value.map(v => _stableStringify(v, seen)).join(',') + ']';
}
const keys = Object.keys(value as object).sort();
const parts: string[] = [];
for (const k of keys) {
const v = (value as Record<string, unknown>)[k];
if (v === undefined) {
continue;
}
parts.push(JSON.stringify(k) + ':' + _stableStringify(v, seen));
}
return '{' + parts.join(',') + '}';
}
type obj = { [key: string]: any };
/**
* Returns an object that has keys for each value that is different in the base object. Keys
* that do not exist in the target but in the base object are not considered.
*
* Note: This is not a deep-diffing method, so the values are strictly taken into the resulting
* object if they differ.
*
* @param base the object to diff against
* @param obj the object to use for diffing
*/
export function distinct(base: obj, target: obj): obj {
if (!base || !target) {
return result;
}
const targetKeys = Object.keys(target);
targetKeys.forEach(k => {
const baseValue = base[k];
const targetValue = target[k];
if (!equals(baseValue, targetValue)) {
result[k] = targetValue;
}
});
return result;
}
export function getCaseInsensitive(target: obj, key: string): unknown {
const equivalentKey = Object.keys(target).find(k => k.toLowerCase() === lowercaseKey);
return equivalentKey ? target[equivalentKey] : target[key];
}
export function filter(obj: obj, predicate: (key: string, value: any) => boolean): obj {
for (const [key, value] of Object.entries(obj)) {
if (predicate(key, value)) {
result[key] = value;
}
}
return result;
}
export function mapValues<T extends {}, R>(obj: T, fn: (value: T[keyof T], key: string) => R): { [K in keyof T]: R } {
for (const [key, value] of Object.entries(obj)) {
result[key] = fn(<T[keyof T]>value, key);
}
return result as { [K in keyof T]: R };
}