Path: blob/main/extensions/copilot/src/util/vs/base/common/objects.ts
13405 views
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'12/*---------------------------------------------------------------------------------------------3* Copyright (c) Microsoft Corporation. All rights reserved.4* Licensed under the MIT License. See License.txt in the project root for license information.5*--------------------------------------------------------------------------------------------*/67import { isTypedArray, isObject, isUndefinedOrNull } from './types';89export function deepClone<T>(obj: T): T {10if (!obj || typeof obj !== 'object') {11return obj;12}13if (obj instanceof RegExp) {14return obj;15}16const result: any = Array.isArray(obj) ? [] : {};17Object.entries(obj).forEach(([key, value]) => {18result[key] = value && typeof value === 'object' ? deepClone(value) : value;19});20return result;21}2223export function deepFreeze<T>(obj: T): T {24if (!obj || typeof obj !== 'object') {25return obj;26}27const stack: any[] = [obj];28while (stack.length > 0) {29const obj = stack.shift();30Object.freeze(obj);31for (const key in obj) {32if (_hasOwnProperty.call(obj, key)) {33const prop = obj[key];34if (typeof prop === 'object' && !Object.isFrozen(prop) && !isTypedArray(prop)) {35stack.push(prop);36}37}38}39}40return obj;41}4243const _hasOwnProperty = Object.prototype.hasOwnProperty;444546export function cloneAndChange(obj: any, changer: (orig: any) => any): any {47return _cloneAndChange(obj, changer, new Set());48}4950function _cloneAndChange(obj: any, changer: (orig: any) => any, seen: Set<any>): any {51if (isUndefinedOrNull(obj)) {52return obj;53}5455const changed = changer(obj);56if (typeof changed !== 'undefined') {57return changed;58}5960if (Array.isArray(obj)) {61const r1: any[] = [];62for (const e of obj) {63r1.push(_cloneAndChange(e, changer, seen));64}65return r1;66}6768if (isObject(obj)) {69if (seen.has(obj)) {70throw new Error('Cannot clone recursive data-structure');71}72seen.add(obj);73const r2: Record<string, unknown> = {};74for (const i2 in obj) {75if (_hasOwnProperty.call(obj, i2)) {76r2[i2] = _cloneAndChange(obj[i2], changer, seen);77}78}79seen.delete(obj);80return r2;81}8283return obj;84}8586/**87* Copies all properties of source into destination. The optional parameter "overwrite" allows to control88* if existing properties on the destination should be overwritten or not. Defaults to true (overwrite).89*/90export function mixin(destination: any, source: any, overwrite: boolean = true): any {91if (!isObject(destination)) {92return source;93}9495if (isObject(source)) {96Object.keys(source).forEach(key => {97if (key in destination) {98if (overwrite) {99if (isObject(destination[key]) && isObject(source[key])) {100mixin(destination[key], source[key], overwrite);101} else {102destination[key] = source[key];103}104}105} else {106destination[key] = source[key];107}108});109}110return destination;111}112113export function equals(one: any, other: any): boolean {114if (one === other) {115return true;116}117if (one === null || one === undefined || other === null || other === undefined) {118return false;119}120if (typeof one !== typeof other) {121return false;122}123if (typeof one !== 'object') {124return false;125}126if ((Array.isArray(one)) !== (Array.isArray(other))) {127return false;128}129130let i: number;131let key: string;132133if (Array.isArray(one)) {134if (one.length !== other.length) {135return false;136}137for (i = 0; i < one.length; i++) {138if (!equals(one[i], other[i])) {139return false;140}141}142} else {143const oneKeys: string[] = [];144145for (key in one) {146oneKeys.push(key);147}148oneKeys.sort();149const otherKeys: string[] = [];150for (key in other) {151otherKeys.push(key);152}153otherKeys.sort();154if (!equals(oneKeys, otherKeys)) {155return false;156}157for (i = 0; i < oneKeys.length; i++) {158if (!equals(one[oneKeys[i]], other[oneKeys[i]])) {159return false;160}161}162}163return true;164}165166/**167* Calls `JSON.Stringify` with a replacer to break apart any circular references.168* This prevents `JSON`.stringify` from throwing the exception169* "Uncaught TypeError: Converting circular structure to JSON"170*/171export function safeStringify(obj: any): string {172const seen = new Set<any>();173return JSON.stringify(obj, (key, value) => {174if (isObject(value) || Array.isArray(value)) {175if (seen.has(value)) {176return '[Circular]';177} else {178seen.add(value);179}180}181if (typeof value === 'bigint') {182return `[BigInt ${value.toString()}]`;183}184return value;185});186}187188type obj = { [key: string]: any };189/**190* Returns an object that has keys for each value that is different in the base object. Keys191* that do not exist in the target but in the base object are not considered.192*193* Note: This is not a deep-diffing method, so the values are strictly taken into the resulting194* object if they differ.195*196* @param base the object to diff against197* @param obj the object to use for diffing198*/199export function distinct(base: obj, target: obj): obj {200const result = Object.create(null);201202if (!base || !target) {203return result;204}205206const targetKeys = Object.keys(target);207targetKeys.forEach(k => {208const baseValue = base[k];209const targetValue = target[k];210211if (!equals(baseValue, targetValue)) {212result[k] = targetValue;213}214});215216return result;217}218219export function getCaseInsensitive(target: obj, key: string): unknown {220const lowercaseKey = key.toLowerCase();221const equivalentKey = Object.keys(target).find(k => k.toLowerCase() === lowercaseKey);222return equivalentKey ? target[equivalentKey] : target[key];223}224225export function filter(obj: obj, predicate: (key: string, value: any) => boolean): obj {226const result = Object.create(null);227for (const [key, value] of Object.entries(obj)) {228if (predicate(key, value)) {229result[key] = value;230}231}232return result;233}234235export function mapValues<T extends {}, R>(obj: T, fn: (value: T[keyof T], key: string) => R): { [K in keyof T]: R } {236const result: { [key: string]: R } = {};237for (const [key, value] of Object.entries(obj)) {238result[key] = fn(<T[keyof T]>value, key);239}240return result as { [K in keyof T]: R };241}242243244