datagrok-api
Advanced tools
Comparing version 0.1.40 to 0.1.41
@@ -18,3 +18,3 @@ import * as _chem from './src/chem.js'; | ||
export * from './src/wrappers_impl'; | ||
export {time} from './src/utils.js'; | ||
export {time, LruCache} from './src/utils.js'; | ||
export {JsEntityMeta} from './ui'; | ||
@@ -21,0 +21,0 @@ |
{ | ||
"name": "datagrok-api", | ||
"version": "0.1.40", | ||
"version": "0.1.41", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -25,3 +25,3 @@ /** | ||
* sorted in descending order by the score | ||
* @returns {Promise<DataFrame>} | ||
* @returns {Promise<DataFrame>, if sorted; Promise<Column>, otherwise} | ||
* */ | ||
@@ -37,3 +37,4 @@ export async function similarityScoring(column, pattern, settings = { sorted: false }) { | ||
await call.call(); | ||
return call.getParamValue('result'); | ||
let result = call.getParamValue('result'); | ||
return settings.sorted ? result : result.columns.byIndex(0); | ||
@@ -63,3 +64,3 @@ } | ||
* */ | ||
export async function substructureSearch(column, pattern, settings = {}) { | ||
export async function substructureSearch(column, pattern, settings = null) { | ||
@@ -69,3 +70,6 @@ let foo = await grok.functions.eval('Chem:substructureSearch'); | ||
'molStringsColumn': column, | ||
'molString': pattern | ||
'molString': pattern, | ||
'substructLibrary': | ||
(settings && settings.hasOwnProperty('substructLibrary') && settings.substructLibrary === false) ? | ||
false : true | ||
}); | ||
@@ -72,0 +76,0 @@ await call.call(); |
@@ -68,3 +68,3 @@ import {DataFrame} from "./dataframe"; | ||
type DemoDatasetName = 'wells' | 'demog' | 'biosensor' | 'random walk'; | ||
type DemoDatasetName = 'wells' | 'demog' | 'biosensor' | 'random walk' | 'geo'; | ||
@@ -81,2 +81,3 @@ /** Provides convenient access to demo datasets. */ | ||
* "random walk": random walk data for the specified number of dimensions | ||
* "geo": geographic coordinates given as latitude/longitude pairs: lat, lng, value | ||
* | ||
@@ -124,2 +125,3 @@ * @returns {DataFrame} */ | ||
* "random walk": random walk data for the specified number of dimensions | ||
* "geo": geographic coordinates given as latitude/longitude pairs: lat, lng, value | ||
* | ||
@@ -135,3 +137,3 @@ * @returns {DataFrame} */ | ||
* */ | ||
parseCsv(csv: string): DataFrame | ||
parseCsv(csv: string, options: object): DataFrame | ||
@@ -138,0 +140,0 @@ /** |
@@ -30,2 +30,3 @@ import {DataFrame} from "./dataframe"; | ||
* "random walk": random walk data for the specified number of dimensions | ||
* "geo": geographic coordinates given as latitude/longitude pairs: lat, lng, value | ||
* | ||
@@ -82,2 +83,3 @@ * @returns {DataFrame} */ | ||
* "random walk": random walk data for the specified number of dimensions | ||
* "geo": geographic coordinates given as latitude/longitude pairs: lat, lng, value | ||
* | ||
@@ -90,10 +92,19 @@ * @returns {DataFrame} */ | ||
getDemoTable(path) { | ||
return new Promise((resolve, reject) => grok_GetDemoTable(path, (t) => resolve(toJs(t)))); | ||
return new Promise((resolve, reject) => grok_GetDemoTable(path, (t) => resolve(toJs(t)), (e) => reject(e))); | ||
} | ||
/** | ||
* @typedef {Object} CsvImportOptions | ||
* @property {string} delimiter | ||
* @property {string} decimalSeparator | ||
* @property {Object} thousandSeparator | ||
**/ | ||
/** | ||
* Parses the CSV string. | ||
* @param {string} csv - The content of the comma-separated values file. | ||
* @param {CsvImportOptions} options | ||
* @returns {DataFrame} | ||
* */ | ||
parseCsv(csv) { return new DataFrame(grok_ParseCsv(csv)); } | ||
parseCsv(csv, options) { return new DataFrame(grok_ParseCsv(csv, options)); } | ||
@@ -106,3 +117,3 @@ /** | ||
loadTable(csvUrl) { | ||
return new Promise((resolve, reject) => grok_LoadDataFrame(csvUrl, (t) => resolve(toJs(t, false)))); | ||
return new Promise((resolve, reject) => grok_LoadDataFrame(csvUrl, (t) => resolve(toJs(t, false)), (e) => reject(e))); | ||
} | ||
@@ -153,14 +164,14 @@ | ||
*/ | ||
openTable(id) { return new Promise((resolve, reject) => grok_OpenTable(id, (t) => resolve(new DataFrame(t)))); } | ||
openTable(id) { return new Promise((resolve, reject) => grok_OpenTable(id, (t) => resolve(new DataFrame(t)), (e) => reject(e))); } | ||
query(queryName, queryParameters = null, adHoc = false, pollingInterval = 1000) { | ||
return new Promise((resolve, reject) => grok_Query(queryName, queryParameters, adHoc, pollingInterval, (t) => resolve(toJs(t)))); | ||
return new Promise((resolve, reject) => grok_Query(queryName, queryParameters, adHoc, pollingInterval, (t) => resolve(toJs(t)), (e) => reject(e))); | ||
} | ||
callQuery(queryName, queryParameters = null, adHoc = false, pollingInterval = 1000) { | ||
return new Promise((resolve, reject) => grok_CallQuery(queryName, queryParameters, adHoc, pollingInterval, (c) => resolve(new FuncCall(c)))); | ||
return new Promise((resolve, reject) => grok_CallQuery(queryName, queryParameters, adHoc, pollingInterval, (c) => resolve(new FuncCall(c)), (e) => reject(e))); | ||
} | ||
detectSemanticTypes(t) { return new Promise((resolve, reject) => grok_DetectSemanticTypes(t.d, (_) => resolve())); } | ||
detectSemanticTypes(t) { return new Promise((resolve, reject) => grok_DetectSemanticTypes(t.d, (_) => resolve(), (e) => reject(e))); } | ||
} |
@@ -35,3 +35,3 @@ import {Observable} from 'rxjs'; | ||
/** List of columns. | ||
/** List of rows. | ||
* @returns {RowList} */ | ||
@@ -38,0 +38,0 @@ get rows(): RowList |
@@ -58,4 +58,5 @@ import * as rxjs from 'rxjs'; | ||
* @param {string} csv - The content of the comma-separated values file. | ||
* @param {CsvImportOptions} options | ||
* @returns {DataFrame} */ | ||
static fromCsv(csv) { return grok.data.parseCsv(csv); } | ||
static fromCsv(csv, options) { return grok.data.parseCsv(csv, options); } | ||
@@ -434,8 +435,19 @@ /** Constructs {@link DataFrame} from the specified JSON string. | ||
// getDateTime(i) | ||
// | ||
// /** Returns i-th value as integer. Works for IntColumns only. | ||
// * @param {number} i | ||
// * @returns {number} */ | ||
// getString(i); | ||
/** Returns i-th value as string, taking into account value format defined for the column. | ||
* An empty string is returned if there is no value. | ||
* @param {number} i | ||
* @returns {string} */ | ||
getString(i) { return grok_Column_GetString(this.d, i); } | ||
/** Attempts to set i-th value by converting a provided string to the corresponding strongly-typed value. | ||
* Returns true if text was successfully parsed and set, otherwise false. | ||
* Examples: dateColumn.setString('April 1, 2020'); | ||
* intColumn.setString('42'); | ||
* @param {number} i | ||
* @param {string} str | ||
* @param {boolean} notify | ||
* @returns {boolean} */ | ||
setString(i, str, notify = true) { grok_Column_SetString(this.d, i, str, notify); } | ||
/** | ||
@@ -461,4 +473,5 @@ * Sets [i]-th value to [x] | ||
* @param {string} tag - Key. | ||
* @param {string} value - Value. */ | ||
setTag(tag, value) { grok_Column_Set_Tag(this.d, tag, value); } | ||
* @param {string} value - Value. | ||
* @returns {Column}. * */ | ||
setTag(tag, value) { grok_Column_Set_Tag(this.d, tag, value); return this; } | ||
@@ -465,0 +478,0 @@ /** Compacts the internal column representation. |
@@ -0,1 +1,4 @@ | ||
import {toJs} from "./wrappers"; | ||
import {_toIterable} from "./utils"; | ||
/** | ||
@@ -32,2 +35,8 @@ * @typedef {string} DockType | ||
removeChild(childNode) { return grok_DockNode_RemoveChild(this.d, childNode); } | ||
/** @returns {DockNode} */ | ||
get parent() { return toJs(grok_DockNode_Parent(this.d)); } | ||
/** @returns {Iterable.<DockNode>} */ | ||
get children() { return _toIterable(grok_DockNode_Children(this.d)); } | ||
} | ||
@@ -76,3 +85,3 @@ | ||
get rootNode() { return new DockNode(grok_DockManager_Get_RootNode(this.d)); } | ||
get rootNode() { return toJs(grok_DockManager_Get_RootNode(this.d)); } | ||
@@ -88,3 +97,3 @@ /** | ||
* Docks the element relative to the reference node. | ||
* @param {HTMLElement} element - Element to dock | ||
* @param {HTMLElement | Viewer} element - Element to dock | ||
* @param {DockType} dockType - Dock type (left | right | top | bottom | fill). | ||
@@ -102,6 +111,18 @@ * @param {DockNode|null} refNode - reference node | ||
* Undocks the element. | ||
* @param {HTMLElement} element - Element to undock | ||
* @param {HTMLElement | DockNode} object - Element to undock | ||
* */ | ||
undock(element) { grok_DockManager_Undock(this.d, element); } | ||
close(object) { | ||
if (object.d === undefined) | ||
grok_DockManager_UndockByElement(this.d, object); | ||
else | ||
grok_DockManager_UndockNode(this.d, object.d); | ||
} | ||
/** | ||
* Finds node of the element. | ||
* @param {HTMLElement} element - Element to find | ||
* @returns {DockNode} | ||
* */ | ||
findNode(element) { return toJs(grok_DockManager_FindNode(this.d, element)); } | ||
// /** | ||
@@ -108,0 +129,0 @@ // * Docks the element relative to the reference node. |
@@ -354,3 +354,3 @@ import {SEMTYPE, TYPE} from "./const"; | ||
/** Property getter is a function that acccepts one parameter (item) | ||
/** Property getter is a function that accepts one parameter (item) | ||
* and returns the property value. */ | ||
@@ -357,0 +357,0 @@ get get(): PropertyGetter |
@@ -29,3 +29,3 @@ import {TYPE} from "./const"; | ||
getProperties() { | ||
return new Promise((resolve, reject) => grok_EntitiesDataSource_GetProperties(grok.dapi.entities.s, this.d, (p) => resolve(p))); | ||
return new Promise((resolve, reject) => grok_EntitiesDataSource_GetProperties(grok.dapi.entities.s, this.d, (p) => resolve(p), (e) => reject(e))); | ||
} | ||
@@ -37,3 +37,3 @@ | ||
setProperties(props) { | ||
return new Promise((resolve, reject) => grok_EntitiesDataSource_SetProperties(grok.dapi.entities.s, this.d, props, (_) => resolve(_))); | ||
return new Promise((resolve, reject) => grok_EntitiesDataSource_SetProperties(grok.dapi.entities.s, this.d, props, (_) => resolve(_), (e) => reject(e))); | ||
} | ||
@@ -201,3 +201,3 @@ | ||
* @returns {Promise<string>} Current notebook's name */ | ||
edit() { return new Promise((resolve, reject) => grok_Notebook_Edit(this.d, (f) => resolve(f))); } | ||
edit() { return new Promise((resolve, reject) => grok_Notebook_Edit(this.d, (f) => resolve(f), (e) => reject(e))); } | ||
@@ -216,3 +216,3 @@ /** Environment name | ||
* @returns {Promise<string>} */ | ||
toHtml() { return new Promise((resolve, reject) => grok_Notebook_ToHtml(this.d, (html) => resolve(html))); } | ||
toHtml() { return new Promise((resolve, reject) => grok_Notebook_ToHtml(this.d, (html) => resolve(html), (e) => reject(e))); } | ||
} | ||
@@ -373,3 +373,3 @@ | ||
/** Setup environment */ | ||
setup() { return new Promise((resolve, reject) => grok_ScriptEnvironment_Setup(this.d, () => resolve())); } | ||
setup() { return new Promise((resolve, reject) => grok_ScriptEnvironment_Setup(this.d, () => resolve(), (e) => reject(e))); } | ||
} | ||
@@ -474,4 +474,12 @@ | ||
resolve(cred); | ||
})); | ||
}, (e) => reject(e))); | ||
} | ||
/** Returns properties for package | ||
* @returns {Promise<object>} */ | ||
getProperties() { | ||
return new Promise((resolve, reject) => grok_Package_Get_Properties(this.name, (c) => { | ||
resolve(c); | ||
}, (e) => reject(e))); | ||
} | ||
} | ||
@@ -478,0 +486,0 @@ |
@@ -97,2 +97,5 @@ import * as rxjs from "rxjs"; | ||
/** @returns {Observable} */ get onTooltipClosed () { return __obs('d4-tooltip-closed'); } | ||
/** @returns {Observable} */ get onViewerAdded () { return __obs('d4-viewer-added'); } | ||
/** @returns {Observable} */ get onViewerClosed () { return __obs('d4-viewer-closed'); } | ||
} | ||
@@ -99,0 +102,0 @@ |
@@ -148,2 +148,10 @@ import {Viewer} from "./viewer.js"; | ||
set selected(x) { return grok_GridColumn_Set_Selected(this.d, x); } | ||
/** Column position from the left side. | ||
* @returns {number} */ | ||
get left() { return grok_GridColumn_Get_Left(this.d); } | ||
/** Column position from the right side. | ||
* @returns {number} */ | ||
get right() { return grok_GridColumn_Get_Right(this.d); } | ||
} | ||
@@ -160,3 +168,3 @@ | ||
/** Returns a grid column by name, or null if it does not exist. | ||
/** Returns a grid column by index, or null if it does not exist. | ||
* @param {number} index | ||
@@ -178,2 +186,6 @@ * @returns {GridColumn} */ | ||
setVisible(columnNames) { grok_GridColumnList_SetVisible(this.d, columnNames); } | ||
/** GridColumnList length. | ||
* @returns {number} */ | ||
get length() { return grok_GridColumnList_Get_Length(this.d); } | ||
} | ||
@@ -180,0 +192,0 @@ |
@@ -86,2 +86,6 @@ import {DataFrame} from "./dataframe"; | ||
/** Shows warning message (red background) | ||
* @param {string} s - message */ | ||
warning(s) { grok_Balloon(s, 'warning'); } | ||
/** Docks element in a separate window. | ||
@@ -88,0 +92,0 @@ * Sample: {@link https://public.datagrok.ai/js/samples/ui/docking/docking} |
138
src/utils.js
@@ -93,2 +93,140 @@ import {Balloon} from "./widgets"; | ||
return values; | ||
} | ||
/** | ||
* Inspired by https://github.com/Yomguithereal/mnemonist/blob/master/lru-cache.js | ||
* */ | ||
export class LruCache { | ||
constructor() { | ||
this.capacity = 100; | ||
this.forward = new Uint16Array(this.capacity); | ||
this.backward = new Uint16Array(this.capacity); | ||
this.V = new Array(this.capacity); | ||
this.K = new Array(this.capacity); | ||
this.size = 0; | ||
this.head = 0; | ||
this.tail = 0; | ||
this.items = {}; | ||
this.onItemEvicted = null; | ||
} | ||
/** | ||
* Splays a value on top. | ||
* @param {number} pointer - Pointer of the value to splay on top. | ||
* @return {LruCache} | ||
*/ | ||
splayOnTop(pointer) { | ||
let oldHead = this.head; | ||
if (this.head === pointer) | ||
return this; | ||
let previous = this.backward[pointer]; | ||
let next = this.forward[pointer]; | ||
if (this.tail === pointer) | ||
this.tail = previous; | ||
else | ||
this.backward[next] = previous; | ||
this.forward[previous] = next; | ||
this.backward[oldHead] = pointer; | ||
this.head = pointer; | ||
this.forward[pointer] = oldHead; | ||
return this; | ||
}; | ||
/** | ||
* Checks whether the key exists in the cache. | ||
* | ||
* @param {any} key - Key. | ||
* @return {boolean} | ||
*/ | ||
has(key) { | ||
return key in this.items; | ||
}; | ||
/** | ||
* Sets the value for the given key in the cache. | ||
* | ||
* @param {any} key - Key. | ||
* @param {any} value - Value. | ||
* @return {undefined} | ||
*/ | ||
set(key, value) { | ||
// The key already exists, we just need to update the value and splay on top | ||
let pointer = this.items[key]; | ||
if (typeof pointer !== 'undefined') { | ||
this.splayOnTop(pointer); | ||
this.V[pointer] = value; | ||
return; | ||
} | ||
// The cache is not yet full | ||
if (this.size < this.capacity) { | ||
pointer = this.size++; | ||
} | ||
// Cache is full, we need to drop the last value | ||
else { | ||
pointer = this.tail; | ||
this.tail = this.backward[pointer]; | ||
if (this.onItemEvicted != null) | ||
this.onItemEvicted(this.V[pointer]); | ||
delete this.items[this.K[pointer]]; | ||
} | ||
// Storing key & value | ||
this.items[key] = pointer; | ||
this.K[pointer] = key; | ||
this.V[pointer] = value; | ||
// Moving the item at the front of the list | ||
this.forward[pointer] = this.head; | ||
this.backward[this.head] = pointer; | ||
this.head = pointer; | ||
}; | ||
/** | ||
* Gets the value attached to the given key, and makes it the most recently used item. | ||
* | ||
* @param {any} key - Key. | ||
* @return {any} | ||
*/ | ||
get(key) { | ||
let pointer = this.items[key]; | ||
if (typeof pointer === 'undefined') | ||
return; | ||
this.splayOnTop(pointer); | ||
return this.V[pointer]; | ||
}; | ||
/** | ||
* Returns the value with the specified key, if it already exists in the cache, | ||
* or creates a new one by calling the provided function. | ||
* | ||
* @param {any} key - Key. | ||
* @param {Function} createFromKey - Function to create a new item. | ||
* @return {any} | ||
*/ | ||
getOrCreate(key, createFromKey) { | ||
let value = this.get(key); | ||
if (typeof value !== 'undefined') | ||
return value; | ||
else { | ||
let item = createFromKey(key); | ||
this.set(key, item); | ||
return item; | ||
} | ||
} | ||
} |
@@ -68,3 +68,3 @@ /** A viewer that is typically docked inside a [TableView]. */ | ||
* @returns: {object} */ | ||
getOptions() { return grok_Viewer_Serialize(this.d); } | ||
getOptions() { return JSON.parse(grok_Viewer_Serialize(this.d)); } | ||
@@ -80,3 +80,3 @@ /** Closes and detaches the viewer. */ | ||
* @returns {string} */ | ||
get type() { return JSON.parse(args.args.context.getOptions()).type; } | ||
get type() { return this.getOptions().type; } | ||
@@ -83,0 +83,0 @@ get table() { return toJs(grok_Viewer_Get_DataFrame(this.d)); } |
@@ -536,2 +536,14 @@ import {toDart, toJs} from "./wrappers"; | ||
remove(item) { grok_HtmlTable_Remove(this.d, item); } | ||
} | ||
export class ColumnComboBox extends InputBase { | ||
/** @constructs {ColumnComboBox} */ | ||
constructor(d) { super(d); } | ||
/** Creates a column combo box with specified [dataframe] and [predicate] | ||
* @returns {ColumnComboBox} */ | ||
static create(dataframe, predicate) { return toJs(grok_ColumnComboBox(dataframe.d, (x) => predicate(toJs(x)))); } | ||
/** @type {boolean} */ | ||
get vertical() { return grok_ColumnComboBox_Get_Vertical(this.d); } | ||
} |
12
ui.js
@@ -176,3 +176,3 @@ /** | ||
if (typeof options === 'string') | ||
element.className += ` ${options}`; | ||
element.className += ` ${options.replace(/,/g,' ')}`; | ||
if (options.id != null) | ||
@@ -301,2 +301,10 @@ element.id = options.id; | ||
/** Shows popup with the [element] near the [anchor]. | ||
* tooltip, and popup menu. | ||
* @param {Element} element | ||
* @param {Element} anchor | ||
* @param {boolean} vertical | ||
* @returns {Element}. */ | ||
export function showPopup(element, anchor, vertical = false) { return grok_UI_ShowPopup(element, anchor, vertical); } | ||
/** | ||
@@ -356,3 +364,3 @@ * @param {string} value | ||
export function inputs(inputs) { return div(inputs.map((x) => x.root), 'pure-form,pure-form-aligned'); } | ||
export function inputs(inputs) { return div(inputs.map((x) => x.root), 'pure-form pure-form-aligned'); } | ||
@@ -359,0 +367,0 @@ /** Creates new nodes tree |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
360983
8373
1