Skip to content

Commit

Permalink
refactor: replace ReadonlyRecord, ReadonlyJSONType with Record, JSONT…
Browse files Browse the repository at this point in the history
…ype and add MutableRecord, MutableJSONType (#629)

* chore: replace ReadonlyRecord with Record and add MutableRecord

* chore: replace ReadonlyJSONType with JSONType and add MutableJSONType
  • Loading branch information
noshiro-pf authored Jan 9, 2023
1 parent 2b00785 commit 496a214
Show file tree
Hide file tree
Showing 42 changed files with 124 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const calcBalloonPosition = ({
arrowDirection: NWES;
balloonSize: RectSize;
marginBetweenCardAndBalloon: number;
}>): ReadonlyRecord<'left' | 'top', `${number}px`> =>
}>): Record<'left' | 'top', `${number}px`> =>
pipe(
match(arrowDirection, {
W: {
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/algo-app/src/functions/get-card-direction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Card, NWES } from '../types';
import { cardEq } from './card-eq';

export const getCardDirection = (
playerCards: ReadonlyRecord<NWES, ArrayOfLength<6, Card>>,
playerCards: Record<NWES, ArrayOfLength<6, Card>>,
target: Card
): NWES =>
playerCards.W.some((c) => cardEq(c, target))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const cardPositionsDispatcher = (
};

export const cardPositions$: InitializedObservable<
ReadonlyRecord<CardColor, ArrayOfLength<12, Rect>> | undefined
Record<CardColor, ArrayOfLength<12, Rect>> | undefined
> = cardPositionsAction$.chain(scan(cardPositionsReducer, undefined));
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const playerNamePositionsDispatcher = (
};

export const playerNamePositions$: InitializedObservable<
ReadonlyRecord<NWES, Rect> | undefined
Record<NWES, Rect> | undefined
> = playerNamePositionsAction$.chain(
scan(playerNamePositionsReducer, undefined)
);
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const initialState = (): Record<CardColor, ArrayOfLength<12, Rect>> => ({
});

export const cardPositionsReducer: ReducerType<
ReadonlyRecord<CardColor, ArrayOfLength<12, Rect>> | undefined,
Record<CardColor, ArrayOfLength<12, Rect>> | undefined,
readonly [CardColor, CardNumber, Rect]
> = (state, [color, number, rect]) =>
produce(state === undefined ? initialState() : state, (draft) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const initialState = (): Record<NWES, Rect> => ({
});

export const playerNamePositionsReducer: ReducerType<
ReadonlyRecord<NWES, Rect> | undefined,
Record<NWES, Rect> | undefined,
readonly [NWES, Rect]
> = (state, [direction, rect]) =>
produce(state ?? initialState(), (draft) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/algo-app/src/types/display-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CardWithDisplayValue } from './card-type';
import type { NWES } from './direction';

export type DisplayValues = Readonly<{
playerCards: ReadonlyRecord<NWES, ArrayOfLength<6, CardWithDisplayValue>>;
playerCards: Record<NWES, ArrayOfLength<6, CardWithDisplayValue>>;
gameMessage: string;
turnPlayer: NWES;
endTurnButtonDisabled: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const controlGroupStack = [
'select-caret',
];

export const controlGroupStackZIndex: ReadonlyRecord<
export const controlGroupStackZIndex: Record<
typeof controlGroupStack[number],
number
> = Obj.fromEntries(controlGroupStack.map((key, index) => [key, index]));
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/cant-stop-probability/src/types/count.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type Count = ReadonlyRecord<'noLine' | 'oneLine' | 'twoLine', number>;
export type Count = Record<'noLine' | 'oneLine' | 'twoLine', number>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AnswerSymbolId } from '../enum';
import type { SymbolSetting } from './base';
import { fillSymbolSetting } from './base';

export type SymbolSettings = ReadonlyRecord<AnswerSymbolId, SymbolSetting>;
export type SymbolSettings = Record<AnswerSymbolId, SymbolSetting>;

export type PartialSymbolSettings = DeepPartial<SymbolSettings>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import type { AnswerIconId } from '../enum';
import type { AnswerIconSetting } from './base';
import { fillAnswerIconSetting } from './base';

export type AnswerIconSettings = ReadonlyRecord<
AnswerIconId,
AnswerIconSetting
>;
export type AnswerIconSettings = Record<AnswerIconId, AnswerIconSetting>;

export type PartialAnswerIconSettings = DeepPartial<AnswerIconSettings>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import type { AnswerIconId } from '../enum';
import type { AnswerIconSetting } from './base';
import { fillAnswerIconSetting, isAnswerIconSetting } from './base';

export type AnswerIconSettings = ReadonlyRecord<
AnswerIconId,
AnswerIconSetting
>;
export type AnswerIconSettings = Record<AnswerIconId, AnswerIconSetting>;

export const answerIconSettingsDefaultValue: AnswerIconSettings = {
good: { description: '', point: 10 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export const isDatetimeSpecificationEnumType = datetimeSpecificationTypeDef.is;
assertType<
TypeExtends<
typeof datetimeSpecificationOptions,
ReadonlyRecord<DatetimeSpecificationEnumType, DatetimeSpecificationEnumType>
Record<DatetimeSpecificationEnumType, DatetimeSpecificationEnumType>
>
>();
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { AnswerIconSetting } from './base';

describe('AnswerIconSettings', () => {
assertType<
TypeEq<AnswerIconSettings, ReadonlyRecord<AnswerIconId, AnswerIconSetting>>
TypeEq<AnswerIconSettings, Record<AnswerIconId, AnswerIconSetting>>
>();

test('defaultValue', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const controlGroupStack = [
'select-caret',
];

export const controlGroupStackZIndex: ReadonlyRecord<
export const controlGroupStackZIndex: Record<
typeof controlGroupStack[number],
number
> = Obj.fromEntries(controlGroupStack.map((key, index) => [key, index]));
Expand Down
5 changes: 1 addition & 4 deletions packages/apps/event-schedule-app/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ export const isDevelopment = process.env.NODE_ENV === 'development';
export const experimentalFeature = {} as const;

assertType<
TypeExtends<
typeof experimentalFeature,
ReadonlyRecord<never, 'hidden' | 'shown'>
>
TypeExtends<typeof experimentalFeature, Record<never, 'hidden' | 'shown'>>
>();
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const datetimeRange2str = (
const datetimeRangeListDiff = (
a: Pick<EventSchedule, 'datetimeRangeList' | 'datetimeSpecification'>,
b: Pick<EventSchedule, 'datetimeRangeList' | 'datetimeSpecification'>
): ReadonlyRecord<'added' | 'deleted', readonly string[]> | undefined => {
): Record<'added' | 'deleted', readonly string[]> | undefined => {
const setA = ISetMapped.new(
a.datetimeRangeList,
datetimeRangeToMapKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export namespace EventScheduleAppLocalStorage {
createEventPage: EventSchedule;
}>;

type StoreKeys = ReadonlyRecord<keyof Store, string>;
type StoreKeys = Record<keyof Store, string>;

const keys: StoreKeys = {
createEventPage: attachSuffix('createEventPage'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const toClassName = (flags: ReadonlyRecord<string, boolean>): string =>
export const toClassName = (flags: Record<string, boolean>): string =>
Obj.entries(flags)
.filter(([_k, v]) => v)
.map(([k, _v]) => k)
Expand Down
6 changes: 1 addition & 5 deletions packages/apps/poll-discord-app/src/discord/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ const onRefreshClick = async (
if (result.some(Result.isErr)) {
return Result.err(
Result.unwrapThrow(
Json.stringify(
result.map((a) => a.value) as ReadonlyJSONValue,
undefined,
2
)
Json.stringify(result.map((a) => a.value) as JSONValue, undefined, 2)
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/apps/poll-discord-app/src/in-memory-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const addPoll = (
psqlClient: PsqlClient,
poll: Poll,
messageId: CommandMessageId
): Promise<Result<undefined, ReadonlyJSONValue>> =>
): Promise<Result<undefined, JSONValue>> =>
setDatabase(
ref,
psqlClient,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const updatePoll = (
ref: DatabaseRef,
psqlClient: PsqlClient,
poll: Poll
): Promise<Result<undefined, ReadonlyJSONValue>> =>
): Promise<Result<undefined, JSONValue>> =>
setDatabase(
ref,
psqlClient,
Expand Down Expand Up @@ -133,7 +133,7 @@ const setDatabase = (
mut_ref: DatabaseMutRef,
psqlClient: DeepReadonly<PsqlClient>,
next: Database
): Promise<Result<undefined, ReadonlyJSONValue>> => {
): Promise<Result<undefined, JSONValue>> => {
mut_ref.db = next;
return psql.setJsonData(psqlClient, databaseToJson(next));
};
Expand Down
5 changes: 1 addition & 4 deletions packages/apps/poll-discord-app/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export const log = (
value: ReadonlyJSONValue,
prettyPrint: boolean = true
): void => {
export const log = (value: JSONValue, prettyPrint: boolean = true): void => {
console.log(
Result.unwrapThrow(
Json.stringify(value, undefined, prettyPrint ? 2 : undefined)
Expand Down
6 changes: 3 additions & 3 deletions packages/apps/poll-discord-app/src/postgre-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export namespace psql {

export const setJsonData = (
psqlClient: PsqlClient,
jsonData: ReadonlyJSONType
): Promise<Result<undefined, ReadonlyJSONValue>> => {
jsonData: JSONType
): Promise<Result<undefined, JSONValue>> => {
const query = `update ${psqlTableName} SET ${
psqlRowType.data
} = '${Result.unwrapThrow(Json.stringify(jsonData))}', ${
Expand All @@ -49,7 +49,7 @@ export namespace psql {
return new Promise((resolve) => {
psqlClient.query(query, (error) => {
if (toBoolean(error)) {
resolve(Result.err(error as unknown as ReadonlyJSONValue));
resolve(Result.err(error as unknown as JSONValue));
} else {
resolve(Result.ok(undefined));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/poll-discord-app/src/types/answer-of-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type AnswerOfDateJson = DeepReadonly<{
poor: string[];
}>;

assertType<TypeExtends<AnswerOfDateJson, ReadonlyJSONType>>();
assertType<TypeExtends<AnswerOfDateJson, JSONType>>();

export const answerOfDateDefaultValue: AnswerOfDate = {
good: ISet.new<UserId>([]),
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/poll-discord-app/src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type DatabaseJson = DeepReadonly<{
commandMessageIdToPollIdMap: Record<CommandMessageId, PollId>;
}>;

assertType<TypeExtends<DatabaseJson, ReadonlyJSONType>>();
assertType<TypeExtends<DatabaseJson, JSONType>>();

export const databaseDefaultValue: Database = {
polls: IMap.new<PollId, Poll>([]),
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/poll-discord-app/src/types/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type PollJson = DeepReadonly<{
titleMessageId: TitleMessageId;
}>;

assertType<TypeExtends<PollJson, ReadonlyJSONType>>();
assertType<TypeExtends<PollJson, JSONType>>();

const pollDefaultValue: Poll = {
id: createPollId(''),
Expand Down
18 changes: 9 additions & 9 deletions packages/utils/io-ts/src/combination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('nested record', () => {

describe('is', () => {
test('truthy case', () => {
const x: ReadonlyRecordBase = {
const x: RecordBase = {
xs: [1, 2, 3],
rec: {
a: 1,
Expand All @@ -47,14 +47,14 @@ describe('nested record', () => {
if (nestedRecord.is(x)) {
assertType<TypeEq<typeof x, NestedRecord>>();
} else {
assertType<TypeEq<typeof x, ReadonlyRecordBase>>();
assertType<TypeEq<typeof x, RecordBase>>();
}

expect(nestedRecord.is(x)).toBe(true);
});

test('falsy case', () => {
const x: ReadonlyRecordBase = {
const x: RecordBase = {
xs: [-1, 2.2, 3.3],
rec: {
a: 123,
Expand All @@ -66,7 +66,7 @@ describe('nested record', () => {
if (nestedRecord.is(x)) {
assertType<TypeEq<typeof x, NestedRecord>>();
} else {
assertType<TypeEq<typeof x, ReadonlyRecordBase>>();
assertType<TypeEq<typeof x, RecordBase>>();
}

expect(nestedRecord.is(x)).toBe(false);
Expand All @@ -75,7 +75,7 @@ describe('nested record', () => {

describe('validate', () => {
test('falsy case', () => {
const x: ReadonlyRecordBase = {
const x: RecordBase = {
xs: [-1, 2.2, 3.3],
rec: {
a: 123,
Expand All @@ -94,7 +94,7 @@ describe('nested record', () => {

describe('fill', () => {
test('from an empty record', () => {
const x: ReadonlyRecordBase = {};
const x: RecordBase = {};

expect(nestedRecord.fill(x)).toStrictEqual({
xs: [],
Expand All @@ -107,7 +107,7 @@ describe('nested record', () => {
});

test('from a filled record', () => {
const x: ReadonlyRecordBase = {
const x: RecordBase = {
xs: [-1, 2.2, 3.3],
rec: {
a: 123,
Expand All @@ -127,7 +127,7 @@ describe('nested record', () => {
});

test('from a partial record', () => {
const x: ReadonlyRecordBase = {
const x: RecordBase = {
xs: [11, 22],
rec: {
a: 3,
Expand All @@ -145,7 +145,7 @@ describe('nested record', () => {
});

test('from a partial record with excess property', () => {
const x: ReadonlyRecordBase = {
const x: RecordBase = {
xs: [11, 22],
rec: {
a: 3,
Expand Down
Loading

0 comments on commit 496a214

Please sign in to comment.