generated from hywax/vite-vanilla-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: changed property keys to symbols for consistency
- Loading branch information
1 parent
a8b47dc
commit 9d276ad
Showing
2 changed files
with
27 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |