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

tserialize

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tserialize - npm Package Compare versions

Comparing version 1.3.0 to 1.3.1

src/decorators/JsonNameLate.ts

6

CHANGELOG.md

@@ -0,1 +1,7 @@

# 1.3.1
- Добавлен декоратор JsonNameLate, который работает как JsonName, но десериализацию проводит в два этапа
- сперва создается объект из всех полей кроме JsonNameLate,
а потом добавляются JsonNameLate, десериализатор которого принимает уже не сырые данные,
а объект
# 1.3.0

@@ -2,0 +8,0 @@ - Просто рефакторинг внутренней реализации

2

index.d.ts

@@ -27,2 +27,4 @@ // Generated by dts-bundle v0.7.2

export function JsonNameLate<T>(name?: string, serialize?: (value: T, instance: any) => any, deserialize?: (rawValue: any, rawData?: T) => T): Decorator;
/**

@@ -29,0 +31,0 @@ * @description Хэлпер для разбора данных, пришедших по JSONRPC от сервера в нашу модель

165

index.js

@@ -76,3 +76,3 @@ (function webpackUniversalModuleDefinition(root, factory) {

/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 11);
/******/ return __webpack_require__(__webpack_require__.s = 12);
/******/ })

@@ -101,3 +101,3 @@ /************************************************************************/

Object.defineProperty(exports, "__esModule", { value: true });
var deserialize_1 = __webpack_require__(10);
var deserialize_1 = __webpack_require__(11);
exports.deserialize = deserialize_1.deserialize;

@@ -114,3 +114,44 @@

var utils_1 = __webpack_require__(0);
var helpers_1 = __webpack_require__(12);
function JsonName(name, serialize, deserialize) {
return function (target, propertyKey) {
var metaStore = utils_1.MetaStore.getMetaStore(target);
var rawKey = name ? name : propertyKey;
metaStore.make(propertyKey).name(rawKey).serializator(serialize).deserializator(deserialize);
};
}
exports.JsonName = JsonName;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = __webpack_require__(0);
function serializeValue(metadata, value, instance) {
if (!metadata) {
return;
}
if (metadata.isStruct) {
var serializer = value ? value.toServer : null;
return serializer ? serializer.call(value) : (value ? serialize(value) : null);
}
else {
var serializer = metadata.serialize;
return serializer ? serializer(value, instance) : value;
}
}
function assignSerializedValueToResult(metadata, serializedValue, result) {
if (![null, undefined].includes(serializedValue)) {
var jsonName = metadata.rawKey;
if (jsonName !== utils_1.ParentKey) {
result[jsonName] = serializedValue;
}
else {
Object.assign(result, serializedValue);
}
}
}
/**

@@ -130,4 +171,4 @@ * @description Хэлпер для сериализации классов, имеющих поля с навешанным декоратором JsonName. Сериализует только те

var metadata = metaStore.getPropertyMeta(propertyKey);
var serializedValue = helpers_1.serializeValue(metadata, model[propertyKey], model);
helpers_1.assignSerializedValueToResult(metadata, serializedValue, result);
var serializedValue = serializeValue(metadata, model[propertyKey], model);
assignSerializedValueToResult(metadata, serializedValue, result);
}

@@ -140,20 +181,2 @@ return result;

/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = __webpack_require__(0);
function JsonName(name, serialize, deserialize) {
return function (target, propertyKey) {
var metaStore = utils_1.MetaStore.getMetaStore(target);
var rawKey = name ? name : propertyKey;
metaStore.make(propertyKey).name(rawKey).serializator(serialize).deserializator(deserialize);
};
}
exports.JsonName = JsonName;
/***/ }),
/* 4 */

