From c6eecfe2c6730345f9fce25b78cf4186f487dd5d Mon Sep 17 00:00:00 2001 From: 100pah Date: Fri, 16 Oct 2020 00:47:36 +0800 Subject: [PATCH] test: update ut for 5.0. --- test/ut/jest.config.js | 1 - test/ut/spec/model/Global.test.ts | 239 +++++----- .../spec/model/timelineMediaOptions.test.ts | 59 ++- test/ut/spec/scale/interval.test.ts | 94 ++-- test/ut/spec/util/graphic.test.ts | 97 ++-- test/ut/spec/util/layout.test.ts | 44 +- test/ut/spec/util/model.test.ts | 19 +- test/ut/spec/util/number.test.ts | 434 +++++++++--------- 8 files changed, 523 insertions(+), 464 deletions(-) diff --git a/test/ut/jest.config.js b/test/ut/jest.config.js index a5fc425ab..8e61d79b6 100644 --- a/test/ut/jest.config.js +++ b/test/ut/jest.config.js @@ -39,7 +39,6 @@ module.exports = { '**/spec/model/*.test.ts', '**/spec/scale/*.test.ts', '**/spec/util/*.test.ts', - '!**/spec/api/containPixel.test.ts', '!**/spec/component/graphic/setOption.test.ts' ] }; diff --git a/test/ut/spec/model/Global.test.ts b/test/ut/spec/model/Global.test.ts index 63ae4f995..399f6537d 100755 --- a/test/ut/spec/model/Global.test.ts +++ b/test/ut/spec/model/Global.test.ts @@ -18,45 +18,60 @@ * under the License. */ -const utHelper = require('../../core/utHelper'); +import { EChartsType } from '../../../../src/echarts'; +import { createChart } from '../../core/utHelper'; +import { ComponentMainType, ParsedValue } from '../../../../src/util/types'; +import SeriesModel from '../../../../src/model/Series'; +import ComponentModel from '../../../../src/model/Component'; +import ChartView from '../../../../src/view/Chart'; +import { EChartsFullOption } from '../../../../src/option'; + +type OriginModelView = { + model: SeriesModel; + view: ChartView; +}; + describe('modelAndOptionMapping', function () { - function getData0(chart, seriesIndex) { + function getData0(chart: EChartsType, seriesIndex: number): ParsedValue { return getSeries(chart, seriesIndex).getData().get('y', 0); } - function getSeries(chart, seriesIndex) { - return chart.getModel().getComponent('series', seriesIndex); + function getSeries(chart: EChartsType, seriesIndex: number): SeriesModel { + return chart.getModel().getComponent('series', seriesIndex) as SeriesModel; } - function getModel(chart, type, index) { + function getModel(chart: EChartsType, type: ComponentMainType, index: number): ComponentModel { return chart.getModel().getComponent(type, index); } - function countSeries(chart) { + function countSeries(chart: EChartsType): number { return countModel(chart, 'series'); } - function countModel(chart, type) { + function countModel(chart: EChartsType, type: ComponentMainType): number { // FIXME // access private + // @ts-ignore return chart.getModel()._componentsMap.get(type).length; } - function getChartView(chart, series) { + function getChartView(chart: EChartsType, series: SeriesModel): ChartView { + // @ts-ignore return chart._chartsMap[series.__viewId]; } - function countChartViews(chart) { + function countChartViews(chart: EChartsType): number { + // @ts-ignore return chart._chartsViews.length; } - function saveOrigins(chart) { - var count = countSeries(chart); - var origins = []; - for (var i = 0; i < count; i++) { - var series = getSeries(chart, i); + function saveOrigins(chart: EChartsType): OriginModelView[] { + const count = countSeries(chart); + const origins = []; + for (let i = 0; i < count; i++) { + const series = getSeries(chart, i); origins.push({ model: series, view: getChartView(chart, series) @@ -65,16 +80,26 @@ describe('modelAndOptionMapping', function () { return origins; } - function modelEqualsToOrigin(chart, idxList, origins, boolResult) { - for (var i = 0; i < idxList.length; i++) { - var idx = idxList[i]; + function modelEqualsToOrigin( + chart: EChartsType, + idxList: number[], + origins: OriginModelView[], + boolResult: boolean + ): void { + for (let i = 0; i < idxList.length; i++) { + const idx = idxList[i]; expect(origins[idx].model === getSeries(chart, idx)).toEqual(boolResult); } } - function viewEqualsToOrigin(chart, idxList, origins, boolResult) { - for (var i = 0; i < idxList.length; i++) { - var idx = idxList[i]; + function viewEqualsToOrigin( + chart: EChartsType, + idxList: number[], + origins: OriginModelView[], + boolResult: boolean + ): void { + for (let i = 0; i < idxList.length; i++) { + const idx = idxList[i]; expect( origins[idx].view === getChartView(chart, getSeries(chart, idx)) ).toEqual(boolResult); @@ -82,9 +107,9 @@ describe('modelAndOptionMapping', function () { } - var chart; + let chart: EChartsType; beforeEach(function () { - chart = utHelper.createChart(); + chart = createChart(); }); afterEach(function () { @@ -94,7 +119,7 @@ describe('modelAndOptionMapping', function () { describe('idNoNameNo', function () { it('sameTypeNotMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -106,7 +131,7 @@ describe('modelAndOptionMapping', function () { chart.setOption(option); // Not merge - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption(option, true); expect(countChartViews(chart)).toEqual(3); expect(countSeries(chart)).toEqual(3); @@ -115,7 +140,7 @@ describe('modelAndOptionMapping', function () { }); it('sameTypeMergeFull', function () { - var option = { + chart.setOption({ xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -123,11 +148,10 @@ describe('modelAndOptionMapping', function () { {type: 'line', data: [22]}, {type: 'line', data: [33]} ] - }; - chart.setOption(option); + }); // Merge - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption({ series: [ {type: 'line', data: [111]}, @@ -146,7 +170,7 @@ describe('modelAndOptionMapping', function () { }); it('sameTypeMergePartial', function () { - var option = { + chart.setOption({ xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -154,11 +178,10 @@ describe('modelAndOptionMapping', function () { {type: 'line', data: [22]}, {type: 'line', data: [33]} ] - }; - chart.setOption(option); + }); // Merge - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption({ series: [ {data: [22222]} @@ -175,7 +198,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeMerge', function () { - var option = { + chart.setOption({ xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -183,11 +206,10 @@ describe('modelAndOptionMapping', function () { {type: 'line', data: [22]}, {type: 'line', data: [33]} ] - }; - chart.setOption(option); + }); // Merge - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption({ series: [ {type: 'line', data: [111]}, @@ -217,7 +239,7 @@ describe('modelAndOptionMapping', function () { describe('idSpecified', function () { it('sameTypeNotMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -238,7 +260,7 @@ describe('modelAndOptionMapping', function () { expect(getData0(chart, 3)).toEqual(44); expect(getData0(chart, 4)).toEqual(55); - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption(option, true); expect(countChartViews(chart)).toEqual(5); expect(countSeries(chart)).toEqual(5); @@ -248,7 +270,7 @@ describe('modelAndOptionMapping', function () { }); it('sameTypeMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -261,7 +283,7 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption(option); expect(countChartViews(chart)).toEqual(5); expect(countSeries(chart)).toEqual(5); @@ -271,7 +293,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeNotMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -284,8 +306,8 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); - var option2 = { + const origins = saveOrigins(chart); + const option2: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -306,7 +328,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeMergeFull', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -319,8 +341,8 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); - var option2 = { + const origins = saveOrigins(chart); + const option2: EChartsFullOption = { series: [ {type: 'line', data: [11]}, {type: 'bar', data: [22], id: 20, name: 'a'}, @@ -340,7 +362,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeMergePartial1', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -353,8 +375,8 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); - var option2 = { + const origins = saveOrigins(chart); + const option2: EChartsFullOption = { series: [ {type: 'bar', data: [444], id: 40}, {type: 'line', data: [333]}, @@ -377,7 +399,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeMergePartial2', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -387,7 +409,7 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var option2 = { + const option2: EChartsFullOption = { series: [ {type: 'bar', data: [444], id: 40}, {type: 'line', data: [333]}, @@ -407,7 +429,7 @@ describe('modelAndOptionMapping', function () { it('mergePartialDoNotMapToOtherId', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -417,7 +439,7 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var option2 = { + const option2: EChartsFullOption = { series: [ {type: 'bar', data: [444], id: 40} ] @@ -433,7 +455,7 @@ describe('modelAndOptionMapping', function () { it('idDuplicate', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -451,7 +473,7 @@ describe('modelAndOptionMapping', function () { it('nameTheSameButIdNotTheSame', function () { - var option = { + const option = { grid: {}, xAxis: [ {id: 'x1', name: 'a', xxxx: 'x1_a'}, @@ -462,13 +484,16 @@ describe('modelAndOptionMapping', function () { chart.setOption(option); - var xAxisModel0 = getModel(chart, 'xAxis', 0); - var xAxisModel1 = getModel(chart, 'xAxis', 1); - expect(xAxisModel0.option.xxxx).toEqual('x1_a'); - expect(xAxisModel1.option.xxxx).toEqual('x2_b'); + let xAxisModel0; + let xAxisModel1; + + xAxisModel0 = getModel(chart, 'xAxis', 0); + xAxisModel1 = getModel(chart, 'xAxis', 1); + expect((xAxisModel0.option as any).xxxx).toEqual('x1_a'); + expect((xAxisModel1.option as any).xxxx).toEqual('x2_b'); expect(xAxisModel1.option.name).toEqual('b'); - var option2 = { + const option2 = { xAxis: [ {id: 'k1', name: 'a', xxxx: 'k1_a'}, {id: 'x2', name: 'a', xxxx: 'x2_a'} @@ -476,13 +501,13 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option2); - var xAxisModel0 = getModel(chart, 'xAxis', 0); - var xAxisModel1 = getModel(chart, 'xAxis', 1); - var xAxisModel2 = getModel(chart, 'xAxis', 2); - expect(xAxisModel0.option.xxxx).toEqual('x1_a'); - expect(xAxisModel1.option.xxxx).toEqual('x2_a'); + xAxisModel0 = getModel(chart, 'xAxis', 0); + xAxisModel1 = getModel(chart, 'xAxis', 1); + const xAxisModel2 = getModel(chart, 'xAxis', 2); + expect((xAxisModel0.option as any).xxxx).toEqual('x1_a'); + expect((xAxisModel1.option as any).xxxx).toEqual('x2_a'); expect(xAxisModel1.option.name).toEqual('a'); - expect(xAxisModel2.option.xxxx).toEqual('k1_a'); + expect((xAxisModel2.option as any).xxxx).toEqual('k1_a'); }); }); @@ -499,7 +524,7 @@ describe('modelAndOptionMapping', function () { describe('noIdButNameExists', function () { it('sameTypeNotMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -523,7 +548,7 @@ describe('modelAndOptionMapping', function () { expect(getData0(chart, 3)).toEqual(44); expect(getData0(chart, 4)).toEqual(55); - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption(option, true); expect(countChartViews(chart)).toEqual(5); expect(countSeries(chart)).toEqual(5); @@ -533,7 +558,7 @@ describe('modelAndOptionMapping', function () { }); it('sameTypeMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -546,7 +571,7 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); + const origins = saveOrigins(chart); chart.setOption(option); expect(countChartViews(chart)).toEqual(5); expect(countSeries(chart)).toEqual(5); @@ -556,7 +581,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeNotMerge', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -569,8 +594,8 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); - var option2 = { + const origins = saveOrigins(chart); + const option2: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -591,7 +616,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeMergePartialOneMapTwo', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -604,8 +629,8 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var origins = saveOrigins(chart); - var option2 = { + const origins = saveOrigins(chart); + const option2: EChartsFullOption = { series: [ {type: 'bar', data: [444], id: 40}, {type: 'line', data: [333]}, @@ -629,7 +654,7 @@ describe('modelAndOptionMapping', function () { }); it('differentTypeMergePartialTwoMapOne', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -639,7 +664,7 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var option2 = { + const option2: EChartsFullOption = { series: [ {type: 'bar', data: [444], name: 'a'}, {type: 'line', data: [333]}, @@ -659,7 +684,7 @@ describe('modelAndOptionMapping', function () { it('mergePartialCanMapToOtherName', function () { // Consider case: axis.name = 'some label', which can be overwritten. - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, series: [ @@ -669,7 +694,7 @@ describe('modelAndOptionMapping', function () { }; chart.setOption(option); - var option2 = { + const option2: EChartsFullOption = { series: [ {type: 'bar', data: [444], name: 40}, {type: 'bar', data: [999], name: 40}, @@ -695,24 +720,22 @@ describe('modelAndOptionMapping', function () { describe('ohters', function () { it('aBugCase', function () { - var option = { + const option: EChartsFullOption = { series: [ { type: 'pie', radius: ['20%', '25%'], center: ['20%', '20%'], avoidLabelOverlap: true, - hoverAnimation: false, label: { - normal: { - show: true, - position: 'center', - textStyle: { - fontSize: '30', - fontWeight: 'bold' - } - }, - emphasis: { + show: true, + position: 'center', + fontSize: '30', + fontWeight: 'bold' + }, + emphasis: { + scale: false, + label: { show: true } }, @@ -732,16 +755,11 @@ describe('modelAndOptionMapping', function () { radius: ['20%', '25%'], center: ['20%', '20%'], avoidLabelOverlap: true, - hoverAnimation: false, label: { - normal: { - show: true, - position: 'center', - textStyle: { - fontSize: '30', - fontWeight: 'bold' - } - } + show: true, + position: 'center', + fontSize: '30', + fontWeight: 'bold' }, data: [ {value: 135, name: '视频广告'}, @@ -753,16 +771,11 @@ describe('modelAndOptionMapping', function () { radius: ['20%', '25%'], center: ['60%', '20%'], avoidLabelOverlap: true, - hoverAnimation: false, label: { - normal: { - show: true, - position: 'center', - textStyle: { - fontSize: '30', - fontWeight: 'bold' - } - } + show: true, + position: 'center', + fontSize: '30', + fontWeight: 'bold' }, data: [ {value: 135, name: '视频广告'}, @@ -777,7 +790,7 @@ describe('modelAndOptionMapping', function () { }); it('color', function () { - var option = { + const option: EChartsFullOption = { backgroundColor: 'rgba(1,1,1,1)', xAxis: {data: ['a']}, yAxis: {}, @@ -788,18 +801,18 @@ describe('modelAndOptionMapping', function () { ] }; chart.setOption(option); - expect(chart._model.option.backgroundColor).toEqual('rgba(1,1,1,1)'); + expect(chart.getModel().option.backgroundColor).toEqual('rgba(1,1,1,1)'); // Not merge chart.setOption({ backgroundColor: '#fff' }, true); - expect(chart._model.option.backgroundColor).toEqual('#fff'); + expect(chart.getModel().option.backgroundColor).toEqual('#fff'); }); it('innerId', function () { - var option = { + const option: EChartsFullOption = { xAxis: {data: ['a']}, yAxis: {}, toolbox: { diff --git a/test/ut/spec/model/timelineMediaOptions.test.ts b/test/ut/spec/model/timelineMediaOptions.test.ts index 07db6bfd8..2751a14fc 100755 --- a/test/ut/spec/model/timelineMediaOptions.test.ts +++ b/test/ut/spec/model/timelineMediaOptions.test.ts @@ -17,26 +17,37 @@ * specific language governing permissions and limitations * under the License. */ -const utHelper = require('../../core/utHelper'); + +import { EChartsType } from '../../../../src/echarts'; +import SeriesModel from '../../../../src/model/Series'; +import { ParsedValue } from '../../../../src/util/types'; +import { LegendOption } from '../../../../src/component/legend/LegendModel'; +import TimelineModel from '../../../../src/component/timeline/TimelineModel'; +import { createChart } from '../../core/utHelper'; +import { EChartsFullOption } from '../../../../src/option'; + describe('timelineMediaOptions', function () { - function getData0(chart, seriesIndex) { + function getData0(chart: EChartsType, seriesIndex: number): ParsedValue { return getSeries(chart, seriesIndex).getData().get('y', 0); } - function getSeries(chart, seriesIndex) { - return chart.getModel().getComponent('series', seriesIndex); + function getSeries(chart: EChartsType, seriesIndex: number): SeriesModel { + return chart.getModel().getComponent('series', seriesIndex) as SeriesModel; } - function getLegendOption(chart) { + function getLegendOption(chart: EChartsType): LegendOption { return chart.getModel().getComponent('legend', 0).option; } - function getTimelineComponent(chart) { - return chart.getModel().getComponent('timeline', 0); + function getTimelineComponent(chart: EChartsType): TimelineModel { + return chart.getModel().getComponent('timeline', 0) as TimelineModel; } - var chart; + let chart: EChartsType; beforeEach(function () { - chart = utHelper.createChart(10, 10); + chart = createChart({ + width: 10, + height: 10 + }); }); afterEach(function () { @@ -49,7 +60,7 @@ describe('timelineMediaOptions', function () { describe('parse_timeline_media_option', function () { it('parse_media_has_baseOption_has_default', function () { - var option = { + const option: EChartsFullOption = { baseOption: { xAxis: { data: ['a'] }, yAxis: {}, @@ -77,7 +88,7 @@ describe('timelineMediaOptions', function () { }); it('parse_media_no_baseOption_has_default', function () { - var option = { + const option: EChartsFullOption = { xAxis: { data: ['a'] }, yAxis: {}, legend: { left: 10 }, @@ -101,7 +112,7 @@ describe('timelineMediaOptions', function () { }); it('parse_media_no_baseOption_no_default', function () { - var option = { + const option: EChartsFullOption = { xAxis: { data: ['a'] }, yAxis: {}, legend: { left: 10 }, @@ -121,7 +132,7 @@ describe('timelineMediaOptions', function () { }); it('parse_timeline_media_has_baseOption', function () { - var option = { + const option: EChartsFullOption = { baseOption: { timeline: { axisType: 'category' }, xAxis: { data: ['a'] }, @@ -161,7 +172,7 @@ describe('timelineMediaOptions', function () { }); it('parse_timeline_media_no_baseOption', function () { - var option = { + const option: EChartsFullOption = { timeline: { axisType: 'category' }, xAxis: { data: ['a'] }, yAxis: {}, @@ -197,7 +208,7 @@ describe('timelineMediaOptions', function () { }); it('parse_timeline_has_baseOption', function () { - var option = { + const option: EChartsFullOption = { baseOption: { timeline: { axisType: 'category' }, xAxis: { data: ['a'] }, @@ -221,7 +232,7 @@ describe('timelineMediaOptions', function () { }); it('parse_timeline_has_baseOption_compat', function () { - var option = { + const option: EChartsFullOption = { timeline: { axisType: 'category' }, baseOption: { xAxis: { data: ['a'] }, @@ -245,7 +256,7 @@ describe('timelineMediaOptions', function () { }); it('parse_timeline_has_baseOption_compat', function () { - var option = { + const option: EChartsFullOption = { timeline: { axisType: 'category' }, baseOption: { xAxis: { data: ['a'] }, @@ -269,7 +280,7 @@ describe('timelineMediaOptions', function () { }); it('parse_timeline_no_baseOption', function () { - var option = { + const option: EChartsFullOption = { timeline: { axisType: 'category' }, xAxis: { data: ['a'] }, yAxis: {}, @@ -299,7 +310,7 @@ describe('timelineMediaOptions', function () { describe('timeline_onceMore', function () { it('timeline_setOptionOnceMore_baseOption', function () { - var option = { + const option: EChartsFullOption = { baseOption: { timeline: { axisType: 'category', @@ -347,7 +358,7 @@ describe('timelineMediaOptions', function () { it('timeline_setOptionOnceMore_substitudeTimelineOptions', function () { - var option = { + const option: EChartsFullOption = { baseOption: { timeline: { axisType: 'category', @@ -377,7 +388,7 @@ describe('timelineMediaOptions', function () { }; chart.setOption(option); - var ecModel = chart.getModel(); + let ecModel = chart.getModel(); expect(getData0(chart, 0)).toEqual(1111); expect(getData0(chart, 1)).toEqual(2222); @@ -399,9 +410,9 @@ describe('timelineMediaOptions', function () { }] }); - var ecModel = chart.getModel(); - var option = ecModel.getOption(); - expect(option.backgroundColor).toEqual('#987654'); + ecModel = chart.getModel(); + const optionGotten = ecModel.getOption(); + expect(optionGotten.backgroundColor).toEqual('#987654'); expect(getData0(chart, 0)).toEqual(1111); expect(getData0(chart, 1)).toEqual(2222); diff --git a/test/ut/spec/scale/interval.test.ts b/test/ut/spec/scale/interval.test.ts index e6633069f..c151ad007 100755 --- a/test/ut/spec/scale/interval.test.ts +++ b/test/ut/spec/scale/interval.test.ts @@ -18,15 +18,19 @@ * under the License. */ -const scaleHelper = require('../../../../lib/scale/helper'); -const numberUtil = require('../../../../lib/util/number'); -const IntervalScale = require('../../../../lib/scale/Interval'); -const utHelper = require('../../core/utHelper'); +import { createChart, isValueFinite } from '../../core/utHelper'; +import { EChartsType } from '../../../../src/echarts'; +import CartesianAxisModel from '../../../../src/coord/cartesian/AxisModel'; +import IntervalScale from '../../../../src/scale/Interval'; +import { intervalScaleNiceTicks } from '../../../../src/scale/helper'; +import { getPrecisionSafe } from '../../../../src/util/number'; + describe('scale_interval', function () { - var chart; + + let chart: EChartsType; beforeEach(function () { - chart = utHelper.createChart(); + chart = createChart(); }); afterEach(function () { @@ -37,9 +41,9 @@ describe('scale_interval', function () { describe('extreme', function () { it('ticks_min_max', function () { - var min = 0; - var max = 54.090909; - var splitNumber = 5; + const min = 0; + const max = 54.090909; + const splitNumber = 5; chart.setOption({ xAxis: {}, @@ -53,12 +57,12 @@ describe('scale_interval', function () { series: [{type: 'line', data: []}] }); - var yAxis = chart.getModel().getComponent('yAxis', 0); - var scale = yAxis.axis.scale; - var ticks = scale.getTicks(); + const yAxis = chart.getModel().getComponent('yAxis', 0) as CartesianAxisModel; + const scale = yAxis.axis.scale; + const ticks = scale.getTicks(); - expect(ticks[0]).toEqual(min); - expect(ticks[ticks.length - 1]).toEqual(max); + expect(ticks[0].value).toEqual(min); + expect(ticks[ticks.length - 1].value).toEqual(max); }); it('ticks_small_value', function () { @@ -87,16 +91,16 @@ describe('scale_interval', function () { ] }); - var yAxis = chart.getModel().getComponent('yAxis', 0); - var scale = yAxis.axis.scale; - var ticks = scale.getTicks(); - var labels = yAxis.axis.getViewLabels().map(function (item) { + const yAxis = chart.getModel().getComponent('yAxis', 0) as CartesianAxisModel; + const scale = yAxis.axis.scale as IntervalScale; + const ticks = scale.getTicks(); + const labels = yAxis.axis.getViewLabels().map(function (item) { return item.formattedLabel; }); - var labelPrecisioned = scale.getLabel(0.0000005, {precision: 10}); + const labelPrecisioned = scale.getLabel({ value: 0.0000005 }, { precision: 10 }); - expect(ticks).toEqual( + expect(ticks.map(tick => tick.value)).toEqual( [0, 0.0000005, 0.000001, 0.0000015, 0.000002, 0.0000025, 0.000003, 0.0000035] ); expect(labels).toEqual( @@ -110,51 +114,51 @@ describe('scale_interval', function () { describe('ticks', function () { - function randomNumber(quantity) { + function randomNumber(quantity: number): number { return (Math.random() - 0.5) * Math.pow(10, (Math.random() - 0.5) * quantity); } - function doSingleTest(extent, splitNumber) { - var result = scaleHelper.intervalScaleNiceTicks(extent, splitNumber); - var intervalPrecision = result.intervalPrecision; - var interval = result.interval; - var niceTickExtent = result.niceTickExtent; + function doSingleTest(extent: [number, number], splitNumber: number): void { + const result = intervalScaleNiceTicks(extent, splitNumber); + const intervalPrecision = result.intervalPrecision; + const resultInterval = result.interval; + const niceTickExtent = result.niceTickExtent; - expect(interval).toBeFinite(); - expect(intervalPrecision).toBeFinite(); - expect(niceTickExtent[0]).toBeFinite(); - expect(niceTickExtent[1]).toBeFinite(); + expect(isValueFinite(resultInterval)).toEqual(true); + expect(isValueFinite(intervalPrecision)).toEqual(true); + expect(isValueFinite(niceTickExtent[0])).toEqual(true); + expect(isValueFinite(niceTickExtent[1])).toEqual(true); - expect(niceTickExtent[0]).toBeGeaterThanOrEqualTo(extent[0]); + expect(niceTickExtent[0]).toBeGreaterThanOrEqual(extent[0]); expect(niceTickExtent[1]).not.toBeGreaterThan(extent[1]); - expect(niceTickExtent[1]).toBeGeaterThanOrEqualTo(niceTickExtent[1]); + expect(niceTickExtent[1]).toBeGreaterThanOrEqual(niceTickExtent[1]); - var interval = new IntervalScale(); + const interval = new IntervalScale(); interval.setExtent(extent[0], extent[1]); interval.niceExtent({ fixMin: true, fixMax: true, splitNumber }); - var ticks = interval.getTicks(); + const ticks = interval.getTicks(); - expect(ticks).not.toBeEmptyArray(); - expect(ticks[0]).toEqual(extent[0]); - expect(ticks[ticks.length - 1]).toEqual(extent[1]); + expect(ticks.length > 0); + expect(ticks[0].value).toEqual(extent[0]); + expect(ticks[ticks.length - 1].value).toEqual(extent[1]); - for (var i = 1; i < ticks.length; i++) { - expect(ticks[i - 1]).not.toBeGeaterThanOrEqualTo(ticks[i]); + for (let i = 1; i < ticks.length; i++) { + expect(ticks[i - 1].value).not.toBeGreaterThanOrEqual(ticks[i].value); - if (ticks[i] !== extent[0] && ticks[i] !== extent[1]) { - var tickPrecision = numberUtil.getPrecisionSafe(ticks[i]); + if (ticks[i].value !== extent[0] && ticks[i].value !== extent[1]) { + const tickPrecision = getPrecisionSafe(ticks[i].value); expect(tickPrecision).not.toBeGreaterThan(intervalPrecision); } } } - function doRandomTest(count, splitNumber, quantity) { - for (var i = 0; i < count; i++) { - var extent = []; + function doRandomTest(count: number, splitNumber: number, quantity: number): void { + for (let i = 0; i < count; i++) { + const extent: number[] = []; extent[0] = randomNumber(quantity); extent[1] = extent[0] + randomNumber(quantity); if (extent[1] === extent[0]) { @@ -163,7 +167,7 @@ describe('scale_interval', function () { if (extent[0] > extent[1]) { extent.reverse(); } - doSingleTest(extent, splitNumber); + doSingleTest(extent as [number, number], splitNumber); } } diff --git a/test/ut/spec/util/graphic.test.ts b/test/ut/spec/util/graphic.test.ts index d68df349b..5a3b1962d 100755 --- a/test/ut/spec/util/graphic.test.ts +++ b/test/ut/spec/util/graphic.test.ts @@ -18,67 +18,70 @@ * under the License. */ -const graphic = require('../../../../lib/util/graphic'); +import { + subPixelOptimize, subPixelOptimizeLine, subPixelOptimizeRect +} from 'zrender/src/graphic/helper/subPixelOptimize'; +import { lineLineIntersect } from '../../../../src/util/graphic'; + describe('util/graphic', function () { describe('subPixelOptimize', function () { - it('subPixelOptimize_base', function (done) { - expect(graphic.subPixelOptimize(5, 1)).toEqual(4.5); - expect(graphic.subPixelOptimize(5, 2)).toEqual(5); - expect(graphic.subPixelOptimize(5, 43)).toEqual(4.5); - expect(graphic.subPixelOptimize(7.5, 1)).toEqual(7.5); - expect(graphic.subPixelOptimize(7.5, 2)).toEqual(7); - expect(graphic.subPixelOptimize(14, 1, true)).toEqual(14.5); - expect(graphic.subPixelOptimize(14, 2, true)).toEqual(14); - expect(graphic.subPixelOptimize(-11, 1)).toEqual(-11.5); - expect(graphic.subPixelOptimize(-11, 2)).toEqual(-11); - expect(graphic.subPixelOptimize(0, 2)).toEqual(0); - expect(graphic.subPixelOptimize(0, 1)).toEqual(-0.5); - expect(graphic.subPixelOptimize(5, 0)).toEqual(5); - done(); + it('subPixelOptimize_base', function () { + expect(subPixelOptimize(5, 1)).toEqual(4.5); + expect(subPixelOptimize(5, 2)).toEqual(5); + expect(subPixelOptimize(5, 43)).toEqual(4.5); + expect(subPixelOptimize(7.5, 1)).toEqual(7.5); + expect(subPixelOptimize(7.5, 2)).toEqual(7); + expect(subPixelOptimize(14, 1, true)).toEqual(14.5); + expect(subPixelOptimize(14, 2, true)).toEqual(14); + expect(subPixelOptimize(-11, 1)).toEqual(-11.5); + expect(subPixelOptimize(-11, 2)).toEqual(-11); + expect(subPixelOptimize(0, 2)).toEqual(0); + expect(subPixelOptimize(0, 1)).toEqual(-0.5); + expect(subPixelOptimize(5, 0)).toEqual(5); }); - it('subPixelOptimize_line', function (done) { - function doSubPixelOptimizeLine(x, y, width, height, lineWidth) { - return graphic.subPixelOptimizeLine(makeParam(x, y, width, height, lineWidth)); + it('subPixelOptimize_line', function () { + function doSubPixelOptimizeLine(x: number, y: number, width: number, height: number, lineWidth: number) { + const params = makeParam(x, y, width, height, lineWidth); + return subPixelOptimizeLine(params.shape, params.shape, params.style); } - function makeParam(x1, y1, x2, y2, lineWidth) { + function makeParam(x1: number, y1: number, x2: number, y2: number, lineWidth: number) { return { shape: {x1: x1, y1: y1, x2: x2, y2: y2}, style: {lineWidth: lineWidth} }; } - expect(doSubPixelOptimizeLine(5, 11, 3, 7, 1)).toEqual(makeParam(5, 11, 3, 7, 1)); - expect(doSubPixelOptimizeLine(5, 11, 5, 7, 1)).toEqual(makeParam(5.5, 11, 5.5, 7, 1)); - expect(doSubPixelOptimizeLine(5, 11, 5, 7, 2)).toEqual(makeParam(5, 11, 5, 7, 2)); - expect(doSubPixelOptimizeLine(5, 11, 15, 11, 1)).toEqual(makeParam(5, 11.5, 15, 11.5, 1)); - expect(doSubPixelOptimizeLine(5, 11, 15, 11, 2)).toEqual(makeParam(5, 11, 15, 11, 2)); - expect(doSubPixelOptimizeLine(5, 11, 15, 11, 3)).toEqual(makeParam(5, 11.5, 15, 11.5, 3)); - expect(doSubPixelOptimizeLine(5, 11, 15, 11.5, 3)).toEqual(makeParam(5, 11, 15, 11.5, 3)); - expect(doSubPixelOptimizeLine(5, 11.5, 15, 11.5, 3)).toEqual(makeParam(5, 11.5, 15, 11.5, 3)); - expect(doSubPixelOptimizeLine(5, 11.5, 15, 11.5, 4)).toEqual(makeParam(5, 12, 15, 12, 4)); - done(); + expect(doSubPixelOptimizeLine(5, 11, 3, 7, 1)).toEqual(makeParam(5, 11, 3, 7, 1).shape); + expect(doSubPixelOptimizeLine(5, 11, 5, 7, 1)).toEqual(makeParam(5.5, 11, 5.5, 7, 1).shape); + expect(doSubPixelOptimizeLine(5, 11, 5, 7, 2)).toEqual(makeParam(5, 11, 5, 7, 2).shape); + expect(doSubPixelOptimizeLine(5, 11, 15, 11, 1)).toEqual(makeParam(5, 11.5, 15, 11.5, 1).shape); + expect(doSubPixelOptimizeLine(5, 11, 15, 11, 2)).toEqual(makeParam(5, 11, 15, 11, 2).shape); + expect(doSubPixelOptimizeLine(5, 11, 15, 11, 3)).toEqual(makeParam(5, 11.5, 15, 11.5, 3).shape); + expect(doSubPixelOptimizeLine(5, 11, 15, 11.5, 3)).toEqual(makeParam(5, 11, 15, 11.5, 3).shape); + expect(doSubPixelOptimizeLine(5, 11.5, 15, 11.5, 3)).toEqual(makeParam(5, 11.5, 15, 11.5, 3).shape); + expect(doSubPixelOptimizeLine(5, 11.5, 15, 11.5, 4)).toEqual(makeParam(5, 12, 15, 12, 4).shape); }); - it('subPixelOptimize_rect', function (done) { - function doSubPixelOptimizeRect(x, y, width, height, lineWidth) { - return graphic.subPixelOptimizeRect(makeParam(x, y, width, height, lineWidth)); + it('subPixelOptimize_rect', function () { + function doSubPixelOptimizeRect(x: number, y: number, width: number, height: number, lineWidth: number) { + const params = makeParam(x, y, width, height, lineWidth); + return subPixelOptimizeRect(params.shape, params.shape, params.style); } - function makeParam(x, y, width, height, lineWidth) { + function makeParam(x: number, y: number, width: number, height: number, lineWidth: number) { return { shape: {x: x, y: y, width: width, height: height}, style: {lineWidth: lineWidth} }; } - expect(doSubPixelOptimizeRect(5, 11, 3, 7, 1)).toEqual(makeParam(5.5, 11.5, 2, 6, 1)); - expect(doSubPixelOptimizeRect(5, 11, 3, 7, 2)).toEqual(makeParam(5, 11, 3, 7, 2)); - expect(doSubPixelOptimizeRect(5, 11, 3, 7, 3)).toEqual(makeParam(5.5, 11.5, 2, 6, 3)); + expect(doSubPixelOptimizeRect(5, 11, 3, 7, 1)).toEqual(makeParam(5.5, 11.5, 2, 6, 1).shape); + expect(doSubPixelOptimizeRect(5, 11, 3, 7, 2)).toEqual(makeParam(5, 11, 3, 7, 2).shape); + expect(doSubPixelOptimizeRect(5, 11, 3, 7, 3)).toEqual(makeParam(5.5, 11.5, 2, 6, 3).shape); // Boundary value tests - expect(doSubPixelOptimizeRect(5, 11, 1, 7, 1)).toEqual(makeParam(5.5, 11.5, 1, 6, 1)); - expect(doSubPixelOptimizeRect(5, 11, 1, 0, 1)).toEqual(makeParam(5.5, 11.5, 1, 0, 1)); - done(); + expect(doSubPixelOptimizeRect(5, 11, 1, 7, 1)).toEqual(makeParam(5.5, 11.5, 1, 6, 1).shape); + expect(doSubPixelOptimizeRect(5, 11, 1, 0, 1)).toEqual(makeParam(5.5, 11.5, 1, 0, 1).shape); }); }); @@ -86,20 +89,20 @@ describe('util/graphic', function () { describe('lineLineIntersect', function () { it('extreme', function () { - expect(graphic.lineLineIntersect(10, 10, 30, 30, 10, 10, 10, 10)).toEqual(false); + expect(lineLineIntersect(10, 10, 30, 30, 10, 10, 10, 10)).toEqual(false); }); it('parallel and colinear', function () { - expect(graphic.lineLineIntersect(10, 20, 30, 40, 100, 220, 120, 240)).toEqual(false); - expect(graphic.lineLineIntersect(10, 10, 30, 30, 40, 40, 50, 50)).toEqual(false); - expect(graphic.lineLineIntersect(10, 10, 30, 30, 10, 10, 30, 30)).toEqual(false); - expect(graphic.lineLineIntersect(10, 10, 30, 30, 20, 20, 30, 30)).toEqual(false); - expect(graphic.lineLineIntersect(10, 10, 30, 30, 20, 20, 22, 22)).toEqual(false); + expect(lineLineIntersect(10, 20, 30, 40, 100, 220, 120, 240)).toEqual(false); + expect(lineLineIntersect(10, 10, 30, 30, 40, 40, 50, 50)).toEqual(false); + expect(lineLineIntersect(10, 10, 30, 30, 10, 10, 30, 30)).toEqual(false); + expect(lineLineIntersect(10, 10, 30, 30, 20, 20, 30, 30)).toEqual(false); + expect(lineLineIntersect(10, 10, 30, 30, 20, 20, 22, 22)).toEqual(false); }); it('intersect', function () { - expect(graphic.lineLineIntersect(10, 20, 30, 40, 12, 20, 30, 40)).toEqual(true); - expect(graphic.lineLineIntersect(10, 20, 30, 40, 12, 20, 20, 42)).toEqual(true); + expect(lineLineIntersect(10, 20, 30, 40, 12, 20, 30, 40)).toEqual(true); + expect(lineLineIntersect(10, 20, 30, 40, 12, 20, 20, 42)).toEqual(true); }); }); diff --git a/test/ut/spec/util/layout.test.ts b/test/ut/spec/util/layout.test.ts index 46ab3d210..4c43ceb17 100644 --- a/test/ut/spec/util/layout.test.ts +++ b/test/ut/spec/util/layout.test.ts @@ -17,22 +17,25 @@ * specific language governing permissions and limitations * under the License. */ -const layoutUtil = require('../../../../lib/util/layout'); + +// import { Dictionary } from 'zrender/src/core/types'; +import { mergeLayoutParam } from '../../../../src/util/layout'; +import { BoxLayoutOptionMixin } from '../../../../src/util/types'; describe('util/number', function () { describe('mergeLayoutParam', function () { // The given obj has exactly the given props, has no other props. - function expectPropsEqual(obj, props) { + function expectPropsEqual(obj: object, props: object): void { expect(propContain(obj, props) && propContain(props, obj)).toEqual(true); } - function propContain(more, less) { - for (var key in more) { + function propContain(more: object, less: object): boolean { + for (const key in more) { if (more.hasOwnProperty(key)) { - if (more[key] !== less[key] - && !(more[key] == null && less[key] == null) + if ((more as any)[key] !== (less as any)[key] + && !((more as any)[key] == null && (less as any)[key] == null) ) { return false; } @@ -41,9 +44,9 @@ describe('util/number', function () { return true; } - function shadowClone(obj) { - var newObj = {}; - for (var key in obj) { + function shadowClone(obj: T): T { + const newObj = {} as T; + for (const key in obj) { if (obj.hasOwnProperty(key)) { newObj[key] = obj[key]; } @@ -53,18 +56,23 @@ describe('util/number', function () { it('all', function () { - function testMerge(targetOption, newOption, result, resultIgnoreSize) { - var t1 = shadowClone(targetOption); - var t2 = shadowClone(targetOption); - var n1 = shadowClone(newOption); - var n2 = shadowClone(newOption); - layoutUtil.mergeLayoutParam(t1, n1); - layoutUtil.mergeLayoutParam(t2, n2, {ignoreSize: true}); + function testMerge( + targetOption: BoxLayoutOptionMixin, + newOption: BoxLayoutOptionMixin, + result: BoxLayoutOptionMixin, + resultIgnoreSize?: BoxLayoutOptionMixin + ) { + const t1 = shadowClone(targetOption); + const t2 = shadowClone(targetOption); + const n1 = shadowClone(newOption); + const n2 = shadowClone(newOption); + mergeLayoutParam(t1, n1); + mergeLayoutParam(t2, n2, {ignoreSize: true}); expectPropsEqual(t1, result); expectPropsEqual(t2, resultIgnoreSize || result); } - function singleValueAdd(val) { + function singleValueAdd(val: number | string): void { testMerge({}, {width: val}, {width: val}); testMerge({}, {left: val}, {left: val}); testMerge({}, {right: val}, {right: val}); @@ -79,7 +87,7 @@ describe('util/number', function () { singleValueAdd('right'); singleValueAdd('center'); - function singleValueReplace(val) { + function singleValueReplace(val: number | string): void { testMerge({width: -999}, {width: val}, {width: val}); testMerge({left: -999}, {left: val}, {left: val}); testMerge({right: -999}, {right: val}, {right: val}); diff --git a/test/ut/spec/util/model.test.ts b/test/ut/spec/util/model.test.ts index 8decb98e5..bde88823b 100755 --- a/test/ut/spec/util/model.test.ts +++ b/test/ut/spec/util/model.test.ts @@ -18,20 +18,21 @@ * under the License. */ -const modelUtil = require('../../../../lib/util/model'); +import { compressBatches } from '../../../../src/util/model'; + describe('util/model', function () { describe('compressBatches', function () { - function item(seriesId, dataIndex) { + function item(seriesId: number | string, dataIndex: number | number[]) { return {seriesId, dataIndex}; } it('base', function () { // Remove dupliate between A and B - expect(modelUtil.compressBatches( + expect(compressBatches( [item(3, 4), item(3, 5), item(4, 5)], [item(4, 6), item(4, 5), item(3, 3), item(3, 4)] )).toEqual([ @@ -40,7 +41,7 @@ describe('util/model', function () { ]); // Compress - expect(modelUtil.compressBatches( + expect(compressBatches( [item(3, 4), item(3, 6), item(3, 5), item(4, 5)], [item(4, 6), item(4, 5), item(3, 3), item(3, 4), item(4, 7)] )).toEqual([ @@ -49,7 +50,7 @@ describe('util/model', function () { ]); // Remove duplicate in themselves - expect(modelUtil.compressBatches( + expect(compressBatches( [item(3, 4), item(3, 6), item(3, 5), item(4, 5)], [item(4, 6), item(4, 5), item(3, 3), item(3, 4), item(4, 7), item(4, 6)] )).toEqual([ @@ -58,7 +59,7 @@ describe('util/model', function () { ]); // dataIndex is array - expect(modelUtil.compressBatches( + expect(compressBatches( [item(3, [4, 5, 8]), item(4, 4), item(3, [5, 7, 7])], [item(3, [8, 9])] )).toEqual([ @@ -67,14 +68,14 @@ describe('util/model', function () { ]); // empty - expect(modelUtil.compressBatches( + expect(compressBatches( [item(3, [4, 5, 8]), item(4, 4), item(3, [5, 7, 7])], [] )).toEqual([ [item('3', [4, 5, 7, 8]), item('4', [4])], [] ]); - expect(modelUtil.compressBatches( + expect(compressBatches( [], [item(3, [4, 5, 8]), item(4, 4), item(3, [5, 7, 7])] )).toEqual([ @@ -83,7 +84,7 @@ describe('util/model', function () { ]); // should not has empty array - expect(modelUtil.compressBatches( + expect(compressBatches( [item(3, [4, 5, 8])], [item(3, [4, 5, 8])] )).toEqual([ diff --git a/test/ut/spec/util/number.test.ts b/test/ut/spec/util/number.test.ts index 205b59a18..1e5d2d232 100755 --- a/test/ut/spec/util/number.test.ts +++ b/test/ut/spec/util/number.test.ts @@ -18,147 +18,161 @@ * under the License. */ -const numberUtil = require('../../../../lib/util/number'); +import { + linearMap, parseDate, reformIntervals, getPrecisionSafe, + getPercentWithPrecision, quantityExponent, quantity, nice, + isNumeric, numericToNumber +} from '../../../../src/util/number'; + describe('util/number', function () { describe('linearMap', function () { it('accuracyError', function () { - var range = [-15918.3, 17724.9]; - var result = numberUtil.linearMap(100, [0, 100], range, true); + let range; + let result; + + range = [-15918.3, 17724.9]; + result = linearMap(100, [0, 100], range, true); // Should not be 17724.899999999998. expect(result).toEqual(range[1]); - var range = [-62.83, 83.56]; - var result = numberUtil.linearMap(100, [0, 100], range, true); + range = [-62.83, 83.56]; + result = linearMap(100, [0, 100], range, true); // Should not be 83.55999999999999. expect(result).toEqual(range[1]); }); it('clamp', function () { + let range; + let result; + // (1) normal order. - var range = [-15918.3, 17724.9]; + range = [-15918.3, 17724.9]; // bigger than max - var result = numberUtil.linearMap(100.1, [0, 100], range, true); + result = linearMap(100.1, [0, 100], range, true); expect(result).toEqual(range[1]); // smaller than min - var result = numberUtil.linearMap(-2, [0, 100], range, true); + result = linearMap(-2, [0, 100], range, true); expect(result).toEqual(range[0]); // equals to max - var result = numberUtil.linearMap(100, [0, 100], range, true); + result = linearMap(100, [0, 100], range, true); expect(result).toEqual(range[1]); // equals to min - var result = numberUtil.linearMap(0, [0, 100], range, true); + result = linearMap(0, [0, 100], range, true); expect(result).toEqual(range[0]); // (2) inverse range - var range = [17724.9, -15918.3]; + range = [17724.9, -15918.3]; // bigger than max - var result = numberUtil.linearMap(102, [0, 100], range, true); + result = linearMap(102, [0, 100], range, true); expect(result).toEqual(range[1]); // smaller than min - var result = numberUtil.linearMap(-0.001, [0, 100], range, true); + result = linearMap(-0.001, [0, 100], range, true); expect(result).toEqual(range[0]); // equals to max - var result = numberUtil.linearMap(100, [0, 100], range, true); + result = linearMap(100, [0, 100], range, true); expect(result).toEqual(range[1]); // equals to min - var result = numberUtil.linearMap(0, [0, 100], range, true); + result = linearMap(0, [0, 100], range, true); expect(result).toEqual(range[0]); // (2) inverse domain // bigger than max, inverse domain - var range = [-15918.3, 17724.9]; + range = [-15918.3, 17724.9]; // bigger than max - var result = numberUtil.linearMap(102, [100, 0], range, true); + result = linearMap(102, [100, 0], range, true); expect(result).toEqual(range[0]); // smaller than min - var result = numberUtil.linearMap(-0.001, [100, 0], range, true); + result = linearMap(-0.001, [100, 0], range, true); expect(result).toEqual(range[1]); // equals to max - var result = numberUtil.linearMap(100, [100, 0], range, true); + result = linearMap(100, [100, 0], range, true); expect(result).toEqual(range[0]); // equals to min - var result = numberUtil.linearMap(0, [100, 0], range, true); + result = linearMap(0, [100, 0], range, true); expect(result).toEqual(range[1]); // (3) inverse domain, inverse range - var range = [17724.9, -15918.3]; + range = [17724.9, -15918.3]; // bigger than max - var result = numberUtil.linearMap(100.1, [100, 0], range, true); + result = linearMap(100.1, [100, 0], range, true); expect(result).toEqual(range[0]); // smaller than min - var result = numberUtil.linearMap(-2, [100, 0], range, true); + result = linearMap(-2, [100, 0], range, true); expect(result).toEqual(range[1]); // equals to max - var result = numberUtil.linearMap(100, [100, 0], range, true); + result = linearMap(100, [100, 0], range, true); expect(result).toEqual(range[0]); // equals to min - var result = numberUtil.linearMap(0, [100, 0], range, true); + result = linearMap(0, [100, 0], range, true); expect(result).toEqual(range[1]); }); it('noClamp', function () { + let range; + let result; + // (1) normal order. - var range = [-15918.3, 17724.9]; + range = [-15918.3, 17724.9]; // bigger than max - var result = numberUtil.linearMap(100.1, [0, 100], range, false); + result = linearMap(100.1, [0, 100], range, false); expect(result).toEqual(17758.543199999996); // smaller than min - var result = numberUtil.linearMap(-2, [0, 100], range, false); + result = linearMap(-2, [0, 100], range, false); expect(result).toEqual(-16591.164); // equals to max - var result = numberUtil.linearMap(100, [0, 100], range, false); + result = linearMap(100, [0, 100], range, false); expect(result).toEqual(17724.9); // equals to min - var result = numberUtil.linearMap(0, [0, 100], range, false); + result = linearMap(0, [0, 100], range, false); expect(result).toEqual(-15918.3); // (2) inverse range - var range = [17724.9, -15918.3]; + range = [17724.9, -15918.3]; // bigger than max - var result = numberUtil.linearMap(102, [0, 100], range, false); + result = linearMap(102, [0, 100], range, false); expect(result).toEqual(-16591.163999999997); // smaller than min - var result = numberUtil.linearMap(-0.001, [0, 100], range, false); + result = linearMap(-0.001, [0, 100], range, false); expect(result).toEqual(17725.236432); // equals to max - var result = numberUtil.linearMap(100, [0, 100], range, false); + result = linearMap(100, [0, 100], range, false); expect(result).toEqual(-15918.3); // equals to min - var result = numberUtil.linearMap(0, [0, 100], range, false); + result = linearMap(0, [0, 100], range, false); expect(result).toEqual(17724.9); // (2) inverse domain // bigger than max, inverse domain - var range = [-15918.3, 17724.9]; + range = [-15918.3, 17724.9]; // bigger than max - var result = numberUtil.linearMap(102, [100, 0], range, false); + result = linearMap(102, [100, 0], range, false); expect(result).toEqual(-16591.164); // smaller than min - var result = numberUtil.linearMap(-0.001, [100, 0], range, false); + result = linearMap(-0.001, [100, 0], range, false); expect(result).toEqual(17725.236432); // equals to max - var result = numberUtil.linearMap(100, [100, 0], range, false); + result = linearMap(100, [100, 0], range, false); expect(result).toEqual(-15918.3); // equals to min - var result = numberUtil.linearMap(0, [100, 0], range, false); + result = linearMap(0, [100, 0], range, false); expect(result).toEqual(17724.9); // (3) inverse domain, inverse range - var range = [17724.9, -15918.3]; + range = [17724.9, -15918.3]; // bigger than max - var result = numberUtil.linearMap(100.1, [100, 0], range, false); + result = linearMap(100.1, [100, 0], range, false); expect(result).toEqual(17758.5432); // smaller than min - var result = numberUtil.linearMap(-2, [100, 0], range, false); + result = linearMap(-2, [100, 0], range, false); expect(result).toEqual(-16591.163999999997); // equals to max - var result = numberUtil.linearMap(100, [100, 0], range, false); + result = linearMap(100, [100, 0], range, false); expect(result).toEqual(17724.9); // equals to min - var result = numberUtil.linearMap(0, [100, 0], range, false); + result = linearMap(0, [100, 0], range, false); expect(result).toEqual(-15918.3); }); @@ -167,25 +181,28 @@ describe('util/number', function () { doTest(true); doTest(false); - function doTest(clamp) { + function doTest(clamp: boolean) { + let range; + let result; + // normal - var range = [444, 555]; - var result = numberUtil.linearMap(40, [0, 100], range, clamp); + range = [444, 555]; + result = linearMap(40, [0, 100], range, clamp); expect(result).toEqual(488.4); // inverse range - var range = [555, 444]; - var result = numberUtil.linearMap(40, [0, 100], range, clamp); + range = [555, 444]; + result = linearMap(40, [0, 100], range, clamp); expect(result).toEqual(510.6); // inverse domain and range - var range = [555, 444]; - var result = numberUtil.linearMap(40, [100, 0], range, clamp); + range = [555, 444]; + result = linearMap(40, [100, 0], range, clamp); expect(result).toEqual(488.4); // inverse domain - var range = [444, 555]; - var result = numberUtil.linearMap(40, [100, 0], range, clamp); + range = [444, 555]; + result = linearMap(40, [100, 0], range, clamp); expect(result).toEqual(510.6); } }); @@ -195,20 +212,23 @@ describe('util/number', function () { doTest(true); doTest(false); - function doTest(clamp) { + function doTest(clamp: boolean) { + let range; + let result; + // zero domain interval - var range = [444, 555]; - var result = numberUtil.linearMap(40, [1212222223.2323232, 1212222223.2323232], range, clamp); + range = [444, 555]; + result = linearMap(40, [1212222223.2323232, 1212222223.2323232], range, clamp); expect(result).toEqual(499.5); // half of range. // zero range interval - var range = [1221212.1221372238, 1221212.1221372238]; - var result = numberUtil.linearMap(40, [0, 100], range, clamp); + range = [1221212.1221372238, 1221212.1221372238]; + result = linearMap(40, [0, 100], range, clamp); expect(result).toEqual(1221212.1221372238); // zero domain interval and range interval - var range = [1221212.1221372238, 1221212.1221372238]; - var result = numberUtil.linearMap(40, [43.55454545, 43.55454545], range, clamp); + range = [1221212.1221372238, 1221212.1221372238]; + result = linearMap(40, [43.55454545, 43.55454545], range, clamp); expect(result).toEqual(1221212.1221372238); } }); @@ -218,53 +238,53 @@ describe('util/number', function () { it('parseDate', function () { // Invalid Date - expect('' + numberUtil.parseDate(null)).toEqual('Invalid Date'); - expect('' + numberUtil.parseDate(void 0)).toEqual('Invalid Date'); - expect('' + numberUtil.parseDate('asdf')).toEqual('Invalid Date'); - expect('' + numberUtil.parseDate(NaN)).toEqual('Invalid Date'); - expect('' + numberUtil.parseDate('-')).toEqual('Invalid Date'); - expect('' + numberUtil.parseDate('20120304')).toEqual('Invalid Date'); + expect('' + parseDate(null)).toEqual('Invalid Date'); + expect('' + parseDate(void 0)).toEqual('Invalid Date'); + expect('' + parseDate('asdf')).toEqual('Invalid Date'); + expect('' + parseDate(NaN)).toEqual('Invalid Date'); + expect('' + parseDate('-')).toEqual('Invalid Date'); + expect('' + parseDate('20120304')).toEqual('Invalid Date'); // Input instance of Date or timestamp - expect(+numberUtil.parseDate(new Date('2012-03-04'))).toEqual(1330819200000); - expect(+numberUtil.parseDate(1330819200000)).toEqual(1330819200000); - expect(+numberUtil.parseDate(1330819199999.99)).toEqual(1330819200000); - expect(+numberUtil.parseDate(1330819200000.01)).toEqual(1330819200000); + expect(+parseDate(new Date('2012-03-04'))).toEqual(1330819200000); + expect(+parseDate(1330819200000)).toEqual(1330819200000); + expect(+parseDate(1330819199999.99)).toEqual(1330819200000); + expect(+parseDate(1330819200000.01)).toEqual(1330819200000); // ISO string - expect(+numberUtil.parseDate('2012-03')).toEqual(+new Date('2012-03-01T00:00:00')); - expect(+numberUtil.parseDate('2012-03-04')).toEqual(+new Date('2012-03-04T00:00:00')); - expect(+numberUtil.parseDate('2012-03-04 05')).toEqual(+new Date('2012-03-04T05:00:00')); - expect(+numberUtil.parseDate('2012-03-04T05')).toEqual(+new Date('2012-03-04T05:00:00')); - expect(+numberUtil.parseDate('2012-03-04 05:06')).toEqual(+new Date('2012-03-04T05:06:00')); - expect(+numberUtil.parseDate('2012-03-04T05:06')).toEqual(+new Date('2012-03-04T05:06:00')); - expect(+numberUtil.parseDate('2012-03-04 05:06:07')).toEqual(+new Date('2012-03-04T05:06:07')); - expect(+numberUtil.parseDate('2012-03-04T05:06:07')).toEqual(+new Date('2012-03-04T05:06:07')); - expect(+numberUtil.parseDate('2012-03-04T05:06:07.123')).toEqual(+new Date('2012-03-04T05:06:07.123')); - expect(+numberUtil.parseDate('2012-03-04T05:06:07,123')).toEqual(+new Date('2012-03-04T05:06:07.123')); + expect(+parseDate('2012-03')).toEqual(+new Date('2012-03-01T00:00:00')); + expect(+parseDate('2012-03-04')).toEqual(+new Date('2012-03-04T00:00:00')); + expect(+parseDate('2012-03-04 05')).toEqual(+new Date('2012-03-04T05:00:00')); + expect(+parseDate('2012-03-04T05')).toEqual(+new Date('2012-03-04T05:00:00')); + expect(+parseDate('2012-03-04 05:06')).toEqual(+new Date('2012-03-04T05:06:00')); + expect(+parseDate('2012-03-04T05:06')).toEqual(+new Date('2012-03-04T05:06:00')); + expect(+parseDate('2012-03-04 05:06:07')).toEqual(+new Date('2012-03-04T05:06:07')); + expect(+parseDate('2012-03-04T05:06:07')).toEqual(+new Date('2012-03-04T05:06:07')); + expect(+parseDate('2012-03-04T05:06:07.123')).toEqual(+new Date('2012-03-04T05:06:07.123')); + expect(+parseDate('2012-03-04T05:06:07,123')).toEqual(+new Date('2012-03-04T05:06:07.123')); // TODO new Date('2012-03-04T05:06:07.12') is same to '2012-03-04T05:06:07.120', not '2012-03-04T05:06:07.012' - expect(+numberUtil.parseDate('2012-03-04T05:06:07.12')).toEqual(+new Date('2012-03-04T05:06:07.012')); - expect(+numberUtil.parseDate('2012-03-04T05:06:07.1')).toEqual(+new Date('2012-03-04T05:06:07.001')); - expect(+numberUtil.parseDate('2012-03-04T05:06:07,123Z')).toEqual(+new Date('2012-03-04T05:06:07.123Z')); - expect(+numberUtil.parseDate('2012-03-04T05:06:07.123+0800')).toEqual(1330808767123); - expect(+numberUtil.parseDate('2012-03-04T05:06:07.123+08:00')).toEqual(1330808767123); - expect(+numberUtil.parseDate('2012-03-04T05:06:07.123-0700')).toEqual(1330862767123); - expect(+numberUtil.parseDate('2012-03-04T05:06:07.123-07:00')).toEqual(1330862767123); - expect(+numberUtil.parseDate('2012-03-04T5:6:7.123-07:00')).toEqual(1330862767123); + expect(+parseDate('2012-03-04T05:06:07.12')).toEqual(+new Date('2012-03-04T05:06:07.012')); + expect(+parseDate('2012-03-04T05:06:07.1')).toEqual(+new Date('2012-03-04T05:06:07.001')); + expect(+parseDate('2012-03-04T05:06:07,123Z')).toEqual(+new Date('2012-03-04T05:06:07.123Z')); + expect(+parseDate('2012-03-04T05:06:07.123+0800')).toEqual(1330808767123); + expect(+parseDate('2012-03-04T05:06:07.123+08:00')).toEqual(1330808767123); + expect(+parseDate('2012-03-04T05:06:07.123-0700')).toEqual(1330862767123); + expect(+parseDate('2012-03-04T05:06:07.123-07:00')).toEqual(1330862767123); + expect(+parseDate('2012-03-04T5:6:7.123-07:00')).toEqual(1330862767123); // Other string - expect(+numberUtil.parseDate('2012')).toEqual(+new Date('2012-01-01T00:00:00')); - expect(+numberUtil.parseDate('2012/03')).toEqual(+new Date('2012-03-01T00:00:00')); - expect(+numberUtil.parseDate('2012/03/04')).toEqual(+new Date('2012-03-04T00:00:00')); - expect(+numberUtil.parseDate('2012-3-4')).toEqual(+new Date('2012-03-04T00:00:00')); - expect(+numberUtil.parseDate('2012/3')).toEqual(+new Date('2012-03-01T00:00:00')); - expect(+numberUtil.parseDate('2012/3/4')).toEqual(+new Date('2012-03-04T00:00:00')); - expect(+numberUtil.parseDate('2012/3/4 2:05')).toEqual(+new Date('2012-03-04T02:05:00')); - expect(+numberUtil.parseDate('2012/03/04 2:05')).toEqual(+new Date('2012-03-04T02:05:00')); - expect(+numberUtil.parseDate('2012/3/4 2:05:08')).toEqual(+new Date('2012-03-04T02:05:08')); - expect(+numberUtil.parseDate('2012/03/04 2:05:08')).toEqual(+new Date('2012-03-04T02:05:08')); - expect(+numberUtil.parseDate('2012/3/4 2:05:08.123')).toEqual(+new Date('2012-03-04T02:05:08.123')); - expect(+numberUtil.parseDate('2012/03/04 2:05:08.123')).toEqual(+new Date('2012-03-04T02:05:08.123')); + expect(+parseDate('2012')).toEqual(+new Date('2012-01-01T00:00:00')); + expect(+parseDate('2012/03')).toEqual(+new Date('2012-03-01T00:00:00')); + expect(+parseDate('2012/03/04')).toEqual(+new Date('2012-03-04T00:00:00')); + expect(+parseDate('2012-3-4')).toEqual(+new Date('2012-03-04T00:00:00')); + expect(+parseDate('2012/3')).toEqual(+new Date('2012-03-01T00:00:00')); + expect(+parseDate('2012/3/4')).toEqual(+new Date('2012-03-04T00:00:00')); + expect(+parseDate('2012/3/4 2:05')).toEqual(+new Date('2012-03-04T02:05:00')); + expect(+parseDate('2012/03/04 2:05')).toEqual(+new Date('2012-03-04T02:05:00')); + expect(+parseDate('2012/3/4 2:05:08')).toEqual(+new Date('2012-03-04T02:05:08')); + expect(+parseDate('2012/03/04 2:05:08')).toEqual(+new Date('2012-03-04T02:05:08')); + expect(+parseDate('2012/3/4 2:05:08.123')).toEqual(+new Date('2012-03-04T02:05:08.123')); + expect(+parseDate('2012/03/04 2:05:08.123')).toEqual(+new Date('2012-03-04T02:05:08.123')); }); }); @@ -273,7 +293,7 @@ describe('util/number', function () { it('basic', function () { // all - expect(numberUtil.reformIntervals([ + expect(reformIntervals([ {interval: [18, 62], close: [1, 1]}, {interval: [-Infinity, -70], close: [0, 0]}, {interval: [-70, -26], close: [1, 1]}, @@ -291,7 +311,7 @@ describe('util/number', function () { ]); // remove overlap - expect(numberUtil.reformIntervals([ + expect(reformIntervals([ {interval: [18, 62], close: [1, 1]}, {interval: [50, 150], close: [1, 1]} ])).toEqual([ @@ -300,7 +320,7 @@ describe('util/number', function () { ]); // remove overlap on edge - expect(numberUtil.reformIntervals([ + expect(reformIntervals([ {interval: [18, 62], close: [1, 1]}, {interval: [62, 150], close: [1, 1]} ])).toEqual([ @@ -309,7 +329,7 @@ describe('util/number', function () { ]); // remove included interval - expect(numberUtil.reformIntervals([ + expect(reformIntervals([ {interval: [30, 40], close: [1, 1]}, {interval: [42, 54], close: [1, 1]}, {interval: [45, 60], close: [1, 1]}, @@ -319,7 +339,7 @@ describe('util/number', function () { ]); // remove edge - expect(numberUtil.reformIntervals([ + expect(reformIntervals([ {interval: [18, 62], close: [1, 1]}, {interval: [30, 62], close: [1, 1]} ])).toEqual([ @@ -331,16 +351,16 @@ describe('util/number', function () { describe('getPrecisionSafe', function () { it('basic', function () { - expect(numberUtil.getPrecisionSafe(10)).toEqual(0); - expect(numberUtil.getPrecisionSafe(1)).toEqual(0); - expect(numberUtil.getPrecisionSafe(0)).toEqual(0); - expect(numberUtil.getPrecisionSafe(100000000000000000000000000000)).toEqual(0); - expect(numberUtil.getPrecisionSafe(1e+100)).toEqual(0); - expect(numberUtil.getPrecisionSafe(0.1)).toEqual(1); - expect(numberUtil.getPrecisionSafe(0.100)).toEqual(1); - expect(numberUtil.getPrecisionSafe(0.0032)).toEqual(4); - expect(numberUtil.getPrecisionSafe(0.0000000000034)).toEqual(12); - expect(numberUtil.getPrecisionSafe(3.4e-10)).toEqual(10); + expect(getPrecisionSafe(10)).toEqual(0); + expect(getPrecisionSafe(1)).toEqual(0); + expect(getPrecisionSafe(0)).toEqual(0); + expect(getPrecisionSafe(100000000000000000000000000000)).toEqual(0); + expect(getPrecisionSafe(1e+100)).toEqual(0); + expect(getPrecisionSafe(0.1)).toEqual(1); + expect(getPrecisionSafe(0.100)).toEqual(1); + expect(getPrecisionSafe(0.0032)).toEqual(4); + expect(getPrecisionSafe(0.0000000000034)).toEqual(12); + expect(getPrecisionSafe(3.4e-10)).toEqual(10); }); }); @@ -349,102 +369,102 @@ describe('util/number', function () { // console.log(numberUtil.getPercentWithPrecision([-1.678, -4.783, -2.664, -0.875], 0, 2)); - // var arr = [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]; - var arr = [49.5, NaN]; - var result = []; - for (var i = 0; i < arr.length; i++) { + // const arr = [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]; + const arr = [49.5, NaN]; + const result = []; + for (let i = 0; i < arr.length; i++) { result.push( - numberUtil.getPercentWithPrecision(arr, i, 0) + getPercentWithPrecision(arr, i, 0) ); } - var sum = 0; - for (var i = 0; i < result.length; i++) { - sum += result[i]; - } - - expect(numberUtil.getPercentWithPrecision([50.5, 49.5], 0, 0)).toEqual(51); - expect(numberUtil.getPercentWithPrecision([50.5, 49.5], 1, 0)).toEqual(49); - - expect(numberUtil.getPercentWithPrecision([12.34, 34.56, 53.1], 0, 1)).toEqual(12.3); - expect(numberUtil.getPercentWithPrecision([12.34, 34.56, 53.1], 1, 1)).toEqual(34.6); - expect(numberUtil.getPercentWithPrecision([12.34, 34.56, 53.1], 2, 1)).toEqual(53.1); - - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 0, 0)).toEqual(17); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 1, 0)).toEqual(48); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 2, 0)).toEqual(26); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 3, 0)).toEqual(9); + // let sum = 0; + // for (let i = 0; i < result.length; i++) { + // sum += result[i]; + // } + + expect(getPercentWithPrecision([50.5, 49.5], 0, 0)).toEqual(51); + expect(getPercentWithPrecision([50.5, 49.5], 1, 0)).toEqual(49); + + expect(getPercentWithPrecision([12.34, 34.56, 53.1], 0, 1)).toEqual(12.3); + expect(getPercentWithPrecision([12.34, 34.56, 53.1], 1, 1)).toEqual(34.6); + expect(getPercentWithPrecision([12.34, 34.56, 53.1], 2, 1)).toEqual(53.1); + + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 0, 0)).toEqual(17); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 1, 0)).toEqual(48); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 2, 0)).toEqual(26); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875], 3, 0)).toEqual(9); }); it('NaN data', function () { - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-'], 0, 0)).toEqual(17); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-'], 1, 0)).toEqual(48); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-'], 2, 0)).toEqual(26); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-'], 3, 0)).toEqual(9); - expect(numberUtil.getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-'], 4, 0)).toEqual(0); - - expect(numberUtil.getPercentWithPrecision([0, undefined, '-', null, NaN], 0, 0)).toEqual(0); - expect(numberUtil.getPercentWithPrecision([0, undefined, '-', null, NaN], 1, 0)).toEqual(0); - expect(numberUtil.getPercentWithPrecision([0, undefined, '-', null, NaN], 2, 0)).toEqual(0); - expect(numberUtil.getPercentWithPrecision([0, undefined, '-', null, NaN], 3, 0)).toEqual(0); - expect(numberUtil.getPercentWithPrecision([0, undefined, '-', null, NaN], 4, 0)).toEqual(0); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-' as any], 0, 0)).toEqual(17); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-' as any], 1, 0)).toEqual(48); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-' as any], 2, 0)).toEqual(26); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-' as any], 3, 0)).toEqual(9); + expect(getPercentWithPrecision([1.678, 4.783, 2.664, 0.875, '-' as any], 4, 0)).toEqual(0); + + expect(getPercentWithPrecision([0, undefined, '-' as any, null, NaN], 0, 0)).toEqual(0); + expect(getPercentWithPrecision([0, undefined, '-' as any, null, NaN], 1, 0)).toEqual(0); + expect(getPercentWithPrecision([0, undefined, '-' as any, null, NaN], 2, 0)).toEqual(0); + expect(getPercentWithPrecision([0, undefined, '-' as any, null, NaN], 3, 0)).toEqual(0); + expect(getPercentWithPrecision([0, undefined, '-' as any, null, NaN], 4, 0)).toEqual(0); }); }); describe('quantityExponent', function () { it('basic', function () { - expect(numberUtil.quantityExponent(1)).toEqual(0); - expect(numberUtil.quantityExponent(9)).toEqual(0); - expect(numberUtil.quantityExponent(12)).toEqual(1); - expect(numberUtil.quantityExponent(123)).toEqual(2); - expect(numberUtil.quantityExponent(1234)).toEqual(3); - expect(numberUtil.quantityExponent(1234.5678)).toEqual(3); - expect(numberUtil.quantityExponent(10)).toEqual(1); - expect(numberUtil.quantityExponent(1000)).toEqual(3); - expect(numberUtil.quantityExponent(10000)).toEqual(4); + expect(quantityExponent(1)).toEqual(0); + expect(quantityExponent(9)).toEqual(0); + expect(quantityExponent(12)).toEqual(1); + expect(quantityExponent(123)).toEqual(2); + expect(quantityExponent(1234)).toEqual(3); + expect(quantityExponent(1234.5678)).toEqual(3); + expect(quantityExponent(10)).toEqual(1); + expect(quantityExponent(1000)).toEqual(3); + expect(quantityExponent(10000)).toEqual(4); }); it('decimals', function () { - expect(numberUtil.quantityExponent(0.1)).toEqual(-1); - expect(numberUtil.quantityExponent(0.001)).toEqual(-3); - expect(numberUtil.quantityExponent(0.00123)).toEqual(-3); + expect(quantityExponent(0.1)).toEqual(-1); + expect(quantityExponent(0.001)).toEqual(-3); + expect(quantityExponent(0.00123)).toEqual(-3); }); it('large number', function () { - expect(numberUtil.quantityExponent(3.14e100)).toEqual(100); - expect(numberUtil.quantityExponent(3.14e-100)).toEqual(-100); + expect(quantityExponent(3.14e100)).toEqual(100); + expect(quantityExponent(3.14e-100)).toEqual(-100); }); it('zero', function () { - expect(numberUtil.quantityExponent(0)).toEqual(0); + expect(quantityExponent(0)).toEqual(0); }); }); describe('quantity', function () { it('basic', function () { - expect(numberUtil.quantity(1)).toEqual(1); - expect(numberUtil.quantity(9)).toEqual(1); - expect(numberUtil.quantity(12)).toEqual(10); - expect(numberUtil.quantity(123)).toEqual(100); - expect(numberUtil.quantity(1234)).toEqual(1000); - expect(numberUtil.quantity(1234.5678)).toEqual(1000); - expect(numberUtil.quantity(10)).toEqual(10); - expect(numberUtil.quantity(1000)).toEqual(1000); - expect(numberUtil.quantity(10000)).toEqual(10000); + expect(quantity(1)).toEqual(1); + expect(quantity(9)).toEqual(1); + expect(quantity(12)).toEqual(10); + expect(quantity(123)).toEqual(100); + expect(quantity(1234)).toEqual(1000); + expect(quantity(1234.5678)).toEqual(1000); + expect(quantity(10)).toEqual(10); + expect(quantity(1000)).toEqual(1000); + expect(quantity(10000)).toEqual(10000); }); it('decimals', function () { - expect(numberUtil.quantity(0.2)).toEqual(0.1); - expect(numberUtil.quantity(0.002)).toEqual(0.001); - expect(numberUtil.quantity(0.00123)).toEqual(0.001); + expect(quantity(0.2)).toEqual(0.1); + expect(quantity(0.002)).toEqual(0.001); + expect(quantity(0.00123)).toEqual(0.001); }); it('large number', function () { - // expect(numberUtil.quantity(3.14e100)).toEqual(1e100); - // expect(numberUtil.quantity(3.14e-100)).toEqual(1e-100); + // expect(quantity(3.14e100)).toEqual(1e100); + // expect(quantity(3.14e-100)).toEqual(1e-100); }); it('zero', function () { - expect(numberUtil.quantity(0)).toEqual(1); + expect(quantity(0)).toEqual(1); }); }); @@ -452,28 +472,28 @@ describe('util/number', function () { describe('nice', function () { it('extreme', function () { // Should not be 0.30000000000000004 - expect(numberUtil.nice(0.3869394696651766, true)).toEqual(0.3); - expect(numberUtil.nice(0.3869394696651766)).toEqual(0.5); - expect(numberUtil.nice(0.00003869394696651766, true)).toEqual(0.00003); - expect(numberUtil.nice(0.00003869394696651766, false)).toEqual(0.00005); - // expect(numberUtil.nice(0, true)).toEqual(0); - // expect(numberUtil.nice(0)).toEqual(0); - expect(numberUtil.nice(13, true)).toEqual(10); - expect(numberUtil.nice(13)).toEqual(20); - expect(numberUtil.nice(3900000000000000000021, true)).toEqual(3000000000000000000000); - expect(numberUtil.nice(3900000000000000000021)).toEqual(5000000000000000000000); - expect(numberUtil.nice(0.00000000000000000656939, true)).toEqual(0.000000000000000005); - expect(numberUtil.nice(0.00000000000000000656939)).toEqual(0.00000000000000001); - expect(numberUtil.nice(0.10000000000000000656939, true)).toEqual(0.1); - expect(numberUtil.nice(0.10000000000000000656939)).toEqual(0.2); + expect(nice(0.3869394696651766, true)).toEqual(0.3); + expect(nice(0.3869394696651766)).toEqual(0.5); + expect(nice(0.00003869394696651766, true)).toEqual(0.00003); + expect(nice(0.00003869394696651766, false)).toEqual(0.00005); + // expect(nice(0, true)).toEqual(0); + // expect(nice(0)).toEqual(0); + expect(nice(13, true)).toEqual(10); + expect(nice(13)).toEqual(20); + expect(nice(3900000000000000000021, true)).toEqual(3000000000000000000000); + expect(nice(3900000000000000000021)).toEqual(5000000000000000000000); + expect(nice(0.00000000000000000656939, true)).toEqual(0.000000000000000005); + expect(nice(0.00000000000000000656939)).toEqual(0.00000000000000001); + expect(nice(0.10000000000000000656939, true)).toEqual(0.1); + expect(nice(0.10000000000000000656939)).toEqual(0.2); }); }); describe('numeric', function () { - function testNumeric(rawVal, tarVal, beNumeric) { - expect(numberUtil.isNumeric(rawVal)).toEqual(beNumeric); - expect(numberUtil.numericToNumber(rawVal)).toEqual(tarVal); + function testNumeric(rawVal: unknown, tarVal: number, beNumeric: boolean) { + expect(isNumeric(rawVal)).toEqual(beNumeric); + expect(numericToNumber(rawVal)).toEqual(tarVal); } testNumeric(123, 123, true); @@ -485,20 +505,20 @@ describe('util/number', function () { testNumeric('-555.6', -555.6, true); testNumeric(' 555 ', 555, true); testNumeric(' -555 ', -555, true); - testNumeric(1e3, 1000, true, true); - testNumeric(-1e3, -1000, true, true); - testNumeric('1e3', 1000, true, true); - testNumeric('-1e3', -1000, true, true); + testNumeric(1e3, 1000, true); + testNumeric(-1e3, -1000, true); + testNumeric('1e3', 1000, true); + testNumeric('-1e3', -1000, true); testNumeric(' \r \n 555 \t ', 555, true); testNumeric(' \r \n -555.6 \t ', -555.6, true); - testNumeric(Infinity, Infinity, true, true); - testNumeric(-Infinity, -Infinity, true, true); - testNumeric('Infinity', Infinity, true, true); - testNumeric('-Infinity', -Infinity, true, true); - - testNumeric(NaN, NaN); - testNumeric(-NaN, NaN); - testNumeric('NaN', NaN); + testNumeric(Infinity, Infinity, true); + testNumeric(-Infinity, -Infinity, true); + testNumeric('Infinity', Infinity, true); + testNumeric('-Infinity', -Infinity, true); + + testNumeric(NaN, NaN, false); + testNumeric(-NaN, NaN, false); + testNumeric('NaN', NaN, false); testNumeric('-NaN', NaN, false); testNumeric(' NaN ', NaN, false); testNumeric(true, NaN, false); -- GitLab