Skip to content

Commit

Permalink
Merge pull request #30 from cachho/feat/SerializeDeserialize
Browse files Browse the repository at this point in the history
feat: serialize deserialize
  • Loading branch information
cachho committed Jan 12, 2024
2 parents 15436f4 + 738bfaf commit 2fc4772
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib/__tests__/CnLink.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Marketplace } from '../../models';
import { CnLink } from '../../objects';

describe('CnLink', () => {
Expand Down Expand Up @@ -71,4 +72,24 @@ describe('CnLink', () => {
)
);
});

test('should be able to serialize', () => {
const href =
'https://www.superbuy.com/en/page/buy?from=search-input&url=https%3A%2F%2Fweidian.com%2Fitem.html%3FitemID%3D3053526244';
const link = new CnLink(href);
expect(link.serialize()).toEqual({
marketplace: 'weidian',
id: '3053526244',
});
});

test('should be able to deserialize', () => {
const serial = {
marketplace: 'weidian' as Marketplace,
id: '3053526244',
};
const cnlink = CnLink.deserialize(serial);
expect(cnlink.marketplace).toEqual(serial.marketplace);
expect(cnlink.id).toEqual(serial.id);
});
});
28 changes: 28 additions & 0 deletions src/objects/CnLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,32 @@ export class CnLink implements ICnLink {
ra
);
}

/**
* Serialize CnLink object
* @returns serialized CnLink object
*/
serialize(): { marketplace: Marketplace; id: string } {
return {
marketplace: this.marketplace,
id: this.id,
};
}

/**
* Create CnLinks instance from serial
* @param marketplace
* @param id
* @returns new CnLinks instance
*/
static deserialize({
marketplace,
id,
}: {
marketplace: Marketplace;
id: string;
}) {
const rawUrl = generateRawLink(marketplace, id);
return new CnLink(rawUrl);
}
}

0 comments on commit 2fc4772

Please sign in to comment.