Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Compiled
dist
**/dist

# Logs
logs
Expand Down
11 changes: 10 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
src/
coverage/
examples/
examples/
spec/
.travis.yml
gulpfile.js
tsconfig.json
tslint.json
yarn.lock
dist/examples
dist/spec
.vscode/
12 changes: 12 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"msjsdiag.debugger-for-chrome",
"eg2.tslint",
"wix.vscode-import-cost"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"tslint.enable": true,
"tslint.autoFixOnSave": true,
"editor.formatOnSave": true
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ For example:
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
Expand Down
22 changes: 22 additions & 0 deletions examples/json/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Panther } from '../models/panther';

import { Gender } from '../models/gender';
import { Status } from '../models/status';
import { Snake } from '../models/snake';

export const data: any = {
'id': 15,
Expand Down Expand Up @@ -82,6 +83,17 @@ export const data: any = {
'isSpeckled': false,
'status': 'Dead and alive'
}
],
'snakes': [
{
'id': 1,
'name': 'Ka',
'birthdate': '2018-09-09T00:00:00.000Z',
'numberOfPaws': 0,
'gender': 1,
'isPoisonous': true,
'status': 'Alive'
}
]
};

Expand Down Expand Up @@ -144,6 +156,15 @@ schrodinger.color = data.Panthers[2].color;
schrodinger.isSpeckled = data.Panthers[2].isSpeckled;
schrodinger.status = Status.deadAndAlive;

const ka: Snake = new Snake();
ka.id = data.snakes[0].id;
ka.name = data.snakes[0].name;
ka.birthdate = new Date(data.snakes[0].birthdate);
ka.numberOfPaws = data.snakes[0].numberOfPaws;
ka.gender = Gender.male;
ka.isPoisonous = data.snakes[0].isPoisonous;
ka.status = Status.alive;

const zoo: Zoo = new Zoo();
zoo.id = data.id;
zoo.name = data.name;
Expand All @@ -152,5 +173,6 @@ zoo.city = data.city;
zoo.boss = boss;
zoo.employees = [boss, mikasa, red, fried];
zoo.panthers = [bagheera, jolene, schrodinger];
zoo.snakes = [ka];

export const deserializedData: Zoo = zoo;
1 change: 1 addition & 0 deletions examples/models/dummy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class Dummy { }
4 changes: 3 additions & 1 deletion examples/models/snake.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Serializable, JsonProperty } from './../../src';
import { Animal } from './animal';
import { JsonProperty, Serializable } from '../../src';

@Serializable('Animal')
export class Snake extends Animal {

@JsonProperty()
public isPoisonous: boolean;

public constructor() {
Expand Down
5 changes: 4 additions & 1 deletion examples/models/zoo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Serializable, JsonProperty } from './../../src';
import { Animal } from './animal';

import { Employee } from './employee';
import { Panther } from './panther';
import { Snake } from './snake';

@Serializable()
export class Zoo {
Expand All @@ -20,6 +21,8 @@ export class Zoo {
public name: string;
@JsonProperty({ name: 'Panthers', type: Panther })
public panthers: Array<Panther>;
@JsonProperty({ type: Snake })
public snakes: Array<Snake>;

public isOpen: boolean = true;

Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"build": "gulp build",
"test": "yarn build && mocha -r ts-node/register dist/spec/**/*.spec.js",
"cover": "yarn build && istanbul cover node_modules/mocha/bin/_mocha dist/spec/**/*.spec.js -- -R spec"
"cover": "yarn build && istanbul cover node_modules/mocha/bin/_mocha dist/spec/**/*.spec.js -- -R spec",
"lint": "./node_modules/tslint/bin/tslint -p tsconfig.json"
},
"repository": {
"type": "git",
Expand All @@ -31,19 +32,20 @@
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"@types/node": "^11.12.1",
"@types/node": "^11.13.4",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"del": "^4.0.0",
"del": "^4.1.0",
"gulp": "^4.0.0",
"gulp-tslint": "^8.1.4",
"gulp-typescript": "^5.0.1",
"istanbul": "^0.4.5",
"mocha": "^6.0.2",
"mocha": "^6.1.3",
"rewire": "^4.0.1",
"ts-node": "^8.0.3",
"tslint": "^5.14.0",
"typescript": "^3.3.4000"
"ts-node": "^8.1.0",
"tslint": "^5.15.0",
"typescript": "^3.4.3"
}
}
54 changes: 44 additions & 10 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { expect } from 'chai';
import 'reflect-metadata';
import * as rewire from 'rewire';

import { JsonProperty, Serializable, serialize, deserialize } from '../src/index';
import { serialize, deserialize } from '../src/index';

import { Employee } from '../examples/models/employee';
import { Dummy } from '../examples/models/dummy';
import { Panther } from '../examples/models/panther';
import { Snake } from '../examples/models/snake';
import { Zoo } from '../examples/models/zoo';

import { data, deserializedData } from '../examples/json/data';

import 'reflect-metadata';
import * as _ from 'lodash';
const rewire: any = require('rewire');
const tjs: any = rewire('../src/index');

describe('Serializable', () => {

it('should return false', () => {
const hasMetadata: boolean = Reflect.hasOwnMetadata('api:map:serializable', Snake);
const hasMetadata: boolean = Reflect.hasOwnMetadata('api:map:serializable', Dummy);
expect(hasMetadata).to.equal(false);
});

Expand All @@ -43,19 +42,54 @@ describe('serialize', () => {
it('should return 3', () => {
const result: any = serialize(deserializedData, false);
let count: number = 0;
_.forEach(result.Panthers, (panther: any) => {
result.Panthers.forEach((panther: any) => {
if (panther.hasOwnProperty('childrenIdentifiers')) {
count++;
}
});
expect(count).to.equal(3);
expect(count).to.equal(1);
});

it('empty zoo should return an empty object', () => {
const zoo: Zoo = new Zoo();
expect(serialize(zoo)).to.deep.equal({});
});

it('{} should return an empty object', () => {
expect(serialize({})).to.deep.equal({});
});

const zooWithUndefinedValue: Zoo = new Zoo();
zooWithUndefinedValue.id = 4;
zooWithUndefinedValue.name = undefined;

it('zooWithUndefinedValue should return an object with undefined value', () => {
expect(serialize(zooWithUndefinedValue, false)).to.deep.equal({ id: 4, name: undefined });
});

it('zooWithUndefinedValue should return an object without undefined value', () => {
expect(serialize(zooWithUndefinedValue)).to.deep.equal({ id: 4 });
});
});

describe('deserialize', () => {
it('should return true', () => {
expect(deserialize(data, Zoo)).to.deep.equal(deserializedData);
});

it('should return true even if there are fake data included', () => {
const alteredData: any = { ...data };
alteredData['fake'] = 'fake';
alteredData['Panthers'][0]['fake'] = 'fake';
expect(deserialize(alteredData, Zoo)).to.deep.equal(deserializedData);
});

it('should return an empty zoo (except for the isOpen property)', () => {
const badData: any = {
fake: 'fake'
};
expect(deserialize(badData, Zoo)).to.deep.equal({ isOpen: true });
});
});

describe('castSimpleData', () => {
Expand Down Expand Up @@ -121,7 +155,7 @@ describe('isSerializable', () => {
});

it('should return false', () => {
expect(isSerializable(Snake)).to.equal(false);
expect(isSerializable(Dummy)).to.equal(false);
});
});

Expand Down
Loading