Skip to content

Commit 334057f

Browse files
committed
Merge branch 'release/v0.28.14'
2 parents 7f82328 + df1b629 commit 334057f

2 files changed

Lines changed: 8 additions & 37 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "zeed",
33
"type": "module",
4-
"version": "0.28.13",
4+
"version": "0.28.14",
55
"packageManager": "pnpm@10.11.0",
66
"description": "🌱 Simple foundation library",
77
"author": {

src/common/schema/parse-env.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Type } from './schema'
2-
import { A } from 'vitest/dist/chunks/environment.d.Dmw5ulng.js'
32
import { assert } from '../assert'
4-
import { fromCamelCase, toCamelCase } from '../data/camelcase'
3+
import { fromCamelCase } from '../data/camelcase'
54
import { valueToBoolean, valueToBooleanNotFalse, valueToInteger } from '../data/convert'
6-
import { objectFilter, objectMap } from '../data/object'
5+
import { objectMap } from '../data/object'
76
import { isSchemaObjectFlat } from './utils'
87

98
declare module './schema' {
@@ -13,10 +12,10 @@ declare module './schema' {
1312
}
1413
}
1514

16-
type EnvType = Record<string, any>
15+
export type SchemaEnvType = Record<string, any>
1716

18-
interface SchemaEnvOptions {
19-
env?: EnvType
17+
export interface SchemaEnvOptions {
18+
env?: SchemaEnvType
2019
prefix?: string
2120
prefixOptional?: boolean // if true, prefix is optional
2221
}
@@ -25,9 +24,9 @@ export function parseSchemaEnv<T>(schema: Type<T>, opt?: SchemaEnvOptions): T {
2524
assert(schema?._object, 'schema should be of type object')
2625
assert(isSchemaObjectFlat(schema), 'schema should be a flat object')
2726

28-
let {
27+
const {
2928
// eslint-disable-next-line node/prefer-global/process
30-
env = (process?.env ?? {}) as EnvType,
29+
env = (process?.env ?? {}) as SchemaEnvType,
3130
prefix = '',
3231
prefixOptional = false,
3332
} = opt ?? {}
@@ -59,34 +58,6 @@ export function parseSchemaEnv<T>(schema: Type<T>, opt?: SchemaEnvOptions): T {
5958
}
6059
return schema.parse(value)
6160
}) as T
62-
63-
if (pl > 0)
64-
env = objectFilter(env, key => key.startsWith(prefix))
65-
env = objectMap(env, (key, value) => {
66-
if (pl > 0)
67-
key = key.substring(pl)
68-
return [toCamelCase(key), value]
69-
})
70-
const result = objectMap(schema._object!, (key, schema) => {
71-
let value = env[toCamelCase(key)]
72-
if (schema.type === 'number') {
73-
value = valueToInteger(value, schema._default)
74-
}
75-
else if (schema.type === 'boolean') {
76-
if (schema._default === true)
77-
value = valueToBooleanNotFalse(value)
78-
else
79-
value = valueToBoolean(value, false)
80-
}
81-
return schema.parse(value)
82-
}) as T
83-
84-
if (prefixOptional !== true || pl <= 0) {
85-
return result
86-
}
87-
88-
const resultWithoutPrefix = parseSchemaEnv(schema, { ...opt, prefix: '', prefixOptional: false })
89-
return { ...resultWithoutPrefix, ...result }
9061
}
9162

9263
export function stringFromSchemaEnv<T>(schema: Type<T>, prefix = '', commentOut = false, showPrivate = false): string {

0 commit comments

Comments
 (0)