UNPKG

@chartshq/datamodel

Version:

An environment with `node`, `rustup` and `wasm-pack` set up.

1 lines 2.96 kB
var __extends=this&&this.__extends||function(){var t=function(e,a){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var a in e)e.hasOwnProperty(a)&&(t[a]=e[a])})(e,a)};return function(e,a){function i(){this.constructor=e}t(e,a),e.prototype=null===a?Object.create(a):(i.prototype=a.prototype,new i)}}(),__read=this&&this.__read||function(t,e){var a="function"==typeof Symbol&&t[Symbol.iterator];if(!a)return t;var i,r,n=a.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(i=n.next()).done;)o.push(i.value)}catch(t){r={error:t}}finally{try{i&&!i.done&&(a=n.return)&&a.call(n)}finally{if(r)throw r.error}}return o},__spread=this&&this.__spread||function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(__read(arguments[e]));return t};import{AbstractWasmField}from"./field";import{sanitizeStrings,saveIndecesToMemory,getIndecesFromMemory,getWasmSchema}from"./utils";import DataModel from"../../../main";import Invalid from"../../../data/invalid/invalid";var Categorical=function(t){function e(e,a){var i=t.call(this,e)||this;return i._cachedData=[],i._cachedFormattedData=[],i._cachedDomain=[],i._wasmField=a,i._uniqueStrings=[],i}return __extends(e,t),e.prototype._setMeta=function(t){this._uniqueStrings=t},e.prototype._getMeta=function(){return this._uniqueStrings},e.prototype.domain=function(){var t=this;if(!this._cachedDomain.length&&this._wasmField){var e=[],a=this._wasmField.get_domain(),i=getIndecesFromMemory(a.data_ptr,a.data_len);a.free(),i.forEach(function(a){-1===a?e.push(DataModel.defaultInvalidValue()):e.push(t._uniqueStrings[a])}),this._cachedDomain=__spread(e)}return this._cachedDomain},e.prototype.data=function(){var t=this;if(0===this._cachedData.length&&this._wasmField){var e=getIndecesFromMemory(this._wasmField.get_data_info(),this._wasmField.get_rows_count()).map(function(e){var a=t._uniqueStrings[t._wasmField.get_value_at_index(e)];return void 0===a?new Invalid(DataModel.defaultInvalidValue()):a});this._cachedData=e}return this._cachedData},e.prototype.formattedData=function(t){if(0===this._cachedFormattedData.length||t){var e=this.data(),a=t||this._schema.format,i=[];this._cachedFormattedData=e,a instanceof Function&&(e.forEach(function(t){var e;e=t instanceof Invalid?t:a(t),i.push(e)}),this._cachedFormattedData=i)}return this._cachedFormattedData},e.prototype.getRowsCount=function(){return this._wasmField?this._wasmField.get_rows_count():0},e.prototype.dispose=function(){this._cachedDomain=[],this._cachedData=[],this._cachedFormattedData=[],this._wasmField&&this._wasmField.free()},e}(AbstractWasmField);export default Categorical;export var categoricalFieldCreator=function(t,e,a){var i=sanitizeStrings(t),r=i.uniqueStrings,n=i.indices,o=void 0,s=Number.MAX_VALUE;if(a){var c=a.add_field(getWasmSchema(e),r),d=(o=a.get_categorical_field(c)).get_data_indices();saveIndecesToMemory(d,n)}var _=new Categorical(e,o);return _._setMeta(r),{field:_,index:s}};