Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@scaleforge/joser

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scaleforge/joser - npm Package Compare versions

Comparing version 0.2.0 to 0.3.1

dist/libs/from-pairs.d.ts

10

dist/index.d.ts

@@ -1,5 +0,5 @@

export type Serializer<TValue = unknown, TRaw = unknown> = {
export type Serializer<T = unknown, TSerialized = unknown> = {
type: Function;
serialize: (value: TValue) => TRaw;
deserialize: (raw: TRaw) => TValue;
serialize: (value: T) => TSerialized;
deserialize: (serialized: TSerialized) => T;
};

@@ -12,4 +12,4 @@ export type Options = {

constructor(opts?: Options);
serialize(value: unknown): unknown;
deserialize<T = unknown>(raw: unknown): T;
deserialize(obj: Record<string, unknown>): Record<string, unknown>;
serialize(obj: Record<string, unknown>): Record<string, unknown>;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Joser = void 0;
const from_pairs_1 = require("./libs/from-pairs");
const get_1 = require("./libs/get");
const set_1 = require("./libs/set");
const to_pairs_1 = require("./libs/to-pairs");
const BUILT_IN_SERIALIZERS = [

@@ -10,4 +14,4 @@ {

},
deserialize(raw) {
return new Date(raw);
deserialize(serialized) {
return new Date(serialized);
},

@@ -18,6 +22,6 @@ },

serialize(value) {
return value.toString('hex');
return value.toString('base64');
},
deserialize(raw) {
return Buffer.from(raw, 'hex');
deserialize(serialized) {
return Buffer.from(serialized, 'base64');
},

@@ -30,4 +34,4 @@ },

},
deserialize(raw) {
return new Set(raw);
deserialize(serialized) {
return new Set(serialized);
},

@@ -38,71 +42,55 @@ },

constructor(opts) {
this.serializers = [...BUILT_IN_SERIALIZERS, ...((opts === null || opts === void 0 ? void 0 : opts.serializers) || [])].reduce((acc, item) => {
var _a;
this.serializers = [...BUILT_IN_SERIALIZERS, ...((_a = opts === null || opts === void 0 ? void 0 : opts.serializers) !== null && _a !== void 0 ? _a : [])].reduce((acc, item) => {
return Object.assign(Object.assign({}, acc), { [item.type.name]: item });
}, {});
}
serialize(value) {
if (value === undefined) {
return undefined;
deserialize(obj) {
const __t = obj['__t'];
if (!__t) {
return obj;
}
if (value === null) {
return null;
}
const _type = typeof value;
if (_type === 'number' || _type === 'string' || _type === 'boolean') {
return value;
}
if (_type !== 'object') {
throw new Error(`unable to serialize ${value}`);
}
if (value instanceof Array) {
return value.map((v) => this.serialize(v), value);
}
const obj = {};
for (const key of Object.keys(value)) {
const type = Object.values(this.serializers).find(({ type }) => value[key] instanceof type);
if (type) {
Object.assign(obj, {
[key]: type.serialize(value[key]),
__t: Object.assign(Object.assign({}, (obj['__t'] || {})), { [key]: type.type.name }),
});
delete obj['__t'];
return (0, to_pairs_1.toPairs)(__t.i).reduce((accum, [key, value]) => {
const serializer = this.serializers[__t.t[value]];
return (0, set_1.set)(key, serializer.deserialize((0, get_1.get)(key, obj)), accum);
}, obj);
}
serialize(obj) {
const serializers = Object.values(this.serializers);
const t = [];
const i = [];
const o = [];
for (const [key, value] of (0, to_pairs_1.toPairs)(obj, [], serializers.map((item) => item.type))) {
const type = typeof value;
if (value === null ||
value instanceof Array ||
type === 'number' ||
type === 'string' ||
type === 'boolean') {
o.push([key, value]);
continue;
}
obj[key] = this.serialize(value[key]);
}
return obj;
}
deserialize(raw) {
if (raw === undefined) {
return undefined;
}
if (raw === null) {
return null;
}
const _type = typeof raw;
if (_type === 'number' || _type === 'string' || _type === 'boolean') {
return raw;
}
if (_type !== 'object') {
throw new Error(`unable to deserialize ${raw}`);
}
if (raw instanceof Array) {
return raw.map((v) => this.deserialize(v), raw);
}
if (raw instanceof Object) {
const data = {};
const __t = raw['__t'] || {};
Object.keys(raw)
.filter((key) => key !== '__t')
.map((key) => {
const type = Object.values(this.serializers).find(({ type }) => type.name === __t[key]);
if (type) {
data[key] = type.deserialize(raw[key]);
if (type === 'object') {
const serializer = serializers.find((item) => value instanceof item.type);
if (!serializer) {
o.push([key, value]);
continue;
}
else {
data[key] = this.deserialize(raw[key]);
if (!t.includes(serializer.type.name)) {
t.push(serializer.type.name);
}
});
return data;
i.push([key, t.indexOf(serializer.type.name)]);
o.push([key, serializer.serialize(value)]);
continue;
}
throw new Error(`cannot serialize: ${value}`);
}
throw new Error(`unable to deserialize ${raw}`);
if (t.length === 0) {
return (0, from_pairs_1.fromPairs)(o);
}
return Object.assign(Object.assign({}, (0, from_pairs_1.fromPairs)(o)), { __t: {
t,
i: (0, from_pairs_1.fromPairs)(i),
} });
}

@@ -109,0 +97,0 @@ }

{
"name": "@scaleforge/joser",
"version": "0.2.0",
"version": "0.3.1",
"description": "Javascript object serializer",

@@ -13,3 +13,3 @@ "main": "dist/index.js",

"build": "rimraf dist && tsc --project tsconfig.build.json",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm test && npm run lint && npm run build"
},

@@ -19,13 +19,14 @@ "author": "ScaleForge",

"devDependencies": {
"@types/jest": "^29.5.1",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^8.39.0",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"@types/jest": "~29.5.5",
"@typescript-eslint/eslint-plugin": "~6.7.4",
"@typescript-eslint/parser": "~6.7.4",
"eslint": "~8.51.0",
"jest": "~29.7.0",
"ts-jest": "~29.1.1",
"ts-node": "~10.9.1",
"typescript": "~5.2.2"
},
"keywords": [
"serialize"
"serialization",
"json"
],

@@ -32,0 +33,0 @@ "publishConfig": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc