Skip to content

Commit

Permalink
refactor: changed property keys to symbols for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
kpietraszko committed Jan 1, 2025
1 parent a8b47dc commit 9d276ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
26 changes: 16 additions & 10 deletions src/indexedKeysSchema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import type { NonNegativeInteger } from "type-fest";

export const indexesPathReversed = Symbol("indexesPathReversed");
const fieldType = Symbol("fieldType");
const isSchemaLeafTag = Symbol("isSchemaLeaf");
const isValidSchemaLeaf = Symbol("isValidSchemaLeaf");
const schemaRoot = Symbol("schemaRoot");

type IndexesPath = number[];
export type SchemaLeaf<TField> = {
indexesPathReversed: IndexesPath,
fieldType: TField,
[indexesPathReversed]: IndexesPath,
[fieldType]: TField,
[isSchemaLeafTag]: true
};

export type ValidSchemaLeaf<TField> = SchemaLeaf<TField> & { [isValidSchemaLeaf]: true };
export type ValidSchemaLeaf<TField> = SchemaLeaf<TField> & {
[isValidSchemaLeaf]: true,
[schemaRoot]: ValidIndexedKeysMessageSchema<unknown>
};

export type IndexedKeysMessageSchema<TSchema> = {
[K in keyof TSchema]: TSchema[K] extends SchemaLeaf<infer TField>
Expand Down Expand Up @@ -69,17 +75,17 @@ function validateSchemaRecursively(

function validateSchemaLeaf(schemaLeaf: SchemaLeaf<unknown>, encounteredIndexesPaths: IndexesPath[], currentTreeLevel: number){
const duplicateIndexesPathDetected = encounteredIndexesPaths.some(encounteredPath =>
encounteredPath.length === schemaLeaf.indexesPathReversed.length &&
encounteredPath.every((pathElement, index) => pathElement === schemaLeaf.indexesPathReversed[index]));
encounteredPath.length === schemaLeaf[indexesPathReversed].length &&
encounteredPath.every((pathElement, index) => pathElement === schemaLeaf[indexesPathReversed][index]));

if (duplicateIndexesPathDetected)
{
throw new InvalidSchemaError()
}

encounteredIndexesPaths.push(schemaLeaf.indexesPathReversed);
encounteredIndexesPaths.push(schemaLeaf[indexesPathReversed]);

const indexesPathLengthDoesntMatchLevel = schemaLeaf.indexesPathReversed.length !== (currentTreeLevel + 1);
const indexesPathLengthDoesntMatchLevel = schemaLeaf[indexesPathReversed].length !== (currentTreeLevel + 1);
if (indexesPathLengthDoesntMatchLevel)
{
throw new InvalidSchemaError()
Expand All @@ -95,8 +101,8 @@ export function withIndex<const TIndex extends number>(index: NonNegativeInteger
}

return {
indexesPathReversed: [index] as number[],
fieldType: undefined as TField,
[indexesPathReversed]: [index] as number[],
[fieldType]: undefined as TField,
[isSchemaLeafTag]: true
} as const as ReturnedSchemaNode<TField, TNestedSchema>;
};
Expand All @@ -115,7 +121,7 @@ function addIndexToPathsRecursively(
for (const [_, nestedSchemaNode] of Object.entries(schemaNode)) {
const nestedNode = nestedSchemaNode as IndexedKeysMessageSchema<unknown> | SchemaLeaf<unknown>;
if (isSchemaLeaf(nestedNode)) {
nestedNode.indexesPathReversed.push(indexToAdd);
nestedNode[indexesPathReversed].push(indexToAdd);
} else {
addIndexToPathsRecursively(nestedNode, indexToAdd);
}
Expand Down
21 changes: 11 additions & 10 deletions src/raw.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import type { ValidSchemaLeaf } from "./indexedKeysSchema";
import {indexesPathReversed} from './indexedKeysSchema';

export function get<const TField>(message: readonly unknown[], schemaField: ValidSchemaLeaf<TField>) {
const indexesPathReversed = schemaField.indexesPathReversed;
const reversedIndexesPath = schemaField[indexesPathReversed];
let currentSlice: readonly unknown[] = message;

for (let pathIndex = schemaField.indexesPathReversed.length - 1; pathIndex >= 1; pathIndex--) {
currentSlice = currentSlice[indexesPathReversed[pathIndex]] as readonly unknown[];
for (let pathIndex = schemaField[indexesPathReversed].length - 1; pathIndex >= 1; pathIndex--) {
currentSlice = currentSlice[reversedIndexesPath[pathIndex]] as readonly unknown[];
}

const lastIndexInPath = indexesPathReversed[0];
const lastIndexInPath = reversedIndexesPath[0];
return currentSlice[lastIndexInPath] as TField;
}

export function set<const TField>(targetMessage: unknown[], schemaField: ValidSchemaLeaf<TField>, value: TField) {
const indexesPathReversed = schemaField.indexesPathReversed;
const reversedIndexesPath = schemaField[indexesPathReversed];
let currentSlice: unknown[] = targetMessage;

for (let pathIndex = schemaField.indexesPathReversed.length - 1; pathIndex >= 1; pathIndex--) {
if (currentSlice[indexesPathReversed[pathIndex]] === undefined) {
currentSlice[indexesPathReversed[pathIndex]] = [];
for (let pathIndex = schemaField[indexesPathReversed].length - 1; pathIndex >= 1; pathIndex--) {
if (currentSlice[reversedIndexesPath[pathIndex]] === undefined) {
currentSlice[reversedIndexesPath[pathIndex]] = [];
}

currentSlice = currentSlice[indexesPathReversed[pathIndex]] as unknown[];
currentSlice = currentSlice[reversedIndexesPath[pathIndex]] as unknown[];
}

const lastIndexInPath = indexesPathReversed[0];
const lastIndexInPath = reversedIndexesPath[0];
currentSlice[lastIndexInPath] = value;
}

0 comments on commit 9d276ad

Please sign in to comment.