@@ -201,8 +224,10 @@ /***/ (function(module, exports, __webpack_require__) {

exports.JsonMeta = JsonMeta_1.JsonMeta;
var JsonName_1 = __webpack_require__(3);
var JsonName_1 = __webpack_require__(2);
exports.JsonName = JsonName_1.JsonName;
var JsonNameReadonly_1 = __webpack_require__(9);
var JsonNameReadonly_1 = __webpack_require__(10);
exports.JsonNameReadonly = JsonNameReadonly_1.JsonNameReadonly;
var JsonStruct_1 = __webpack_require__(4);
exports.JsonStruct = JsonStruct_1.JsonStruct;
var JsonNameLate_1 = __webpack_require__(9);
exports.JsonNameLate = JsonNameLate_1.JsonNameLate;

@@ -217,3 +242,3 @@

Object.defineProperty(exports, "__esModule", { value: true });
var JsonName_1 = __webpack_require__(3);
var JsonName_1 = __webpack_require__(2);
var serialize_1 = __webpack_require__(13);

@@ -265,3 +290,21 @@ var deserialize_1 = __webpack_require__(1);

Object.defineProperty(exports, "__esModule", { value: true });
var JsonName_1 = __webpack_require__(3);
var utils_1 = __webpack_require__(0);
function JsonNameLate(name, serialize, deserialize) {
return function (target, propertyKey) {
var metaStore = utils_1.MetaStore.getMetaStore(target);
var rawKey = name ? name : propertyKey;
metaStore.make(propertyKey).name(rawKey).serializator(serialize).deserializator(deserialize).late();
};
}
exports.JsonNameLate = JsonNameLate;
/***/ }),
/* 10 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var JsonName_1 = __webpack_require__(2);
function JsonNameReadonly(name, deserialize) {

@@ -274,3 +317,3 @@ return JsonName_1.JsonName.call(null, name, function () { return null; }, deserialize);

/***/ }),
/* 10 */
/* 11 */
/***/ (function(module, exports, __webpack_require__) {

@@ -292,5 +335,10 @@

var metaStore = Reflect.getMetadata(utils_1.JsonNameMetadataKey, target);
var lateFields = [];
for (var _i = 0, _a = metaStore.getPropertyKeys(); _i < _a.length; _i++) {
var propertyKey = _a[_i];
var serializeProps = metaStore.getPropertyMeta(propertyKey);
if (serializeProps.isLate) {
lateFields.push(propertyKey);
continue;
}
if (serializeProps) {

@@ -301,6 +349,18 @@ var deserialize_1 = serializeProps.deserialize;

if (typeof jsonValue !== 'undefined') {
retVal[serializeProps.propertyKey] = deserialize_1 ? deserialize_1(jsonValue) : jsonValue;
retVal[serializeProps.propertyKey] = deserialize_1 ? deserialize_1(jsonValue, data) : jsonValue;
}
}
}
for (var _b = 0, lateFields_1 = lateFields; _b < lateFields_1.length; _b++) {
var propertyKey = lateFields_1[_b];
var serializeProps = metaStore.getPropertyMeta(propertyKey);
if (serializeProps) {
var deserialize_2 = serializeProps.deserialize;
var jsonName = serializeProps.rawKey;
var jsonValue = jsonName !== utils_1.ParentKey ? data[jsonName] : data;
if (typeof jsonValue !== 'undefined') {
retVal[serializeProps.propertyKey] = deserialize_2 ? deserialize_2(jsonValue, retVal) : jsonValue;
}
}
}
return retVal;

@@ -312,3 +372,3 @@ }

/***/ }),
/* 11 */
/* 12 */
/***/ (function(module, exports, __webpack_require__) {

@@ -322,6 +382,7 @@

exports.JsonName = decorators_1.JsonName;
exports.JsonNameLate = decorators_1.JsonNameLate;
exports.JsonNameReadonly = decorators_1.JsonNameReadonly;
exports.JsonStruct = decorators_1.JsonStruct;
exports.JsonMeta = decorators_1.JsonMeta;
var serialize_1 = __webpack_require__(2);
var serialize_1 = __webpack_require__(3);
exports.serialize = serialize_1.serialize;

@@ -333,39 +394,2 @@ var deserialize_1 = __webpack_require__(1);

/***/ }),
/* 12 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = __webpack_require__(0);
var serialize_1 = __webpack_require__(2);
function serializeValue(metadata, value, instance) {
if (!metadata) {
return;
}
if (metadata.isStruct) {
var serializer = value ? value.toServer : null;
return serializer ? serializer.call(value) : (value ? serialize_1.serialize(value) : null);
}
else {
var serializer = metadata.serialize;
return serializer ? serializer(value, instance) : value;
}
}
exports.serializeValue = serializeValue;
function assignSerializedValueToResult(metadata, serializedValue, result) {
if (![null, undefined].includes(serializedValue)) {
var jsonName = metadata.rawKey;
if (jsonName !== utils_1.ParentKey) {
result[jsonName] = serializedValue;
}
else {
Object.assign(result, serializedValue);
}
}
}
exports.assignSerializedValueToResult = assignSerializedValueToResult;
/***/ }),
/* 13 */

