@mailwoman/core 7.2.0 → 7.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/APIClient.ts +175 -0
- package/api/headless.ts +23 -0
- package/api/index.ts +11 -0
- package/api/responses.ts +135 -0
- package/coarse-placer/coarse-placer.ts +300 -0
- package/coarse-placer/featurize.ts +171 -0
- package/coarse-placer/tools/build-dataset.ts +223 -0
- package/coarse-placer/tools/build-outlier-exposure.ts +172 -0
- package/coarse-placer/tools/build-outlier-latin.ts +200 -0
- package/coarse-placer/tools/build-outlier-oa.ts +231 -0
- package/coarse-placer/tools/eval-latin-offmap.ts +140 -0
- package/coarse-placer/tools/eval-openset.ts +519 -0
- package/coarse-placer/tools/eval-quant-compare.ts +151 -0
- package/coarse-placer/tools/eval.ts +187 -0
- package/coarse-placer/tools/fnv-hash.ts +24 -0
- package/coarse-placer/tools/index.ts +23 -0
- package/coarse-placer/tools/probe-frontier.ts +216 -0
- package/coarse-placer/tools/quantize.ts +116 -0
- package/coarse-placer/tools/train.ts +244 -0
- package/collections.ts +134 -0
- package/db/schema.ts +21 -0
- package/decoder/build-tree.ts +228 -0
- package/decoder/calibration.ts +101 -0
- package/decoder/containment.ts +87 -0
- package/decoder/index.ts +17 -0
- package/decoder/proposals-to-tree.ts +83 -0
- package/decoder/resolve-proposal-overlaps.ts +79 -0
- package/decoder/serialize-json.ts +70 -0
- package/decoder/serialize-tuples.ts +61 -0
- package/decoder/serialize-xml.ts +209 -0
- package/decoder/types.ts +159 -0
- package/decoder/unknown-spans.ts +103 -0
- package/decoder/validate-tree.ts +100 -0
- package/env/index.ts +42 -0
- package/env/load.ts +14 -0
- package/env/paths.ts +40 -0
- package/env/schema.ts +125 -0
- package/errors/index.ts +9 -0
- package/errors/schema.ts +155 -0
- package/fs/node.ts +38 -0
- package/identifiers.ts +58 -0
- package/index.ts +13 -0
- package/kysley/adapter.ts +29 -0
- package/kysley/client.ts +30 -0
- package/kysley/dialect-config.ts +30 -0
- package/kysley/dialect.ts +67 -0
- package/kysley/driver.ts +175 -0
- package/layers/index.ts +12 -0
- package/layers/manifest.ts +154 -0
- package/layers/schema.ts +105 -0
- package/lifecycle/ServiceSymbol.ts +95 -0
- package/lifecycle/index.ts +11 -0
- package/lifecycle/lru-cache.ts +44 -0
- package/lifecycle/services.ts +322 -0
- package/locale/index.ts +9 -0
- package/locale/locale.ts +49 -0
- package/locale/profiles/en-us.ts +69 -0
- package/locale/profiles/fr-fr.ts +75 -0
- package/locale/profiles/index.ts +9 -0
- package/locale/profiles/ja-jp.ts +33 -0
- package/locale/registry.ts +68 -0
- package/logging/index.ts +8 -0
- package/logging/shared.ts +145 -0
- package/logging/utils.ts +46 -0
- package/objects.ts +211 -0
- package/package.json +195 -104
- package/pipeline/index.ts +46 -0
- package/pipeline/reconcile-lookups.ts +108 -0
- package/pipeline/reconcile.ts +542 -0
- package/pipeline/runtime-pipeline.ts +732 -0
- package/pipeline/span-logit-aggregation.ts +151 -0
- package/pipeline/span-proposer.ts +587 -0
- package/pipeline/types.ts +348 -0
- package/policy/defaults.ts +35 -0
- package/policy/index.ts +10 -0
- package/policy/policy.ts +60 -0
- package/policy/proposal-collection.ts +64 -0
- package/policy/registry.ts +149 -0
- package/resolver/index.ts +29 -0
- package/resolver/types.ts +584 -0
- package/resources/LocaleIndex.ts +124 -0
- package/resources/ResourceMapCache.ts +126 -0
- package/resources/collections.ts +93 -0
- package/resources/debugging.ts +19 -0
- package/resources/git.ts +54 -0
- package/resources/index.ts +17 -0
- package/resources/languages/index.ts +49 -0
- package/resources/languages/types.gen.ts +1368 -0
- package/resources/libaddressinput.ts +105 -0
- package/resources/libpostal.ts +182 -0
- package/resources/locale.ts +23 -0
- package/resources/set.ts +179 -0
- package/resources/whosonfirst/DataSourceCache.ts +72 -0
- package/resources/whosonfirst/PlacetypeDataSource.ts +199 -0
- package/resources/whosonfirst/index.ts +10 -0
- package/resources/whosonfirst/loader.ts +157 -0
- package/resources/whosonfirst/placetypes/Placetype.ts +438 -0
- package/resources/whosonfirst/placetypes/admin.ts +173 -0
- package/resources/whosonfirst/placetypes/definition.ts +135 -0
- package/resources/whosonfirst/placetypes/graph.ts +0 -0
- package/resources/whosonfirst/placetypes/index.ts +12 -0
- package/resources/whosonfirst/placetypes/mermaid.ts +168 -0
- package/resources/whosonfirst/placetypes/tree.ts +46 -0
- package/routing/index.ts +208 -0
- package/scripting/index.ts +40 -0
- package/scripting/utils/index.ts +106 -0
- package/tokenization/Graph.ts +50 -0
- package/tokenization/Span.ts +297 -0
- package/tokenization/index.ts +9 -0
- package/tokenization/normalizer.ts +123 -0
- package/tools/download-libpostal-resources.ts +98 -0
- package/tools/download-ssl-address.ts +99 -0
- package/tools/generate-language-types.ts +194 -0
- package/tools/index.ts +13 -0
- package/types/Classification.ts +211 -0
- package/types/classifier.ts +133 -0
- package/types/component.ts +75 -0
- package/types/index.ts +10 -0
- package/types/mapping.ts +81 -0
- package/utils/data-root.ts +56 -0
- package/utils/hash.ts +27 -0
- package/utils/index.ts +14 -0
- package/utils/jsonl.ts +43 -0
- package/utils/python-json.ts +105 -0
- package/utils/python-random.ts +90 -0
- package/utils/repo.ts +118 -0
- package/utils/sealed-db.ts +85 -0
- package/utils/stats.ts +32 -0
- package/vitest.config.ts +53 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { BaseLogger as PinoBaseLogger, Level as PinoLevel, LogFn as PinoLogFn, Logger as PinoLogger } from "pino"
|
|
8
|
+
|
|
9
|
+
export type Logger = PinoLogger
|
|
10
|
+
|
|
11
|
+
export type Level = PinoLevel
|
|
12
|
+
|
|
13
|
+
export type LogFn = PinoLogFn
|
|
14
|
+
|
|
15
|
+
export type BaseLogger = Pick<PinoBaseLogger, Exclude<Level, "fatal">>
|
|
16
|
+
|
|
17
|
+
//#region Constants
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Labels log levels in the browser console.
|
|
21
|
+
*/
|
|
22
|
+
export const LogLevelLabel = {
|
|
23
|
+
info: "[INFO]",
|
|
24
|
+
warn: "[WARN]",
|
|
25
|
+
error: "[ERROR]",
|
|
26
|
+
debug: "[DEBUG]",
|
|
27
|
+
trace: "[TRACE]",
|
|
28
|
+
fatal: "[FATAL]",
|
|
29
|
+
} as const satisfies Record<Level, string>
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Predefined log levels.
|
|
33
|
+
*/
|
|
34
|
+
export const LogLevels = Object.keys(LogLevelLabel) as Level[]
|
|
35
|
+
|
|
36
|
+
type LoggerFactory = (prefix?: string | null, ...args: string[]) => Logger
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Colors for log levels in the browser console.
|
|
40
|
+
*/
|
|
41
|
+
const LogLevelColors = {
|
|
42
|
+
info: `light-dark(#0043CE, #4589FF)`,
|
|
43
|
+
warn: `light-dark(#F1C21B, #F1C21B)`,
|
|
44
|
+
error: `light-dark(#DA1E28, #FA4D56)`,
|
|
45
|
+
debug: `light-dark(#8A3FFC, #A56EFF)`,
|
|
46
|
+
trace: `light-dark(#8A3FFC, #A56EFF)`,
|
|
47
|
+
fatal: `light-dark(#DA1E28, #FA4D56)`,
|
|
48
|
+
} as const
|
|
49
|
+
|
|
50
|
+
//#region Functions
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a logger with the given prefix.
|
|
54
|
+
*/
|
|
55
|
+
export const createConsoleLogger: LoggerFactory = (prefix, ...args) => {
|
|
56
|
+
const msgPrefix = prefix ? `(${prefix}):` : ":"
|
|
57
|
+
|
|
58
|
+
const logger: Partial<Logger> = {
|
|
59
|
+
msgPrefix,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const level of LogLevels) {
|
|
63
|
+
const label = LogLevelLabel[level]
|
|
64
|
+
|
|
65
|
+
// @ts-expect-error Alias the log method to the appropriate console method,
|
|
66
|
+
// defaulting to console.log if the level is not supported.
|
|
67
|
+
const method = level in console ? console[level] : console.log
|
|
68
|
+
|
|
69
|
+
logger[level] = method.bind(console, `${label} ${msgPrefix}`, ...args)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return logger as Logger
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type BaseConsoleLogger = Record<Level, LogFn>
|
|
76
|
+
|
|
77
|
+
export type IRuntimeLogger = BaseConsoleLogger
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
|
|
81
|
+
//#region Functions
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Creates a logger with the given prefix.
|
|
85
|
+
*/
|
|
86
|
+
export function createLogger(prefix?: string, ...args: string[][]): Logger {
|
|
87
|
+
const msgPrefix = prefix ? `(${prefix}):` : ":"
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @type {Partial<Logger>}
|
|
91
|
+
*/
|
|
92
|
+
const logger: Partial<Logger> = {
|
|
93
|
+
msgPrefix,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const level of LogLevels) {
|
|
97
|
+
const label = LogLevelLabel[level]
|
|
98
|
+
const color = LogLevelColors[level]
|
|
99
|
+
|
|
100
|
+
// @ts-expect-error Alias the log method to the appropriate console method,
|
|
101
|
+
// defaulting to console.log if the level is not supported.
|
|
102
|
+
const method = level in console ? console[level] : console.log
|
|
103
|
+
|
|
104
|
+
logger[level] = method.bind(
|
|
105
|
+
console,
|
|
106
|
+
`%c${label}%c ${msgPrefix}%c`,
|
|
107
|
+
`font-weight: 700; color: ${color};`,
|
|
108
|
+
`font-weight: 600; color: CanvasText;`,
|
|
109
|
+
"",
|
|
110
|
+
...args
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return logger as Logger
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A singleton logger instance for the browser.
|
|
119
|
+
*
|
|
120
|
+
* ```js
|
|
121
|
+
* import { ConsoleLogger } from "@mailwoman/core/logging"
|
|
122
|
+
*
|
|
123
|
+
* ConsoleLogger.info("Hello, world!")
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* @implements {IRuntimeLogger}
|
|
127
|
+
*/
|
|
128
|
+
export class ConsoleLogger {
|
|
129
|
+
static info: typeof console.info
|
|
130
|
+
static warn: typeof console.warn
|
|
131
|
+
static error: typeof console.error
|
|
132
|
+
static debug: typeof console.debug
|
|
133
|
+
static trace: typeof console.trace
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Creates a logger with the given prefix.
|
|
137
|
+
*/
|
|
138
|
+
static prefix(logPrefix: string) {
|
|
139
|
+
return createLogger(logPrefix)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
Object.assign(ConsoleLogger, createLogger())
|
|
144
|
+
|
|
145
|
+
//#endregion
|
package/logging/utils.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Casts an object to an array of log-friendly entries.
|
|
8
|
+
*
|
|
9
|
+
* @category Object
|
|
10
|
+
* @category Logger
|
|
11
|
+
*/
|
|
12
|
+
export function castToLoggableEntries<T extends Record<string, unknown>>(input: T) {
|
|
13
|
+
return Object.entries(input)
|
|
14
|
+
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
|
|
15
|
+
.filter(([_key, value]) => value && typeof value === "string") as [keyof T, string][]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Options for pretty-printing a logged object.
|
|
20
|
+
*
|
|
21
|
+
* @category Logger
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export interface StringifyLoggedObjectOptions {
|
|
25
|
+
description: string
|
|
26
|
+
showValues?: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Pretty-prints the public environment variables.
|
|
31
|
+
*
|
|
32
|
+
* @category Object
|
|
33
|
+
* @category Logger
|
|
34
|
+
*/
|
|
35
|
+
export function stringifyLoggedObject(
|
|
36
|
+
input: Record<string, unknown>,
|
|
37
|
+
{ description, showValues }: StringifyLoggedObjectOptions
|
|
38
|
+
): string {
|
|
39
|
+
const lines = castToLoggableEntries(input).map(([key, value]) => {
|
|
40
|
+
const printedValue = showValues ? value : new Array(value.length).fill("*").join("")
|
|
41
|
+
|
|
42
|
+
return `${key}: ${printedValue}`
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
return description + "\n\n" + lines.join("\n") + "\n"
|
|
46
|
+
}
|
package/objects.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Utility functions for working with objects.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { JsonObject } from "type-fest"
|
|
10
|
+
|
|
11
|
+
import { isIterable } from "./collections.ts"
|
|
12
|
+
|
|
13
|
+
type SetLike<T> = { has(value: T): boolean } | Iterable<T>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Type-utility for extracting the string keys of an object.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export type StringKeyOf<O> = Extract<keyof O, string>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param input Source object.
|
|
24
|
+
* @param scalarEnum Unconstrained scalar enum whose values present in `input` will be used as keys, such as an
|
|
25
|
+
* enum-like object.
|
|
26
|
+
*
|
|
27
|
+
* @returns A subset of the source object with only properties present in `scalarEnum`.
|
|
28
|
+
*/
|
|
29
|
+
export function pick<O extends object, K extends string>(
|
|
30
|
+
input: O,
|
|
31
|
+
scalarEnum: Record<K, K>,
|
|
32
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
33
|
+
): Pick<O, StringKeyOf<O>>
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param input Source object.
|
|
37
|
+
* @param setLike Set-like object whose members represent the subset of keys to pick.
|
|
38
|
+
*
|
|
39
|
+
* @returns A subset of the source object with only properties present in `scalarEnum`.
|
|
40
|
+
*/
|
|
41
|
+
export function pick<O extends object, K extends keyof O>(
|
|
42
|
+
input: O,
|
|
43
|
+
setLike: SetLike<K>,
|
|
44
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
45
|
+
): Pick<O, K>
|
|
46
|
+
/**
|
|
47
|
+
* @param input Source object.
|
|
48
|
+
* @param scalarEnum Enum-like object whose members represent the subset of keys to pick.
|
|
49
|
+
*
|
|
50
|
+
* @returns A subset of the source object with only properties present in `scalarEnum`.
|
|
51
|
+
*/
|
|
52
|
+
export function pick<O extends object, K extends keyof O>(
|
|
53
|
+
input: O,
|
|
54
|
+
scalarEnum: Record<K, K>,
|
|
55
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
56
|
+
): Pick<O, StringKeyOf<O>>
|
|
57
|
+
/**
|
|
58
|
+
* @param input Source object.
|
|
59
|
+
* @param constraints Unconstrained iterable of keys to pick, such as in array or Map.
|
|
60
|
+
*
|
|
61
|
+
* @returns A subset of the source object with only properties present in `constraints`.
|
|
62
|
+
*/
|
|
63
|
+
export function pick<O extends object, K extends keyof O = StringKeyOf<O>>(
|
|
64
|
+
input: O,
|
|
65
|
+
constraints: Iterable<K>,
|
|
66
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
67
|
+
): Pick<O, K>
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param input Source object.
|
|
71
|
+
* @param constraints Enum-like object whose members represent the subset of keys to pick.
|
|
72
|
+
*
|
|
73
|
+
* @returns A subset of the source object with only properties present in `constraints`.
|
|
74
|
+
*/
|
|
75
|
+
export function pick<O extends object, K extends keyof O = StringKeyOf<O>>(
|
|
76
|
+
input: O,
|
|
77
|
+
constraints: Record<K, K>,
|
|
78
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
79
|
+
): Pick<O, K>
|
|
80
|
+
/**
|
|
81
|
+
* @param input Source object.
|
|
82
|
+
* @param keys Unconstrained iterable of keys to pick, such as in array or Map.
|
|
83
|
+
*
|
|
84
|
+
* @returns A subset of the source object with only properties present in `keys`.
|
|
85
|
+
*/
|
|
86
|
+
export function pick<O extends object, K extends string>(
|
|
87
|
+
input: O,
|
|
88
|
+
keys: Iterable<K>,
|
|
89
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
90
|
+
): Pick<O, StringKeyOf<O>>
|
|
91
|
+
/**
|
|
92
|
+
* @param input Source object.
|
|
93
|
+
* @param constraints Unconstrained iterable of keys to pick, such as in array or Map.
|
|
94
|
+
*
|
|
95
|
+
* @returns A subset of the source object with only properties present in `constraints`.
|
|
96
|
+
*/
|
|
97
|
+
export function pick<O extends object, K extends keyof O = StringKeyOf<O>>(
|
|
98
|
+
input: O,
|
|
99
|
+
constraints: Iterable<K> | Record<K, K> | SetLike<K>,
|
|
100
|
+
transform?: (value: O[keyof O], key: keyof O, input: O) => never
|
|
101
|
+
): Pick<O, K> {
|
|
102
|
+
const picked: Partial<Pick<O, keyof O>> = {}
|
|
103
|
+
|
|
104
|
+
if (!input) return picked as Pick<O, K>
|
|
105
|
+
|
|
106
|
+
if (!constraints) return input as Pick<O, K>
|
|
107
|
+
|
|
108
|
+
const keys = isIterable(constraints) ? Array.from(constraints) : Object.values(constraints)
|
|
109
|
+
|
|
110
|
+
for (const key of keys) {
|
|
111
|
+
const value = (input as never)[key]
|
|
112
|
+
picked[key as keyof O] = transform ? transform(value, key, input) : value
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return picked as Pick<O, K>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Type-predicate for checking if a value appears to be a record, i.e. an object that is not an array.
|
|
120
|
+
*
|
|
121
|
+
* @category Type Guard
|
|
122
|
+
* @category Object
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
export function isRecordLike(input: unknown): input is object {
|
|
126
|
+
return typeof input === "object" && input !== null && !Array.isArray(input)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Type-helper to remove nullability from an object's properties.
|
|
131
|
+
*
|
|
132
|
+
* @category Object
|
|
133
|
+
*/
|
|
134
|
+
export type NonNullableObject<T> = { [P in keyof T]-?: NonNullable<T[P]> } & NonNullable<T>
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Given an object, returns a new object with all nullable properties removed.
|
|
138
|
+
*
|
|
139
|
+
* This is useful for cleaning up objects before serializing them to JSON.
|
|
140
|
+
*
|
|
141
|
+
* @category Object
|
|
142
|
+
*/
|
|
143
|
+
export function omitNullable<T extends object>(input: T): NonNullableObject<T> {
|
|
144
|
+
const result: Record<string, unknown> = {}
|
|
145
|
+
|
|
146
|
+
for (const [key, currentValue] of Object.entries(input)) {
|
|
147
|
+
if (isRecordLike(currentValue)) {
|
|
148
|
+
const childResult = omitNullable(currentValue)
|
|
149
|
+
|
|
150
|
+
if (Object.keys(childResult).length > 0) {
|
|
151
|
+
result[key] = childResult
|
|
152
|
+
}
|
|
153
|
+
} else if (Array.isArray(currentValue)) {
|
|
154
|
+
const arr = currentValue
|
|
155
|
+
.map((entryValue) => (isRecordLike(entryValue) ? omitNullable(entryValue) : entryValue))
|
|
156
|
+
.filter((val) => val != null)
|
|
157
|
+
|
|
158
|
+
if (arr.length > 0) {
|
|
159
|
+
result[key] = arr
|
|
160
|
+
}
|
|
161
|
+
} else if (currentValue !== null) {
|
|
162
|
+
result[key] = currentValue
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return result as NonNullableObject<T>
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Given serialized JSON, attempt to parse it.
|
|
171
|
+
*/
|
|
172
|
+
export function tryParsingJSON<T = unknown>(input: unknown, fallback?: undefined): T | undefined
|
|
173
|
+
export function tryParsingJSON<T = unknown>(input: unknown, fallback: null): T | null
|
|
174
|
+
export function tryParsingJSON<T = unknown, F = null>(input: unknown, fallback?: F): T | F {
|
|
175
|
+
if (typeof input !== "string") return (fallback ?? null) as F
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
return JSON.parse(input)
|
|
179
|
+
} catch {
|
|
180
|
+
return (fallback ?? null) as F
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export type FlattenObjectKeys<T extends JsonObject, Key = keyof T> = Key extends string
|
|
185
|
+
? T[Key] extends JsonObject
|
|
186
|
+
? `${Key}.${FlattenObjectKeys<T[Key]>}`
|
|
187
|
+
: `${Key}`
|
|
188
|
+
: never
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Flattens an object into a single-level object with dot-separated keys.
|
|
192
|
+
*/
|
|
193
|
+
export function flattenObject<T extends JsonObject>(
|
|
194
|
+
obj: T,
|
|
195
|
+
prefix: string[] = [],
|
|
196
|
+
current: Record<string, unknown> = {}
|
|
197
|
+
): Record<FlattenObjectKeys<T>, unknown> {
|
|
198
|
+
prefix = prefix || []
|
|
199
|
+
current = current || {}
|
|
200
|
+
|
|
201
|
+
// Remember kids, null is also an object!
|
|
202
|
+
if (typeof obj === "object" && obj !== null) {
|
|
203
|
+
Object.keys(obj).forEach((key) => {
|
|
204
|
+
;(flattenObject as typeof flattenObject)(obj[key] as JsonObject, prefix.concat(key), current)
|
|
205
|
+
})
|
|
206
|
+
} else {
|
|
207
|
+
current[prefix.join(".")] = obj
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return current
|
|
211
|
+
}
|