Skip to content

Commit

Permalink
add error(), date() methods (DatePayload is the original CarbonPayloa…
Browse files Browse the repository at this point in the history
…d class)
  • Loading branch information
patinthehat authored and Patrick committed Feb 7, 2021
1 parent 0dc9f8c commit ea4bd66
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
33 changes: 0 additions & 33 deletions src/Payloads/CarbonPayload.ts.ignore

This file was deleted.

55 changes: 55 additions & 0 deletions src/Payloads/DatePayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import dayjs from 'dayjs';
import { Payload } from '../Payloads/Payload';


export class DatePayload extends Payload
{
protected date: Date | null;

protected format: string;

public constructor(date: Date | null, format = 'YYYY-MM-DD hh:mm:ss')
{
super();

this.date = date;

this.format = format;
}

public getType(): string
{
return 'carbon';
}

public getContent(): Record<string, string | number | null>
{
return {
formatted: this.date ? this.getFormatted() : null,
timestamp: this.date ? this.getTimestamp() : null,
timezone: this.date ? this.getTimezoneName() : null,
};
}

protected getTimestamp(): number
{
return dayjs(this.date?.toISOString()).unix();
}

protected getFormatted(): string
{
return dayjs(this.date?.toISOString()).format(this.format);
}

protected getTimezoneName(): string
{
if (this.date === null) {
return '--';
}

const dateObj = this.date ? this.date : new Date();
const matches = /\((.*)\)/.exec(dateObj.toString());

return matches ? matches[1] : '--';
}
}
8 changes: 8 additions & 0 deletions src/Ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { OriginData } from './Origin/Origin';
import StackTrace from 'stacktrace-js';
import PACKAGE_VERSION from './lib/version';
import { ErrorPayload } from './Payloads/ErrorPayload';
import { DatePayload } from './Payloads/DatePayload';

export type BoolFunction = () => boolean;

Expand Down Expand Up @@ -333,6 +334,13 @@ export class Ray extends Mixin(RayColors, RaySizes) {
return this.sendRequest(payload);
}

public date(date: Date): this
{
const payload = new DatePayload(date);

return this.sendRequest(payload);
}

public raw(...args: any[]): this
{
if (!args.length) {
Expand Down

0 comments on commit ea4bd66

Please sign in to comment.