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: moved public (exported) functions to separate files
- Loading branch information
1 parent
a390d3c
commit 6776454
Showing
4 changed files
with
99 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
type IndexedKeysMessageSchema, type Invalid, isSchemaLeaf, type SchemaLeaf, | ||
type ValidIndexedKeysMessageSchema, | ||
type ValidSchemaLeaf | ||
} from "./indexedKeysSchema"; | ||
import { get, set } from "./raw"; | ||
|
||
type PlainObjectOfSchema<TSchema> = TSchema extends ValidIndexedKeysMessageSchema<unknown> ? { | ||
[K in keyof TSchema]: TSchema[K] extends ValidSchemaLeaf<infer TField> | ||
? TField | ||
: TSchema[K] extends SchemaLeaf<unknown> | ||
? Invalid<"Schema needs to be validated before you use it!"> | ||
: PlainObjectOfSchema<TSchema[K]>; | ||
} | ||
: never; | ||
|
||
export function toPlainObject<TSchema extends ValidIndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>( | ||
message: readonly unknown[], | ||
schema: TSchema): PlainObjectOfSchema<TSchema> { | ||
|
||
const object: Partial<PlainObjectOfSchema<TSchema>> = {}; | ||
|
||
for (const [fieldName, nestedSchemaNode] of Object.entries(schema)) { | ||
const nestedNode = nestedSchemaNode as ValidIndexedKeysMessageSchema<unknown> | ValidSchemaLeaf<unknown>; | ||
let valueToSet = undefined; | ||
if (isSchemaLeaf(nestedNode)) { | ||
valueToSet = get(message, nestedNode); | ||
} else { | ||
valueToSet = toPlainObject(message, nestedNode); | ||
} | ||
|
||
object[fieldName as keyof PlainObjectOfSchema<TSchema>] = valueToSet as any; | ||
} | ||
|
||
return object as PlainObjectOfSchema<TSchema>; | ||
} | ||
|
||
export function toIndexedKeysMessage<TSchema extends ValidIndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>( | ||
plainObject: PlainObjectOfSchema<TSchema>, | ||
schema: TSchema): unknown[] { | ||
|
||
const message: unknown[] = []; | ||
populateIndexedKeysMessage(message, plainObject, schema); | ||
return message; | ||
} | ||
|
||
function populateIndexedKeysMessage<TSchema extends ValidIndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>( | ||
messageToPopulate: unknown[], | ||
plainObject: PlainObjectOfSchema<TSchema>, | ||
schema: TSchema) { | ||
|
||
for (const [fieldName, nestedSchemaNode] of Object.entries(schema)) { | ||
const nestedNode = nestedSchemaNode as IndexedKeysMessageSchema<unknown> | ValidSchemaLeaf<unknown>; | ||
const leafValueOrSubObject = plainObject[fieldName as keyof PlainObjectOfSchema<TSchema>]; | ||
if (isSchemaLeaf(nestedNode)) { | ||
set(messageToPopulate, nestedNode, leafValueOrSubObject); | ||
} else { | ||
populateIndexedKeysMessage(messageToPopulate, leafValueOrSubObject as PlainObjectOfSchema<TSchema>, nestedNode) | ||
} | ||
} | ||
} |
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,2 +1,5 @@ | ||
export { validateSchema, withIndex, get, set, toPlainObject, toIndexedKeysMessage, InvalidSchemaError } from './indexedKeysSchema' | ||
export { validateSchema, withIndex, InvalidSchemaError } from './indexedKeysSchema' | ||
export { get, set } from "./raw"; | ||
export { toPlainObject, toIndexedKeysMessage } from "./convenient"; | ||
|
||
export type { ValidIndexedKeysMessageSchema } from './indexedKeysSchema' |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { ValidSchemaLeaf } from "./indexedKeysSchema"; | ||
|
||
export function get<const TField>(message: readonly unknown[], schemaField: ValidSchemaLeaf<TField>) { | ||
const indexesPathReversed = schemaField.indexesPathReversed; | ||
let currentSlice: readonly unknown[] = message; | ||
|
||
for (let pathIndex = schemaField.indexesPathReversed.length - 1; pathIndex >= 1; pathIndex--) { | ||
currentSlice = currentSlice[indexesPathReversed[pathIndex]] as readonly unknown[]; | ||
} | ||
|
||
const lastIndexInPath = indexesPathReversed[0]; | ||
return currentSlice[lastIndexInPath] as TField; | ||
} | ||
|
||
export function set<const TField>(targetMessage: unknown[], schemaField: ValidSchemaLeaf<TField>, value: TField) { | ||
const indexesPathReversed = schemaField.indexesPathReversed; | ||
let currentSlice: unknown[] = targetMessage; | ||
|
||
for (let pathIndex = schemaField.indexesPathReversed.length - 1; pathIndex >= 1; pathIndex--) { | ||
if (currentSlice[indexesPathReversed[pathIndex]] === undefined) { | ||
currentSlice[indexesPathReversed[pathIndex]] = []; | ||
} | ||
|
||
currentSlice = currentSlice[indexesPathReversed[pathIndex]] as unknown[]; | ||
} | ||
|
||
const lastIndexInPath = indexesPathReversed[0]; | ||
currentSlice[lastIndexInPath] = value; | ||
} |