@difizen/libro-common 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/index.d.ts +1 -0
- package/es/index.d.ts.map +1 -1
- package/es/index.js +2 -1
- package/es/output-utils.d.ts +22 -0
- package/es/output-utils.d.ts.map +1 -0
- package/es/output-utils.js +76 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/output-utils.ts +98 -0
package/es/index.d.ts
CHANGED
package/es/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAE1B,cAAc,mBAAmB,CAAC"}
|
package/es/index.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PartialJSONObject, PartialJSONValue, ReadonlyPartialJSONObject } from './json.js';
|
|
2
|
+
import type { IOutput } from './protocol/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Get the data from a notebook output.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getData(output: IOutput): PartialJSONObject;
|
|
7
|
+
/**
|
|
8
|
+
* Get the metadata from an output message.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getMetadata(output: IOutput): PartialJSONObject;
|
|
11
|
+
/**
|
|
12
|
+
* Get the bundle options given output model options.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getBundleOptions(options: IOutput): {
|
|
15
|
+
data: IOutput;
|
|
16
|
+
metadata: PartialJSONObject;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Extract a value from a JSONObject.
|
|
20
|
+
*/
|
|
21
|
+
export declare function extract(value: ReadonlyPartialJSONObject, key: string): PartialJSONValue | undefined;
|
|
22
|
+
//# sourceMappingURL=output-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output-utils.d.ts","sourceRoot":"","sources":["../src/output-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,gBAAgB,EAChB,yBAAyB,EAC1B,MAAM,WAAW,CAAC;AAEnB,OAAO,KAAK,EAGV,OAAO,EAER,MAAM,qBAAqB,CAAC;AAS7B;;GAEG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,iBAAiB,CA4B1D;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,GAAG,iBAAiB,CAQ9D;AACD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO;;;EAIhD;AAED;;GAEG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,MAAM,GACV,gBAAgB,GAAG,SAAS,CAM9B"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { isPrimitive } from "./json.js";
|
|
2
|
+
import { isDisplayData, isDisplayUpdate, isError, isExecuteResult, isStream } from "./utils.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get the data from a notebook output.
|
|
6
|
+
*/
|
|
7
|
+
export function getData(output) {
|
|
8
|
+
var bundle = {};
|
|
9
|
+
if (isExecuteResult(output) || isDisplayData(output) || isDisplayUpdate(output)) {
|
|
10
|
+
bundle = output.data;
|
|
11
|
+
} else if (isStream(output)) {
|
|
12
|
+
if (output.name === 'stderr') {
|
|
13
|
+
bundle['application/vnd.jupyter.stderr'] = output.text;
|
|
14
|
+
} else {
|
|
15
|
+
bundle['application/vnd.jupyter.stdout'] = output.text;
|
|
16
|
+
}
|
|
17
|
+
if (output['display_text']) {
|
|
18
|
+
if (output.name === 'stderr') {
|
|
19
|
+
bundle['application/vnd.libro.large.output.stderr'] = output['display_text'];
|
|
20
|
+
} else {
|
|
21
|
+
bundle['application/vnd.libro.large.output.stdout'] = output['display_text'];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} else if (isError(output)) {
|
|
25
|
+
bundle['application/vnd.jupyter.error'] = output;
|
|
26
|
+
var traceback = output.traceback.join('\n');
|
|
27
|
+
bundle['application/vnd.jupyter.stderr'] = traceback || "".concat(output.ename, ": ").concat(output.evalue);
|
|
28
|
+
}
|
|
29
|
+
return convertBundle(bundle);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get the metadata from an output message.
|
|
34
|
+
*/
|
|
35
|
+
export function getMetadata(output) {
|
|
36
|
+
var value = Object.create(null);
|
|
37
|
+
if (isExecuteResult(output) || isDisplayData(output)) {
|
|
38
|
+
for (var key in output.metadata) {
|
|
39
|
+
value[key] = extract(output.metadata, key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the bundle options given output model options.
|
|
46
|
+
*/
|
|
47
|
+
export function getBundleOptions(options) {
|
|
48
|
+
var data = getData(options);
|
|
49
|
+
var metadata = getMetadata(options);
|
|
50
|
+
return {
|
|
51
|
+
data: data,
|
|
52
|
+
metadata: metadata
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Extract a value from a JSONObject.
|
|
58
|
+
*/
|
|
59
|
+
export function extract(value, key) {
|
|
60
|
+
var item = value[key];
|
|
61
|
+
if (item === undefined || isPrimitive(item)) {
|
|
62
|
+
return item;
|
|
63
|
+
}
|
|
64
|
+
return JSON.parse(JSON.stringify(item));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Convert a mime bundle to mime data.
|
|
69
|
+
*/
|
|
70
|
+
function convertBundle(bundle) {
|
|
71
|
+
var map = Object.create(null);
|
|
72
|
+
for (var mimeType in bundle) {
|
|
73
|
+
map[mimeType] = extract(bundle, mimeType);
|
|
74
|
+
}
|
|
75
|
+
return map;
|
|
76
|
+
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PartialJSONObject,
|
|
3
|
+
PartialJSONValue,
|
|
4
|
+
ReadonlyPartialJSONObject,
|
|
5
|
+
} from './json.js';
|
|
6
|
+
import { isPrimitive } from './json.js';
|
|
7
|
+
import type {
|
|
8
|
+
IExecuteResult,
|
|
9
|
+
IMimeBundle,
|
|
10
|
+
IOutput,
|
|
11
|
+
MultilineString,
|
|
12
|
+
} from './protocol/index.js';
|
|
13
|
+
import {
|
|
14
|
+
isDisplayData,
|
|
15
|
+
isDisplayUpdate,
|
|
16
|
+
isError,
|
|
17
|
+
isExecuteResult,
|
|
18
|
+
isStream,
|
|
19
|
+
} from './utils.js';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get the data from a notebook output.
|
|
23
|
+
*/
|
|
24
|
+
export function getData(output: IOutput): PartialJSONObject {
|
|
25
|
+
let bundle: IMimeBundle = {};
|
|
26
|
+
if (isExecuteResult(output) || isDisplayData(output) || isDisplayUpdate(output)) {
|
|
27
|
+
bundle = (output as IExecuteResult).data;
|
|
28
|
+
} else if (isStream(output)) {
|
|
29
|
+
if (output.name === 'stderr') {
|
|
30
|
+
bundle['application/vnd.jupyter.stderr'] = output.text;
|
|
31
|
+
} else {
|
|
32
|
+
bundle['application/vnd.jupyter.stdout'] = output.text;
|
|
33
|
+
}
|
|
34
|
+
if (output['display_text']) {
|
|
35
|
+
if (output.name === 'stderr') {
|
|
36
|
+
bundle['application/vnd.libro.large.output.stderr'] = output[
|
|
37
|
+
'display_text'
|
|
38
|
+
] as MultilineString;
|
|
39
|
+
} else {
|
|
40
|
+
bundle['application/vnd.libro.large.output.stdout'] = output[
|
|
41
|
+
'display_text'
|
|
42
|
+
] as MultilineString;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} else if (isError(output)) {
|
|
46
|
+
bundle['application/vnd.jupyter.error'] = output;
|
|
47
|
+
const traceback = output.traceback.join('\n');
|
|
48
|
+
bundle['application/vnd.jupyter.stderr'] =
|
|
49
|
+
traceback || `${output.ename}: ${output.evalue}`;
|
|
50
|
+
}
|
|
51
|
+
return convertBundle(bundle);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get the metadata from an output message.
|
|
56
|
+
*/
|
|
57
|
+
export function getMetadata(output: IOutput): PartialJSONObject {
|
|
58
|
+
const value: PartialJSONObject = Object.create(null);
|
|
59
|
+
if (isExecuteResult(output) || isDisplayData(output)) {
|
|
60
|
+
for (const key in output.metadata) {
|
|
61
|
+
value[key] = extract(output.metadata, key);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get the bundle options given output model options.
|
|
68
|
+
*/
|
|
69
|
+
export function getBundleOptions(options: IOutput) {
|
|
70
|
+
const data = getData(options) as IOutput;
|
|
71
|
+
const metadata = getMetadata(options);
|
|
72
|
+
return { data, metadata };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Extract a value from a JSONObject.
|
|
77
|
+
*/
|
|
78
|
+
export function extract(
|
|
79
|
+
value: ReadonlyPartialJSONObject,
|
|
80
|
+
key: string,
|
|
81
|
+
): PartialJSONValue | undefined {
|
|
82
|
+
const item = value[key];
|
|
83
|
+
if (item === undefined || isPrimitive(item)) {
|
|
84
|
+
return item;
|
|
85
|
+
}
|
|
86
|
+
return JSON.parse(JSON.stringify(item));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Convert a mime bundle to mime data.
|
|
91
|
+
*/
|
|
92
|
+
function convertBundle(bundle: IMimeBundle): PartialJSONObject {
|
|
93
|
+
const map: PartialJSONObject = Object.create(null);
|
|
94
|
+
for (const mimeType in bundle) {
|
|
95
|
+
map[mimeType] = extract(bundle, mimeType);
|
|
96
|
+
}
|
|
97
|
+
return map;
|
|
98
|
+
}
|