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.

528 lines (527 loc) 21.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../../../../index"); const support_base_1 = __importDefault(require("../support-base")); const var_utils_1 = require("../../../utils/var.utils"); const rhine_var_array_class_1 = __importDefault(require("../../items/rhine-var-array.class")); const native_utils_1 = require("../../../utils/native.utils"); class SupportArray extends support_base_1.default { static convertProperty(key, object) { if (!(object.native instanceof index_1.YArray) || !(object instanceof rhine_var_array_class_1.default)) { console.error('Unsupported convertProperty:', object, object.native); return; } const array = object; const native = object.native; const get = (i) => { if (i in array) { return Reflect.get(array, i); } else { return native.get(i); } }; const getJson = (i) => { let item = get(i); if ((0, native_utils_1.isNative)(item)) { return item.toJSON(); } if ((0, var_utils_1.isRhineVar)(item)) { return item.json(); } return item; }; switch (key) { case 'insert': return (index, ...items) => { native.insert(index, items.map(var_utils_1.ensureNativeOrBasic)); return native.length; }; case 'delete': return (index, length) => { native.delete(index, length); return native.length; }; case 'clear': return () => { native.delete(0, native.length); }; case 'length': return native.length; case 'push': return (...items) => { native.push(items.map(var_utils_1.ensureNativeOrBasic)); return native.length; }; case 'unshift': return (...items) => { native.unshift(items.map(var_utils_1.ensureNativeOrBasic)); return native.length; }; case 'pop': return () => { if (native.length === 0) return undefined; let key = native.length - 1; let item = get(key); native.delete(key); return item; }; case 'shift': return () => { if (native.length === 0) return undefined; let key = 0; let item = get(key); native.delete(key); return item; }; case 'slice': return (start, end) => { if (end === undefined) end = native.length; if (start < 0) start = native.length + start; if (end < 0) end = native.length + end; if (start < 0) start = 0; if (end > native.length) end = native.length; let result = []; for (let i = start; i < end; i++) { result.push(get(i)); } return result; }; case 'splice': return (start, deleteCount, ...items) => { const removed = []; for (let i = start; i < start + deleteCount; i++) { let item = getJson(i); removed.push(item); } native.delete(start, deleteCount); if (items.length > 0) { let nativeItems = []; for (let i = 0; i < items.length; i++) { nativeItems.push((0, var_utils_1.ensureNativeOrBasic)(items[i])); } native.insert(start, nativeItems); } return removed; }; case 'forEach': return (callback) => { return native.forEach((yValue, yIndex, yArray) => { callback(get(yIndex), yIndex, array); }); }; case 'map': return (callback) => { const result = []; for (let i = 0; i < native.length; i++) { result.push(callback(get(i), i, array)); } return result; }; case 'filter': return (callback) => { let result = []; for (let i = 0; i < native.length; i++) { let item = get(i); if (callback(item, i, array)) result.push(item); } return result; }; case 'indexOf': return (searchElement, fromIndex = 0) => { // fromIndex Optional // Zero-based index at which to start searching, converted to an integer. // // Negative index counts back from the end of the array — if -array.length <= fromIndex < 0, fromIndex + array.length is used. Note, the array is still searched from front to back in this case. // If fromIndex < -array.length or fromIndex is omitted, 0 is used, causing the entire array to be searched. // If fromIndex >= array.length, the array is not searched and -1 is returned. if (fromIndex < 0) fromIndex = native.length + fromIndex; if (fromIndex < 0) fromIndex = 0; if (fromIndex >= native.length || native.length === 0) return -1; const element = (0, var_utils_1.ensureJsonOrBasic)(searchElement); for (let i = fromIndex; i < native.length; i++) { if (getJson(i) === element) return i; } return -1; }; case 'lastIndexOf': return (searchElement, fromIndex = native.length - 1) => { // fromIndex Optional // Zero-based index at which to start searching backwards, converted to an integer. // // Negative index counts back from the end of the array — if -array.length <= fromIndex < 0, fromIndex + array.length is used. // If fromIndex < -array.length, the array is not searched and -1 is returned. You can think of it conceptually as starting at a nonexistent position before the beginning of the array and going backwards from there. There are no array elements on the way, so searchElement is never found. // If fromIndex >= array.length or fromIndex is omitted, array.length - 1 is used, causing the entire array to be searched. You can think of it conceptually as starting at a nonexistent position beyond the end of the array and going backwards from there. It eventually reaches the real end position of the array, at which point it starts searching backwards through the actual array elements. if (fromIndex < 0) fromIndex = native.length + fromIndex; if (fromIndex < 0) return -1; if (fromIndex >= native.length) fromIndex = native.length - 1; const element = (0, var_utils_1.ensureJsonOrBasic)(searchElement); for (let i = fromIndex; i >= 0; i--) { if (getJson(i) === element) return i; } return -1; }; case 'includes': return (searchElement, fromIndex) => { // fromIndex Optional // Zero-based index at which to start searching, converted to an integer. // // Negative index counts back from the end of the array — if -array.length <= fromIndex < 0, fromIndex + array.length is used. However, the array is still searched from front to back in this case. // If fromIndex < -array.length or fromIndex is omitted, 0 is used, causing the entire array to be searched. // If fromIndex >= array.length, the array is not searched and false is returned. if (fromIndex === undefined) fromIndex = 0; if (fromIndex < 0) fromIndex = native.length + fromIndex; if (fromIndex < 0) fromIndex = 0; if (fromIndex >= native.length) return false; const element = (0, var_utils_1.ensureJsonOrBasic)(searchElement); for (let i = fromIndex; i < native.length; i++) { if (getJson(i) === element) return true; } return false; }; case 'at': return (i) => { if (i < 0) i = native.length + i; if (i < 0 || i >= native.length) return undefined; return get(i); }; case 'with': return (i, value) => { if (i < 0) i = native.length + i; if (i < 0 || i >= native.length) throw 'RangeError: Unexpect index ' + i + ' in RhineVarArray(' + native.length + ')'; const arr = array.json(); arr[i] = (0, var_utils_1.ensureJsonOrBasic)(value); return arr; }; case 'join': return (str = ',') => { let result = ''; for (let i = 0; i < native.length; i++) { if (i > 0) result += str; let item = getJson(i); result += item; } return result; }; case 'some': return (callback, thisArg) => { for (let i = 0; i < native.length; i++) { if (callback(get(i), i, array)) return true; } return false; }; case 'every': return (callback, thisArg) => { for (let i = 0; i < native.length; i++) { if (!callback(get(i), i, array)) return false; } return true; }; case 'find': return (callback, thisArg) => { for (let i = 0; i < native.length; i++) { let item = get(i); if (callback(item, i, array)) return item; } return undefined; }; case 'findLast': return (callback, thisArg) => { for (let i = native.length - 1; i >= 0; i--) { let item = get(i); if (callback(item, i, array)) return item; } return undefined; }; case 'findIndex': return (callback, thisArg) => { for (let i = 0; i < native.length; i++) { if (callback(get(i), i, array)) return i; } return -1; }; case 'findLastIndex': return (callback, thisArg) => { for (let i = native.length - 1; i >= 0; i--) { if (callback(get(i), i, array)) return i; } return -1; }; case 'entries': return () => { let i = 0; return { next() { if (i < native.length) { return { value: [i, get(i++)], done: false }; } else { return { value: undefined, done: true }; } }, [Symbol.iterator]() { return this; } }; }; case 'keys': return () => { let i = 0; return { next() { if (i < native.length) { return { value: i++, done: false }; } else { return { value: undefined, done: true }; } }, [Symbol.iterator]() { return this; }, }; }; case 'values': return () => { let i = 0; return { next() { if (i < native.length) { return { value: get(i++), done: false }; } else { return { value: undefined, done: true }; } }, [Symbol.iterator]() { return this; }, }; }; case 'reverse': return () => { const items = object.json().reverse().map(var_utils_1.ensureNativeOrBasic); native.delete(0, native.length); native.insert(0, items); return array; }; case 'sort': return (compareFn) => { const items = object.json(); items.sort(compareFn); native.delete(0, native.length); native.insert(0, items.map(var_utils_1.ensureNativeOrBasic)); return array; }; case 'fill': return (value, start = 0, end = native.length) => { if (start < 0) start = native.length + start; if (end < 0) end = native.length + end; if (start < 0) start = 0; if (end > native.length) end = native.length; if (start >= end) return array; native.delete(start, end - start); let items = []; for (let i = start; i < end; i++) { items.push((0, var_utils_1.ensureNativeOrBasic)(value)); } native.insert(start, items); return array; }; case 'concat': return (...items) => { let result = array.json(); return result.concat(...items.map(var_utils_1.ensureNativeOrBasic)); }; case 'toReversed': return () => { return array.json().reverse(); }; case 'toSorted': return (compareFn) => { return array.json().sort(compareFn); }; case 'toSpliced': return (start, deleteCount = native.length - start, ...items) => { return array.json().splice(start, deleteCount, ...items); }; case 'copyWithin': return (target, start = 0, end = native.length) => { if (target < 0) target = native.length + target; if (start < 0) start = native.length + start; if (end < 0) end = native.length + end; if (target < 0) target = 0; if (target > native.length) target = native.length; if (start < 0) start = 0; if (end > native.length) end = native.length; if (start >= end) return array; const length = end - start; const items = []; for (let i = start; i < end; i++) { items.push(getJson(i)); } native.delete(target, length); native.insert(target, items); return array; }; case 'toString': return () => { return array.json().toString(); }; case 'toLocaleString': return () => { return array.json().toLocaleString(); }; case 'flat': return (depth = 1) => { return array.json().flat(depth); }; case 'flatMap': return (callback, thisArg) => { return array.json().flatMap(callback, thisArg); }; case 'reduce': return (callback, initialValue) => { return array.json().reduce(callback, initialValue); }; case 'reduceRight': return (callback, initialValue) => { return array.json().reduceRight(callback, initialValue); }; case Symbol.iterator: return () => { let i = 0; return { next() { if (i < native.length) { return { value: get(i++), done: false }; } else { return { value: undefined, done: true }; } }, [Symbol.iterator]() { return this; }, }; }; case Symbol.unscopables: return { copyWithin: true, entries: true, fill: true, find: true, findIndex: true, flat: true, flatMap: true, includes: true, keys: true, values: true, join: true, map: true, reverse: true, slice: true, some: true, splice: true, toLocaleString: true, toString: true, }; default: return undefined; } } } SupportArray.TARGET_TAG = 'RhineVarArray'; SupportArray.SUPPORTED_PROPERTIES = new Set([ Symbol.iterator, Symbol.unscopables, 'length', 'push', 'pop', 'unshift', 'shift', 'slice', 'splice', 'forEach', 'map', 'indexOf', 'lastIndexOf', 'includes', 'at', 'with', 'join', 'filter', 'some', 'every', 'find', 'findIndex', 'findLast', 'findLastIndex', 'entries', 'keys', 'values', 'toString', 'toLocaleString', 'fill', 'concat', 'copyWithin', 'reverse', 'sort', 'toReversed', 'toSorted', 'toSpliced', 'flat', 'flatMap', 'reduce', 'reduceRight', ]); SupportArray.UNSUPPORTED_PROPERTIES = new Set([]); exports.default = SupportArray;