提交 c7e4bf0f 编写于 作者: 雪洛's avatar 雪洛

fix: UTSJSONObject使用下标进行赋值不符合预期的Bug、vue proxy包裹后访问UTSJSONObject方法报错的Bug

上级 d7b42f1c
declare function arrayAt<T>(array: T[], index: number): T | null;
declare function arrayFind<T>(array: T[], predicate: any): T | null;
declare function arrayFindLast<T>(array: T[], predicate: any): T | null;
declare function arrayPop<T>(array: T[]): T | null;
declare function arrayShift<T>(array: T[]): T | null;
declare function isInstanceOf(value: any, type: Function): boolean; declare function isInstanceOf(value: any, type: Function): boolean;
declare const JSON_2: { declare function mapGet<K, V>(map: Map<K, V>, key: K): V | null;
parse: (text: string, reviver: (this: any, key: string, value: any) => any) => any | null;
parseArray(text: string): Array<any> | null; declare function stringCodePointAt(str: string, pos: number): number | null;
parseObject(text: string): UTSJSONObject | null;
stringify: (value: any) => string;
};
export { JSON_2 as JSON }
export declare const UTS: { export declare const UTS: {
arrayAt: typeof arrayAt;
arrayFind: typeof arrayFind;
arrayFindLast: typeof arrayFindLast;
arrayPop: typeof arrayPop;
arrayShift: typeof arrayShift;
isInstanceOf: typeof isInstanceOf; isInstanceOf: typeof isInstanceOf;
mapGet: typeof mapGet;
stringCodePointAt: typeof stringCodePointAt;
weakMapGet: typeof weakMapGet;
JSON: {
parse: (text: string, reviver: (this: any, key: string, value: any) => any) => any;
parseArray(text: string): any[] | null;
parseObject(text: string): UTSJSONObject | null;
stringify: (value: any) => string;
};
}; };
export declare class UTSJSONObject { declare class UTSJSONObject {
[key: string]: any; [key: string]: any;
private _content;
constructor(content?: Record<string, any>); constructor(content?: Record<string, any>);
toJSON(): Record<string, any>; private _resolveKeyPath;
private _getValue; private _getValue;
get(key: string): any | null; get(key: string): any | null;
set(key: string, value: any): void; set(key: string, value: any): void;
...@@ -30,6 +49,8 @@ export declare class UTSJSONObject { ...@@ -30,6 +49,8 @@ export declare class UTSJSONObject {
forEach(callback: (value: any, key: string) => void): void; forEach(callback: (value: any, key: string) => void): void;
} }
declare function weakMapGet<K extends symbol | object, V>(map: WeakMap<K, V>, key: K): V | null;
export * from "tslib"; export * from "tslib";
......
function initUTSJSONObjectProperties(obj) { function initUTSJSONObjectProperties(obj) {
const propertyDescriptor = { const propertyDescriptor = {
enumerable: false, enumerable: false,
configurable: false,
writable: false,
}; };
const propertyList = [ const propertyList = [
'_content', '_resolveKeyPath',
'_getValue', '_getValue',
'toJSON', 'toJSON',
'get', 'get',
...@@ -28,36 +26,83 @@ function initUTSJSONObjectProperties(obj) { ...@@ -28,36 +26,83 @@ function initUTSJSONObjectProperties(obj) {
} }
class UTSJSONObject { class UTSJSONObject {
constructor(content = {}) { constructor(content = {}) {
this._content = content;
Object.assign(this, content); Object.assign(this, content);
initUTSJSONObjectProperties(this); initUTSJSONObjectProperties(this);
} }
toJSON() { _resolveKeyPath(keyPath) {
return this._content; // 非法keyPath不抛出错误,直接返回空数组
} let token = '';
_getValue(keyPathOrKey) { const keyPathArr = [];
if (keyPathOrKey.includes('.')) { let inOpenParentheses = false;
let keys = keyPathOrKey.split('.'); for (let i = 0; i < keyPath.length; i++) {
let value = this._content; const word = keyPath[i];
for (let key of keys) { switch (word) {
if (value instanceof Object) { case '.':
value = value[key]; if (token.length > 0) {
keyPathArr.push(token);
token = '';
}
break;
case '[': {
inOpenParentheses = true;
if (token.length > 0) {
keyPathArr.push(token);
token = '';
}
else {
return [];
}
break;
} }
else { case ']':
return null; if (inOpenParentheses) {
if (token.length > 0) {
keyPathArr.push(token);
token = '';
}
else {
return [];
}
inOpenParentheses = false;
}
else {
return [];
}
break;
default:
token += word;
break;
}
if (i === keyPath.length - 1) {
if (token.length > 0) {
keyPathArr.push(token);
token = '';
} }
} }
return value;
} }
else { return keyPathArr;
return this._content[keyPathOrKey]; }
_getValue(keyPath) {
const keyPathArr = this._resolveKeyPath(keyPath);
if (keyPathArr.length === 0) {
return null;
}
let value = this;
for (let key of keyPathArr) {
if (value instanceof Object) {
value = value[key];
}
else {
return null;
}
} }
return value;
} }
get(key) { get(key) {
return this._getValue(key); return this._getValue(key);
} }
set(key, value) { set(key, value) {
this._content[key] = value; this[key] = value;
} }
getAny(key) { getAny(key) {
return this._getValue(key); return this._getValue(key);
...@@ -109,63 +154,53 @@ class UTSJSONObject { ...@@ -109,63 +154,53 @@ class UTSJSONObject {
} }
toMap() { toMap() {
let map = new Map(); let map = new Map();
for (let key in this._content) { for (let key in this) {
map.set(key, this._content[key]); map.set(key, this[key]);
} }
return map; return map;
} }
forEach(callback) { forEach(callback) {
for (let key in this._content) { for (let key in this) {
callback(this._content[key], key); callback(this[key], key);
} }
} }
} }
const OriginalJSON = JSON; // @ts-ignore
const UTSJSON = { globalThis.UTSJSONObject = UTSJSONObject;
parse: (text, reviver) => {
try { function arrayPop(array) {
const parseResult = OriginalJSON.parse(text, reviver); if (array.length === 0) {
if (Array.isArray(parseResult)) { return null;
return parseResult; }
} return array.pop();
return new UTSJSONObject(parseResult); }
} function arrayShift(array) {
catch (error) { if (array.length === 0) {
console.error(error); return null;
return null; }
} return array.shift();
}, }
parseArray(text) { function arrayFind(array, predicate) {
try { const index = array.findIndex(predicate);
const parseResult = OriginalJSON.parse(text); if (index < 0) {
if (Array.isArray(parseResult)) { return null;
return parseResult; }
} return array[index];
return null; }
} function arrayFindLast(array, predicate) {
catch (error) { const index = array.findLastIndex(predicate);
console.error(error); if (index < 0) {
return null; return null;
} }
}, return array[index];
parseObject(text) { }
try { function arrayAt(array, index) {
const parseResult = OriginalJSON.parse(text); if (index < 0 || index >= array.length) {
if (Array.isArray(parseResult)) { return null;
return null; }
} return array[index];
return new UTSJSONObject(parseResult); }
}
catch (error) {
console.error(error);
return null;
}
},
stringify: (value) => {
return OriginalJSON.stringify(value);
},
};
var IDENTIFIER; var IDENTIFIER;
(function (IDENTIFIER) { (function (IDENTIFIER) {
...@@ -262,8 +297,84 @@ function isInstanceOf(value, type) { ...@@ -262,8 +297,84 @@ function isInstanceOf(value, type) {
return isImplementationOf(proto, type); return isImplementationOf(proto, type);
} }
const OriginalJSON = JSON;
const UTSJSON = {
parse: (text, reviver) => {
try {
const parseResult = OriginalJSON.parse(text, reviver);
if (Array.isArray(parseResult)) {
return parseResult;
}
return new UTSJSONObject(parseResult);
}
catch (error) {
console.error(error);
return null;
}
},
parseArray(text) {
try {
const parseResult = OriginalJSON.parse(text);
if (Array.isArray(parseResult)) {
return parseResult;
}
return null;
}
catch (error) {
console.error(error);
return null;
}
},
parseObject(text) {
try {
const parseResult = OriginalJSON.parse(text);
if (Array.isArray(parseResult)) {
return null;
}
return new UTSJSONObject(parseResult);
}
catch (error) {
console.error(error);
return null;
}
},
stringify: (value) => {
return OriginalJSON.stringify(value);
},
};
function mapGet(map, key) {
if (!map.has(key)) {
return null;
}
return map.get(key);
}
function stringCodePointAt(str, pos) {
if (pos < 0 || pos >= str.length) {
return null;
}
return str.codePointAt(pos);
}
function weakMapGet(map, key) {
if (!map.has(key)) {
return null;
}
return map.get(key);
}
const UTS = { const UTS = {
arrayAt,
arrayFind,
arrayFindLast,
arrayPop,
arrayShift,
isInstanceOf, isInstanceOf,
mapGet,
stringCodePointAt,
weakMapGet,
JSON: UTSJSON,
}; };
/****************************************************************************** /******************************************************************************
...@@ -602,9 +713,4 @@ function __disposeResources(env) { ...@@ -602,9 +713,4 @@ function __disposeResources(env) {
return next(); return next();
} }
// @ts-ignore export { UTS, __addDisposableResource, __assign, __asyncDelegator, __asyncGenerator, __asyncValues, __await, __awaiter, __classPrivateFieldGet, __classPrivateFieldIn, __classPrivateFieldSet, __createBinding, __decorate, __disposeResources, __esDecorate, __exportStar, __extends, __generator, __importDefault, __importStar, __makeTemplateObject, __metadata, __param, __propKey, __read, __rest, __runInitializers, __setFunctionName, __spread, __spreadArray, __spreadArrays, __values };
globalThis.JSON = UTSJSON;
// @ts-ignore
globalThis.UTSJSONObject = UTSJSONObject;
export { UTSJSON as JSON, UTS, UTSJSONObject, __addDisposableResource, __assign, __asyncDelegator, __asyncGenerator, __asyncValues, __await, __awaiter, __classPrivateFieldGet, __classPrivateFieldIn, __classPrivateFieldSet, __createBinding, __decorate, __disposeResources, __esDecorate, __exportStar, __extends, __generator, __importDefault, __importStar, __makeTemplateObject, __metadata, __param, __propKey, __read, __rest, __runInitializers, __setFunctionName, __spread, __spreadArray, __spreadArrays, __values };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册