2.0 first commit
This commit is contained in:
33
src/helpers/assertions.ts
Normal file
33
src/helpers/assertions.ts
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Ensures that value is defined.
|
||||
* Throws if the value is undefined, returns the original value otherwise.
|
||||
*
|
||||
* @param value - The value, or undefined.
|
||||
* @returns The passed value, if it is not undefined
|
||||
*/
|
||||
export function ensureDefined(value: undefined): never;
|
||||
export function ensureDefined<T>(value: T | undefined): T;
|
||||
export function ensureDefined<T>(value: T | undefined): T {
|
||||
if (value === undefined) {
|
||||
throw new Error('Value is undefined');
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that value is not null.
|
||||
* Throws if the value is null, returns the original value otherwise.
|
||||
*
|
||||
* @param value - The value, or null.
|
||||
* @returns The passed value, if it is not null
|
||||
*/
|
||||
export function ensureNotNull(value: null): never;
|
||||
export function ensureNotNull<T>(value: T | null): T;
|
||||
export function ensureNotNull<T>(value: T | null): T {
|
||||
if (value === null) {
|
||||
throw new Error('Value is null');
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
6
src/helpers/dimensions/common.ts
Normal file
6
src/helpers/dimensions/common.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface BitmapPositionLength {
|
||||
/** coordinate for use with a bitmap rendering scope */
|
||||
position: number;
|
||||
/** length for use with a bitmap rendering scope */
|
||||
length: number;
|
||||
}
|
||||
23
src/helpers/dimensions/crosshair-width.ts
Normal file
23
src/helpers/dimensions/crosshair-width.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Default grid / crosshair line width in Bitmap sizing
|
||||
* @param horizontalPixelRatio - horizontal pixel ratio
|
||||
* @returns default grid / crosshair line width in Bitmap sizing
|
||||
*/
|
||||
export function gridAndCrosshairBitmapWidth(
|
||||
horizontalPixelRatio: number
|
||||
): number {
|
||||
return Math.max(1, Math.floor(horizontalPixelRatio));
|
||||
}
|
||||
|
||||
/**
|
||||
* Default grid / crosshair line width in Media sizing
|
||||
* @param horizontalPixelRatio - horizontal pixel ratio
|
||||
* @returns default grid / crosshair line width in Media sizing
|
||||
*/
|
||||
export function gridAndCrosshairMediaWidth(
|
||||
horizontalPixelRatio: number
|
||||
): number {
|
||||
return (
|
||||
gridAndCrosshairBitmapWidth(horizontalPixelRatio) / horizontalPixelRatio
|
||||
);
|
||||
}
|
||||
29
src/helpers/dimensions/full-width.ts
Normal file
29
src/helpers/dimensions/full-width.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { BitmapPositionLength } from './common';
|
||||
|
||||
/**
|
||||
* Calculates the position and width which will completely full the space for the bar.
|
||||
* Useful if you want to draw something that will not have any gaps between surrounding bars.
|
||||
* @param xMedia - x coordinate of the bar defined in media sizing
|
||||
* @param halfBarSpacingMedia - half the width of the current barSpacing (un-rounded)
|
||||
* @param horizontalPixelRatio - horizontal pixel ratio
|
||||
* @returns position and width which will completely full the space for the bar
|
||||
*/
|
||||
export function fullBarWidth(
|
||||
xMedia: number,
|
||||
halfBarSpacingMedia: number,
|
||||
horizontalPixelRatio: number
|
||||
): BitmapPositionLength {
|
||||
const fullWidthLeftMedia = xMedia - halfBarSpacingMedia;
|
||||
const fullWidthRightMedia = xMedia + halfBarSpacingMedia;
|
||||
const fullWidthLeftBitmap = Math.round(
|
||||
fullWidthLeftMedia * horizontalPixelRatio
|
||||
);
|
||||
const fullWidthRightBitmap = Math.round(
|
||||
fullWidthRightMedia * horizontalPixelRatio
|
||||
);
|
||||
const fullWidthBitmap = fullWidthRightBitmap - fullWidthLeftBitmap;
|
||||
return {
|
||||
position: fullWidthLeftBitmap,
|
||||
length: fullWidthBitmap,
|
||||
};
|
||||
}
|
||||
48
src/helpers/dimensions/positions.ts
Normal file
48
src/helpers/dimensions/positions.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { BitmapPositionLength } from './common';
|
||||
|
||||
function centreOffset(lineBitmapWidth: number): number {
|
||||
return Math.floor(lineBitmapWidth * 0.5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the bitmap position for an item with a desired length (height or width), and centred according to
|
||||
* an position coordinate defined in media sizing.
|
||||
* @param positionMedia - position coordinate for the bar (in media coordinates)
|
||||
* @param pixelRatio - pixel ratio. Either horizontal for x positions, or vertical for y positions
|
||||
* @param desiredWidthMedia - desired width (in media coordinates)
|
||||
* @returns Position of of the start point and length dimension.
|
||||
*/
|
||||
export function positionsLine(
|
||||
positionMedia: number,
|
||||
pixelRatio: number,
|
||||
desiredWidthMedia: number = 1,
|
||||
widthIsBitmap?: boolean
|
||||
): BitmapPositionLength {
|
||||
const scaledPosition = Math.round(pixelRatio * positionMedia);
|
||||
const lineBitmapWidth = widthIsBitmap
|
||||
? desiredWidthMedia
|
||||
: Math.round(desiredWidthMedia * pixelRatio);
|
||||
const offset = centreOffset(lineBitmapWidth);
|
||||
const position = scaledPosition - offset;
|
||||
return { position, length: lineBitmapWidth };
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the bitmap position and length for a dimension of a shape to be drawn.
|
||||
* @param position1Media - media coordinate for the first point
|
||||
* @param position2Media - media coordinate for the second point
|
||||
* @param pixelRatio - pixel ratio for the corresponding axis (vertical or horizontal)
|
||||
* @returns Position of of the start point and length dimension.
|
||||
*/
|
||||
export function positionsBox(
|
||||
position1Media: number,
|
||||
position2Media: number,
|
||||
pixelRatio: number
|
||||
): BitmapPositionLength {
|
||||
const scaledPosition1 = Math.round(pixelRatio * position1Media);
|
||||
const scaledPosition2 = Math.round(pixelRatio * position2Media);
|
||||
return {
|
||||
position: Math.min(scaledPosition1, scaledPosition2),
|
||||
length: Math.abs(scaledPosition2 - scaledPosition1) + 1,
|
||||
};
|
||||
}
|
||||
34
src/helpers/time.ts
Normal file
34
src/helpers/time.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { Time, isUTCTimestamp, isBusinessDay } from 'lightweight-charts';
|
||||
|
||||
export function convertTime(t: Time): number {
|
||||
if (isUTCTimestamp(t)) return t * 1000;
|
||||
if (isBusinessDay(t)) return new Date(t.year, t.month, t.day).valueOf();
|
||||
const [year, month, day] = t.split('-').map(parseInt);
|
||||
return new Date(year, month, day).valueOf();
|
||||
}
|
||||
|
||||
export function displayTime(time: Time): string {
|
||||
if (typeof time == 'string') return time;
|
||||
const date = isBusinessDay(time)
|
||||
? new Date(time.year, time.month, time.day)
|
||||
: new Date(time * 1000);
|
||||
return date.toLocaleDateString();
|
||||
}
|
||||
|
||||
export function formattedDateAndTime(timestamp: number | undefined): [string, string] {
|
||||
if (!timestamp) return ['', ''];
|
||||
const dateObj = new Date(timestamp);
|
||||
|
||||
// Format date string
|
||||
const year = dateObj.getFullYear();
|
||||
const month = dateObj.toLocaleString('default', { month: 'short' });
|
||||
const date = dateObj.getDate().toString().padStart(2, '0');
|
||||
const formattedDate = `${date} ${month} ${year}`;
|
||||
|
||||
// Format time string
|
||||
const hours = dateObj.getHours().toString().padStart(2, '0');
|
||||
const minutes = dateObj.getMinutes().toString().padStart(2, '0');
|
||||
const formattedTime = `${hours}:${minutes}`;
|
||||
|
||||
return [formattedDate, formattedTime];
|
||||
}
|
||||
Reference in New Issue
Block a user