Skip to content

Commit

Permalink
Prefix the convert method with __ so it is less likely to have na…
Browse files Browse the repository at this point in the history
…ming collisions with object properties.
  • Loading branch information
Jarom Loveridge committed Apr 12, 2018
1 parent 109f38a commit 47daae2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
41 changes: 25 additions & 16 deletions __tests__/castable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Product extends Castable {

test('Convert a simple object', () => {
const serverResponse = `{
"name": "Milk",
"price": "200",
"tax": "10",
"date": "2017-10-20T06:28:08Z",
"name": "Milk",
"price": "200",
"tax": "10",
"date": "2017-10-20T06:28:08Z",
"onSale": "false"
}`;
const product = new Product(JSON.parse(serverResponse));
Expand All @@ -25,10 +25,10 @@ test('Convert a simple object', () => {

test('Convert a simple object with unknown field', () => {
const serverResponse = `{
"name": "Milk",
"price": "200",
"tax": "10",
"date": "2017-10-20T06:28:08Z",
"name": "Milk",
"price": "200",
"tax": "10",
"date": "2017-10-20T06:28:08Z",
"onSale": "false",
"unknown": 10
}`;
Expand All @@ -43,16 +43,16 @@ test('Convert a simple object with unknown field', () => {
test('Convert an object array', () => {
const serverResponse = `[
{
"name": "Milk",
"price": "200",
"tax": "10",
"name": "Milk",
"price": "200",
"tax": "10",
"date": "2017-10-20T06:28:08Z",
"onSale": "true"
},
{
"name": "Water",
"price": "50",
"tax": "5",
"name": "Water",
"price": "50",
"tax": "5",
"date": "2017-10-25T06:28:08Z",
"onSale": "false"
}
Expand Down Expand Up @@ -150,10 +150,10 @@ test('Convert 2D object array', () => {
@element(Array, Pair)
arr: Pair[][];
}
const s = `{
const s = `{
"arr": [
[ { "name": "abc", "n": "123" }, { "name": "def", "n": "999" } ],
[ { "name": "abc2", "n": "200" }, { "name": "def2", "n": "300" } ]
[ { "name": "abc2", "n": "200" }, { "name": "def2", "n": "300" } ]
]
}`;
const c = new C(JSON.parse(s));
Expand All @@ -168,3 +168,12 @@ test('Convert 2D object array', () => {
expect(typeof c.arr[1][0].n).toBe('number');
expect(c.arr[1][0].n).toBe(200);
});

test('should allow a property called `convert`', () => {
class C extends Castable {
@cast convert: string;
}
const s = '{"convert": "value"}';
const c = new C(JSON.parse(s));
expect(c.convert).toEqual('value');
});
7 changes: 4 additions & 3 deletions lib/castable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ export class Castable {
const designType = Reflect.getMetadata('design:type', this, propertyKey);
const customType = Reflect.getMetadata('custom:type', this, propertyKey);
const type = customType !== undefined ? customType : designType;
this[propertyKey] = this.convert(source[propertyKey], propertyKey, type, 0);
this[propertyKey] = this.__convert(source[propertyKey], propertyKey, type, 0);
});
}

private convert(source: any, propertyKey: string, type: any, depth: number) {
// Prefix with __ in an attempt to prevent naming colisions with property names.
private __convert(source: any, propertyKey: string, type: any, depth: number) {
if (type === undefined) {
return source;
}
Expand All @@ -42,7 +43,7 @@ export class Castable {
case 'Array':
const elementType = Reflect.getMetadata('custom:element-type' + depth, this, propertyKey) as Function;
const nextDepth = depth + 1;
return (source as any[]).map(el => this.convert(el, propertyKey, elementType, nextDepth));
return (source as any[]).map(el => this.__convert(el, propertyKey, elementType, nextDepth));
default:
return new type(source);
}
Expand Down

0 comments on commit 47daae2

Please sign in to comment.