@visactor/vgrammar-util
Advanced tools
Comparing version 0.3.0 to 0.3.1
import type { ReturnNumberFunction } from './types'; | ||
export declare const extent: (array: number[], func?: ReturnNumberFunction) => number[]; | ||
export declare const extent: (array: any[], func?: ReturnNumberFunction) => number[]; |
@@ -14,3 +14,3 @@ "use strict"; | ||
const value = valueGetter(array[i]); | ||
(0, vutils_1.isNil)(value) || Number.isNaN(value) || ((0, vutils_1.isNil)(min) ? (min = value, | ||
(0, vutils_1.isNumber)(value) && !Number.isNaN(value) && ((0, vutils_1.isNil)(min) ? (min = value, | ||
max = value) : (min = Math.min(min, value), max = Math.max(max, value))); | ||
@@ -17,0 +17,0 @@ } |
@@ -5,3 +5,2 @@ export { accessor, accessorName, accessorFields } from './accessor'; | ||
export { Logger, setLogLevel, setLogger, getLogger, clearLogger } from './logger'; | ||
export { mergeConfig, writeConfig } from './mergeConfig'; | ||
export { compare, ascending } from './compare'; | ||
@@ -14,4 +13,3 @@ export { error } from './error'; | ||
export { toPercent } from './toPercent'; | ||
export * as vGrammarConstants from './constants'; | ||
export * from './types'; | ||
export { regressionLinear } from './regression-linear'; |
@@ -14,15 +14,3 @@ "use strict"; | ||
void 0 === k2 && (k2 = k), o[k2] = m[k]; | ||
}), __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function(o, v) { | ||
Object.defineProperty(o, "default", { | ||
enumerable: !0, | ||
value: v | ||
}); | ||
} : function(o, v) { | ||
o.default = v; | ||
}), __importStar = this && this.__importStar || function(mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (null != mod) for (var k in mod) "default" !== k && Object.prototype.hasOwnProperty.call(mod, k) && __createBinding(result, mod, k); | ||
return __setModuleDefault(result, mod), result; | ||
}, __exportStar = this && this.__exportStar || function(m, exports) { | ||
}), __exportStar = this && this.__exportStar || function(m, exports) { | ||
for (var p in m) "default" === p || Object.prototype.hasOwnProperty.call(exports, p) || __createBinding(exports, m, p); | ||
@@ -33,3 +21,3 @@ }; | ||
value: !0 | ||
}), exports.regressionLinear = exports.vGrammarConstants = exports.toPercent = exports.splitAccessPath = exports.isEqual = exports.field = exports.extent = exports.error = exports.ascending = exports.compare = exports.writeConfig = exports.mergeConfig = exports.clearLogger = exports.getLogger = exports.setLogger = exports.setLogLevel = exports.Logger = exports.emptyObject = exports.falsy = exports.truthy = exports.one = exports.zero = exports.identity = exports.id = exports.getter = exports.accessorFields = exports.accessorName = exports.accessor = void 0; | ||
}), exports.regressionLinear = exports.toPercent = exports.splitAccessPath = exports.isEqual = exports.field = exports.extent = exports.error = exports.ascending = exports.compare = exports.clearLogger = exports.getLogger = exports.setLogger = exports.setLogLevel = exports.Logger = exports.emptyObject = exports.falsy = exports.truthy = exports.one = exports.zero = exports.identity = exports.id = exports.getter = exports.accessorFields = exports.accessorName = exports.accessor = void 0; | ||
@@ -132,16 +120,2 @@ var accessor_1 = require("./accessor"); | ||
var mergeConfig_1 = require("./mergeConfig"); | ||
Object.defineProperty(exports, "mergeConfig", { | ||
enumerable: !0, | ||
get: function() { | ||
return mergeConfig_1.mergeConfig; | ||
} | ||
}), Object.defineProperty(exports, "writeConfig", { | ||
enumerable: !0, | ||
get: function() { | ||
return mergeConfig_1.writeConfig; | ||
} | ||
}); | ||
var compare_1 = require("./compare"); | ||
@@ -213,3 +187,3 @@ | ||
} | ||
}), exports.vGrammarConstants = __importStar(require("./constants")), __exportStar(require("./types"), exports); | ||
}), __exportStar(require("./types"), exports); | ||
@@ -216,0 +190,0 @@ var regression_linear_1 = require("./regression-linear"); |
@@ -24,5 +24,5 @@ import type { ILogger } from './types'; | ||
} | ||
export declare const setLogger: (logger: Logger) => void; | ||
export declare const setLogger: (logger: ILogger) => void; | ||
export declare const setLogLevel: (level: number) => void; | ||
export declare const clearLogger: () => void; | ||
export declare const getLogger: (level?: number, method?: string) => Logger; | ||
export declare const getLogger: (level?: number, method?: string) => ILogger; |
@@ -36,3 +36,3 @@ "use strict"; | ||
canLogWarn() { | ||
return this._level >= LoggerLevel.Error; | ||
return this._level >= LoggerLevel.Warn; | ||
} | ||
@@ -39,0 +39,0 @@ level(levelValue) { |
export declare function ordinaryLeastSquares(uX: number, uY: number, uXY: number, uX2: number): number[]; | ||
export declare function points(data: any[], x: (datum: any) => number, y: (datum: any) => number, sort?: boolean): (number | Float64Array)[]; | ||
export declare function visitPoints(data: any[], x: (datum: any) => number, y: (datum: any) => number, callback: (x: number, y: number, index: number) => void): void; | ||
export declare function rSquared(data: any[], x: (datum: any) => number, y: (datum: any) => number, uY: number, predict: (x: number) => number): number; | ||
export declare function regressionLinear(data: any[], x: (datum: any) => number, y: (datum: any) => number): { | ||
export declare function regressionLinear(data: any[], x?: (datum: any) => number, y?: (datum: any) => number): { | ||
coef: number[]; | ||
@@ -7,0 +6,0 @@ predict: (x: number) => number; |
@@ -5,3 +5,3 @@ "use strict"; | ||
value: !0 | ||
}), exports.regressionLinear = exports.rSquared = exports.visitPoints = exports.points = exports.ordinaryLeastSquares = void 0; | ||
}), exports.regressionLinear = exports.rSquared = exports.visitPoints = exports.ordinaryLeastSquares = void 0; | ||
@@ -15,15 +15,2 @@ const vutils_1 = require("@visactor/vutils"); | ||
function points(data, x, y, sort) { | ||
data = data.filter((d => { | ||
let u = x(d), v = y(d); | ||
return !(0, vutils_1.isNil)(u) && (u = +u) >= u && !(0, vutils_1.isNil)(v) && (v = +v) >= v; | ||
})), sort && data.sort(((a, b) => x(a) - x(b))); | ||
const n = data.length, X = new Float64Array(n), Y = new Float64Array(n); | ||
let xv, yv, i = 0, ux = 0, uy = 0; | ||
for (data.forEach((d => { | ||
X[i] = xv = +x(d), Y[i] = yv = +y(d), ++i, ux += (xv - ux) / i, uy += (yv - uy) / i; | ||
})), i = 0; i < n; ++i) X[i] -= ux, Y[i] -= uy; | ||
return [ X, Y, ux, uy ]; | ||
} | ||
function visitPoints(data, x, y, callback) { | ||
@@ -44,3 +31,3 @@ let u, v, i = -1; | ||
function regressionLinear(data, x, y) { | ||
function regressionLinear(data, x = (datum => datum.x), y = (datum => datum.y)) { | ||
let X = 0, Y = 0, XY = 0, X2 = 0, n = 0; | ||
@@ -58,4 +45,4 @@ visitPoints(data, x, y, ((dx, dy) => { | ||
exports.ordinaryLeastSquares = ordinaryLeastSquares, exports.points = points, exports.visitPoints = visitPoints, | ||
exports.ordinaryLeastSquares = ordinaryLeastSquares, exports.visitPoints = visitPoints, | ||
exports.rSquared = rSquared, exports.regressionLinear = regressionLinear; | ||
//# sourceMappingURL=regression-linear.js.map |
@@ -20,2 +20,3 @@ "use strict"; | ||
value: !0 | ||
}), __exportStar(require("./function"), exports), __exportStar(require("./logger"), exports); | ||
}), __exportStar(require("./function"), exports), __exportStar(require("./logger"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -322,56 +322,34 @@ (function (global, factory) { | ||
var isType = function isType(value, type) { | ||
return toString.call(value) === "[object ".concat(type, "]"); | ||
}; | ||
const isType = (value, type) => toString.call(value) === `[object ${type}]`; | ||
var isFunction = function isFunction(value) { | ||
return "function" == typeof value; | ||
}; | ||
const isFunction = value => "function" == typeof value; | ||
var isNil = function isNil(value) { | ||
return null == value; | ||
}; | ||
const isNil = value => null == value; | ||
var isValid = function isValid(value) { | ||
return null != value; | ||
}; | ||
const isValid = value => null != value; | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}, _typeof(obj); | ||
} | ||
var isObject = function isObject(value) { | ||
var type = _typeof(value); | ||
const isObject = value => { | ||
const type = typeof value; | ||
return null !== value && "object" === type || "function" === type; | ||
}; | ||
var isObjectLike = function isObjectLike(value) { | ||
return "object" == _typeof(value) && null !== value; | ||
}; | ||
const isObjectLike = value => "object" == typeof value && null !== value; | ||
var isString = function isString(value) { | ||
var fuzzy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !1; | ||
var type = _typeof(value); | ||
const isString = function (value) { | ||
let fuzzy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !1; | ||
const type = typeof value; | ||
return fuzzy ? "string" === type : "string" === type || isType(value, "String"); | ||
}; | ||
var isArray = function isArray(value) { | ||
return Array.isArray ? Array.isArray(value) : isType(value, "Array"); | ||
}; | ||
const isArray = value => Array.isArray ? Array.isArray(value) : isType(value, "Array"); | ||
var isNumber = function isNumber(value) { | ||
var fuzzy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !1; | ||
var type = _typeof(value); | ||
const isNumber = function (value) { | ||
let fuzzy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !1; | ||
const type = typeof value; | ||
return fuzzy ? "number" === type : "number" === type || isType(value, "Number"); | ||
}; | ||
var get = function get(obj, path, defaultValue) { | ||
var paths = isString(path) ? path.split(".") : path; | ||
for (var p = 0; p < paths.length; p++) obj = obj ? obj[paths[p]] : void 0; | ||
const get = (obj, path, defaultValue) => { | ||
const paths = isString(path) ? path.split(".") : path; | ||
for (let p = 0; p < paths.length; p++) obj = obj ? obj[paths[p]] : void 0; | ||
return void 0 === obj ? defaultValue : obj; | ||
@@ -384,3 +362,3 @@ }; | ||
var hasRaf = !1; | ||
let hasRaf = !1; | ||
try { | ||
@@ -443,3 +421,3 @@ hasRaf = "function" == typeof requestAnimationFrame && "function" == typeof cancelAnimationFrame; | ||
canLogWarn() { | ||
return this._level >= LoggerLevel.Error; | ||
return this._level >= LoggerLevel.Warn; | ||
} | ||
@@ -611,59 +589,2 @@ level(levelValue) { | ||
const isLegalKey = (key) => key !== '__proto__'; | ||
const writeConfig = (output, key, value, recurse) => { | ||
if (!isLegalKey(key)) { | ||
return; | ||
} | ||
if (isObject(value) && !isArray(value) && !isFunction(value)) { | ||
if (!isObject(output[key])) { | ||
output[key] = {}; | ||
} | ||
const outputValue = output[key]; | ||
Object.keys(value).forEach((subKey) => { | ||
if (recurse && (recurse === true || recurse[subKey])) { | ||
writeConfig(outputValue, subKey, value[subKey]); | ||
} | ||
else if (isLegalKey(subKey)) { | ||
outputValue[subKey] = value[subKey]; | ||
} | ||
}); | ||
} | ||
else { | ||
output[key] = value; | ||
} | ||
}; | ||
function mergeNamed(a, b) { | ||
if (isNil(a)) { | ||
return b; | ||
} | ||
const map = {}; | ||
const out = []; | ||
const add = (_) => { | ||
if (!map[_.name]) { | ||
map[_.name] = 1; | ||
out.push(_); | ||
} | ||
}; | ||
b.forEach(add); | ||
a.forEach(add); | ||
return out; | ||
} | ||
const mergeConfig = (...configs) => { | ||
return configs.reduce((out, source) => { | ||
if (!source) { | ||
return out; | ||
} | ||
Object.keys(source).forEach(key => { | ||
if (key === 'signals') { | ||
out.signals = mergeNamed(out.signals, source.signals); | ||
} | ||
else { | ||
const r = key === 'legend' ? { layout: 1 } : key === 'style' ? true : null; | ||
writeConfig(out, key, source[key], r); | ||
} | ||
}); | ||
return out; | ||
}, {}); | ||
}; | ||
const DESCENDING = 'desc'; | ||
@@ -733,3 +654,3 @@ const ascending = (u, v) => { | ||
const value = valueGetter(array[i]); | ||
if (!isNil(value) && !Number.isNaN(value)) { | ||
if (isNumber(value) && !Number.isNaN(value)) { | ||
if (isNil(min)) { | ||
@@ -790,23 +711,2 @@ min = value; | ||
const defaultFont = 'sans-serif'; | ||
const defaultFontSize = 11; | ||
const defaultSymbolSize = 30; | ||
const defaultStrokeWidth = 2; | ||
const defaultColor = '#4c78a8'; | ||
const black = '#000'; | ||
const gray = '#888'; | ||
const lightGray = '#ddd'; | ||
var constants = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
black: black, | ||
defaultColor: defaultColor, | ||
defaultFont: defaultFont, | ||
defaultFontSize: defaultFontSize, | ||
defaultStrokeWidth: defaultStrokeWidth, | ||
defaultSymbolSize: defaultSymbolSize, | ||
gray: gray, | ||
lightGray: lightGray | ||
}); | ||
function ordinaryLeastSquares(uX, uY, uXY, uX2) { | ||
@@ -841,3 +741,3 @@ const delta = uX2 - uX * uX; | ||
} | ||
function regressionLinear(data, x, y) { | ||
function regressionLinear(data, x = datum => datum.x, y = datum => datum.y) { | ||
let X = 0; | ||
@@ -881,3 +781,2 @@ let Y = 0; | ||
exports.isEqual = isEqual; | ||
exports.mergeConfig = mergeConfig; | ||
exports.one = one; | ||
@@ -890,6 +789,4 @@ exports.regressionLinear = regressionLinear; | ||
exports.truthy = truthy; | ||
exports.vGrammarConstants = constants; | ||
exports.writeConfig = writeConfig; | ||
exports.zero = zero; | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).VGrammar=e.VGrammar||{},e.VGrammar.Util={}))}(this,(function(e){"use strict";!function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,s||e,i),u=n?n+t:t;return e._events[u]?e._events[u].fn?e._events[u]=[e._events[u],c]:e._events[u].push(c):(e._events[u]=c,e._eventsCount++),e}function i(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},c.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,s=r.length,i=new Array(s);o<s;o++)i[o]=r[o].fn;return i},c.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},c.prototype.emit=function(e,t,r,o,s,i){var c=n?n+e:e;if(!this._events[c])return!1;var u,l,f=this._events[c],a=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),a){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,r),!0;case 4:return f.fn.call(f.context,t,r,o),!0;case 5:return f.fn.call(f.context,t,r,o,s),!0;case 6:return f.fn.call(f.context,t,r,o,s,i),!0}for(l=1,u=new Array(a-1);l<a;l++)u[l-1]=arguments[l];f.fn.apply(f.context,u)}else{var h,p=f.length;for(l=0;l<p;l++)switch(f[l].once&&this.removeListener(e,f[l].fn,void 0,!0),a){case 1:f[l].fn.call(f[l].context);break;case 2:f[l].fn.call(f[l].context,t);break;case 3:f[l].fn.call(f[l].context,t,r);break;case 4:f[l].fn.call(f[l].context,t,r,o);break;default:if(!u)for(h=1,u=new Array(a-1);h<a;h++)u[h-1]=arguments[h];f[l].fn.apply(f[l].context,u)}}return!0},c.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},c.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},c.prototype.removeListener=function(e,t,r,o){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return i(this,s),this;var c=this._events[s];if(c.fn)c.fn!==t||o&&!c.once||r&&c.context!==r||i(this,s);else{for(var u=0,l=[],f=c.length;u<f;u++)(c[u].fn!==t||o&&!c[u].once||r&&c[u].context!==r)&&l.push(c[u]);l.length?this._events[s]=1===l.length?l[0]:l:i(this,s)}return this},c.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&i(this,t)):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,e.exports=c}({exports:{}});var t=function(e,t){return toString.call(e)==="[object ".concat(t,"]")},n=function(e){return"function"==typeof e},r=function(e){return null==e};function o(e){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o(e)}var s=function(e){var t=o(e);return null!==e&&"object"===t||"function"===t},i=function(e){return"object"==o(e)&&null!==e},c=function(e){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=o(e);return n?"string"===r:"string"===r||t(e,"String")},u=function(e){return Array.isArray?Array.isArray(e):t(e,"Array")};function l(e){return null!=e?u(e)?e:[e]:[]}var f;try{"function"==typeof requestAnimationFrame&&"function"==typeof cancelAnimationFrame}catch(e){!1}(function(e){e[e.NONE=0]="NONE",e[e.BBOX1=1]="BBOX1",e[e.BBOX2=2]="BBOX2"})(f||(f={}));const a=(e,t,n)=>(e.fields=t||[],e.fname=n,e);function h(e){return r(e)?null:e.fields}const p=e=>t=>function(e,t,n){for(var r=c(t)?t.split("."):t,o=0;o<r.length;o++)e=e?e[r[o]]:void 0;return void 0===e?n:e}(t,e);function v(e,t,n){const r=[t].concat([].slice.call(n));console[e].apply(console,r)}var g;!function(e){e[e.None=0]="None",e[e.Error=1]="Error",e[e.Warn=2]="Warn",e[e.Info=3]="Info",e[e.Debug=4]="Debug"}(g||(g={}));class y{_level;_method;constructor(e=g.None,t){this._level=e,this._method=t}canLogInfo(){return this._level>=g.Info}canLogDebug(){return this._level>=g.Debug}canLogError(){return this._level>=g.Error}canLogWarn(){return this._level>=g.Error}level(e){return arguments.length?(this._level=+e,this):this._level}error(...e){return this._level>=g.Error&&v(this._method??"error","ERROR",e),this}warn(...e){return this._level>=g.Warn&&v(this._method||"warn","WARN",e),this}info(...e){return this._level>=g.Info&&v(this._method||"log","INFO",e),this}debug(...e){return this._level>=g.Debug&&v(this._method||"log","DEBUG",e),this}}let m=null;const b=(e,n)=>(m&&function(e){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=o(e);return n?"number"===r:"number"===r||t(e,"Number")}(e)?m.level(e):m||(m=new y(e,n)),m),_=e=>{b().error(e)},d=e=>{const t=[],n=e.length;let r,o,s,i=null,c=0,u="";e+="";const l=()=>{t.push(u+e.substring(r,o)),u="",r=o+1};for(r=0,o=0;o<n;o+=1)if(s=e[o],"\\"===s)u+=e.substring(r,o),u+=e.substring(++o,++o),r=o;else if(s===i)l(),i=null,c=-1;else{if(i)continue;r===c&&'"'===s||r===c&&"'"===s?(r=o+1,i=s):"."!==s||c?"["===s?(o>r&&l(),r=o+1,c=r):"]"===s&&(c||_("Access path missing open bracket: "+e),c>0&&l(),c=0,r=o+1):o>r?l():r=o+1}return c&&_("Access path missing closing bracket: "+e),i&&_("Access path missing closing quote: "+e),o>r&&(o+=1,l()),t},N=(e,t,n={})=>{const r=d(e),o=1===r.length?r[0]:e;return a((n&&n.get||p)(r),[o],t||o)},O=N("id"),x=a((function(e){return e}),[],"identity"),E=a((function(){return 0}),[],"zero"),w=a((function(){return 1}),[],"one"),j=a((function(){return!0}),[],"true"),L=a((function(){return!1}),[],"false"),A=a((function(){return{}}),[],"emptyObject"),k=e=>"__proto__"!==e,S=(e,t,r,o)=>{if(k(t))if(!s(r)||u(r)||n(r))e[t]=r;else{s(e[t])||(e[t]={});const n=e[t];Object.keys(r).forEach((e=>{o&&(!0===o||o[e])?S(n,e,r[e]):k(e)&&(n[e]=r[e])}))}};const C=(e,t)=>{if((e<t||r(e))&&!r(t))return-1;if((e>t||r(t))&&!r(e))return 1;const n=t instanceof Date?+t:t,o=e instanceof Date?+e:e;return Number.isNaN(o)&&!Number.isNaN(n)?-1:Number.isNaN(n)&&!Number.isNaN(o)?1:0},B=(e,t)=>{return 1===e.length?(n=e[0],r=t[0],(e,t)=>C(n(e),n(t))*r):((e,t,n)=>(t.push(0),(r,o)=>{let s,i=0,c=-1;for(;0===i&&c+1<n;)c+=1,s=e[c],i=C(s(r),s(o));return i*t[c]}))(e,t,e.length);var n,r},D=(e,t,n,o=!0)=>{if(t===n)return!0;if(r(t)||r(n))return r(t)&&r(n);if(!i(t)&&!i(n))return t===n;const c=u(t)?t:t[e],l=u(n)?n:n[e];return c===l||!1!==o&&(u(l)?!(!u(c)||l.length!==c.length||!l.every(((e,t)=>e===c[t]))):!!s(l)&&!(!s(c)||Object.keys(l).length!==Object.keys(c).length||!Object.keys(l).every((e=>D(e,l,c)))))};var F=Object.freeze({__proto__:null,black:"#000",defaultColor:"#4c78a8",defaultFont:"sans-serif",defaultFontSize:11,defaultStrokeWidth:2,defaultSymbolSize:30,gray:"#888",lightGray:"#ddd"});function G(e,t,n,o){let s,i,c=-1;e.forEach((e=>{s=t(e),i=n(e),!r(s)&&(s=+s)>=s&&!r(i)&&(i=+i)>=i&&o(s,i,++c)}))}function I(e,t,n,r,o){let s=0,i=0;return G(e,t,n,((e,t)=>{const n=t-o(e),c=t-r;s+=n*n,i+=c*c})),1-s/i}e.Logger=y,e.accessor=a,e.accessorFields=h,e.accessorName=function(e){return r(e)?null:e.fname},e.ascending=C,e.clearLogger=()=>{m=null},e.compare=(e,t,o={})=>{const s=l(t)||[],i=[],c=[],u={},f=o.comparator||B;return l(e).forEach(((e,t)=>{if(r(e))return;i.push("desc"===s[t]?-1:1);const l=n(e)?e:N(e,null,o);c.push(l),(h(l)||[]).forEach((e=>{u[e]=1}))})),0===c.length?null:a(f(c,i),Object.keys(u))},e.emptyObject=A,e.error=_,e.extent=(e,t)=>{const o=n(t)?t:e=>e;let s,i;if(e&&e.length){const t=e.length;for(let n=0;n<t;n+=1){const t=o(e[n]);r(t)||Number.isNaN(t)||(r(s)?(s=t,i=t):(s=Math.min(s,t),i=Math.max(i,t)))}return[s,i]}return[s,i]},e.falsy=L,e.field=N,e.getLogger=b,e.getter=p,e.id=O,e.identity=x,e.isEqual=D,e.mergeConfig=(...e)=>e.reduce(((e,t)=>t?(Object.keys(t).forEach((n=>{if("signals"===n)e.signals=function(e,t){if(r(e))return t;const n={},o=[],s=e=>{n[e.name]||(n[e.name]=1,o.push(e))};return t.forEach(s),e.forEach(s),o}(e.signals,t.signals);else{const r="legend"===n?{layout:1}:"style"===n||null;S(e,n,t[n],r)}})),e):e),{}),e.one=w,e.regressionLinear=function(e,t,n){let r=0,o=0,s=0,i=0,c=0;G(e,t,n,((e,t)=>{++c,r+=(e-r)/c,o+=(t-o)/c,s+=(e*t-s)/c,i+=(e*e-i)/c}));const u=function(e,t,n,r){const o=r-e*e,s=Math.abs(o)<1e-24?0:(n-e*t)/o;return[t-s*e,s]}(r,o,s,i),l=e=>u[0]+u[1]*e;return{coef:u,predict:l,rSquared:I(e,t,n,o,l)}},e.setLogLevel=e=>{m?m.level(e):m=new y(e)},e.setLogger=e=>{m=e},e.splitAccessPath=d,e.toPercent=(e,t)=>r(e)?t:Math.min(c(e)?t*parseFloat(e)/100:e,t),e.truthy=j,e.vGrammarConstants=F,e.writeConfig=S,e.zero=E})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).VGrammar=e.VGrammar||{},e.VGrammar.Util={}))}(this,(function(e){"use strict";!function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,s||e,i),l=n?n+t:t;return e._events[l]?e._events[l].fn?e._events[l]=[e._events[l],c]:e._events[l].push(c):(e._events[l]=c,e._eventsCount++),e}function i(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},c.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,s=r.length,i=new Array(s);o<s;o++)i[o]=r[o].fn;return i},c.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},c.prototype.emit=function(e,t,r,o,s,i){var c=n?n+e:e;if(!this._events[c])return!1;var l,u,f=this._events[c],a=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),a){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,r),!0;case 4:return f.fn.call(f.context,t,r,o),!0;case 5:return f.fn.call(f.context,t,r,o,s),!0;case 6:return f.fn.call(f.context,t,r,o,s,i),!0}for(u=1,l=new Array(a-1);u<a;u++)l[u-1]=arguments[u];f.fn.apply(f.context,l)}else{var h,p=f.length;for(u=0;u<p;u++)switch(f[u].once&&this.removeListener(e,f[u].fn,void 0,!0),a){case 1:f[u].fn.call(f[u].context);break;case 2:f[u].fn.call(f[u].context,t);break;case 3:f[u].fn.call(f[u].context,t,r);break;case 4:f[u].fn.call(f[u].context,t,r,o);break;default:if(!l)for(h=1,l=new Array(a-1);h<a;h++)l[h-1]=arguments[h];f[u].fn.apply(f[u].context,l)}}return!0},c.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},c.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},c.prototype.removeListener=function(e,t,r,o){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return i(this,s),this;var c=this._events[s];if(c.fn)c.fn!==t||o&&!c.once||r&&c.context!==r||i(this,s);else{for(var l=0,u=[],f=c.length;l<f;l++)(c[l].fn!==t||o&&!c[l].once||r&&c[l].context!==r)&&u.push(c[l]);u.length?this._events[s]=1===u.length?u[0]:u:i(this,s)}return this},c.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&i(this,t)):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,e.exports=c}({exports:{}});const t=(e,t)=>toString.call(e)===`[object ${t}]`,n=e=>"function"==typeof e,r=e=>null==e,o=e=>{const t=typeof e;return null!==e&&"object"===t||"function"===t},s=e=>"object"==typeof e&&null!==e,i=function(e){const n=typeof e;return arguments.length>1&&void 0!==arguments[1]&&arguments[1]?"string"===n:"string"===n||t(e,"String")},c=e=>Array.isArray?Array.isArray(e):t(e,"Array"),l=function(e){const n=typeof e;return arguments.length>1&&void 0!==arguments[1]&&arguments[1]?"number"===n:"number"===n||t(e,"Number")};function u(e){return null!=e?c(e)?e:[e]:[]}let f=!1;try{f="function"==typeof requestAnimationFrame&&"function"==typeof cancelAnimationFrame}catch(e){f=!1}var a;f=!1,function(e){e[e.NONE=0]="NONE",e[e.BBOX1=1]="BBOX1",e[e.BBOX2=2]="BBOX2"}(a||(a={}));const h=(e,t,n)=>(e.fields=t||[],e.fname=n,e);function p(e){return r(e)?null:e.fields}const v=e=>t=>((e,t,n)=>{const r=i(t)?t.split("."):t;for(let t=0;t<r.length;t++)e=e?e[r[t]]:void 0;return void 0===e?n:e})(t,e);function g(e,t,n){const r=[t].concat([].slice.call(n));console[e].apply(console,r)}var y;!function(e){e[e.None=0]="None",e[e.Error=1]="Error",e[e.Warn=2]="Warn",e[e.Info=3]="Info",e[e.Debug=4]="Debug"}(y||(y={}));class m{_level;_method;constructor(e=y.None,t){this._level=e,this._method=t}canLogInfo(){return this._level>=y.Info}canLogDebug(){return this._level>=y.Debug}canLogError(){return this._level>=y.Error}canLogWarn(){return this._level>=y.Warn}level(e){return arguments.length?(this._level=+e,this):this._level}error(...e){return this._level>=y.Error&&g(this._method??"error","ERROR",e),this}warn(...e){return this._level>=y.Warn&&g(this._method||"warn","WARN",e),this}info(...e){return this._level>=y.Info&&g(this._method||"log","INFO",e),this}debug(...e){return this._level>=y.Debug&&g(this._method||"log","DEBUG",e),this}}let _=null;const b=(e,t)=>(_&&l(e)?_.level(e):_||(_=new m(e,t)),_),d=e=>{b().error(e)},N=e=>{const t=[],n=e.length;let r,o,s,i=null,c=0,l="";e+="";const u=()=>{t.push(l+e.substring(r,o)),l="",r=o+1};for(r=0,o=0;o<n;o+=1)if(s=e[o],"\\"===s)l+=e.substring(r,o),l+=e.substring(++o,++o),r=o;else if(s===i)u(),i=null,c=-1;else{if(i)continue;r===c&&'"'===s||r===c&&"'"===s?(r=o+1,i=s):"."!==s||c?"["===s?(o>r&&u(),r=o+1,c=r):"]"===s&&(c||d("Access path missing open bracket: "+e),c>0&&u(),c=0,r=o+1):o>r?u():r=o+1}return c&&d("Access path missing closing bracket: "+e),i&&d("Access path missing closing quote: "+e),o>r&&(o+=1,u()),t},x=(e,t,n={})=>{const r=N(e),o=1===r.length?r[0]:e;return h((n&&n.get||v)(r),[o],t||o)},O=x("id"),w=h((function(e){return e}),[],"identity"),L=h((function(){return 0}),[],"zero"),A=h((function(){return 1}),[],"one"),E=h((function(){return!0}),[],"true"),j=h((function(){return!1}),[],"false"),k=h((function(){return{}}),[],"emptyObject"),B=(e,t)=>{if((e<t||r(e))&&!r(t))return-1;if((e>t||r(t))&&!r(e))return 1;const n=t instanceof Date?+t:t,o=e instanceof Date?+e:e;return Number.isNaN(o)&&!Number.isNaN(n)?-1:Number.isNaN(n)&&!Number.isNaN(o)?1:0},D=(e,t)=>{return 1===e.length?(n=e[0],r=t[0],(e,t)=>B(n(e),n(t))*r):((e,t,n)=>(t.push(0),(r,o)=>{let s,i=0,c=-1;for(;0===i&&c+1<n;)c+=1,s=e[c],i=B(s(r),s(o));return i*t[c]}))(e,t,e.length);var n,r},C=(e,t,n,i=!0)=>{if(t===n)return!0;if(r(t)||r(n))return r(t)&&r(n);if(!s(t)&&!s(n))return t===n;const l=c(t)?t:t[e],u=c(n)?n:n[e];return l===u||!1!==i&&(c(u)?!(!c(l)||u.length!==l.length||!u.every(((e,t)=>e===l[t]))):!!o(u)&&!(!o(l)||Object.keys(u).length!==Object.keys(l).length||!Object.keys(u).every((e=>C(e,u,l)))))};function I(e,t,n,o){let s,i,c=-1;e.forEach((e=>{s=t(e),i=n(e),!r(s)&&(s=+s)>=s&&!r(i)&&(i=+i)>=i&&o(s,i,++c)}))}function W(e,t,n,r,o){let s=0,i=0;return I(e,t,n,((e,t)=>{const n=t-o(e),c=t-r;s+=n*n,i+=c*c})),1-s/i}e.Logger=m,e.accessor=h,e.accessorFields=p,e.accessorName=function(e){return r(e)?null:e.fname},e.ascending=B,e.clearLogger=()=>{_=null},e.compare=(e,t,o={})=>{const s=u(t)||[],i=[],c=[],l={},f=o.comparator||D;return u(e).forEach(((e,t)=>{if(r(e))return;i.push("desc"===s[t]?-1:1);const u=n(e)?e:x(e,null,o);c.push(u),(p(u)||[]).forEach((e=>{l[e]=1}))})),0===c.length?null:h(f(c,i),Object.keys(l))},e.emptyObject=k,e.error=d,e.extent=(e,t)=>{const o=n(t)?t:e=>e;let s,i;if(e&&e.length){const t=e.length;for(let n=0;n<t;n+=1){const t=o(e[n]);l(t)&&!Number.isNaN(t)&&(r(s)?(s=t,i=t):(s=Math.min(s,t),i=Math.max(i,t)))}return[s,i]}return[s,i]},e.falsy=j,e.field=x,e.getLogger=b,e.getter=v,e.id=O,e.identity=w,e.isEqual=C,e.one=A,e.regressionLinear=function(e,t=(e=>e.x),n=(e=>e.y)){let r=0,o=0,s=0,i=0,c=0;I(e,t,n,((e,t)=>{++c,r+=(e-r)/c,o+=(t-o)/c,s+=(e*t-s)/c,i+=(e*e-i)/c}));const l=function(e,t,n,r){const o=r-e*e,s=Math.abs(o)<1e-24?0:(n-e*t)/o;return[t-s*e,s]}(r,o,s,i),u=e=>l[0]+l[1]*e;return{coef:l,predict:u,rSquared:W(e,t,n,o,u)}},e.setLogLevel=e=>{_?_.level(e):_=new m(e)},e.setLogger=e=>{_=e},e.splitAccessPath=N,e.toPercent=(e,t)=>r(e)?t:Math.min(i(e)?t*parseFloat(e)/100:e,t),e.truthy=E,e.zero=L})); |
import type { ReturnNumberFunction } from './types'; | ||
export declare const extent: (array: number[], func?: ReturnNumberFunction) => number[]; | ||
export declare const extent: (array: any[], func?: ReturnNumberFunction) => number[]; |
@@ -1,2 +0,2 @@ | ||
import { isFunction, isNil } from "@visactor/vutils"; | ||
import { isFunction, isNumber, isNil } from "@visactor/vutils"; | ||
@@ -10,3 +10,3 @@ export const extent = (array, func) => { | ||
const value = valueGetter(array[i]); | ||
isNil(value) || Number.isNaN(value) || (isNil(min) ? (min = value, max = value) : (min = Math.min(min, value), | ||
isNumber(value) && !Number.isNaN(value) && (isNil(min) ? (min = value, max = value) : (min = Math.min(min, value), | ||
max = Math.max(max, value))); | ||
@@ -13,0 +13,0 @@ } |
@@ -5,3 +5,2 @@ export { accessor, accessorName, accessorFields } from './accessor'; | ||
export { Logger, setLogLevel, setLogger, getLogger, clearLogger } from './logger'; | ||
export { mergeConfig, writeConfig } from './mergeConfig'; | ||
export { compare, ascending } from './compare'; | ||
@@ -14,4 +13,3 @@ export { error } from './error'; | ||
export { toPercent } from './toPercent'; | ||
export * as vGrammarConstants from './constants'; | ||
export * from './types'; | ||
export { regressionLinear } from './regression-linear'; |
@@ -9,4 +9,2 @@ export { accessor, accessorName, accessorFields } from "./accessor"; | ||
export { mergeConfig, writeConfig } from "./mergeConfig"; | ||
export { compare, ascending } from "./compare"; | ||
@@ -26,4 +24,2 @@ | ||
export * as vGrammarConstants from "./constants"; | ||
export * from "./types"; | ||
@@ -30,0 +26,0 @@ |
@@ -24,5 +24,5 @@ import type { ILogger } from './types'; | ||
} | ||
export declare const setLogger: (logger: Logger) => void; | ||
export declare const setLogger: (logger: ILogger) => void; | ||
export declare const setLogLevel: (level: number) => void; | ||
export declare const clearLogger: () => void; | ||
export declare const getLogger: (level?: number, method?: string) => Logger; | ||
export declare const getLogger: (level?: number, method?: string) => ILogger; |
@@ -30,3 +30,3 @@ import { isNumber } from "@visactor/vutils"; | ||
canLogWarn() { | ||
return this._level >= LoggerLevel.Error; | ||
return this._level >= LoggerLevel.Warn; | ||
} | ||
@@ -33,0 +33,0 @@ level(levelValue) { |
export declare function ordinaryLeastSquares(uX: number, uY: number, uXY: number, uX2: number): number[]; | ||
export declare function points(data: any[], x: (datum: any) => number, y: (datum: any) => number, sort?: boolean): (number | Float64Array)[]; | ||
export declare function visitPoints(data: any[], x: (datum: any) => number, y: (datum: any) => number, callback: (x: number, y: number, index: number) => void): void; | ||
export declare function rSquared(data: any[], x: (datum: any) => number, y: (datum: any) => number, uY: number, predict: (x: number) => number): number; | ||
export declare function regressionLinear(data: any[], x: (datum: any) => number, y: (datum: any) => number): { | ||
export declare function regressionLinear(data: any[], x?: (datum: any) => number, y?: (datum: any) => number): { | ||
coef: number[]; | ||
@@ -7,0 +6,0 @@ predict: (x: number) => number; |
@@ -8,15 +8,2 @@ import { isNil } from "@visactor/vutils"; | ||
export function points(data, x, y, sort) { | ||
data = data.filter((d => { | ||
let u = x(d), v = y(d); | ||
return !isNil(u) && (u = +u) >= u && !isNil(v) && (v = +v) >= v; | ||
})), sort && data.sort(((a, b) => x(a) - x(b))); | ||
const n = data.length, X = new Float64Array(n), Y = new Float64Array(n); | ||
let xv, yv, i = 0, ux = 0, uy = 0; | ||
for (data.forEach((d => { | ||
X[i] = xv = +x(d), Y[i] = yv = +y(d), ++i, ux += (xv - ux) / i, uy += (yv - uy) / i; | ||
})), i = 0; i < n; ++i) X[i] -= ux, Y[i] -= uy; | ||
return [ X, Y, ux, uy ]; | ||
} | ||
export function visitPoints(data, x, y, callback) { | ||
@@ -37,3 +24,3 @@ let u, v, i = -1; | ||
export function regressionLinear(data, x, y) { | ||
export function regressionLinear(data, x = (datum => datum.x), y = (datum => datum.y)) { | ||
let X = 0, Y = 0, XY = 0, X2 = 0, n = 0; | ||
@@ -40,0 +27,0 @@ visitPoints(data, x, y, ((dx, dy) => { |
export * from "./function"; | ||
export * from "./logger"; | ||
export * from "./logger"; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@visactor/vgrammar-util", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "The common utils functions of VGrammar", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
138976
101
1622