@@ -377,3 +401,3 @@ /***/ (function(module, exports, __webpack_require__) {

Object.defineProperty(exports, "__esModule", { value: true });
var serialize_1 = __webpack_require__(2);
var serialize_1 = __webpack_require__(3);
exports.serialize = serialize_1.serialize;

@@ -438,2 +462,5 @@

MetaStore.prototype.name = function (rawKey) {
if (!rawKey) {
return this;
}
this.currentMetadata.rawKey = rawKey;

@@ -440,0 +467,0 @@ delete this.keyPropertyInversion[this.currentPropertyKey];

{
"name": "tserialize",
"version": "1.3.0",
"version": "1.3.1",
"description": "TypeScript serialize utils",

@@ -5,0 +5,0 @@ "types": "index.d.ts",

@@ -6,1 +6,2 @@ export { JsonArray } from './JsonArray';

export { JsonStruct } from './JsonStruct';
export { JsonNameLate } from './JsonNameLate';

@@ -13,5 +13,10 @@ import { JsonNameMetadataKey, ParentKey, MetaStore } from 'utils';

const metaStore: MetaStore = (Reflect as any).getMetadata(JsonNameMetadataKey, target);
const lateFields: Array<string> = [];
for (const propertyKey of metaStore.getPropertyKeys()) {
const serializeProps = metaStore.getPropertyMeta(propertyKey);
if (serializeProps.isLate) {
lateFields.push(propertyKey);
continue;
}
if (serializeProps) {

@@ -22,3 +27,3 @@ const deserialize = serializeProps.deserialize;

if (typeof jsonValue !== 'undefined') {
retVal[serializeProps.propertyKey] = deserialize ? deserialize(jsonValue) : jsonValue;
retVal[serializeProps.propertyKey] = deserialize ? deserialize(jsonValue, data) : jsonValue;
}

@@ -28,3 +33,16 @@ }

// TODO remove duplicate
for (const propertyKey of lateFields) {
const serializeProps = metaStore.getPropertyMeta(propertyKey);
if (serializeProps) {
const deserialize = serializeProps.deserialize;
const jsonName = serializeProps.rawKey;
const jsonValue = jsonName !== ParentKey ? data[jsonName] : data;
if (typeof jsonValue !== 'undefined') {
retVal[serializeProps.propertyKey] = deserialize ? deserialize(jsonValue, retVal) : jsonValue;
}
}
}
return retVal;
}

@@ -1,3 +0,3 @@

export { JsonArray, JsonName, JsonNameReadonly, JsonStruct, JsonMeta } from './decorators';
export { JsonArray, JsonName, JsonNameLate, JsonNameReadonly, JsonStruct, JsonMeta } from './decorators';
export { serialize } from './serialize/serialize';
export { deserialize } from './deserialize';

@@ -62,2 +62,5 @@ import { JsonNameMetadataKey } from './consts';

name(rawKey: string): MetaStore {
if (!rawKey) {
return this;
}
this.currentMetadata.rawKey = rawKey;

@@ -64,0 +67,0 @@ delete this.keyPropertyInversion[this.currentPropertyKey];

@@ -12,5 +12,10 @@ import { expect } from 'chai';

class DeserializeWithRawData {
@JsonName('field', value => value, (rawValue, rawData) => `${rawValue}${rawData.related}${rawValue}`)
field: string;
}
describe('Custom deserializer case', () => {
const referenceValue = 'hello';
const data = {fieldToSerialize: referenceValue};
const data = { fieldToSerialize: referenceValue};
const instance = deserialize(data, BaseDeserializerCase);

@@ -30,1 +35,11 @@

});
describe('Deserializer must receive raw data', () => {
const referenceValue = 'hello';
const data = { field: referenceValue, related: '!' };
const instance = deserialize(data, DeserializeWithRawData);
it('must receive raw data', () => {
expect(instance).to.be.eql({field: `${referenceValue}!${referenceValue}`});
});
});
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