Skip to content

Commit

Permalink
arrays also
Browse files Browse the repository at this point in the history
  • Loading branch information
nicu-chiciuc committed Aug 9, 2024
1 parent 6f6b185 commit 8088101
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 21 deletions.
18 changes: 17 additions & 1 deletion packages/sure/esm/meta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ export type InferJustMeta<T> = T extends Sure<unknown, unknown, any, {
}> ? {
type: 'object';
schema: ExtractPrimitives<CSchema>;
} : T extends Sure<unknown, unknown, any, {
meta: {
type: 'optional';
schema: infer CSchema;
};
}> ? {
type: 'optional';
schema: InferJustMeta<CSchema>;
} : T extends Sure<unknown, unknown, any, {
meta: {
type: 'array';
schema: infer CSchema;
};
}> ? {
type: 'array';
schema: InferJustMeta<CSchema>;
} : T extends Sure<unknown, unknown, any, {
meta: infer Meta;
}> ? Meta : 'unknown';
export declare function justMeta<TSchema extends Sure<unknown, unknown, any>>(schema: TSchema): PrettifyRec<InferJustMeta<TSchema>>;
export declare function justMeta<TSchema extends Sure<unknown, unknown, any>>(insure: TSchema): PrettifyRec<InferJustMeta<TSchema>>;
36 changes: 33 additions & 3 deletions packages/sure/esm/meta.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
export function justMeta(schema) {
const something = schema.meta;
return something;
import { object } from './object.js';
import { objMapEntries } from './utils.js';
export function justMeta(insure) {
// object.getMeta(insure) -> if not object -> null
object;
// @ts-expect-error more explicit?
if (insure.meta?.type === 'object') {
const { schema, ...rest } = insure.meta;
const ret = objMapEntries(schema, ([key, value]) => {
return [key, justMeta(value)];
});
return { schema: ret, ...rest };
}
// @ts-expect-error more explicit?
if (insure.meta?.type === 'optional') {
const { schema, ...rest } = insure.meta;
return {
schema: justMeta(schema),
...rest,
};
}
// @ts-expect-error more explicit?
if (insure.meta?.type === 'array') {
const { schema, ...rest } = insure.meta;
return {
schema: justMeta(schema),
...rest,
};
}
if (insure.meta) {
return insure.meta;
}
return 'unknown';
}
7 changes: 7 additions & 0 deletions packages/sure/esm/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ export type Prettify<T> = {
export type PrettifyRec<T> = {
[K in keyof T]: PrettifyRec<T[K]>;
} & {};
type PropKey = string | number | symbol;
/**
* Check source.
* Maps over an object using `Object.entries` and returns a new object.
*/
export declare function objMapEntries<Obj extends Record<PropKey, unknown>, ResultKey extends PropKey, ResultValue>(value: Obj, callback: (entry: [key: keyof Obj, value: Obj[keyof Obj]]) => readonly [ResultKey, ResultValue]): Record<ResultKey, ResultValue>;
export {};
9 changes: 8 additions & 1 deletion packages/sure/esm/utils.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export {};
/**
* Check source.
* Maps over an object using `Object.entries` and returns a new object.
*/
export function objMapEntries(value, callback) {
// @ts-expect-error We expect an error here
return Object.fromEntries(Object.entries(value).map(callback));
}
42 changes: 32 additions & 10 deletions packages/sure/src/__tests__/justMeta.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { describe, expect, it } from 'vitest'
import { MetaNever, MetaObj } from '../core.js'
import { InferJustMeta, ExtractPrimitives, justMeta } from '../meta.js'
import { object } from '../object.js'
import { object, optional } from '../object.js'
import { string, number } from '../primitives.js'
import { PrettifyRec } from '../utils.js'
import { assertEqual } from './typeTestUtils.js'
import { array } from '../array.js'

const simpleObj = object({
name: string,
name: optional(string),
age: number,
info: object({
country: string,
country: array(string),
}),
})

Expand All @@ -19,20 +20,29 @@ type SimplifyMeta<TMeta extends MetaNever | MetaObj> = TMeta extends MetaObj<inf
type InferredJustMeta = InferJustMeta<typeof simpleObj>
type Prett = PrettifyRec<InferredJustMeta>

const temp = optional(number)
type tempType = InferJustMeta<typeof temp>

assertEqual<
Prett,
{
type: 'object'
schema: {
name: {
type: 'string'
type: 'optional'
schema: {
type: 'string'
}
}
age: 'unknown'
info: {
type: 'object'
schema: {
country: {
type: 'string'
type: 'array'
schema: {
type: 'string'
}
}
}
}
Expand All @@ -59,33 +69,45 @@ describe('justMeta', () => {
type: 'object'
schema: {
name: {
type: 'string'
type: 'optional'
schema: {
type: 'string'
}
}
age: 'unknown'
info: {
type: 'object'
schema: {
country: {
type: 'string'
type: 'array'
schema: {
type: 'string'
}
}
}
}
}
}
>(true)

expect(result).toBe({
expect(result).toStrictEqual({
type: 'object',
schema: {
name: {
type: 'string',
type: 'optional',
schema: {
type: 'string',
},
},
age: 'unknown',
info: {
type: 'object',
schema: {
country: {
type: 'string',
type: 'array',
schema: {
type: 'string',
},
},
},
},
Expand Down
71 changes: 65 additions & 6 deletions packages/sure/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Sure } from './core.js'
import { PrettifyRec } from './utils.js'
import { object } from './object.js'
import { PrettifyRec, objMapEntries } from './utils.js'

export type ExtractPrimitives<TSchema> = {
[t in keyof TSchema]: InferJustMeta<TSchema[t]>
Expand All @@ -8,7 +9,7 @@ export type ExtractPrimitives<TSchema> = {
// prettier-ignore
export type InferJustMeta<T
// extends Sure<unknown, unknown, any, {}>
> =
> =


// for objects
Expand All @@ -20,17 +21,75 @@ export type InferJustMeta<T
type: 'object'
schema: ExtractPrimitives<CSchema>
}

// for optional
:T extends Sure<unknown, unknown, any, {meta: {
type: 'optional'
schema: infer CSchema
}}>
?{
type: 'optional'
schema: InferJustMeta<CSchema>
}

// for array
:T extends Sure<unknown, unknown, any, {meta: {
type: 'array'
schema: infer CSchema
}}> ? {
type: 'array'
schema: InferJustMeta<CSchema>
}


// for any other type
:
T extends Sure<unknown, unknown, any, {meta: infer Meta}>
? Meta :
? Meta :

'unknown'

export function justMeta<TSchema extends Sure<unknown, unknown, any>>(
schema: TSchema
insure: TSchema
): PrettifyRec<InferJustMeta<TSchema>> {
const something = schema.meta
return something as any
// object.getMeta(insure) -> if not object -> null

object

// @ts-expect-error more explicit?
if (insure.meta?.type === 'object') {
const { schema, ...rest } = insure.meta as any

const ret = objMapEntries(schema, ([key, value]) => {
return [key, justMeta(value)]
})

return { schema: ret, ...rest }
}

// @ts-expect-error more explicit?
if (insure.meta?.type === 'optional') {
const { schema, ...rest } = insure.meta as any

return {
schema: justMeta(schema),
...rest,
}
}

// @ts-expect-error more explicit?
if (insure.meta?.type === 'array') {
const { schema, ...rest } = insure.meta as any

return {
schema: justMeta(schema),
...rest,
}
}

if (insure.meta) {
return insure.meta as any
}

return 'unknown' as any
}
19 changes: 19 additions & 0 deletions packages/sure/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ export type Prettify<T> = {
export type PrettifyRec<T> = {
[K in keyof T]: PrettifyRec<T[K]>
} & {}

type PropKey = string | number | symbol

/**
* Check source.
* Maps over an object using `Object.entries` and returns a new object.
*/
export function objMapEntries<
//
Obj extends Record<PropKey, unknown>,
ResultKey extends PropKey,
ResultValue,
>(
value: Obj,
callback: (entry: [key: keyof Obj, value: Obj[keyof Obj]]) => readonly [ResultKey, ResultValue]
): Record<ResultKey, ResultValue> {
// @ts-expect-error We expect an error here
return Object.fromEntries(Object.entries(value).map(callback))
}

0 comments on commit 8088101

Please sign in to comment.