Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nteract/commutable

Package Overview
Dependencies
Maintainers
13
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nteract/commutable - npm Package Compare versions

Comparing version 4.1.1 to 4.1.2

LICENSE

73

lib/index.js
"use strict";
var _interopRequireWildcard = require("@babel/runtime-corejs2/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createImmutableMimeBundle = exports.createImmutableOutput = exports.appendCellToNotebook = exports.appendCell = exports.removeCell = exports.insertCellAfter = exports.insertCellAt = exports.stringifyNotebook = exports.parseNotebook = exports.createCodeCell = exports.fromJS = exports.toJS = exports.monocellNotebook = exports.emptyNotebook = exports.emptyMarkdownCell = exports.emptyCodeCell = undefined;
exports.toJS = toJS;
exports.fromJS = fromJS;
exports.parseNotebook = parseNotebook;
exports.stringifyNotebook = stringifyNotebook;
exports.createImmutableMimeBundle = exports.createImmutableOutput = exports.appendCellToNotebook = exports.appendCell = exports.removeCell = exports.insertCellAfter = exports.insertCellAt = exports.createCodeCell = exports.monocellNotebook = exports.emptyNotebook = exports.emptyMarkdownCell = exports.emptyCodeCell = void 0;
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; };
var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));
var _immutable = require("immutable");
var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/typeof"));
var Immutable = _interopRequireWildcard(_immutable);
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/array/is-array"));
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; } }
var _freeze = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/freeze"));
var Immutable = _interopRequireWildcard(require("immutable"));
var v4 = require("./v4");
var v3 = require("./v3");

@@ -31,9 +42,19 @@

exports.removeCell = removeCell;
exports.insertCellAfter = insertCellAfter;
exports.insertCellAt = insertCellAt;
exports.appendCellToNotebook = appendCellToNotebook;
exports.createCodeCell = createCodeCell;
exports.monocellNotebook = monocellNotebook;
exports.appendCell = appendCell;
exports.emptyMarkdownCell = emptyMarkdownCell;
exports.emptyCodeCell = emptyCodeCell;
exports.emptyNotebook = emptyNotebook;
function freezeReviver(k, v) {
return Object.freeze(v);
}
// Expected usage of below is
return (0, _freeze.default)(v);
} // Expected usage of below is
// fromJS(parseNotebook(string|buffer))
function parseNotebook(notebookString) {

@@ -47,13 +68,15 @@ return JSON.parse(notebookString, freezeReviver);

var immNotebook = notebook;
if (immNotebook.has("cellOrder") && immNotebook.has("cellMap")) {
return immNotebook;
}
throw new TypeError("commutable was passed an Immutable.Map structure that is not a notebook");
}
} // $FlowFixMe: isMap doesn't hint to flow that this is a plain Object
// $FlowFixMe: isMap doesn't hint to flow that this is a plain Object
var notebookJSON = notebook;
if (notebookJSON.nbformat === 4 && notebookJSON.nbformat_minor >= 0) {
if (Array.isArray(notebookJSON.cells) && _typeof(notebookJSON.metadata) === "object") {
if ((0, _isArray.default)(notebookJSON.cells) && (0, _typeof2.default)(notebookJSON.metadata) === "object") {
return v4.fromJS(notebookJSON);

@@ -66,3 +89,3 @@ }

if (notebookJSON.nbformat) {
throw new TypeError("nbformat v" + notebookJSON.nbformat + "." + notebookJSON.nbformat_minor + " not recognized");
throw new TypeError("nbformat v".concat(notebookJSON.nbformat, ".").concat(notebookJSON.nbformat_minor, " not recognized"));
}

@@ -79,28 +102,14 @@

}
throw new TypeError("Only notebook formats 3 and 4 are supported!");
}
} // Expected usage is stringifyNotebook(toJS(immutableNotebook))
// Expected usage is stringifyNotebook(toJS(immutableNotebook))
function stringifyNotebook(notebook) {
return JSON.stringify(notebook, null, 2);
return (0, _stringify.default)(notebook, null, 2);
}
var createImmutableOutput = v4.createImmutableOutput;
exports.createImmutableOutput = createImmutableOutput;
var createImmutableMimeBundle = v4.createImmutableMimeBundle;
exports.emptyCodeCell = emptyCodeCell;
exports.emptyMarkdownCell = emptyMarkdownCell;
exports.emptyNotebook = emptyNotebook;
exports.monocellNotebook = monocellNotebook;
exports.toJS = toJS;
exports.fromJS = fromJS;
exports.createCodeCell = createCodeCell;
exports.parseNotebook = parseNotebook;
exports.stringifyNotebook = stringifyNotebook;
exports.insertCellAt = insertCellAt;
exports.insertCellAfter = insertCellAfter;
exports.removeCell = removeCell;
exports.appendCell = appendCell;
exports.appendCellToNotebook = appendCellToNotebook;
exports.createImmutableOutput = createImmutableOutput;
exports.createImmutableMimeBundle = createImmutableMimeBundle;
"use strict";
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -14,12 +16,12 @@ value: true

