- 
                Notifications
    
You must be signed in to change notification settings  - Fork 9
 
chore: Align isObject/isRecord usage #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
 
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,28 @@ | ||
| import { type JSONSchema } from "@commontools/builder"; | ||
| import { isObject } from "@commontools/utils/types"; | ||
| 
     | 
||
| export const isJSONSchema = (source: unknown): source is JSONSchema => { | ||
| if (!isObject(source)) { | ||
| return false; | ||
| } | ||
| 
     | 
||
| if (!("type" in source) || !source.type) { | ||
| return "anyOf" in source && Array.isArray(source.anyOf); | ||
| } | ||
| 
     | 
||
| switch (source.type) { | ||
| case "object": | ||
| case "array": | ||
| case "string": | ||
| case "integer": | ||
| case "number": | ||
| case "boolean": | ||
| case "null": | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| }; | ||
| 
     | 
||
| // Types used by the `common-iframe-sandbox` IPC. | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -107,34 +131,12 @@ export function isGuestError(e: object): e is GuestError { | |
| "stacktrace" in e && typeof e.stacktrace === "string"; | ||
| } | ||
| 
     | 
||
| const isObject = (source: unknown): source is Record<string, unknown> => | ||
| typeof source === "object" && source != null; | ||
| export const isTaskPerform = (source: unknown): source is TaskPerform => | ||
| isObject(source) && | ||
| typeof source?.intent === "string" && | ||
| typeof source?.description === "string" && | ||
| isObject(source?.input) && | ||
| isJSONSchema(source?.output); | ||
| 
     | 
||
| export const isJSONSchema = (source: unknown): source is JSONSchema => { | ||
| if (!isObject(source)) { | ||
| return false; | ||
| } | ||
| 
     | 
||
| switch (source?.type) { | ||
| case "object": | ||
| case "array": | ||
| case "string": | ||
| case "integer": | ||
| case "number": | ||
| case "boolean": | ||
| case "null": | ||
| return true; | ||
| default: { | ||
| return Array.isArray(source?.anyOf); | ||
| } | ||
| } | ||
| }; | ||
| "intent" in source && typeof source.intent === "string" && | ||
                
       | 
||
| "description" in source && typeof source.description === "string" && | ||
| "input" in source && isObject(source.input) && | ||
| "output" in source && isJSONSchema(source.output); | ||
| 
     | 
||
| export enum HostMessageType { | ||
| Ping = "ping", | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1 @@ | ||
| export * from "./defer.ts"; | ||
| export * from "./env.ts"; | ||
| export * from "./isObj.ts"; | ||
| export * from "./sleep.ts"; | ||
| throw new Error(`Import individual exports for utils: \`import { defer } from "@commontools/utils/defer"\``); | 
This file was deleted.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| // Predicate for narrowing a `Record` type, with string, | ||
| // symbol, or number (arrays) keys. | ||
| export function isRecord( | ||
| value: unknown, | ||
| ): value is Record<string | number | symbol, unknown> { | ||
| return typeof value === "object" && value !== null; | ||
| } | ||
| 
     | 
||
| // Predicate for narrowing a non-array/non-null `object` type. | ||
| export function isObject(value: unknown): value is object { | ||
| return typeof value === "object" && value !== null && !Array.isArray(value); | ||
| } | ||
| 
     | 
||
| // Predicate for narrowing a `number` type. | ||
| export function isNumber(value: unknown): value is number { | ||
| return typeof value === "number" && Number.isFinite(value); | ||
| } | ||
| 
     | 
||
| // Predicate for narrowing a `string` type. | ||
| export function isString(value: unknown): value is string { | ||
| return typeof value === "string"; | ||
| } | ||
| 
     | 
||
| // Helper type to recursively remove `readonly` properties from type `T`. | ||
| export type Mutable<T> = T extends ReadonlyArray<infer U> ? Mutable<U>[] | ||
| : T extends object ? ({ -readonly [P in keyof T]: Mutable<T[P]> }) | ||
| : T; | 
Uh oh!
There was an error while loading. Please reload this page.