Skip to content

Commit

Permalink
rename values to palette
Browse files Browse the repository at this point in the history
  • Loading branch information
Leawind committed Jan 25, 2025
1 parent 0b01218 commit 53f1780
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 37 deletions.
16 changes: 8 additions & 8 deletions src/seriall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import type { Context, ContextLike } from '@/seriall/core/context.ts';
import type { Pure } from '@/seriall/core/pure.ts';
import { obj2pures, pures2obj } from '@/seriall/core.ts';
import { BUILTIN_ADAPTERS } from '@/seriall/builtin/adapters.ts';
import { BUILTIN_VALUES } from '@/seriall/builtin/values.ts';
import { BUILTIN_PALETTE } from '@/seriall/builtin/palette.ts';
import { BiMap } from '@/seriall/utils/bimap.ts';

function buildSeriallContext(options: ContextLike): Context {
return {
values: BiMap.fromRecord(options.values || {}),
palette: BiMap.fromRecord(options.palette || {}),
adapters: new Map(Object.entries(options.adapters || {})),
};
}

export type SeriallOptions = ContextLike & {
contexts?: ContextLike[];
builtinValues?: boolean;
builtinPalette?: boolean;
builtinAdapters?: boolean;
};
function parseSeriallOptions(options: SeriallOptions): Context[] {
const contexts: Context[] = [];

if (options.values || options.adapters) {
if (options.palette || options.adapters) {
contexts.push(buildSeriallContext({
values: options.values,
palette: options.palette,
adapters: options.adapters,
}));
}
Expand All @@ -33,11 +33,11 @@ function parseSeriallOptions(options: SeriallOptions): Context[] {
}
}

options.builtinValues = options.builtinValues !== false;
options.builtinPalette = options.builtinPalette !== false;
options.builtinAdapters = options.builtinAdapters !== false;
if (options.builtinValues || options.builtinAdapters) {
if (options.builtinPalette || options.builtinAdapters) {
contexts.push({
values: options.builtinValues ? BUILTIN_VALUES : new BiMap(),
palette: options.builtinPalette ? BUILTIN_PALETTE : new BiMap(),
adapters: options.builtinAdapters ? BUILTIN_ADAPTERS : new Map(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/seriall/builtin.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { BUILTIN_VALUES } from '@/seriall/builtin/values.ts';
export { BUILTIN_PALETTE } from '@/seriall/builtin/palette.ts';
export { BUILTIN_ADAPTERS } from '@/seriall/builtin/adapters.ts';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BiMap } from '@/seriall/utils/bimap.ts';
import type { ContextValues } from '@/seriall/core/context.ts';
import type { ContextPalette } from '@/seriall/core/context.ts';

export const BUILTIN_VALUES: ContextValues = (() => {
export const BUILTIN_PALETTE: ContextPalette = (() => {
const context = new BiMap<string, unknown>();

const globalProps = new Function('return this')();
Expand Down
8 changes: 4 additions & 4 deletions src/seriall/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BiMap } from '@/seriall/utils/bimap.ts';

export type ContextValues = BiMap<string, unknown>;
export type ContextPalette = BiMap<string, unknown>;

export type Adapter<T, P> = {
serialize(obj: T): P;
Expand All @@ -9,15 +9,15 @@ export type Adapter<T, P> = {
export type ContextAdapters = Map<string, Adapter<unknown, unknown>>;

export type Context = {
values: ContextValues;
palette: ContextPalette;
adapters: ContextAdapters;
};

export type ContextValuesLike = Record<string, unknown>;
export type ContextPaletteLike = Record<string, unknown>;

export type ContextAdaptersLike = Record<string, Adapter<unknown, unknown>>;

export type ContextLike = {
values?: ContextValuesLike;
palette?: ContextPaletteLike;
adapters?: ContextAdaptersLike;
};
8 changes: 4 additions & 4 deletions src/seriall/core/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export function serializeRecursively<T>(
// Search obj in context values
let ctxKey: string | null = null;
for (const context of contexts) {
if (context.values.hasValue(obj)) {
ctxKey = context.values.getKey(obj);
if (context.palette.hasValue(obj)) {
ctxKey = context.palette.getKey(obj);
break;
}
}
Expand Down Expand Up @@ -245,8 +245,8 @@ export function deserializeRecursively<T>(
case PureType.RefValue: {
// search the key in contexts
for (const context of contexts) {
if (context.values.hasKey(pure[PureKey.Key])) {
result = context.values
if (context.palette.hasKey(pure[PureKey.Key])) {
result = context.palette
.getValue(pure[PureKey.Key]);
found = true;
break;
Expand Down
4 changes: 2 additions & 2 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Deno.test('test-api', async (t) => {
class A {}
class B extends A {}
const context_ab: seriall.ContextLike = {
values: { A, B },
palette: { A, B },
};

const b = new B();
Expand All @@ -26,7 +26,7 @@ Deno.test('test-api', async (t) => {
deserialize: (id) => new C(id),
};
const context_c: seriall.ContextLike = {
values: { C },
palette: { C },
adapters: { CAdapter },
};

Expand Down
4 changes: 2 additions & 2 deletions test/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Deno.test(function testArray() {

Deno.test(function testRefValue() {
const mySymbol = Symbol('My Symbol');
const options: seriall.ContextLike = { values: { mySymbol } };
const options: seriall.ContextLike = { palette: { mySymbol } };
assertStrictEquals(
seriall.stringify(mySymbol, options),
seriall.stringify(seriall.deepClone(mySymbol, options), options),
Expand All @@ -73,7 +73,7 @@ Deno.test(function testPrototype() {
class A {}
class B extends A {}
class C extends B {}
const options: seriall.ContextLike = { values: { A, B, C } };
const options: seriall.ContextLike = { palette: { A, B, C } };

[
Function.prototype,
Expand Down
6 changes: 3 additions & 3 deletions test/seriall/builtin/adapters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import * as seriall from '@/mod.ts';

import { BUILTIN_ADAPTERS } from '@/seriall/builtin/adapters.ts';
import { BUILTIN_VALUES } from '@/seriall/builtin/values.ts';
import { BUILTIN_PALETTE } from '@/seriall/builtin/palette.ts';
import { looksLikeClass } from '@/seriall/utils.ts';

Deno.test(function find() {
Expand All @@ -20,7 +20,7 @@ Deno.test(function find() {
Promise,
]);
const classes: string[] = [];
for (const [name, object] of BUILTIN_VALUES.entries()) {
for (const [name, object] of BUILTIN_PALETTE.entries()) {
if (!IGNORED.has(object)) {
if (looksLikeClass(object) && !BUILTIN_ADAPTERS.has(name)) {
classes.push(name);
Expand Down Expand Up @@ -272,7 +272,7 @@ Deno.test(function testDataViewInObjectRefValue() {
tail: new DataView(buffer, 7, 4),
};

const options = { values: { buffer } };
const options: seriall.ContextPaletteLike = { palette: { buffer } };

const cloned = seriall.deepClone(original, options);

Expand Down
2 changes: 1 addition & 1 deletion test/seriall/core/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Deno.test(function testSeriallResolveFailedError() {
Deno.test(function testSeriallReferredValueNotFoundError() {
const hey = Symbol('Hey');

const json = seriall.stringify(hey, { values: { hey } });
const json = seriall.stringify(hey, { palette: { hey } });
assertThrows(
() => seriall.parse(json),
SeriallReferredValueNotFoundError,
Expand Down
10 changes: 5 additions & 5 deletions test/seriall/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ Deno.test(function testWithSupers() {
};

assertThrows(
() => seriall.purify(original, { values: { C } }),
() => seriall.purify(original, { palette: { C } }),
seriall.SeriallResolveFailedError,
);
seriall.purify(original, { values: { A, B, C } });
seriall.purify(original, { values: { ...withSupers(C) } });
seriall.purify(original, { values: withSupers(C) });
seriall.purify(original, { palette: { A, B, C } });
seriall.purify(original, { palette: { ...withSupers(C) } });
seriall.purify(original, { palette: withSupers(C) });

const cloned = seriall.deepClone(original, { values: withSupers(C) });
const cloned = seriall.deepClone(original, { palette: withSupers(C) });

assert(cloned.a instanceof A);
assert(cloned.b instanceof B);
Expand Down
10 changes: 5 additions & 5 deletions test/tutorial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Deno.test(function tutorial_Simple_Custom_Class() {
}
}

const options: seriall.ContextLike = { values: { Sheep } };
const options: seriall.ContextLike = { palette: { Sheep } };

const sheep = new Sheep();
const dolly = seriall.deepClone(sheep, options);
Expand All @@ -60,7 +60,7 @@ Deno.test(function tutorial_Simple_Custom_Class() {
});

Deno.test(function tutorial_Many_Types() {
const ctx: seriall.ContextLike = { values: { 'BiMap': seriall.BiMap } };
const ctx: seriall.ContextLike = { palette: { 'BiMap': seriall.BiMap } };

const object = {
simple: [10, 99n, 'str', false, null, undefined],
Expand Down Expand Up @@ -90,11 +90,11 @@ Deno.test(function tutorial_Custom_Class() {
);

// Now you told me it's name is Cat, I will remember it
const pure = seriall.purify(mimi, { values: { 'Cat': Cat } });
const pure = seriall.purify(mimi, { palette: { 'Cat': Cat } });
console.debug(`mimi: `, pure);

// I will be able to find `Cat` by name "Cat" when deserializing.
const clonedMimi = seriall.parsePures(pure, { values: { Cat } });
const clonedMimi = seriall.parsePures(pure, { palette: { Cat } });
assert(clonedMimi instanceof Cat);
});

Expand All @@ -115,7 +115,7 @@ Deno.test(function tutorial_Custom_Class() {
// `mini` has a own property `meow`, it is a function.
// This function is not provided in `values`, so it does not work.
assertThrows(
() => seriall.purify(mimi, { values: { Cat } }),
() => seriall.purify(mimi, { palette: { Cat } }),
seriall.SeriallResolveFailedError,
);
});

0 comments on commit 53f1780

Please sign in to comment.