exports.removeCell = removeCell;
exports.monocellNotebook = exports.emptyNotebook = exports.defaultNotebook = exports.emptyMarkdownCell = exports.emptyCodeCell = void 0;
var _freeze = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/freeze"));
var uuidv4 = require("uuid").v4;
var Immutable = require("immutable");
var Immutable = require("immutable"); // We're hardset to nbformat v4.4 for what we use in-memory
// We're hardset to nbformat v4.4 for what we use in-memory
var defaultCodeCell = Object.freeze({
var defaultCodeCell = (0, _freeze.default)({
cell_type: "code",

@@ -35,4 +37,3 @@ execution_count: null,

});
var defaultMarkdownCell = Object.freeze({
var defaultMarkdownCell = (0, _freeze.default)({
cell_type: "markdown",

@@ -45,3 +46,2 @@ metadata: Immutable.Map(),

var cell = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultCodeCell;
// $FlowFixMe: Immutable

@@ -53,3 +53,2 @@ return Immutable.Map(cell);

var cell = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultMarkdownCell;
// $FlowFixMe: Immutable

@@ -59,6 +58,7 @@ return Immutable.Map(cell);

var emptyCodeCell = exports.emptyCodeCell = createCodeCell();
var emptyMarkdownCell = exports.emptyMarkdownCell = createMarkdownCell();
var defaultNotebook = exports.defaultNotebook = Object.freeze({
var emptyCodeCell = createCodeCell();
exports.emptyCodeCell = emptyCodeCell;
var emptyMarkdownCell = createMarkdownCell();
exports.emptyMarkdownCell = emptyMarkdownCell;
var defaultNotebook = (0, _freeze.default)({
nbformat: 4,

@@ -70,6 +70,6 @@ nbformat_minor: 4,

});
exports.defaultNotebook = defaultNotebook;
function createNotebook() {
var notebook = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultNotebook;
// $FlowFixMe: Immutable

@@ -79,3 +79,4 @@ return Immutable.Map(notebook);

var emptyNotebook = exports.emptyNotebook = createNotebook();
var emptyNotebook = createNotebook();
exports.emptyNotebook = emptyNotebook;

@@ -85,3 +86,2 @@ // Intended to make it easy to use this with (temporary mutable cellOrder + cellMap)

var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : uuidv4();
return {

@@ -112,4 +112,3 @@ cellOrder: cellStructure.cellOrder.push(id),

return notebook.withMutations(function (nb) {
return nb.setIn(["cellMap", cellID], cell)
// $FlowFixMe: Fixed by making ImmutableNotebook a typed record.
return nb.setIn(["cellMap", cellID], cell) // $FlowFixMe: Fixed by making ImmutableNotebook a typed record.
.set("cellOrder", nb.get("cellOrder").insert(index, cellID));

@@ -120,4 +119,3 @@ });

function insertCellAfter(notebook, cell, cellID, priorCellID) {
return insertCellAt(notebook, cell, cellID,
// $FlowFixMe: Fixed by making ImmutableNotebook a typed record.
return insertCellAt(notebook, cell, cellID, // $FlowFixMe: Fixed by making ImmutableNotebook a typed record.
notebook.get("cellOrder").indexOf(priorCellID) + 1);

@@ -134,2 +132,3 @@ }

var monocellNotebook = exports.monocellNotebook = appendCellToNotebook(emptyNotebook, emptyCodeCell);
var monocellNotebook = appendCellToNotebook(emptyNotebook, emptyCodeCell);
exports.monocellNotebook = monocellNotebook;
"use strict";
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -8,2 +10,8 @@ value: true

var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));
var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/array/is-array"));
var _v = require("./v4");

@@ -13,8 +21,10 @@

var Immutable = require("immutable");
var appendCell = require("./structures").appendCell;
function demultiline(s) {
if (Array.isArray(s)) {
if ((0, _isArray.default)(s)) {
return s.join("");
}
return s;

@@ -48,3 +58,3 @@ }

var mimeBundle = {};
Object.keys(output).map(function (key) {
(0, _keys.default)(output).map(function (key) {
if (key !== "prompt_number" || key !== "metadata" || key !== "output_type") {

@@ -54,3 +64,3 @@ mimeBundle[VALID_MIMETYPES[key]] = output[key];

});
return Object.keys(mimeBundle).reduce(_v.cleanMimeAtKey.bind(null, mimeBundle), Immutable.Map());
return (0, _keys.default)(mimeBundle).reduce(_v.cleanMimeAtKey.bind(null, mimeBundle), Immutable.Map());
}

@@ -60,4 +70,7 @@

if (o.metadata) {
return { metadata: Immutable.fromJS(o.metadata) };
return {
metadata: Immutable.fromJS(o.metadata)
};
}
return {};

@@ -69,3 +82,3 @@ }

case "pyout":
return Immutable.Map(Object.assign({}, {
return Immutable.Map((0, _assign.default)({}, {
output_type: output.output_type,

@@ -75,7 +88,9 @@ execution_count: output.prompt_number,

}, sanitize(output)));
case "display_data":
return Immutable.Map(Object.assign({}, {
return Immutable.Map((0, _assign.default)({}, {
output_type: output.output_type,
data: createImmutableMimeBundle(output)
}, sanitize(output)));
case "stream":

@@ -87,2 +102,3 @@ return Immutable.Map({

});
case "pyerr":

@@ -95,4 +111,5 @@ return Immutable.Map({

});
default:
throw new TypeError("Output type " + output.output_type + " not recognized");
throw new TypeError("Output type ".concat(output.output_type, " not recognized"));
}

@@ -126,3 +143,3 @@ }

cell_type: "markdown",
source: Array.isArray(cell.source) ? demultiline(cell.source.map(function (line) {
source: (0, _isArray.default)(cell.source) ? demultiline(cell.source.map(function (line) {
return Array(cell.level).join("#").concat(" ").concat(line);

@@ -138,10 +155,14 @@ })) : cell.source,

return createImmutableMarkdownCell(cell);
case "code":
return createImmutableCodeCell(cell);
case "raw":
return createImmutableRawCell(cell);
case "heading":
return createImmutableHeadingCell(cell);
default:
throw new TypeError("Cell type " + cell.cell_type + " unknown");
throw new TypeError("Cell type ".concat(cell.cell_type, " unknown"));
}

@@ -152,3 +173,3 @@ }

if (notebook.nbformat !== 3 || notebook.nbformat_minor < 0) {
throw new TypeError("Notebook is not a valid v3 notebook. v3 notebooks must be of form 3.x\n It lists nbformat v" + notebook.nbformat + "." + notebook.nbformat_minor);
throw new TypeError("Notebook is not a valid v3 notebook. v3 notebooks must be of form 3.x\n It lists nbformat v".concat(notebook.nbformat, ".").concat(notebook.nbformat_minor));
}

@@ -160,3 +181,2 @@

};
var cellStructure = [].concat.apply([], notebook.worksheets.map(function (worksheet) {

@@ -167,3 +187,2 @@ return worksheet.cells.reduce(function (cellStruct, cell) {

}))[0];
return Immutable.Map({

@@ -170,0 +189,0 @@ cellOrder: cellStructure.cellOrder.asImmutable(),

"use strict";
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -13,3 +15,8 @@ value: true

var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));
var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/array/is-array"));
/*

@@ -30,47 +37,25 @@ * Functions in this module are provided for converting from Jupyter Notebook

*/
var Immutable = require("immutable");
var Immutable = require("immutable");
var appendCell = require("./structures").appendCell;
//
// MimeBundle example (disk format)
//
// {
// "application/json": {"a": 3, "b": 2},
// "text/html": ["<p>\n", "Hey\n", "</p>"],
// "text/plain": "Hey"
// }
//
// On disk multi-line strings are used to accomodate line-by-line diffs in tools
// like git and GitHub. They get converted to strings for the in-memory format.
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Output Types
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Cell Types
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function demultiline(s) {
if (Array.isArray(s)) {
if ((0, _isArray.default)(s)) {
return s.join("");
}
return s;
}
/**
* Split string into a list of strings delimited by newlines
*/
function remultiline(s) {
if (Array.isArray(s)) {
if ((0, _isArray.default)(s)) {
// Assume
return s;
}
// Use positive lookahead regex to split on newline and retain newline char
} // Use positive lookahead regex to split on newline and retain newline char
return s.split(/(.+?(?:\r\n|\n))/g).filter(function (x) {

@@ -82,4 +67,3 @@ return x !== "";

function isJSONKey(key) {
return (/^application\/(.*\+)?json$/.test(key)
);
return /^application\/(.*\+)?json$/.test(key);
}

@@ -94,7 +78,7 @@

if (typeof data === "string" || Array.isArray(data)) {
if (typeof data === "string" || (0, _isArray.default)(data)) {
return demultiline(data);
}
throw new TypeError("Data for " + key + " is expected to be a string or an Array of strings");
throw new TypeError("Data for ".concat(key, " is expected to be a string or an Array of strings"));
}

@@ -123,3 +107,3 @@

//
return Object.keys(mimeBundle).reduce(cleanMimeAtKey.bind(null, mimeBundle), Immutable.Map());
return (0, _keys.default)(mimeBundle).reduce(cleanMimeAtKey.bind(null, mimeBundle), Immutable.Map());
}

@@ -129,4 +113,7 @@

if (o.metadata) {
return { metadata: Immutable.fromJS(o.metadata) };
return {
metadata: Immutable.fromJS(o.metadata)
};
}
return {};

@@ -138,3 +125,3 @@ }

case "execute_result":
return Immutable.Map(Object.assign({}, {
return Immutable.Map((0, _assign.default)({}, {
output_type: output.output_type,

@@ -144,7 +131,9 @@ execution_count: output.execution_count,

}, sanitize(output)));
case "display_data":
return Immutable.Map(Object.assign({}, {
return Immutable.Map((0, _assign.default)({}, {
output_type: output.output_type,
data: createImmutableMimeBundle(output.data)
}, sanitize(output)));
case "stream":

@@ -156,2 +145,3 @@ return Immutable.Map({

});
case "error":

@@ -166,4 +156,5 @@ return Immutable.Map({

});
default:
throw new TypeError("Output type " + output.output_type + " not recognized");
throw new TypeError("Output type ".concat(output.output_type, " not recognized"));
}

@@ -178,7 +169,7 @@ }

if (Array.isArray(v)) {
if ((0, _isArray.default)(v)) {
return Immutable.Set(v);
}
} // The notebook spec requires that this field is an Array of strings
// The notebook spec requires that this field is an Array of strings
return Immutable.Set();

@@ -222,8 +213,11 @@ });

return createImmutableMarkdownCell(cell);
case "code":
return createImmutableCodeCell(cell);
case "raw":
return createImmutableRawCell(cell);
default:
throw new TypeError("Cell type " + cell.cell_type + " unknown");
throw new TypeError("Cell type ".concat(cell.cell_type, " unknown"));
}

@@ -234,7 +228,7 @@ }

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\n 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".concat(notebook.nbformat, ".").concat(notebook.nbformat_minor));
} // Since we're doing N cell operations all at once, switch to mutable then
// switch back after.
// Since we're doing N cell operations all at once, switch to mutable then
// switch back after.
var starterCellStructure = {

@@ -244,7 +238,5 @@ cellOrder: Immutable.List().asMutable(),

};
var cellStructure = notebook.cells.reduce(function (cellStruct, cell) {
return appendCell(cellStruct, createImmutableCell(cell));
}, starterCellStructure);
return Immutable.Map({

@@ -274,4 +266,3 @@ cellOrder: cellStructure.cellOrder.asImmutable(),

var bundle = immMimeBundle.toObject();
Object.keys(bundle).map(function (key) {
(0, _keys.default)(bundle).map(function (key) {
if (isJSONKey(key)) {

@@ -281,2 +272,3 @@ if (Immutable.Map.isMap(bundle[key])) {

}
return bundle;

@@ -287,9 +279,9 @@ }

if (typeof data === "string" || Array.isArray(data)) {
if (typeof data === "string" || (0, _isArray.default)(data)) {
bundle[key] = remultiline(data);
return bundle;
}
throw new TypeError("Data for " + key + " is expected to be a string or an Array of strings");
throw new TypeError("Data for ".concat(key, " is expected to be a string or an Array of strings"));
});
return bundle;

@@ -310,2 +302,3 @@ }

};
case "display_data":

@@ -317,2 +310,3 @@ return {

};
case "stream":

@@ -324,2 +318,3 @@ return {

};
case "error":

@@ -330,4 +325,5 @@ // Note: this is one of the cases where the Array of strings (for traceback)

return immOutput.toJS();
default:
throw new TypeError("Output type " + output.output_type + " not recognized");
throw new TypeError("Output type ".concat(output.output_type, " not recognized"));
}

@@ -339,3 +335,2 @@ }

var cell = immCell.toObject();
return {

@@ -353,3 +348,2 @@ cell_type: "code",

var cell = immCell.toObject();
return {

@@ -365,11 +359,15 @@ cell_type: "raw",

var cellType = immCell.get("cell_type");
switch (cellType) {
case "markdown":
return markdownCellToJS(immCell);
case "code":
return codeCellToJS(immCell);
case "raw":
return rawCellToJS(immCell);
default:
throw new TypeError("Cell type " + cellType + " unknown");
throw new TypeError("Cell type ".concat(cellType, " unknown"));
}

@@ -381,10 +379,7 @@ }

var plainNotebook = immnb.toObject();
var plainCellOrder = plainNotebook.cellOrder.toArray();
var plainCellMap = plainNotebook.cellMap.toObject();
var cells = plainCellOrder.map(function (cellID) {
return cellToJS(plainCellMap[cellID]);
});
return {

@@ -391,0 +386,0 @@ cells: cells,

{
"name": "@nteract/commutable",
"version": "4.1.1",
"version": "4.1.2",
"description": "library for immutable notebook operations",

@@ -13,3 +13,3 @@ "main": "lib/index.js",

"build:flow": "flow-copy-source -v -i '**/__tests__/**' src lib",
"build:lib": "babel -d lib src --ignore '**/__tests__/**'",
"build:lib": "babel -d lib src --ignore '**/__tests__/**' --config-file ../../babel.config.js",
"build:lib:watch": "npm run build:lib -- --watch",

@@ -33,3 +33,4 @@ "build:watch": "npm run build:clean && npm run build:lib:watch && npm run build:flow"

"uuid": "^3.1.0"
}
},
"gitHead": "d520489d19db4d06db5a00b17c06f751663d3be3"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc