Skip to content

Commit

Permalink
Merge branch 'release/v0.24.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Aug 28, 2024
2 parents 1290d84 + e6b32ed commit cac3b27
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 161 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.9",
"version": "0.24.10",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
34 changes: 34 additions & 0 deletions src/common/msg/emitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,40 @@ describe('emitter', () => {
expect(counter).toBe(100)
})

it('should work with addEventListener', async () => {
interface TestMessages {
a: () => void
b: () => void
}

const e1 = new Emitter<TestMessages>()
const e2 = new Emitter<TestMessages>()

let counter = 99

e1.addEventListener('a', () => counter++)

e2.addEventListener('a', () => counter--)
e2.addEventListener('b', () => counter--)

void e1.emit('a')

expect(counter).toBe(100)

void e2.emit('a')
void e2.emit('b')

expect(counter).toBe(98)

e1.addEventListener('a', () => counter++)
void e1.emit('a') // twice!
expect(counter).toBe(100)

e1.removeAllListeners()
void e1.emit('a') // no listeners!
expect(counter).toBe(100)
})

it('should wait on', async () => {
expect.assertions(2)

Expand Down
4 changes: 4 additions & 0 deletions src/common/msg/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ export class Emitter<
}, timeoutMS)
})
}

// For compatibility reasons
addEventListener = this.on.bind(this)
removeEventListener = this.off.bind(this)
}

declare global {
Expand Down
3 changes: 0 additions & 3 deletions src/common/schema/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ export function parseSchemaEnv<T>(schema: Type<T>, env: any = process?.env ?? {}

export function stringFromSchemaEnv<T>(schema: Type<T>, prefix = '', commentOut = false): string {
assert(isSchemaObjectFlat(schema), 'schema should be a flat object')

const lines: string[] = []

objectMap(schema._object!, (key, schema) => {
lines.push(`${commentOut ? '# ' : ''}${prefix + fromCamelCase(key, '_').toUpperCase()}=${schema._default ?? ''}`)
}) as T

return lines.join('\n')
}
35 changes: 4 additions & 31 deletions src/common/schema/rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import { string } from './schema'
import { func, object, string } from './schema'
import type { Infer, Type } from './schema'

// see https://github.com/colinhacks/zod?tab=readme-ov-file#functions

describe('rpc.spec', () => {
it('should do something', async () => {
const rpcSchema = {
echo: string(),
}

type RpcRaw = typeof rpcSchema

type RpcFunc<T> = {
[K in keyof T]: (arg: Infer<T[K]>) => void
}

type Rpc = RpcFunc<RpcRaw>
})

it('should do something2', async () => {
function func(args: Type<any>[], result?: Type<any>) {
return {
args,
result,
}
}

const rpcSchema = {
const rpcSchema = object({
echo: func([string()], string()),
}

type RpcRaw = typeof rpcSchema

type RpcFunc<T> = {
[K in keyof T]: (arg: Infer<T[K]>) => void
}
})

type Rpc = RpcFunc<RpcRaw>
type RpcRaw = Infer<typeof rpcSchema>
})
})
13 changes: 13 additions & 0 deletions src/common/schema/sandbox-inherit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class TypeClass<T = unknown> {
optional(): TypeClass<T | undefined> {
return this
}
}

class TypeStringClass<T extends string> extends TypeClass<T> {

}

const o = new TypeStringClass()
const v = o.optional()
type t = typeof v // expect: TypeStringClass<string | undefined>
45 changes: 45 additions & 0 deletions src/common/schema/sandbox.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable ts/no-unsafe-declaration-merging */
// // Define an interface for the instance type
// interface MyTypeInstance {
// value: number
// test: () => number
// }

// // Constructor function
// function MyType(this: MyTypeInstance, value: number) {
// this.value = value
// }

// MyType.prototype.test = function () {
// return this.value
// }

class MyType {
value: number
constructor(value: number) {
this.value = value
}
}

interface MyType {
test: () => number
}

describe('sandbox.spec', () => {
it('should do something', async () => {
// Create an instance of MyType using the 'new' keyword
const my = new MyType(123)

MyType.prototype.test = function () {
return this.value + 1
}

// Example usage
expect(my.test()).toMatchInlineSnapshot(`124`)
expect(my).toMatchInlineSnapshot(`
MyType {
"value": 123,
}
`)
})
})
2 changes: 2 additions & 0 deletions src/common/schema/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ function number(): Type<number> {

const tt = tuple([number(), string(), boolean()])
type ttt = Infer<typeof tt> // expected [number, string, boolean]

//
20 changes: 8 additions & 12 deletions src/common/schema/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ describe('schema', () => {
const lit = stringLiterals(['active', 'trialing', 'past_due', 'paused', 'deleted'])
type ScheamLiterals = Infer<typeof lit>
type SchemaLiteralsTest = Expect<IsEqual<ScheamLiterals, Status>> // Should pass
expectTypeOf<ScheamLiterals>().toMatchTypeOf<Status>()

// Tuple
const tup = tuple([number(), string(), boolean()])
type SchemaTuple = Infer<typeof tup> // expected [number, string, boolean]
type SchemaTupleTest = Expect<IsEqual<SchemaTuple, [number, string, boolean]>> // Should pass
expectTypeOf<SchemaTuple>().toMatchTypeOf<[number, string, boolean]>()

const s1 = string().optional() // .pattern(/\d+/)
type t1a = typeof s1
type t1 = Infer<typeof s1>
expectTypeOf<t1>().toMatchTypeOf<string | undefined>()

const schema = object({
id: string().default(() => '123'),
id: string().default('123'), // default(() => '123'),
name: string(),
age: int().optional(),
active: boolean(),
Expand Down Expand Up @@ -157,17 +164,6 @@ describe('schema', () => {
"type": "string",
},
"name": Object {
"_union": Array [
Object {
"type": "string",
},
Object {
"type": "string",
},
Object {
"type": "string",
},
],
"type": "string",
},
},
Expand Down
Loading

0 comments on commit cac3b27

Please sign in to comment.