UNPKG

@kubernetes-models/base

Version:
65 lines 1.78 kB
import { runValidateFunc, } from "@kubernetes-models/validate"; function setDefinedProps(src, dst) { const s = src; const d = dst; for (const key of Object.keys(s)) { if (s[key] !== undefined) { d[key] = filterUndefinedValues(s[key]); } } return dst; } /** * Returns true if the value is a plain object. * * Copied from: https://github.com/toss/es-toolkit/blob/0e8656da6afbe08952e78dcf2cd53d326d12c2f6/src/predicate/isPlainObject.ts#L43 */ function isPlainObject(value) { if (!value || typeof value !== "object") { return false; } const proto = Object.getPrototypeOf(value); const hasObjectPrototype = proto === null || proto === Object.prototype || // Required to support node:vm.runInNewContext({}) Object.getPrototypeOf(proto) === null; if (!hasObjectPrototype) { return false; } return Object.prototype.toString.call(value) === "[object Object]"; } function filterUndefinedValues(data) { if (Array.isArray(data)) { return data.map(filterUndefinedValues); } if (isPlainObject(data)) { return setDefinedProps(data, {}); } return data; } export class Model { constructor(data) { if (data) { setDefinedProps(data, this); } } setDefinedProps(data) { if (data) { setDefinedProps(data, this); } } toJSON() { const result = {}; setDefinedProps(this, result); return result; } validate() { // Use `setValidateFunc` to set the validate function } } export function setValidateFunc(ctor, fn) { ctor.prototype.validate = function () { runValidateFunc(fn, this); }; } //# sourceMappingURL=model.js.map