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.

217 lines (216 loc) 6.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNative = isNative; exports.nativeSet = nativeSet; exports.nativeHas = nativeHas; exports.nativeOwnKeys = nativeOwnKeys; exports.nativeDelete = nativeDelete; exports.nativeGet = nativeGet; exports.jsonToNative = jsonToNative; exports.getKeyFromParent = getKeyFromParent; exports.getPathFromRoot = getPathFromRoot; const index_1 = require("../../index"); const data_utils_1 = require("./data.utils"); const rhine_var_base_class_1 = __importDefault(require("../var/rhine-var-base.class")); const logger_1 = require("../../utils/logger"); function isNative(value) { return value instanceof index_1.YMap || value instanceof index_1.YArray || value instanceof index_1.YText || value instanceof index_1.YXmlFragment || value instanceof index_1.YXmlElement || value instanceof index_1.YXmlText; } function nativeSet(target, key, value) { if (typeof key !== 'string') { return false; } if (value instanceof rhine_var_base_class_1.default) { value = value.native; } try { if (target instanceof index_1.YMap) { target.set(key, value); return true; } else if (target instanceof index_1.YArray) { const index = parseInt(key); if (!isNaN(index)) { if (target.length - 1 >= index) { target.delete(index, 1); } target.insert(index, [value]); return true; } } else { (0, logger_1.error)('Unsupported nativeSet for:', target); } } catch (e) { (0, logger_1.error)('nativeSet.error:', e); } return false; } function nativeHas(target, key) { if (typeof key !== 'string') { return false; } try { if (target instanceof index_1.YMap) { return target.has(key); } else if (target instanceof index_1.YArray) { const index = parseInt(key, 10); if (!isNaN(index)) { return index < target.length && index >= 0; } } } catch (e) { (0, logger_1.error)('nativeHas.error:', e); } return false; } function nativeOwnKeys(target) { let keys = []; if (target instanceof index_1.YMap) { target.forEach((value, key) => { keys.push(key); }); } else if (target instanceof index_1.YArray) { for (let i = 0; i < target.length; i++) { keys.push(String(i)); } } return keys; } function nativeDelete(target, key) { if (typeof key !== 'string') { return false; } try { if (target instanceof index_1.YMap) { if (target.has(key)) { target.delete(key); return true; } } if (target instanceof index_1.YArray) { const pn = parseInt(key); if (!isNaN(pn)) { if (target.length > pn && pn >= 0) { target.delete(pn, 1); } } } } catch (e) { (0, logger_1.error)('nativeDelete.error:', e); } return false; } function nativeGet(target, key) { if (typeof key !== 'string') { return undefined; } try { if (target instanceof index_1.YMap) { return target.get(key); } else if (target instanceof index_1.YArray) { const pn = parseInt(key); if (!isNaN(pn)) return target.get(pn); } } catch (e) { (0, logger_1.error)('nativeGet.error:', e); } return undefined; } function jsonToNative(data) { if (isNative(data)) { return data; } if ((0, data_utils_1.isMap)(data)) { let map = new index_1.YMap(); data.forEach((value, key) => { map.set(key, jsonToNative(value)); }); return map; } if ((0, data_utils_1.isObject)(data)) { let map = new index_1.YMap(); Object.entries(data).forEach(([key, value]) => { map.set(key, jsonToNative(value)); }); map.set('_class', 'RhineVarObject'); return map; } if ((0, data_utils_1.isArray)(data)) { let array = new index_1.YArray(); data.forEach((value, index) => { array.push([jsonToNative(value)]); }); return array; } return data; } function getKeyFromParent(target) { const parent = target.parent; if (!parent) { return undefined; } let result = undefined; if (parent instanceof index_1.YMap) { parent.forEach((value, key) => { if (value === target) { result = key; } }); } else if (parent instanceof index_1.YArray) { parent.forEach((value, key) => { if (value === target) { result = key; } }); } return result; } function getPathFromRoot(target) { const path = []; let current = target; let parent = current.parent; while (parent) { let flag = false; if (parent instanceof index_1.YMap) { parent.forEach((value, key) => { if (value === current) { path.unshift(key); flag = true; } }); } else if (parent instanceof index_1.YArray) { parent.forEach((value, key) => { if (value === current) { path.unshift(key); flag = true; } }); } if (!flag) { (0, logger_1.error)('Failed to get path from root'); break; } current = parent; parent = current.parent; } return path; }