UNPKG

rhine-var

Version:

Variables that support multi-user collaboration and persistence, making collaboration and variable operations as simple as possible, with strict and well-defined type hints.

88 lines (87 loc) 3.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isRhineVar = isRhineVar; exports.ensureRhineVar = ensureRhineVar; exports.ensureNative = ensureNative; exports.ensureNativeOrBasic = ensureNativeOrBasic; exports.isBasic = isBasic; exports.ensureJsonOrBasic = ensureJsonOrBasic; const rhine_proxy_1 = require("../proxy/rhine-proxy"); const data_utils_1 = require("./data.utils"); const native_utils_1 = require("./native.utils"); const rhine_var_array_class_1 = __importDefault(require("../var/items/rhine-var-array.class")); const rhine_var_map_class_1 = __importDefault(require("../var/items/rhine-var-map.class")); const rhine_var_object_class_1 = __importDefault(require("../var/items/rhine-var-object.class")); const rhine_var_text_class_1 = __importDefault(require("../var/items/rhine-var-text.class")); const rhine_var_xml_element_class_1 = __importDefault(require("../var/items/rhine-var-xml-element.class")); const rhine_var_xml_fragment_class_1 = __importDefault(require("../var/items/rhine-var-xml-fragment.class")); const rhine_var_xml_text_class_1 = __importDefault(require("../var/items/rhine-var-xml-text.class")); function isRhineVar(value) { return (value instanceof rhine_var_object_class_1.default || value instanceof rhine_var_map_class_1.default || value instanceof rhine_var_array_class_1.default || value instanceof rhine_var_text_class_1.default || value instanceof rhine_var_xml_fragment_class_1.default || value instanceof rhine_var_xml_element_class_1.default || value instanceof rhine_var_xml_text_class_1.default); } function ensureRhineVar(value, parent) { if ((0, native_utils_1.isNative)(value)) { return (0, rhine_proxy_1.rhineProxyGeneral)(value, parent); } if ((0, data_utils_1.isObjectOrArray)(value)) { if (!isRhineVar(value)) { return (0, rhine_proxy_1.rhineProxyGeneral)(ensureNative(value), parent); } } return value; } function ensureNative(value) { if ((0, native_utils_1.isNative)(value)) { return value; } if (isRhineVar(value)) { return value.getNative(); } if ((0, data_utils_1.isObjectOrArray)(value)) { return (0, native_utils_1.jsonToNative)(value); } return value; } function ensureNativeOrBasic(value) { if (isBasic(value)) { return value; } if ((0, native_utils_1.isNative)(value)) { return value; } if (isRhineVar(value)) { return value.getNative(); } if ((0, data_utils_1.isObjectOrArray)(value)) { return (0, native_utils_1.jsonToNative)(value); } return null; } function isBasic(value) { return (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null || value === undefined); } function ensureJsonOrBasic(value) { if (isBasic(value)) { return value; } if ((0, native_utils_1.isNative)(value)) { return value.toJSON(); } if (isRhineVar(value)) { return value.json(); } return value; }