@cdwr/core
Version:
A set of core utilities for the Codeware ecosystem.
115 lines (110 loc) • 3.56 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/core/src/zod.ts
var zod_exports = {};
__export(zod_exports, {
JsonSchema: () => JsonSchema,
withCamelCase: () => withCamelCase,
withReplaceAll: () => withReplaceAll
});
module.exports = __toCommonJS(zod_exports);
// packages/core/src/lib/zod/json.schema.ts
var import_zod = require("zod");
var LiteralSchema = import_zod.z.union([import_zod.z.string(), import_zod.z.number(), import_zod.z.boolean(), import_zod.z.null()]);
var JsonSchema = import_zod.z.lazy(
() => import_zod.z.union([LiteralSchema, import_zod.z.array(JsonSchema), import_zod.z.record(JsonSchema)])
);
// packages/core/src/lib/zod/with-camel-case.preprocess.ts
var import_zod2 = require("zod");
var toCamelCase = (str, specialCases = {}) => {
if (specialCases[str]) {
return specialCases[str];
}
if (str.includes("_")) {
return str.toLowerCase().replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
}
if (str === str.toUpperCase()) {
return str.toLowerCase();
}
return str.charAt(0).toLowerCase() + str.slice(1);
};
var transformKeys = (options) => {
const { currentPath, data, preserve, specialCases } = options;
if (Array.isArray(data)) {
return data.map(
(item) => transformKeys({
currentPath,
data: item,
preserve,
specialCases
})
);
}
if (data && typeof data === "object" && data !== null) {
return Object.entries(data).reduce((acc, [key, value]) => {
const shouldPreserveValue = preserve.some(
(path) => [...currentPath, key].join(".").match(new RegExp(`^${path}.`))
);
const newKey = shouldPreserveValue ? key : toCamelCase(key, specialCases);
const newPath = [...currentPath, newKey];
return {
...acc,
[newKey]: transformKeys({
currentPath: newPath,
data: value,
preserve,
specialCases
})
};
}, {});
}
return data;
};
var withCamelCase = (schema, options = {}) => {
const { preserve = [], specialCases = {} } = options;
return import_zod2.z.preprocess(
(data) => transformKeys({
currentPath: [],
data,
preserve,
specialCases
}),
schema
);
};
// packages/core/src/lib/zod/with-replace-all.preprocess.ts
var import_zod3 = require("zod");
var withReplaceAll = (schema, options) => {
const { match, value, strict = false } = options;
return import_zod3.z.preprocess((data) => {
if (typeof data !== "string") {
return data;
}
if (strict && !value) {
return data;
}
return data.replaceAll(match, value?.toString() ?? "");
}, schema);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
JsonSchema,
withCamelCase,
withReplaceAll
});