@nteract/commutable
Advanced tools
Comparing version 1.0.5 to 1.0.6
'use strict'; | ||
const v4 = require('./v4'); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
const { | ||
emptyNotebook, | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
appendCell, | ||
monocellNotebook, | ||
var v4 = require('./v4'); | ||
appendCellToNotebook, | ||
var _require = require('./structures'), | ||
emptyNotebook = _require.emptyNotebook, | ||
emptyCodeCell = _require.emptyCodeCell, | ||
emptyMarkdownCell = _require.emptyMarkdownCell, | ||
appendCell = _require.appendCell, | ||
monocellNotebook = _require.monocellNotebook, | ||
appendCellToNotebook = _require.appendCellToNotebook, | ||
insertCellAt = _require.insertCellAt, | ||
insertCellAfter = _require.insertCellAfter, | ||
removeCell = _require.removeCell; | ||
insertCellAt, | ||
insertCellAfter, | ||
removeCell | ||
} = require('./structures'); | ||
function freezeReviver(k, v) { | ||
@@ -32,3 +31,3 @@ return Object.freeze(v); | ||
if (notebookJSON.nbformat === 4 && notebookJSON.nbformat_minor >= 0) { | ||
if (Array.isArray(notebookJSON.cells) && typeof notebookJSON.metadata === 'object') { | ||
if (Array.isArray(notebookJSON.cells) && _typeof(notebookJSON.metadata) === 'object') { | ||
return v4.fromJS(notebookJSON); | ||
@@ -39,3 +38,3 @@ } | ||
if (notebookJSON.nbformat) { | ||
throw new TypeError(`nbformat v${notebookJSON.nbformat}.${notebookJSON.nbformat_minor} not recognized`); | ||
throw new TypeError('nbformat v' + notebookJSON.nbformat + '.' + notebookJSON.nbformat_minor + ' not recognized'); | ||
} | ||
@@ -59,18 +58,18 @@ | ||
module.exports = { | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
emptyNotebook, | ||
monocellNotebook, | ||
toJS, | ||
fromJS, | ||
emptyCodeCell: emptyCodeCell, | ||
emptyMarkdownCell: emptyMarkdownCell, | ||
emptyNotebook: emptyNotebook, | ||
monocellNotebook: monocellNotebook, | ||
toJS: toJS, | ||
fromJS: fromJS, | ||
parseNotebook, | ||
stringifyNotebook, | ||
parseNotebook: parseNotebook, | ||
stringifyNotebook: stringifyNotebook, | ||
insertCellAt, | ||
insertCellAfter, | ||
removeCell, | ||
appendCell, | ||
appendCellToNotebook, | ||
insertCellAt: insertCellAt, | ||
insertCellAfter: insertCellAfter, | ||
removeCell: removeCell, | ||
appendCell: appendCell, | ||
appendCellToNotebook: appendCellToNotebook, | ||
createImmutableOutput: v4.createImmutableOutput | ||
}; |
'use strict'; | ||
const uuidv4 = require('uuid').v4; | ||
const Immutable = require('immutable'); | ||
var uuidv4 = require('uuid').v4; | ||
var Immutable = require('immutable'); | ||
@@ -9,3 +9,3 @@ // We're hardset to nbformat v4.4 for what we use in-memory | ||
const defaultCodeCell = Object.freeze({ | ||
var defaultCodeCell = Object.freeze({ | ||
cell_type: 'code', | ||
@@ -22,3 +22,3 @@ execution_count: null, | ||
const defaultMarkdownCell = Object.freeze({ | ||
var defaultMarkdownCell = Object.freeze({ | ||
cell_type: 'markdown', | ||
@@ -29,3 +29,4 @@ metadata: Immutable.Map(), | ||
function createCodeCell(cell = defaultCodeCell) { | ||
function createCodeCell() { | ||
var cell = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultCodeCell; | ||
// eslint-disable-line max-len | ||
@@ -35,3 +36,4 @@ return Immutable.Map(cell); | ||
function createMarkdownCell(cell = defaultMarkdownCell) { | ||
function createMarkdownCell() { | ||
var cell = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultMarkdownCell; | ||
// eslint-disable-line max-len | ||
@@ -41,6 +43,6 @@ return Immutable.Map(cell); | ||
const emptyCodeCell = createCodeCell(); | ||
const emptyMarkdownCell = createMarkdownCell(); | ||
var emptyCodeCell = createCodeCell(); | ||
var emptyMarkdownCell = createMarkdownCell(); | ||
const defaultNotebook = Object.freeze({ | ||
var defaultNotebook = Object.freeze({ | ||
nbformat: 4, | ||
@@ -53,10 +55,14 @@ nbformat_minor: 4, | ||
function createNotebook(notebook = defaultNotebook) { | ||
function createNotebook() { | ||
var notebook = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultNotebook; | ||
return Immutable.Map(notebook); | ||
} | ||
const emptyNotebook = createNotebook(); | ||
var emptyNotebook = createNotebook(); | ||
// Intended to make it easy to use this with (temporary mutable cellOrder + cellMap) | ||
function appendCell(cellStructure, immutableCell, id = uuidv4()) { | ||
function appendCell(cellStructure, immutableCell) { | ||
var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : uuidv4(); | ||
return { | ||
@@ -69,8 +75,12 @@ cellOrder: cellStructure.cellOrder.push(id), | ||
function appendCellToNotebook(immnb, immCell) { | ||
return immnb.withMutations(nb => { | ||
const cellStructure = { | ||
return immnb.withMutations(function (nb) { | ||
var cellStructure = { | ||
cellOrder: nb.get('cellOrder'), | ||
cellMap: nb.get('cellMap') | ||
}; | ||
const { cellOrder, cellMap } = appendCell(cellStructure, immCell); | ||
var _appendCell = appendCell(cellStructure, immCell), | ||
cellOrder = _appendCell.cellOrder, | ||
cellMap = _appendCell.cellMap; | ||
return nb.set('cellOrder', cellOrder).set('cellMap', cellMap); | ||
@@ -81,3 +91,5 @@ }); | ||
function insertCellAt(notebook, cell, cellID, index) { | ||
return notebook.withMutations(nb => nb.setIn(['cellMap', cellID], cell).set('cellOrder', nb.get('cellOrder').insert(index, cellID))); | ||
return notebook.withMutations(function (nb) { | ||
return nb.setIn(['cellMap', cellID], cell).set('cellOrder', nb.get('cellOrder').insert(index, cellID)); | ||
}); | ||
} | ||
@@ -90,22 +102,26 @@ | ||
function removeCell(notebook, cellID) { | ||
return notebook.removeIn(['cellMap', cellID]).update('cellOrder', cellOrder => cellOrder.filterNot(id => id === cellID)); | ||
return notebook.removeIn(['cellMap', cellID]).update('cellOrder', function (cellOrder) { | ||
return cellOrder.filterNot(function (id) { | ||
return id === cellID; | ||
}); | ||
}); | ||
} | ||
const monocellNotebook = appendCellToNotebook(emptyNotebook, emptyCodeCell); | ||
var monocellNotebook = appendCellToNotebook(emptyNotebook, emptyCodeCell); | ||
module.exports = { | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
emptyNotebook, | ||
monocellNotebook, | ||
emptyCodeCell: emptyCodeCell, | ||
emptyMarkdownCell: emptyMarkdownCell, | ||
emptyNotebook: emptyNotebook, | ||
monocellNotebook: monocellNotebook, | ||
createCodeCell, | ||
createMarkdownCell, | ||
createNotebook, | ||
createCodeCell: createCodeCell, | ||
createMarkdownCell: createMarkdownCell, | ||
createNotebook: createNotebook, | ||
removeCell, | ||
insertCellAfter, | ||
insertCellAt, | ||
appendCellToNotebook, | ||
appendCell | ||
removeCell: removeCell, | ||
insertCellAfter: insertCellAfter, | ||
insertCellAt: insertCellAt, | ||
appendCellToNotebook: appendCellToNotebook, | ||
appendCell: appendCell | ||
}; |
@@ -19,4 +19,4 @@ 'use strict'; | ||
const Immutable = require('immutable'); | ||
const appendCell = require('./structures').appendCell; | ||
var Immutable = require('immutable'); | ||
var appendCell = require('./structures').appendCell; | ||
@@ -64,3 +64,5 @@ // | ||
// Use positive lookahead regex to split on newline and retain newline char | ||
return s.split(/(.+?(?:\r\n|\n))/g).filter(x => x !== ''); | ||
return s.split(/(.+?(?:\r\n|\n))/g).filter(function (x) { | ||
return x !== ''; | ||
}); | ||
} | ||
@@ -84,3 +86,3 @@ | ||
throw new TypeError(`Data for ${key} is expected to be a string or an Array of strings`); | ||
throw new TypeError('Data for ' + key + ' is expected to be a string or an Array of strings'); | ||
} | ||
@@ -148,3 +150,3 @@ | ||
default: | ||
throw new TypeError(`Output type ${output.output_type} not recognized`); | ||
throw new TypeError('Output type ' + output.output_type + ' not recognized'); | ||
} | ||
@@ -188,3 +190,3 @@ } | ||
default: | ||
throw new TypeError(`Cell type ${cell.cell_type} unknown`); | ||
throw new TypeError('Cell type ' + cell.cell_type + ' unknown'); | ||
} | ||
@@ -195,4 +197,3 @@ } | ||
if (notebook.nbformat !== 4 || notebook.nbformat_minor < 0) { | ||
throw new TypeError(`Notebook is not a valid v4 notebook. v4 notebooks must be of form 4.x | ||
It lists nbformat v${notebook.nbformat}.${notebook.nbformat_minor}`); | ||
throw new TypeError('Notebook is not a valid v4 notebook. v4 notebooks must be of form 4.x\n It lists nbformat v' + notebook.nbformat + '.' + notebook.nbformat_minor); | ||
} | ||
@@ -202,3 +203,3 @@ | ||
// switch back after. | ||
const starterCellStructure = { | ||
var starterCellStructure = { | ||
cellOrder: Immutable.List().asMutable(), | ||
@@ -208,3 +209,5 @@ cellMap: Immutable.Map().asMutable() | ||
const cellStructure = notebook.cells.reduce((cellStruct, cell) => appendCell(cellStruct, createImmutableCell(cell)), starterCellStructure); | ||
var cellStructure = notebook.cells.reduce(function (cellStruct, cell) { | ||
return appendCell(cellStruct, createImmutableCell(cell)); | ||
}, starterCellStructure); | ||
@@ -221,3 +224,3 @@ return Immutable.Map({ | ||
function markdownCellToJS(immCell) { | ||
const cell = immCell.toObject(); | ||
var cell = immCell.toObject(); | ||
@@ -232,5 +235,5 @@ return { | ||
function mimeBundleToJS(immMimeBundle) { | ||
const bundle = immMimeBundle.toObject(); | ||
var bundle = immMimeBundle.toObject(); | ||
Object.keys(bundle).map(key => { | ||
Object.keys(bundle).map(function (key) { | ||
if (isJSONKey(key)) { | ||
@@ -243,3 +246,3 @@ if (Immutable.Map.isMap(bundle[key])) { | ||
const data = bundle[key]; | ||
var data = bundle[key]; | ||
@@ -250,3 +253,3 @@ if (typeof data === 'string' || Array.isArray(data)) { | ||
} | ||
throw new TypeError(`Data for ${key} is expected to be a string or an Array of strings`); | ||
throw new TypeError('Data for ' + key + ' is expected to be a string or an Array of strings'); | ||
}); | ||
@@ -259,3 +262,3 @@ | ||
// Technically this is an intermediate output with Immutables inside | ||
const output = immOutput.toObject(); | ||
var output = immOutput.toObject(); | ||
@@ -287,3 +290,3 @@ switch (output.output_type) { | ||
default: | ||
throw new TypeError(`Output type ${output.output_type} not recognized`); | ||
throw new TypeError('Output type ' + output.output_type + ' not recognized'); | ||
} | ||
@@ -293,3 +296,3 @@ } | ||
function codeCellToJS(immCell) { | ||
const cell = immCell.toObject(); | ||
var cell = immCell.toObject(); | ||
@@ -306,3 +309,3 @@ return { | ||
function rawCellToJS(immCell) { | ||
const cell = immCell.toObject(); | ||
var cell = immCell.toObject(); | ||
@@ -317,3 +320,3 @@ return { | ||
function cellToJS(immCell) { | ||
const cellType = immCell.get('cell_type'); | ||
var cellType = immCell.get('cell_type'); | ||
switch (cellType) { | ||
@@ -327,3 +330,3 @@ case 'markdown': | ||
default: | ||
throw new TypeError(`Cell type ${cellType} unknown`); | ||
throw new TypeError('Cell type ' + cellType + ' unknown'); | ||
} | ||
@@ -333,11 +336,13 @@ } | ||
function toJS(immnb) { | ||
const plainNotebook = immnb.toObject(); | ||
var plainNotebook = immnb.toObject(); | ||
const plainCellOrder = plainNotebook.cellOrder.toArray(); | ||
const plainCellMap = plainNotebook.cellMap.toObject(); | ||
var plainCellOrder = plainNotebook.cellOrder.toArray(); | ||
var plainCellMap = plainNotebook.cellMap.toObject(); | ||
const cells = plainCellOrder.map(cellID => cellToJS(plainCellMap[cellID])); | ||
var cells = plainCellOrder.map(function (cellID) { | ||
return cellToJS(plainCellMap[cellID]); | ||
}); | ||
return { | ||
cells, | ||
cells: cells, | ||
metadata: plainNotebook.metadata.toJS(), | ||
@@ -350,7 +355,7 @@ nbformat: plainNotebook.nbformat, | ||
module.exports = { | ||
createImmutableMimeBundle, | ||
createImmutableOutput, | ||
createImmutableMimeBundle: createImmutableMimeBundle, | ||
createImmutableOutput: createImmutableOutput, | ||
toJS, | ||
fromJS | ||
toJS: toJS, | ||
fromJS: fromJS | ||
}; |
{ | ||
"name": "@nteract/commutable", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "library for immutable notebook operations", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
55205
1050