Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/boolean-util/boolean-util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { valueOfBoolean } from './boolean-util';
import { parseBoolean } from './boolean-util';

describe('valueOfBoolean', () => {
describe('parseBoolean', () => {
it('should throw with incompatible input', () => {
expect(() => valueOfBoolean('abc')).toThrow();
expect(() => valueOfBoolean('1111')).toThrow();
expect(() => valueOfBoolean('')).toThrow();
expect(() => valueOfBoolean('nay')).toThrow();
expect(() => parseBoolean('abc')).toThrow();
expect(() => parseBoolean('1111')).toThrow();
expect(() => parseBoolean('')).toThrow();
expect(() => parseBoolean('nay')).toThrow();
});
it('should return parameter in boolean type', () => {
expect(valueOfBoolean('true')).toEqual(true);
expect(valueOfBoolean('false')).toEqual(false);
expect(valueOfBoolean('1')).toEqual(true);
expect(valueOfBoolean('0')).toEqual(false);
expect(valueOfBoolean('yes')).toEqual(true);
expect(valueOfBoolean('no')).toEqual(false);
expect(valueOfBoolean('Y')).toEqual(true);
expect(valueOfBoolean('N')).toEqual(false);
expect(valueOfBoolean('On')).toEqual(true);
expect(valueOfBoolean('Off')).toEqual(false);
expect(valueOfBoolean('TRUE')).toEqual(true);
expect(valueOfBoolean('FALSE')).toEqual(false);
expect(parseBoolean('true')).toEqual(true);
expect(parseBoolean('false')).toEqual(false);
expect(parseBoolean('1')).toEqual(true);
expect(parseBoolean('0')).toEqual(false);
expect(parseBoolean('yes')).toEqual(true);
expect(parseBoolean('no')).toEqual(false);
expect(parseBoolean('Y')).toEqual(true);
expect(parseBoolean('N')).toEqual(false);
expect(parseBoolean('On')).toEqual(true);
expect(parseBoolean('Off')).toEqual(false);
expect(parseBoolean('TRUE')).toEqual(true);
expect(parseBoolean('FALSE')).toEqual(false);
});
});
2 changes: 1 addition & 1 deletion src/boolean-util/boolean-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function valueOfBoolean(boolStr: string): boolean {
export function parseBoolean(boolStr: string): boolean {
const TRUE_REGEXP = /^(t(rue)?|y(es)?|on|1)$/i;
const FALSE_REGEXP = /^(f(alse)?|n(o)?|off|0)$/i;

Expand Down
2 changes: 1 addition & 1 deletion src/boolean-util/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { valueOfBoolean } from './boolean-util';
export { parseBoolean } from './boolean-util';
48 changes: 0 additions & 48 deletions src/date-util/date-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {
durationTo,
endOfByTimezone,
formatDate,
formatInIso8601,
fromNow,
getDateString,
getDatetimeString,
getTimestampString,
getTimezoneOffsetInHours,
isAdult,
isLastDateOfMonth,
Expand All @@ -23,7 +19,6 @@ import {
} from './date-util';
import {
DATE_FORMAT,
DATETIME_FORMAT,
DATETIME_FORMAT_WITH_MILLIS,
TIMESTAMP_FORMAT,
TIMEZONE_PST,
Expand Down Expand Up @@ -487,49 +482,6 @@ describe('format', () => {
});
});

describe('formatInIso8601', () => {
it('should return formatted date string in ISO format string', () => {
const testDatetime = new Date(testDatetimeStr8);
expect(formatInIso8601(testDatetime, { format: DATE_FORMAT })).toEqual(testDateStr);
expect(formatInIso8601(testDatetime, { format: DATETIME_FORMAT })).toEqual(testDatetimeStr5);
expect(formatInIso8601(testDatetime, { format: DATETIME_FORMAT_WITH_MILLIS })).toEqual(testDatetimeStr8);
});
it('should return formatted date string of local time', () => {
const testDatetime = new Date(testDatetimeStr8);
expect(formatInIso8601(testDatetime, { format: DATETIME_FORMAT, isUtc: false, timeZone: 'Asia/Seoul' })).toEqual(
`2022-02-23T10:23:45+09:00`
);
expect(formatInIso8601(testDatetime, { isUtc: false, timeZone: 'PST8PDT' })).toEqual('2022-02-22T17:23:45-08:00');
});
});

describe('getDateString', () => {
it('should return formatted date string', () => {
expect(getDateString(new Date(testDatetimeStr8))).toBe(testDateStr);
});
it('should return formatted date string of local time', () => {
expect(getDateString(new Date(testDatetimeStr6), false)).toBe(testDateStr);
});
});

describe('getDatetimeString', () => {
it('should return format datetime string', () => {
expect(getDatetimeString(new Date(testDatetimeStr8))).toBe(testDatetimeStr5);
});
it('should return formatted datetime string of local time', () => {
expect(getDatetimeString(new Date(testDatetimeStr8), false)).toBe('2022-02-23 10:23:45');
});
});

describe('getTimestampString', () => {
it('should return formatted timestamp string', () => {
expect(getTimestampString(new Date(testDatetimeStr8))).toBe(testTimestampStr);
});
it('should return formatted timestamp string of local time', () => {
expect(getTimestampString(new Date(testDatetimeStr8), false)).toBe('20220223102345678');
});
});

describe('durationTo', () => {
it('시간을 초로 바꾸어 리턴한다.', () => {
expect(durationTo('09:10:30')).toEqual(33030);
Expand Down
58 changes: 1 addition & 57 deletions src/date-util/date-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { LoggerFactory } from '../logger';
import { DatePropertyType, DateType, TimeZoneType } from './date-util-base.type';
import {
ADULT_AGE_DEFAULT,
DATE_FORMAT,
DATETIME_FORMAT,
LOCAL_DATETIME_FORMAT,
ONE_DAY_IN_MILLI,
Expand All @@ -15,7 +14,7 @@ import {
TIMESTAMP_FORMAT,
} from './date-util.const';
import type { TimeAnnotationSet } from './date-util.interface';
import type { CalcDatetimeOpts, DatetimeFormatOpts, IsoDatetimeFormatOpts } from './date-util.type';
import type { CalcDatetimeOpts, DatetimeFormatOpts } from './date-util.type';

const timeZoneMap: Record<TimeZoneType, number> = { 'Asia/Seoul': 540, 'Asia/Tokyo': 540, PST8PDT: -480, UTC: 0 };
const logger = LoggerFactory.getLogger('pebbles:date-util');
Expand Down Expand Up @@ -373,61 +372,6 @@ export function formatDate(d: Date, opts?: Readonly<DatetimeFormatOpts>): string
});
}

/**
*
* @description It converts `date` in `opts.format` if it's given. Otherwise the default format would be `YYYY-MM-DDTHH:mm:ssZ`.
* - Other format options can be `YYYY-MM-DD` or `YYYY-MM-DDTHH:mm:ss.SSSZ`.
*/
export function formatInIso8601(date: Date, opts?: Readonly<IsoDatetimeFormatOpts>): string {
const formatOpts: IsoDatetimeFormatOpts = opts ?? {};
formatOpts.format = opts?.format ?? DATETIME_FORMAT;
return formatDate(date, formatOpts);
}

/**
*
* @description It converts `date` in `YYYY-MM-DD` format.
*/
export function getDateString(date: Date, isUtc = true, timeZone: TimeZoneType = 'Asia/Seoul'): string {
return formatDate(date, { format: DATE_FORMAT, isUtc, timeZone });
}

/**
*
* @description It converts `date` in `YYYY-MM-DDTHH:mm:ssZ` format if isUtc is true by default.
* Otherwise the format would be `YYYY-MM-DD HH:mm:ss`.
*/
export function getDatetimeString(date: Date, isUtc = true, timeZone: TimeZoneType = 'Asia/Seoul'): string {
return formatDate(date, { isUtc, timeZone });
}

/**
*
* @description It converts `date` in `YYYYMMDDHHmmssSSS` format.
*/
export function getTimestampString(date: Date, isUtc = true, timeZone: TimeZoneType = 'Asia/Seoul'): string {
return formatDate(date, { format: TIMESTAMP_FORMAT, isUtc, timeZone });
}

/**
*
* @description It converts `totalSeconds` in `hh:mm:ss` format.
*/
export function getTimeStringFromSeconds(totalSeconds: number): string {
if (totalSeconds < 0) {
throw new Error('Invalid number');
}

const seconds = totalSeconds % 60;
const totalMinutes = (totalSeconds - seconds) / 60;
const minutes = totalMinutes % 60;
const hh = String((totalMinutes - minutes) / 60).padStart(2, '0');
const mm = String(minutes).padStart(2, '0');
const ss = String(seconds).padStart(2, '0');

return `${hh}:${mm}:${ss}`;
}

export function durationTo(duration: string, unitType: DatePropertyType = 'second'): number {
if (unitType !== 'second') {
throw new Error('Not supported yet');
Expand Down
6 changes: 0 additions & 6 deletions src/date-util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ export {
endOfByTimezone,
format12HourInLocale,
formatDate,
formatInIso8601,
fromNow,
getDateString,
getDatetimeString,
getTimestampString,
getTimeStringFromSeconds,
getTimezoneOffsetInHours,
getTimezoneOffsetString,
isAdult,
Expand All @@ -26,7 +21,6 @@ export {
startOfDate,
subtractOneDayIfLocalTimeIsMidnight,
} from './date-util';

export type { DatePropertyType, DateType, Iso8601FormatType, LocaleType, TimeZoneType } from './date-util-base.type';
export {
DATE_FORMAT,
Expand Down
2 changes: 1 addition & 1 deletion src/map-util/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { getMapValue, groupBy, groupByKey } from './map-util';
export { groupBy, groupByKey } from './map-util';
22 changes: 1 addition & 21 deletions src/map-util/map-util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
import { getMapValue, groupBy, groupByKey } from './map-util';

describe('getMapValue', () => {
it('should return defaultValue', () => {
expect(getMapValue(new Map(), 1, 'value')).toBe('value');
});

it('should omit defaultValue', () => {
expect(getMapValue(new Map(), 1)).toBeUndefined();
});

it('should return existing value', () => {
const booleanMap: Map<string, boolean> = new Map([['FALSE', false]]);
const nullableMap: Map<string, string | null> = new Map([['NULL', null]]);
const numberMap: Map<string, number> = new Map([['ZERO', 0]]);

expect(getMapValue(booleanMap, 'FALSE', true)).toBe(booleanMap.get('FALSE'));
expect(getMapValue(nullableMap, 'NULL')).toBe(nullableMap.get('NULL'));
expect(getMapValue(numberMap, 'ZERO', 1)).toBe(numberMap.get('ZERO'));
});
});
import { groupBy, groupByKey } from './map-util';

describe('groupByKey', () => {
test('should group an array of objects by a specified key', () => {
Expand Down
15 changes: 0 additions & 15 deletions src/map-util/map-util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/**
* map 에서 key 에 해당하는 값을 반환한다. 값이 undefined 이면 defaultValue 를 반환한다.
*
* @param map
* @param key
* @param defaultValue
*/

export function getMapValue<K, V>(map: Map<K, V>, key: K): V | undefined;
export function getMapValue<K, V>(map: Map<K, V>, key: K, defaultValue: V): V;
export function getMapValue<K, V>(map: Map<K, V>, key: K, defaultValue?: V): V | undefined {
const value = map.get(key);
return value === undefined ? defaultValue : value;
}

export function groupByKey<T, K extends keyof T>(array: T[], key: K): Map<T[K], T[]> {
return groupBy(array, (item) => item[key]);
}
Expand Down
10 changes: 1 addition & 9 deletions src/number-util/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
export {
decimalRoundDown,
decimalRoundUp,
fromPermyriad,
intValueOf,
isNumeric,
toPermyriad,
valueOfNumber,
} from './number-util';
export { decimalRoundDown, decimalRoundUp, fromPermyriad, intValueOf, isNumeric, toPermyriad } from './number-util';
27 changes: 1 addition & 26 deletions src/number-util/number-util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
decimalRoundDown,
decimalRoundUp,
fromPermyriad,
intValueOf,
isNumeric,
toPermyriad,
valueOfNumber,
} from './number-util';
import { decimalRoundDown, decimalRoundUp, fromPermyriad, intValueOf, isNumeric, toPermyriad } from './number-util';

describe('intValueOf', () => {
it('should throw with incompatible input', () => {
Expand All @@ -28,23 +20,6 @@ describe('intValueOf', () => {
});
});

describe('valueOfNumber', () => {
it('should throw with incompatible input', () => {
expect(() => valueOfNumber('abc')).toThrow();
expect(() => valueOfNumber('4a')).toThrow();
expect(() => valueOfNumber('1.2.3')).toThrow();
expect(() => valueOfNumber('')).toThrow();
});
it('should return parameter in number type', () => {
expect(valueOfNumber('-1')).toEqual(-1);
expect(valueOfNumber('0')).toEqual(0);
expect(valueOfNumber('1.2')).toEqual(1.2);
expect(valueOfNumber('-1.2')).toEqual(-1.2);
expect(valueOfNumber('-93339.228883747849')).toEqual(-93339.22888374786);
expect(valueOfNumber('9488848.29000004833')).toEqual(9488848.290000048);
});
});

describe('isNumeric', () => {
it('should equal to all', () => {
expect(isNumeric('0')).toEqual(true);
Expand Down
9 changes: 0 additions & 9 deletions src/number-util/number-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ export function intValueOf(numStr: string): number {
return result;
}

export function valueOfNumber(numStr: string): number {
const result = Number(numStr);

if (!numStr || !Number.isFinite(result)) {
throw new Error(`unable to convert "${numStr}" to number type`);
}
return result;
}

export function isNumeric(numStr: string): boolean {
const num = Number(numStr);
return !isNaN(num) && isFinite(num) && num === parseFloat(numStr);
Expand Down
8 changes: 6 additions & 2 deletions src/object-util/object-util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { ObjectKeyType, ObjectType } from './object-util.type';

export function serialize(obj: Readonly<ObjectType>): string | null {
/** @deprecated use JSON.stringify instead */
export function serialize(obj: any): string | null {
try {
return JSON.stringify(obj);
} catch {
return null;
}
}

export function deserialize(str: string): ObjectType | null {
/** @deprecated use JSON.parse instead */
export function deserialize(str: string): any {
try {
return JSON.parse(str);
} catch {
return null;
}
}

/** @deprecated use '== null' instead */
export function isNullish(value: unknown): value is null | undefined {
return value === undefined || value === null;
}
Expand All @@ -34,6 +37,7 @@ export function isEmpty(value: unknown): boolean {
return true;
}

/** @deprecated use window.structuredClone() instead */
export function deepClone<Type extends ObjectType>(obj: Type): Type {
const initiator = obj.constructor as new (...arg: unknown[]) => Type;

Expand Down
Loading