@nteract/commutable
Advanced tools
Comparing version 1.0.2 to 1.0.3
76
index.js
@@ -1,75 +0,1 @@ | ||
/* @flow */ | ||
import * as v4 from './v4'; | ||
import type { Notebook as v4Notebook } from './v4'; | ||
import { | ||
emptyNotebook, | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
appendCell, | ||
monocellNotebook, | ||
} from './structures'; | ||
import type { | ||
ImmutableNotebook, | ||
JSONType, | ||
} from './types'; | ||
type PlaceholderNotebook = { | ||
nbformat: number, | ||
nbformat_minor: number, | ||
}; | ||
export type Notebook = PlaceholderNotebook & v4Notebook; | ||
function freezeReviver(k: string, v: JSONType): JSONType { | ||
return Object.freeze(v); | ||
} | ||
// Expected usage of below is | ||
// fromJS(parseNotebook(string|buffer)) | ||
export function parseNotebook(notebookString: string): Notebook { | ||
return JSON.parse(notebookString, freezeReviver); | ||
} | ||
export function fromJS(notebookJSON: Notebook): ImmutableNotebook { | ||
if (notebookJSON.nbformat === 4 && notebookJSON.nbformat_minor >= 0) { | ||
if (Array.isArray(notebookJSON.cells) && typeof notebookJSON.metadata === 'object') { | ||
return v4.fromJS(notebookJSON); | ||
} | ||
} | ||
if (notebookJSON.nbformat) { | ||
throw new TypeError( | ||
`nbformat v${notebookJSON.nbformat}.${notebookJSON.nbformat_minor} not recognized` | ||
); | ||
} | ||
throw new TypeError('This notebook format is not supported'); | ||
} | ||
export function toJS(immnb: ImmutableNotebook): v4Notebook { | ||
if (immnb.get('nbformat') === 4 && immnb.get('nbformat_minor') >= 0) { | ||
return v4.toJS(immnb); | ||
} | ||
throw new TypeError('Only notebook format 4 is supported'); | ||
} | ||
// Expected usage is stringifyNotebook(toJS(immutableNotebook)) | ||
export function stringifyNotebook(notebook: v4Notebook): string { | ||
return JSON.stringify(notebook, null, 2); | ||
} | ||
export type { | ||
ImmutableNotebook, | ||
}; | ||
export { | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
emptyNotebook, | ||
appendCell, | ||
monocellNotebook, | ||
}; | ||
module.exports = require('./lib'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.monocellNotebook = exports.appendCell = exports.emptyNotebook = exports.emptyMarkdownCell = exports.emptyCodeCell = undefined; | ||
exports.parseNotebook = parseNotebook; | ||
exports.fromJS = fromJS; | ||
exports.toJS = toJS; | ||
exports.stringifyNotebook = stringifyNotebook; | ||
const v4 = require('./v4'); | ||
var _v = require('./v4'); | ||
const { | ||
emptyNotebook, | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
appendCell, | ||
monocellNotebook, | ||
var v4 = _interopRequireWildcard(_v); | ||
appendCellToNotebook, | ||
var _structures = require('./structures'); | ||
insertCellAt, | ||
insertCellAfter, | ||
removeCell | ||
} = require('./structures'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function freezeReviver(k, v) { | ||
@@ -39,3 +38,3 @@ return Object.freeze(v); | ||
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`); | ||
} | ||
@@ -58,6 +57,19 @@ | ||
exports.emptyCodeCell = _structures.emptyCodeCell; | ||
exports.emptyMarkdownCell = _structures.emptyMarkdownCell; | ||
exports.emptyNotebook = _structures.emptyNotebook; | ||
exports.appendCell = _structures.appendCell; | ||
exports.monocellNotebook = _structures.monocellNotebook; | ||
module.exports = { | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
emptyNotebook, | ||
monocellNotebook, | ||
toJS, | ||
fromJS, | ||
parseNotebook, | ||
stringifyNotebook, | ||
insertCellAt, | ||
insertCellAfter, | ||
removeCell, | ||
appendCell, | ||
appendCellToNotebook, | ||
createImmutableOutput: v4.createImmutableOutput | ||
}; |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.monocellNotebook = exports.emptyNotebook = exports.emptyMarkdownCell = exports.emptyCodeCell = undefined; | ||
exports.createCodeCell = createCodeCell; | ||
exports.createMarkdownCell = createMarkdownCell; | ||
exports.createNotebook = createNotebook; | ||
exports.appendCell = appendCell; | ||
exports.appendCellToNotebook = appendCellToNotebook; | ||
exports.insertCellAt = insertCellAt; | ||
exports.insertCellAfter = insertCellAfter; | ||
exports.removeCell = removeCell; | ||
const uuidv4 = require('uuid').v4; | ||
const Immutable = require('immutable'); | ||
var _immutable = require('immutable'); | ||
// We're hardset to nbformat v4.4 for what we use in-memory | ||
var Immutable = _interopRequireWildcard(_immutable); | ||
var _uuid = require('uuid'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
// We're hardset to nbformat v4.4 for what we use in-memory | ||
const defaultCodeCell = Object.freeze({ | ||
@@ -53,4 +37,4 @@ cell_type: 'code', | ||
const emptyCodeCell = exports.emptyCodeCell = createCodeCell(); | ||
const emptyMarkdownCell = exports.emptyMarkdownCell = createMarkdownCell(); | ||
const emptyCodeCell = createCodeCell(); | ||
const emptyMarkdownCell = createMarkdownCell(); | ||
@@ -69,6 +53,6 @@ const defaultNotebook = Object.freeze({ | ||
const emptyNotebook = exports.emptyNotebook = createNotebook(); | ||
const emptyNotebook = createNotebook(); | ||
// Intended to make it easy to use this with (temporary mutable cellOrder + cellMap) | ||
function appendCell(cellStructure, immutableCell, id = (0, _uuid.v4)()) { | ||
function appendCell(cellStructure, immutableCell, id = uuidv4()) { | ||
return { | ||
@@ -103,2 +87,19 @@ cellOrder: cellStructure.cellOrder.push(id), | ||
const monocellNotebook = exports.monocellNotebook = appendCellToNotebook(emptyNotebook, emptyCodeCell); | ||
const monocellNotebook = appendCellToNotebook(emptyNotebook, emptyCodeCell); | ||
module.exports = { | ||
emptyCodeCell, | ||
emptyMarkdownCell, | ||
emptyNotebook, | ||
monocellNotebook, | ||
createCodeCell, | ||
createMarkdownCell, | ||
createNotebook, | ||
removeCell, | ||
insertCellAfter, | ||
insertCellAt, | ||
appendCellToNotebook, | ||
appendCell | ||
}; |
105
lib/v4.js
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createImmutableMimeBundle = createImmutableMimeBundle; | ||
exports.fromJS = fromJS; | ||
exports.cellToJS = cellToJS; | ||
exports.toJS = toJS; | ||
/* | ||
* Functions in this module are provided for converting from Jupyter Notebook | ||
* Format v4 to nteract's in-memory format, affectionately referred to as | ||
* commutable. | ||
* | ||
* See: https://github.com/jupyter/nbformat/blob/62d6eb8803616d198eaa2024604d1fe923f2a7b3/nbformat/v4/nbformat.v4.schema.json | ||
* | ||
* The main goal here is consistency and compliance with the v4 spec. The types | ||
* contained in here (non Immutable ones) are constrained to the disk based | ||
* notebook format. | ||
* | ||
* To assist in the developer experience, types are included through the use of | ||
* flow. | ||
* | ||
*/ | ||
var _immutable = require('immutable'); | ||
const Immutable = require('immutable'); | ||
const appendCell = require('./structures').appendCell; | ||
var Immutable = _interopRequireWildcard(_immutable); | ||
var _structures = require('./structures'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
// | ||
@@ -44,18 +47,2 @@ // MimeBundle example (disk format) | ||
/* | ||
* Functions in this module are provided for converting from Jupyter Notebook | ||
* Format v4 to nteract's in-memory format, affectionately referred to as | ||
* commutable. | ||
* | ||
* See: https://github.com/jupyter/nbformat/blob/62d6eb8803616d198eaa2024604d1fe923f2a7b3/nbformat/v4/nbformat.v4.schema.json | ||
* | ||
* The main goal here is consistency and compliance with the v4 spec. The types | ||
* contained in here (non Immutable ones) are constrained to the disk based | ||
* notebook format. | ||
* | ||
* To assist in the developer experience, types are included through the use of | ||
* flow. | ||
* | ||
*/ | ||
function demultiline(s) { | ||
@@ -96,3 +83,3 @@ if (Array.isArray(s)) { | ||
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`); | ||
} | ||
@@ -124,17 +111,22 @@ | ||
function sanitize(o) { | ||
if (o.metadata) { | ||
return { metadata: Immutable.fromJS(o.metadata) }; | ||
} | ||
return {}; | ||
} | ||
function createImmutableOutput(output) { | ||
switch (output.output_type) { | ||
case 'execute_result': | ||
return Immutable.Map({ | ||
return Immutable.Map(Object.assign({}, { | ||
output_type: output.output_type, | ||
execution_count: output.execution_count, | ||
data: createImmutableMimeBundle(output.data), | ||
metadata: Immutable.fromJS(output.metadata) | ||
}); | ||
data: createImmutableMimeBundle(output.data) | ||
}, sanitize(output))); | ||
case 'display_data': | ||
return Immutable.Map({ | ||
return Immutable.Map(Object.assign({}, { | ||
output_type: output.output_type, | ||
data: createImmutableMimeBundle(output.data), | ||
metadata: Immutable.fromJS(output.metadata) | ||
}); | ||
data: createImmutableMimeBundle(output.data) | ||
}, sanitize(output))); | ||
case 'stream': | ||
@@ -147,7 +139,12 @@ return Immutable.Map({ | ||
case 'error': | ||
// Note: this is one of the cases where the Array of strings (for traceback) | ||
// is part of the format, not a multiline string | ||
return Immutable.fromJS(output); | ||
return Immutable.Map({ | ||
output_type: 'error', | ||
ename: output.ename, | ||
evalue: output.evalue, | ||
// Note: this is one of the cases where the Array of strings (for traceback) | ||
// is part of the format, not a multiline string | ||
traceback: Immutable.List(output.traceback) | ||
}); | ||
default: | ||
throw new TypeError(`Output type ${ output.output_type } not recognized`); | ||
throw new TypeError(`Output type ${output.output_type} not recognized`); | ||
} | ||
@@ -191,3 +188,3 @@ } | ||
default: | ||
throw new TypeError(`Cell type ${ cell.cell_type } unknown`); | ||
throw new TypeError(`Cell type ${cell.cell_type} unknown`); | ||
} | ||
@@ -199,3 +196,3 @@ } | ||
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 }`); | ||
It lists nbformat v${notebook.nbformat}.${notebook.nbformat_minor}`); | ||
} | ||
@@ -210,3 +207,3 @@ | ||
const cellStructure = notebook.cells.reduce((cellStruct, cell) => (0, _structures.appendCell)(cellStruct, createImmutableCell(cell)), starterCellStructure); | ||
const cellStructure = notebook.cells.reduce((cellStruct, cell) => appendCell(cellStruct, createImmutableCell(cell)), starterCellStructure); | ||
@@ -249,3 +246,3 @@ return Immutable.Map({ | ||
} | ||
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`); | ||
}); | ||
@@ -285,3 +282,3 @@ | ||
default: | ||
throw new TypeError(`Output type ${ output.output_type } not recognized`); | ||
throw new TypeError(`Output type ${output.output_type} not recognized`); | ||
} | ||
@@ -322,3 +319,3 @@ } | ||
default: | ||
throw new TypeError(`Cell type ${ cellType } unknown`); | ||
throw new TypeError(`Cell type ${cellType} unknown`); | ||
} | ||
@@ -341,2 +338,10 @@ } | ||
}; | ||
} | ||
} | ||
module.exports = { | ||
createImmutableMimeBundle, | ||
createImmutableOutput, | ||
toJS, | ||
fromJS | ||
}; |
{ | ||
"name": "@nteract/commutable", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "library for immutable notebook operations", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"prepublish": "npm run build", | ||
"build": "npm run build:clean && npm run build:lib && npm run build:flow", | ||
"build:clean": "rimraf lib", | ||
"build:flow": "flow-copy-source -v -i '**/__tests__/**' src lib", | ||
"build:lib": "babel -d lib src --ignore '**/__tests__/**'", | ||
"test": "echo \"We need tests here\"" | ||
}, | ||
@@ -26,3 +31,7 @@ "repository": { | ||
"uuid": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.23.0", | ||
"flow-copy-source": "^1.1.0" | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
53676
15
1033
1
2
1