Compare commits

..

No commits in common. "master" and "1.1.1" have entirely different histories.

5 changed files with 146 additions and 194 deletions

View file

@ -23,15 +23,7 @@ uncompressed.
In deno:
```typescript
import { Serializer } from "https://js.thunderk.net/serializer/mod.ts";
```
In browser:
```html
<script type="module">
import { Serializer } from "https://js.thunderk.net/serializer/mod.js";
</script>
import { Serializer } from "https://code.thunderk.net/typescript/serializer/raw/branch/master/mod.ts";
```
## Use

View file

@ -1,3 +1 @@
# TODO
- Add protocol version, to handle breaking changes

View file

@ -3,15 +3,7 @@
In deno:
```typescript
import { Serializer } from "https://js.thunderk.net/serializer/mod.ts";
```
In browser:
```html
<script type="module">
import { Serializer } from "https://js.thunderk.net/serializer/mod.js";
</script>
import { Serializer } from "https://code.thunderk.net/typescript/serializer/raw/branch/master/mod.ts";
```
## Use

View file

@ -1,9 +1,8 @@
import {
describe,
expect,
it,
mock,
} from "https://code.thunderk.net/typescript/devtools/raw/1.3.0/testing.ts";
} from "https://code.thunderk.net/typescript/devtools/raw/1.2.2/testing.ts";
import { Serializer } from "./serializer.ts";
class TestSerializerObj1 {
@ -63,7 +62,6 @@ function checkReversability(
return loaded;
}
describe(Serializer, () => {
it("serializes simple objects", () => {
var obj = {
"a": 5,
@ -99,11 +97,6 @@ describe(Serializer, () => {
expect(loaded).toBeInstanceOf(TestSerializerObj1);
});
it("serializes reference to class type", () => {
let loaded = checkReversability(TestSerializerObj1);
expect(loaded).toBe(TestSerializerObj1);
});
it("stores one version of the same object", () => {
var a = new TestSerializerObj1(8);
var b = new TestSerializerObj1(8);
@ -164,7 +157,7 @@ describe(Serializer, () => {
let data = serializer.serialize({ obj: new TestSerializerObj2() });
let loaded = serializer.unserialize(data);
let expected: any = new TestSerializerObj2();
let expected = <any> new TestSerializerObj2();
expected.a = undefined;
expected.b[0] = undefined;
expect(loaded).toEqual({ obj: expected });
@ -211,4 +204,3 @@ describe(Serializer, () => {
expect(serializer1.unserialize(dumped1)).toEqual(data);
expect(serializer2.unserialize(dumped2)).toEqual(data);
});
});

View file

@ -41,32 +41,18 @@ export class Serializer {
// Collect objects
var objects: Object[] = [];
var stats: any = {};
function add(value: any): void {
if (objects.indexOf(value) < 0) {
objects.push(value);
}
}
crawl(obj, (value) => {
if (isObject(value)) {
var vtype = classname(value);
if (vtype != "" && this.ignored.indexOf(vtype) < 0) {
stats[vtype] = (stats[vtype] || 0) + 1;
add(value);
if (objects.indexOf(value) < 0) {
objects.push(value);
}
return value;
} else {
return STOP_CRAWLING;
}
} else if (typeof value == "function") {
const found = Object.entries(this.namespace).find(([_, cons]) =>
cons === value
);
if (found && this.ignored.indexOf(found[0]) < 0) {
// Keep references to constructors in the namespace
add(value);
}
return STOP_CRAWLING;
} else {
return value;
}
@ -77,8 +63,7 @@ export class Serializer {
var fobjects = objects.map((value) => this.encodeObject(value));
return JSON.stringify(fobjects, (key, value) => {
if (
key != "$f" && (isObject(value) || typeof value == "function") &&
!value.hasOwnProperty("$c") &&
key != "$f" && isObject(value) && !value.hasOwnProperty("$c") &&
!value.hasOwnProperty("$i")
) {
return { $i: objects.indexOf(value) };
@ -147,10 +132,6 @@ export class Serializer {
$c: ctype,
$f: Array.from(obj),
};
} else if (typeof obj == "function") {
return {
$c: obj.name,
};
} else {
return {
$c: this.namespace_rev[ctype] || ctype,
@ -168,10 +149,8 @@ export class Serializer {
return new Set(objdata.$f);
} else if (ctype == "Map") {
return new Map(objdata.$f);
} else if (objdata.$f) {
return Object.assign(this.constructObject(ctype), objdata.$f);
} else {
return this.namespace[ctype];
return Object.assign(this.constructObject(ctype), objdata.$f);
}
}
}
@ -180,8 +159,7 @@ export class Serializer {
* Check if the argument is an instance of a class
*/
function isObject(value: any): boolean {
return typeof value == "object" && value instanceof Object &&
!Array.isArray(value);
return value instanceof Object && !Array.isArray(value);
}
/**
@ -196,7 +174,7 @@ function crawl(
callback: (item: any) => any,
replace = false,
memo: any[] = [],
): any {
) {
if (isObject(obj)) {
if (memo.indexOf(obj) >= 0) {
return obj;
@ -205,11 +183,11 @@ function crawl(
}
}
if (obj !== undefined && obj !== null) {
if (obj !== undefined && obj !== null && typeof obj != "function") {
const result = callback(obj);
if (result === STOP_CRAWLING) {
return undefined;
return;
}
if (Array.isArray(obj)) {
@ -266,5 +244,5 @@ function crawl(
* Get the class name of an object.
*/
function classname(obj: Object): string {
return obj.constructor.name;
return (<any> obj.constructor).name;
}