Skip to content

Commit

Permalink
BREAKING CHANGES: Removing more useless functions
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Aug 16, 2023
1 parent c03d09a commit ccddc2b
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 118 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"eslint-config-prettier": "^9.0.0",
"jest": "^29.6.2",
"jest-environment-jsdom": "^29.6.2",
"prettier": "^3.0.1",
"prettier": "^3.0.2",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions src/dateRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ export class YearQuarter extends DateRange implements IYearQuarterDateRange {

Quarter: number;

StartDate: Date;

EndDate: Date;

static get Current(): YearQuarter {
return new YearQuarter();
}
Expand Down Expand Up @@ -152,10 +148,6 @@ export class YearMonth extends DateRange implements IYearMonthDateRange {

Month: number;

StartDate: Date;

EndDate: Date;

static get Current(): YearMonth {
return new YearMonth();
}
Expand Down Expand Up @@ -216,10 +208,6 @@ export class YearWeek extends DateRange implements IYearWeekDateRange {

Week: number;

StartDate: Date;

EndDate: Date;

static get Current(): YearWeek {
return new YearWeek();
}
Expand Down
24 changes: 3 additions & 21 deletions src/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const dateOptions: Intl.DateTimeFormatOptions = { month: 'numeric', day: 'numeri
const formatWeekDaysRange = (start: Date, end: Date) =>
`[${start.toLocaleDateString('en-US', dateOptions)} - ${end.toLocaleDateString('en-US', dateOptions)}]`;

export const getWeekDaysRange = (week: number, year: number = null) => {
export const getWeekDaysRange = (week: number, year?: number) => {
year = year || new Date().getFullYear();
const firstDayOfYear = new Date(year, 0, 1);

Expand All @@ -30,9 +30,7 @@ export const compareRealDates = (a: Date, b: Date) => {
export const compareDates = (a: string, b: string) => compareRealDates(new Date(a), new Date(b));

export const toLocalTime = (date: string | Date): Date => {
if (typeof date === 'string' && date.toUpperCase().endsWith('Z')) {
return new Date(date);
}
if (typeof date === 'string' && date.toUpperCase().endsWith('Z')) return new Date(date);

const baseDate = date instanceof Date ? date : new Date(date);
return new Date(
Expand All @@ -52,7 +50,7 @@ const regexISO = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:
const regexDate = /\d{4}-(0\d|1[0-2])-([0-2]\d|3[01])/;

export const isDate = (value: unknown): boolean => {
const stringValue = value.toString();
const stringValue = String(value);
const isValidDate = !Number.isNaN(new Date(stringValue).getDate());

if (value instanceof Date && isValidDate) return true;
Expand All @@ -71,19 +69,3 @@ export const toLocaleString = (date: string, locales = 'en-us'): string => {
const dateString = toLocalTime(date).toLocaleDateString(locales, dateTimeFormatOptions);
return dateString !== 'Invalid Date' ? dateString : '';
};

export const toDate = (obj: string | Record<string, unknown>): void => {
Object.keys(obj).forEach((prop) => {
const currentType = typeof obj[prop];
if (currentType === 'string' && isDate(obj[prop])) {
obj[prop] = toLocalTime(obj[prop]);
}
if (currentType === 'object' && obj[prop]) {
if (obj[prop] instanceof Array) {
obj[prop].forEach(toDate);
} else {
Object.keys(obj[prop]).forEach(() => toDate(obj[prop]));
}
}
});
};
2 changes: 1 addition & 1 deletion src/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export type Procedure = (...args: any[]) => void;

export function debounce<F extends Procedure>(func: F, waitMilliseconds = 500): F {
let timeoutId: number;
let timeoutId: number | undefined;

// tslint:disable-next-line: only-arrow-functions
return function (t: any, ...args: any[]) {
Expand Down
10 changes: 5 additions & 5 deletions src/delta.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export const getDeltaString = (delta?: number): 'unchanged' | 'increase' | 'decrease' => {
export type DeltaString = 'unchanged' | 'increase' | 'decrease';

export const getDeltaString = (delta: number | null): DeltaString => {
if (!delta) return 'unchanged';
return delta > 0 ? 'increase' : 'decrease';
};

export const getDeltaStringFromValues = (
current?: number,
previous?: number,
): 'unchanged' | 'increase' | 'decrease' => {
export const getDeltaStringFromValues = (current?: number, previous?: number): DeltaString => {
if (!current || !previous) return 'unchanged';
return getDeltaString(current - previous);
};
Expand All @@ -25,5 +24,6 @@ export const calculateDelta = (current?: number, previous?: number) => {
const delta = calculateDeltaValue(current, previous);
return [delta, calculateDeltaPercent(current, previous), getDeltaString(delta)];
};

export const padDecimal = (number: number, digits?: number) =>
number.toLocaleString('en-US', { minimumFractionDigits: digits || 2, maximumFractionDigits: digits || 2 });
4 changes: 2 additions & 2 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const defaultOptions = { keepFormat: false, decimals: 2, nullValue: 'N/A' };

const internalFotmatter = (
stringData: string,
format: FormatTypes,
{ keepFormat, decimals, nullValue }: { keepFormat: boolean; decimals: number; nullValue: string },
format?: FormatTypes,
): string => {
switch (format) {
case 'money': {
Expand Down Expand Up @@ -51,5 +51,5 @@ export const formatter = (

if (!data && format === 'money') return '$0.00';

return data == null ? nullValue : internalFotmatter(data.toString(), format, { keepFormat, decimals, nullValue });
return data == null ? nullValue : internalFotmatter(data.toString(), { keepFormat, decimals, nullValue }, format);
};
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './debounce';
export { default as humanize } from './humanize';
export { default as objectDifference } from './objectDifference';
export { default as removeDuplicated } from './removeDuplicated';
export { default as SimpleObservable } from './SimpleObservable';
export * from './stringTemplate';
Expand Down
17 changes: 0 additions & 17 deletions src/objectDifference.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/stringTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type stringTemplateType = (value: string, index?: number) => string;
export type stringTemplateType = (value: string, index: number) => string;

export const stringTemplate = (template: string | string[], isPrefix: boolean): stringTemplateType => {
if (typeof template === 'object') {
Expand Down
4 changes: 1 addition & 3 deletions src/trimText.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const trimText = (text: string | Array<string>) =>
export default (text: string | Array<string>) =>
text.constructor === Array
? text
.map((x: string, i: number) => (i === 0 ? x : ` ${x}`))
.toString()
.replace(/\s+/g, ' ')
: text.toString().replace(/\s+/g, ' ');

export default trimText;
15 changes: 1 addition & 14 deletions test/dateUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compareDates, getWeekDaysRange, isDate, toDate, toLocalTime, toLocaleString } from '../src/dateUtils';
import { compareDates, getWeekDaysRange, isDate, toLocalTime, toLocaleString } from '../src/dateUtils';

const value = {
x1: '2020-01-21T06:00:00',
Expand All @@ -10,19 +10,6 @@ const value = {
y3: null,
};

describe('toDate', () => {
toDate(value);
it('should return true if Date', () =>
expect(value.x1.constructor === Date && value.x2.constructor === Date).toBe(true));
it('should return true if object children value is Date', () =>
expect(value.x3.x31.constructor === Date).toBe(true));
it('should return true if array of objects children value is Date', () =>
expect(value.x4.x41[0].x411.constructor === Date && value.x4.x41[1].x412.constructor === Date).toBe(true));
it('should return true if not Date', () =>
expect(value.y1.constructor !== Date && value.y2.constructor !== Date).toBe(true));
it('should return true if null', () => expect(value.y3 === null).toBe(true));
});

const stringDate = '2020-01-21T06:00:00';
const locales = 'en-us';

Expand Down
31 changes: 0 additions & 31 deletions test/objectDifference.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "ES2016",
"target": "ES2022",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
Expand Down

0 comments on commit ccddc2b

Please sign in to comment.