Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/ipynb/src/common.ts
5221 views
1
/*---------------------------------------------------------------------------------------------
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 type * as nbformat from '@jupyterlab/nbformat';
7
8
/**
9
* Metadata we store in VS Code cell output items.
10
* This contains the original metadata from the Jupyter outputs.
11
*/
12
export interface CellOutputMetadata {
13
/**
14
* Cell output metadata.
15
*/
16
metadata?: any;
17
18
/**
19
* Transient data from Jupyter.
20
*/
21
transient?: {
22
/**
23
* This is used for updating the output in other cells.
24
* We don't know of other properties, but this is definitely used.
25
*/
26
display_id?: string;
27
} & any;
28
29
/**
30
* Original cell output type
31
*/
32
outputType: nbformat.OutputType | string;
33
34
executionCount?: nbformat.IExecuteResult['ExecutionCount'];
35
36
/**
37
* Whether the original Mime data is JSON or not.
38
* This properly only exists in metadata for NotebookCellOutputItems
39
* (this is something we have added)
40
*/
41
__isJson?: boolean;
42
}
43
44
45
/**
46
* Metadata we store in VS Code cells.
47
* This contains the original metadata from the Jupyter cells.
48
*/
49
export interface CellMetadata {
50
/**
51
* Cell id for notebooks created with the new 4.5 version of nbformat.
52
*/
53
id?: string;
54
/**
55
* Stores attachments for cells.
56
*/
57
attachments?: nbformat.IAttachments;
58
/**
59
* Stores cell metadata.
60
*/
61
metadata?: Partial<nbformat.ICellMetadata> & { vscode?: { languageId?: string } };
62
/**
63
* The code cell's prompt number. Will be null if the cell has not been run.
64
*/
65
execution_count?: number | null;
66
}
67
68
69
70
type KeysOfUnionType<T> = T extends T ? keyof T : never;
71
type FilterType<T, TTest> = T extends TTest ? T : never;
72
type MakeOptionalAndBool<T extends object> = { [K in keyof T]?: boolean };
73
74
/**
75
* Type guard that checks if an object has specific keys and narrows the type accordingly.
76
*
77
* @param x - The object to check
78
* @param key - An object with boolean values indicating which keys to check for
79
* @returns true if all specified keys exist in the object, false otherwise
80
*
81
* @example
82
* ```typescript
83
* type A = { a: string };
84
* type B = { b: number };
85
* const obj: A | B = getObject();
86
*
87
* if (hasKey(obj, { a: true })) {
88
* // obj is now narrowed to type A
89
* console.log(obj.a);
90
* }
91
* ```
92
*/
93
export function hasKey<T extends object, TKeys>(x: T, key: TKeys & MakeOptionalAndBool<T>): x is FilterType<T, { [K in KeysOfUnionType<T> & keyof TKeys]: unknown }> {
94
for (const k in key) {
95
if (!(k in x)) {
96
return false;
97
}
98
}
99
return true;
100
}
101
102