@nteract/commutable
Advanced tools
Comparing version 7.3.5 to 7.4.0
@@ -1,2 +0,2 @@ | ||
import Immutable from "immutable"; | ||
import Immutable, { Record } from "immutable"; | ||
@@ -9,5 +9,22 @@ import { | ||
} from "../src/outputs"; | ||
import { createCodeCell, createMarkdownCell } from "../src/structures"; | ||
import { cellToJS, outputToJS } from "../src/v4"; | ||
import { | ||
createCodeCell, | ||
createMarkdownCell, | ||
NotebookRecordParams, | ||
ImmutableNotebook, | ||
makeNotebookRecord | ||
} from "../src/structures"; | ||
import * as primitives from "../src/primitives"; | ||
import { | ||
cellToJS, | ||
outputToJS, | ||
fromJS, | ||
toJS, | ||
NotebookV4, | ||
Cell | ||
} from "../src/v4"; | ||
import { CodeCellParams, ImmutableCell } from "../src/cells"; | ||
describe("cellToJS", () => { | ||
@@ -44,1 +61,131 @@ it("throws an error for unkown cell types", () => { | ||
}); | ||
describe("cell ids", () => { | ||
let originalCreateCellId = undefined; | ||
type NotebookParameters = { | ||
cells?: any; | ||
minorVersion?: number; | ||
}; | ||
const initial = createCodeCell().toJS(); | ||
// Obtains a notebook for the following tests | ||
// allows overriding cells collection and minor version | ||
const getNotebook = ({ | ||
cells = undefined, | ||
minorVersion = 5 | ||
}: NotebookParameters = {}) => { | ||
const notebook: NotebookV4 = { | ||
cells: cells || [ | ||
{ | ||
...initial, | ||
id: "test-cell-id" | ||
} | ||
], | ||
metadata: {}, | ||
nbformat: 4, | ||
nbformat_minor: minorVersion || 5 | ||
}; | ||
return notebook; | ||
}; | ||
beforeAll(() => { | ||
// Keep track of the original createCellId method | ||
originalCreateCellId = primitives.createCellId; | ||
// Manually mock out the create cell | ||
(primitives as any).createCellId = () => "one-two-three"; | ||
}); | ||
afterAll(() => { | ||
// Restore the original createCellId method | ||
(primitives as any).createCellId = originalCreateCellId; | ||
}); | ||
describe("fromJS", () => { | ||
it("uses provided cell id when v4.5", () => { | ||
const notebook = getNotebook(); | ||
const immNotebook = fromJS(notebook); | ||
// ensure we're using the provided id for the cell order and cell map | ||
expect(immNotebook.get("cellOrder").toJSON()).toEqual(["test-cell-id"]); | ||
expect(immNotebook.getIn(["cellMap", "test-cell-id"]).toJS()).toEqual( | ||
initial | ||
); | ||
}); | ||
it("uses generated cell id when v4.4 (or prior)", () => { | ||
// override createCellId | ||
const notebook = getNotebook({ minorVersion: 4 }); | ||
const immNotebook = fromJS(notebook); | ||
// ensure we're using the provided id for the cell order and cell map | ||
expect(immNotebook.get("cellOrder").toJSON()).toEqual(["one-two-three"]); | ||
expect(immNotebook.getIn(["cellMap", "one-two-three"]).toJS()).toEqual( | ||
initial | ||
); | ||
}); | ||
it("uses generated cell id when cell id not present", () => { | ||
const notebook = getNotebook({ | ||
cells: [ | ||
{ | ||
...initial | ||
} | ||
] | ||
}); | ||
const immNotebook = fromJS(notebook); | ||
// ensure we're using the provided id for the cell order and cell map | ||
expect(immNotebook.get("cellOrder").toJSON()).toEqual(["one-two-three"]); | ||
expect(immNotebook.getIn(["cellMap", "one-two-three"]).toJS()).toEqual( | ||
initial | ||
); | ||
}); | ||
}); | ||
describe("toJS", () => { | ||
const getCell = () => { | ||
return createCodeCell({ | ||
id: "this-cell-id", | ||
cell_type: "code", | ||
execution_count: null, | ||
source: "" | ||
}); | ||
}; | ||
it("includes cell id when version 4.5+", () => { | ||
const cell = getCell(); | ||
const notebook = makeNotebookRecord({ | ||
nbformat: 4, | ||
nbformat_minor: 5, | ||
cellOrder: Immutable.List(["one-two-three"]), | ||
cellMap: Immutable.Map({ | ||
"one-two-three": cell | ||
}) | ||
}); | ||
const out = toJS(notebook); | ||
expect(out.cells[0].id).toEqual("one-two-three"); | ||
}); | ||
it("does not include notebook when version 4.4 or lower", () => { | ||
const cell = getCell(); | ||
const notebook = makeNotebookRecord({ | ||
nbformat: 4, | ||
nbformat_minor: 4, | ||
cellOrder: Immutable.List(["one-two-three"]), | ||
cellMap: Immutable.Map({ | ||
"one-two-three": cell | ||
}) | ||
}); | ||
const out = toJS(notebook); | ||
expect(out.cells[0].hasOwnProperty("id")).toBe(false); | ||
}); | ||
}); | ||
}); |
@@ -6,2 +6,3 @@ import { ImmutableOutput } from "./outputs"; | ||
cell_type: "code"; | ||
id?: string; | ||
metadata: ImmutableMap<string, any>; | ||
@@ -16,2 +17,3 @@ execution_count: ExecutionCount; | ||
cell_type: "markdown"; | ||
id?: string; | ||
source: string; | ||
@@ -23,2 +25,3 @@ metadata: ImmutableMap<string, any>; | ||
export interface RawCellParams { | ||
id?: string; | ||
cell_type: "raw"; | ||
@@ -25,0 +28,0 @@ source: string; |
@@ -20,2 +20,3 @@ /** | ||
cell_type: "code"; | ||
id?: string; | ||
metadata: JSONObject; | ||
@@ -28,2 +29,3 @@ execution_count: ExecutionCount; | ||
cell_type: "markdown"; | ||
id?: string; | ||
metadata: JSONObject; | ||
@@ -34,2 +36,3 @@ source: MultiLineString; | ||
cell_type: "raw"; | ||
id?: string; | ||
metadata: JSONObject; | ||
@@ -36,0 +39,0 @@ source: MultiLineString; |
@@ -88,2 +88,5 @@ "use strict"; | ||
} | ||
function hasCellId(notebook) { | ||
return notebook.nbformat === 4 && notebook.nbformat_minor >= 5; | ||
} | ||
function fromJS(notebook) { | ||
@@ -101,3 +104,8 @@ if (!isNotebookV4(notebook)) { | ||
}; | ||
const cellStructure = notebook.cells.reduce((cellStruct, cell) => structures_1.appendCell(cellStruct, createImmutableCell(cell)), starterCellStructure); | ||
// Obtain the cell id if we're on a notebook version 4.5 or greater | ||
const shouldUseId = hasCellId(notebook); | ||
const cellStructure = notebook.cells.reduce((cellStruct, cell) => structures_1.appendCell(cellStruct, createImmutableCell(cell), | ||
// Pass in the cell id if it exists, otherwise | ||
// use undefined and let nteract generate a cell id | ||
shouldUseId ? cell.id : undefined), starterCellStructure); | ||
return structures_1.makeNotebookRecord({ | ||
@@ -216,2 +224,3 @@ cellOrder: cellStructure.cellOrder.asImmutable(), | ||
const plainCellMap = plainNotebook.cellMap.toObject(); | ||
const shouldUseCellId = hasCellId(plainNotebook); | ||
const cells = plainCellOrder | ||
@@ -224,3 +233,11 @@ .filter((cellId) => !plainCellMap[cellId].getIn([ | ||
])) | ||
.map((cellId) => cellToJS(plainCellMap[cellId])); | ||
.map((cellId) => { | ||
const cell = cellToJS(plainCellMap[cellId]); | ||
// if this notebook has cell ids, ensure | ||
// that the id is included in the cell output | ||
if (shouldUseCellId) { | ||
cell["id"] = cellId; | ||
} | ||
return cell; | ||
}); | ||
return { | ||
@@ -227,0 +244,0 @@ cells, |
{ | ||
"name": "@nteract/commutable", | ||
"version": "7.3.5", | ||
"version": "7.4.0", | ||
"description": "library for immutable notebook operations", | ||
@@ -27,3 +27,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "e9c556226aa1e8eb081f143b1de76e6428b01bfe" | ||
"gitHead": "4c70d9d84dec6f95c522d4e8388d0b7b12c80ea9" | ||
} |
@@ -16,2 +16,3 @@ import { ImmutableOutput } from "./outputs"; | ||
cell_type: "code"; | ||
id?: string; | ||
// Sadly untyped and widely unspecced | ||
@@ -49,2 +50,3 @@ metadata: ImmutableMap<string, any>; | ||
cell_type: "markdown"; | ||
id?: string; | ||
source: string; | ||
@@ -71,2 +73,3 @@ metadata: ImmutableMap<string, any>; | ||
export interface RawCellParams { | ||
id?: string; | ||
cell_type: "raw"; | ||
@@ -73,0 +76,0 @@ source: string; |
@@ -65,2 +65,3 @@ // Due to the on-disk format needing to be written out in an explicit order, | ||
cell_type: "code"; | ||
id?: string; | ||
metadata: JSONObject; | ||
@@ -74,2 +75,3 @@ execution_count: ExecutionCount; | ||
cell_type: "markdown"; | ||
id?: string; | ||
metadata: JSONObject; | ||
@@ -81,2 +83,3 @@ source: MultiLineString; | ||
cell_type: "raw"; | ||
id?: string; | ||
metadata: JSONObject; | ||
@@ -166,2 +169,6 @@ source: MultiLineString; | ||
function hasCellId(notebook: NotebookV4 | NotebookRecordParams) { | ||
return notebook.nbformat === 4 && notebook.nbformat_minor >= 5; | ||
} | ||
export function fromJS( | ||
@@ -185,4 +192,14 @@ notebook: NotebookV4 | ||
// Obtain the cell id if we're on a notebook version 4.5 or greater | ||
const shouldUseId = hasCellId(notebook); | ||
const cellStructure = notebook.cells.reduce( | ||
(cellStruct, cell) => appendCell(cellStruct, createImmutableCell(cell)), | ||
(cellStruct, cell) => | ||
appendCell( | ||
cellStruct, | ||
createImmutableCell(cell), | ||
// Pass in the cell id if it exists, otherwise | ||
// use undefined and let nteract generate a cell id | ||
shouldUseId ? cell.id : undefined | ||
), | ||
starterCellStructure | ||
@@ -311,2 +328,3 @@ ); | ||
const shouldUseCellId = hasCellId(plainNotebook); | ||
const cells = plainCellOrder | ||
@@ -322,4 +340,14 @@ .filter( | ||
) | ||
.map((cellId: string) => cellToJS(plainCellMap[cellId])); | ||
.map((cellId: string) => { | ||
const cell = cellToJS(plainCellMap[cellId]); | ||
// if this notebook has cell ids, ensure | ||
// that the id is included in the cell output | ||
if (shouldUseCellId) { | ||
cell["id"] = cellId; | ||
} | ||
return cell; | ||
}); | ||
return { | ||
@@ -326,0 +354,0 @@ cells, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
702804
3274