Skip to content

Commit 1c1c946

Browse files
committed
feat: add option to ignore field
1 parent e15f567 commit 1c1c946

5 files changed

Lines changed: 26 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-flat-serializer",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A typescript library to serialize/deserialize classes to/from string in a flat format. Supports inheritance, circular reference and more",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/flat-property.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type PropertyTransformer = (property: any) => any;
66
export interface TSFlatPropertyMetadata {
77
beforeStringify?: PropertyTransformer;
88
afterParse?: PropertyTransformer;
9+
ignore?: boolean
910
}
1011

1112
export type TSFlatPropertyOptions = TSFlatPropertyMetadata;
@@ -16,6 +17,7 @@ export const TSFlatProperty = (options?: TSFlatPropertyOptions): Function => {
1617
{
1718
beforeStringify: options?.beforeStringify,
1819
afterParse: options?.afterParse,
20+
ignore: options?.ignore,
1921
},
2022
target,
2123
key,

src/flat-serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function convertArrayToOriginalCollectionType(value: CollectionType, currentColl
9494
/**
9595
* Get the flatPropertyMetadata searching on the object and on object's parent.
9696
*/
97-
function getFlatPropertyMetadata(obj: any, propertyKey: string): TSFlatPropertyMetadata {
97+
export function getFlatPropertyMetadata(obj: any, propertyKey: string): TSFlatPropertyMetadata {
9898
let propertyMetadata: TSFlatPropertyMetadata = null;
9999

100100
do {

src/rfdc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Probably on the future I'll move this file into a new repository.
55
*/
66
import { Type } from './helpers';
7+
import { getFlatPropertyMetadata } from "./flat-serializer";
78

89
function copyBuffer(cur) {
910
if (cur instanceof Buffer) {
@@ -60,7 +61,9 @@ export function rfdc(opts?: RFDCOptions) {
6061
refs.push(o);
6162
refsNew.push(o2);
6263
for (const k in o) {
63-
if (Object.hasOwnProperty.call(o, k) === false) continue;
64+
const propertyMetadata = getFlatPropertyMetadata(o, k);
65+
66+
if (Object.hasOwnProperty.call(o, k) === false || propertyMetadata?.ignore === true) continue;
6467
const cur = o[k];
6568
if (typeof cur !== 'object' || cur === null) {
6669
o2[k] = cur;

test/core.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ class AnyClassExample {
44
constructor(public str: string) {}
55
}
66

7+
class AnyClassExampleWithIgnore {
8+
@TSFlatProperty({ ignore: true })
9+
public ignoredField: string;
10+
11+
constructor(public str: string) {}
12+
}
13+
714
@TSFlatObject()
815
export class AnyClassExampleWithDecorator {
916
constructor(public str: string) {}
@@ -80,3 +87,14 @@ test('afterParse', () => {
8087

8188
expect(parsedRoot.str).toBe('after works');
8289
});
90+
91+
test('basic stringify/parse with ignore', () => {
92+
const root = new AnyClassExampleWithIgnore('adsdsa');
93+
root.ignoredField = 'nnn';
94+
95+
const str = stringify(root);
96+
const parsedRoot = parse<AnyClassExampleWithIgnore>(str);
97+
98+
expect(parsedRoot.str).toEqual(root.str);
99+
expect(parsedRoot.ignoredField).toBeUndefined()
100+
});

0 commit comments

Comments
 (0)