Skip to content

Commit 9153eb4

Browse files
authored
fix: Issue with converting empty data to array (#75)
* fix: Issue with converting empty data to array * test: add case * chore: bump version --------- Co-authored-by: huang yao <5945154+kirakiray@users.noreply.github.com>
1 parent 6111b71 commit 9153eb4

File tree

11 files changed

+56
-33
lines changed

11 files changed

+56
-33
lines changed

dist/stanz.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! stanz - v8.2.2 https://github.com/ofajs/stanz (c) 2018-2025 YAO
1+
//! stanz - v8.2.3 https://github.com/ofajs/stanz (c) 2018-2025 YAO
22
(function (global, factory) {
33
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
44
typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -771,6 +771,9 @@
771771
_revoke: {
772772
value: revoke,
773773
},
774+
__init_is_array: {
775+
value: Array.isArray(data),
776+
},
774777
});
775778

776779
Object.keys(data).forEach((key) => {
@@ -836,6 +839,8 @@
836839
let isPureArray = true;
837840
let maxId = -1;
838841

842+
const initIsArray = this.__init_is_array;
843+
839844
Object.keys(this).forEach((k) => {
840845
let val = this[k];
841846

@@ -858,8 +863,12 @@
858863
if (isPureArray) {
859864
obj.length = maxId + 1;
860865
obj = Array.from(obj);
861-
}
862866

867+
if (!obj.length && !initIsArray) {
868+
// 初始化不是数组,就是对象
869+
obj = {};
870+
}
871+
}
863872
const xid = this.xid;
864873
defineProperties(obj, {
865874
xid: {

dist/stanz.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/stanz.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/stanz.min.mjs

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

dist/stanz.min.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/stanz.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! stanz - v8.2.2 https://github.com/ofajs/stanz (c) 2018-2025 YAO
1+
//! stanz - v8.2.3 https://github.com/ofajs/stanz (c) 2018-2025 YAO
22
// const error_origin = "http://127.0.0.1:5793/errors";
33
const error_origin = "https://ofajs.github.io/ofa-errors/errors";
44

@@ -765,6 +765,9 @@ function constructor(data, handler$1 = handler) {
765765
_revoke: {
766766
value: revoke,
767767
},
768+
__init_is_array: {
769+
value: Array.isArray(data),
770+
},
768771
});
769772

770773
Object.keys(data).forEach((key) => {
@@ -830,6 +833,8 @@ class Stanz extends Array {
830833
let isPureArray = true;
831834
let maxId = -1;
832835

836+
const initIsArray = this.__init_is_array;
837+
833838
Object.keys(this).forEach((k) => {
834839
let val = this[k];
835840

@@ -852,8 +857,12 @@ class Stanz extends Array {
852857
if (isPureArray) {
853858
obj.length = maxId + 1;
854859
obj = Array.from(obj);
855-
}
856860

861+
if (!obj.length && !initIsArray) {
862+
// 初始化不是数组,就是对象
863+
obj = {};
864+
}
865+
}
857866
const xid = this.xid;
858867
defineProperties(obj, {
859868
xid: {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stanz",
3-
"version": "8.2.2",
3+
"version": "8.2.3",
44
"description": "javascript library for listening to data changes of child objects",
55
"main": "dist/stanz.js",
66
"exports": {

src/main.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export function constructor(data, handler = stanzHandler) {
5454
_revoke: {
5555
value: revoke,
5656
},
57+
__init_is_array: {
58+
value: Array.isArray(data),
59+
},
5760
});
5861

5962
Object.keys(data).forEach((key) => {
@@ -119,6 +122,8 @@ export default class Stanz extends Array {
119122
let isPureArray = true;
120123
let maxId = -1;
121124

125+
const initIsArray = this.__init_is_array;
126+
122127
Object.keys(this).forEach((k) => {
123128
let val = this[k];
124129

@@ -141,8 +146,12 @@ export default class Stanz extends Array {
141146
if (isPureArray) {
142147
obj.length = maxId + 1;
143148
obj = Array.from(obj);
144-
}
145149

150+
if (!obj.length && !initIsArray) {
151+
// 初始化不是数组,就是对象
152+
obj = {};
153+
}
154+
}
146155
const xid = this.xid;
147156
defineProperties(obj, {
148157
xid: {

test/methods.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ describe("Test the methods owned by the Stanz instance", () => {
5757
expect(d.a).toBe(201);
5858
});
5959
});
60+
61+
test("to JSON empty object", () => {
62+
const d = stanz({
63+
obj: {
64+
o1: {},
65+
o2: [],
66+
},
67+
});
68+
69+
expect(d.obj.o1.toJSON()).toEqual({});
70+
expect(d.obj.o2.toJSON()).toEqual([]);
71+
});

0 commit comments

Comments
 (0)