Skip to content

Commit

Permalink
fix: id generation func
Browse files Browse the repository at this point in the history
  • Loading branch information
Estasie committed Dec 27, 2024
1 parent ed2def1 commit ec18654
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/YagrCore/plugins/plotLines/plotLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {DEFAULT_X_SCALE, DEFAULT_CANVAS_PIXEL_RATIO} from '../../defaults';
import {PLineConfig, PlotLineConfig, YagrPlugin} from '../../types';
import {DrawOrderKey} from '../../utils/types';
import {PBandConfig} from 'src/types';
import {deepIsEqual} from '../../utils/common';
import {deepIsEqual, genId} from '../../utils/common';
import {calculateFromTo} from './utils';

const MAX_X_SCALE_LINE_OFFSET = 0;
Expand Down Expand Up @@ -48,13 +48,13 @@ export default function plotLinesPlugin(options: PlotLineOptions): PlotLinesPlug
const drawIndicies = (drawOrder ? drawOrder.map((key) => DRAW_MAP[key]) : [0, 1, 2]).join('');

const hook = HOOKS_MAP[drawIndicies] || 'drawClear';
let lineCounter = 0;

function getLineId(line: PlotLineConfig): string {
if (line.id) {
return line.id;
}
const lineWithoutId = Array.from(plotLines.entries()).find(([_, l]) => deepIsEqual(l, line))?.[0];
return lineWithoutId || `plot-line-${++lineCounter}`;
return lineWithoutId || genId();
}

function renderPlotLines(u: UPlot) {
Expand Down Expand Up @@ -204,9 +204,8 @@ export default function plotLinesPlugin(options: PlotLineOptions): PlotLinesPlug
if (config.axes.hasOwnProperty(scale)) {
const axisConfig = config.axes[scale];
if (axisConfig.plotLines) {
let initialCounter = 0;
for (const plotLine of axisConfig.plotLines) {
plotLines.set(plotLine.id || `plot-line-initial-${++initialCounter}`, {
plotLines.set(plotLine.id || genId(), {
...plotLine,
scale,
});
Expand Down
3 changes: 2 additions & 1 deletion src/YagrCore/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ const interpolateImpl = (
return result;
};

export const genId = () => Math.random().toString(36).substr(2, 9).replace(/^\d+/, '');
// https://stackoverflow.com/a/53116778
export const genId = () => Date.now().toString(36) + Math.random().toString(36).substring(2);

/**
* Processing data series to:
Expand Down

0 comments on commit ec18654

Please sign in to comment.