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.

1 > /*--------------------------------------------------------------------------------------------- objects.ts ×13
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { isTypedArray, isObject, isUndefinedOrNull } from './types.js';
7 >
8 > export function deepClone<T>(obj: T): T {
9 > if (!obj || typeof obj !== 'object') { objects.ts ×3
10 > return obj; objects.ts ×1
11 > }
12 > if (obj instanceof RegExp) { objects.ts ×1
13 > return obj; taskConfiguration.ts ×2
14 > }
15 > const result: any = Array.isArray(obj) ? [] : {}; objects.ts ×3
16 > Object.entries(obj).forEach(([key, value]) => {
17 > result[key] = value && typeof value === 'object' ? deepClone(value) : value; objects.ts ×1
18 > }); objects.ts ×3
19 > return result;
20 > }
22 > export function deepFreeze<T>(obj: T): T {
23 > if (!obj || typeof obj !== 'object') { objects.ts ×3
24 return obj;
25 }
26 > const stack: any[] = [obj]; objects.ts ×3
27 > while (stack.length > 0) {
28 > const obj = stack.shift();
29 > Object.freeze(obj);
30 > for (const key in obj) {
31 > if (_hasOwnProperty.call(obj, key)) {
32 > const prop = obj[key];
33 > if (typeof prop === 'object' && !Object.isFrozen(prop) && !isTypedArray(prop)) {
34 > stack.push(prop); objects.ts ×1
35 > }
37 > }
38 > }
39 > return obj;
40 > }
42 > const _hasOwnProperty = Object.prototype.hasOwnProperty;
43 >
44 >
45 > export function cloneAndChange(obj: any, changer: (orig: any) => any): any {
46 > return _cloneAndChange(obj, changer, new Set()); objects.ts ×6
47 > }
49 > function _cloneAndChange(obj: any, changer: (orig: any) => any, seen: Set<any>): any { objects.ts ×6
50 > if (isUndefinedOrNull(obj)) {
51 > return obj; objects.ts ×1
52 > }
54 > const changed = changer(obj);
55 > if (typeof changed !== 'undefined') {
56 > return changed; objects.ts ×1
57 > }
59 > if (Array.isArray(obj)) {
60 > const r1: any[] = []; objects.ts ×1
61 > for (const e of obj) {
62 > r1.push(_cloneAndChange(e, changer, seen));
63 > }
64 > return r1;
65 > }
67 > if (isObject(obj)) {
68 > if (seen.has(obj)) {
69 throw new Error('Cannot clone recursive data-structure');
70 }
71 > seen.add(obj); objects.ts ×6
72 > const r2: Record<string, unknown> = {};
73 > for (const i2 in obj) {
74 > if (_hasOwnProperty.call(obj, i2)) {
75 > r2[i2] = _cloneAndChange(obj[i2], changer, seen);
76 > }
77 > }
78 > seen.delete(obj);
79 > return r2;
80 > }
82 > return obj;
83 > }
85 > /**
86 > * Copies all properties of source into destination. The optional parameter "overwrite" allows to control
87 > * if existing properties on the destination should be overwritten or not. Defaults to true (overwrite).
88 > */
89 > export function mixin(destination: any, source: any, overwrite: boolean = true): any {
90 > if (!isObject(destination)) { objects.ts ×4
91 return source;
92 }
94 > if (isObject(source)) {
95 > Object.keys(source).forEach(key => {
96 > if (key in destination) {
97 > if (overwrite) { objects.ts ×1
98 if (isObject(destination[key]) && isObject(source[key])) {
99 mixin(destination[key], source[key], overwrite);
100 } else {
101 destination[key] = source[key];
102 }
103 }
104 > } else { objects.ts ×4
105 > destination[key] = source[key]; objects.ts ×1
106 > }
107 > }); objects.ts ×4
108 > }
109 > return destination;
110 > }
112 > export function equals(one: any, other: any): boolean {
113 > if (one === other) { objects.ts ×2
114 > return true; objects.ts ×1
115 > }
116 > if (one === null || one === undefined || other === null || other === undefined) { objects.ts ×2
117 > return false; objects.ts ×1
118 > }
119 > if (typeof one !== typeof other) { objects.ts ×2
120 > return false; objects.ts ×2
121 > }
122 > if (typeof one !== 'object') { objects.ts ×2
123 > return false; objects.ts ×1
124 > }
125 > if ((Array.isArray(one)) !== (Array.isArray(other))) { objects.ts ×3
126 > return false; objects.ts ×2
127 > }
129 > let i: number;
130 > let key: string;
131 >
132 > if (Array.isArray(one)) {
133 > if (one.length !== other.length) {
134 > return false; objects.ts ×1
135 > }
136 > for (i = 0; i < one.length; i++) { objects.ts ×1
137 > if (!equals(one[i], other[i])) { objects.ts ×2
138 > return false; objects.ts ×1
139 > }
140 > } objects.ts ×2
141 > } else { objects.ts ×3
142 > const oneKeys: string[] = []; objects.ts ×4
143 >
144 > for (key in one) {
145 > oneKeys.push(key); objects.ts ×1
146 > }
147 > oneKeys.sort(); objects.ts ×4
148 > const otherKeys: string[] = [];
149 > for (key in other) {
150 > otherKeys.push(key); objects.ts ×1
151 > }
152 > otherKeys.sort(); objects.ts ×4
153 > if (!equals(oneKeys, otherKeys)) {
154 > return false; objects.ts ×1
155 > }
156 > for (i = 0; i < oneKeys.length; i++) { objects.ts ×1
157 > if (!equals(one[oneKeys[i]], other[oneKeys[i]])) { objects.ts ×2
158 > return false; objects.ts ×1
159 > }
160 > } objects.ts ×2
161 > } objects.ts ×4
162 > return true; objects.ts ×1
163 > }
165 > /**
166 > * Calls `JSON.Stringify` with a replacer to break apart any circular references.
167 > * This prevents `JSON`.stringify` from throwing the exception
168 > * "Uncaught TypeError: Converting circular structure to JSON"
169 > */
170 > export function safeStringify(obj: any): string {
171 > const seen = new Set<any>(); objects.ts ×3
172 > return JSON.stringify(obj, (key, value) => {
173 > if (isObject(value) || Array.isArray(value)) {
174 > if (seen.has(value)) {
175 > return '[Circular]'; objects.ts ×2
176 > } else { objects.ts ×3
177 > seen.add(value);
178 > }
179 > }
180 > if (typeof value === 'bigint') {
181 > return `[BigInt ${value.toString()}]`; objects.ts ×2
182 > }
183 > return value; objects.ts ×3
184 > });
185 > }
187 > /**
188 > * Like `JSON.stringify`, but with deterministic ordering of object keys so that
189 > * structurally equal inputs always produce the same string. Useful for cache
190 > * keys derived from arbitrary object payloads.
191 > *
192 > * - Object keys are sorted at every level of nesting.
193 > * - Properties whose value is `undefined` are omitted (matching `JSON.stringify`).
194 > * - Circular references are replaced with the string `"[Circular]"` to avoid
195 > * throwing.
196 > * - A top-level `undefined` returns the string `'undefined'`; any other
197 > * stringification failure returns the empty string.
198 > */
199 > export function stableStringify(value: unknown): string {
200 > if (value === undefined) { objects.ts ×3
201 > return 'undefined';
202 > }
203 > try {
204 > return _stableStringify(value, new WeakSet());
205 > } catch {
206 return '';
207 }
208 > } objects.ts ×3
210 > function _stableStringify(value: unknown, seen: WeakSet<object>): string { objects.ts ×3
211 > if (value === null || typeof value !== 'object') {
212 > return JSON.stringify(value) ?? 'null';
213 > }
214 > if (seen.has(value as object)) {
215 > return '"[Circular]"';
216 > }
217 > seen.add(value as object);
218 > if (Array.isArray(value)) {
219 > return '[' + value.map(v => _stableStringify(v, seen)).join(',') + ']';
220 > }
221 > const keys = Object.keys(value as object).sort();
222 > const parts: string[] = [];
223 > for (const k of keys) {
224 > const v = (value as Record<string, unknown>)[k];
225 > if (v === undefined) {
226 > continue;
227 > }
228 > parts.push(JSON.stringify(k) + ':' + _stableStringify(v, seen));
229 > }
230 > return '{' + parts.join(',') + '}';
231 > }
233 > type obj = { [key: string]: any };
234 > /**
235 > * Returns an object that has keys for each value that is different in the base object. Keys
236 > * that do not exist in the target but in the base object are not considered.
237 > *
238 > * Note: This is not a deep-diffing method, so the values are strictly taken into the resulting
239 > * object if they differ.
240 > *
241 > * @param base the object to diff against
242 > * @param obj the object to use for diffing
243 > */
244 > export function distinct(base: obj, target: obj): obj {
245 > const result = Object.create(null); objects.ts ×2
246 >
247 > if (!base || !target) {
248 return result;
249 }
251 > const targetKeys = Object.keys(target);
252 > targetKeys.forEach(k => {
253 > const baseValue = base[k];
254 > const targetValue = target[k];
255 >
256 > if (!equals(baseValue, targetValue)) {
257 > result[k] = targetValue;
258 > }
259 > });
260 >
261 > return result;
262 > }
264 > export function getCaseInsensitive(target: obj, key: string): unknown {
265 > const lowercaseKey = key.toLowerCase(); objects.ts ×1
266 > const equivalentKey = Object.keys(target).find(k => k.toLowerCase() === lowercaseKey);
267 > return equivalentKey ? target[equivalentKey] : target[key];
268 > }
270 > export function filter(obj: obj, predicate: (key: string, value: any) => boolean): obj {
271 > const result = Object.create(null); debugger.ts ×5
272 > for (const [key, value] of Object.entries(obj)) {
273 > if (predicate(key, value)) {
274 > result[key] = value;
275 > }
276 > }
277 > return result;
278 > }
280 > export function mapValues<T extends {}, R>(obj: T, fn: (value: T[keyof T], key: string) => R): { [K in keyof T]: R } {
281 > const result: { [key: string]: R } = {}; objects.ts ×1
282 > for (const [key, value] of Object.entries(obj)) {
283 > result[key] = fn(<T[keyof T]>value, key);
284 > }
285 > return result as { [K in keyof T]: R };
286 > }