-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @Transform decorator to transform (e.g. parse) properties during …
…type cast
- Loading branch information
1 parent
2abeb67
commit 81b2244
Showing
7 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import autoTypeCast from './autoTypeCast'; | ||
import { classRegistry, registerClass } from './classRegistry'; | ||
import config, { defaultConfig } from './config'; | ||
import { Register, Transform } from './decorators'; | ||
|
||
export default autoTypeCast; | ||
export { | ||
classRegistry, | ||
registerClass, | ||
config, | ||
defaultConfig, | ||
Register, | ||
registerClass, | ||
Transform | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Store transformations for each class's properties | ||
// Format: { className: { propertyName: transformFn } } | ||
const transformRegistry = {}; | ||
|
||
export function registerTransform(target, propertyKey, transformFn) { | ||
const className = target.constructor.name; | ||
transformRegistry[className] = transformRegistry[className] || {}; | ||
transformRegistry[className][propertyKey] = transformFn; | ||
} | ||
|
||
export function getTransforms(className) { | ||
return transformRegistry[className] || {}; | ||
} | ||
|
||
export { transformRegistry }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import autoTypeCast from '../../../src/autoTypeCast'; | ||
import { classRegistry } from '../../../src/classRegistry'; | ||
import { Register, Transform } from '../../../src/decorators'; | ||
import { transformRegistry } from '../../../src/transformRegistry'; | ||
|
||
describe('Transform decorator', () => { | ||
beforeEach(() => { | ||
// Clear registries before each test | ||
Object.keys(classRegistry).forEach(key => delete classRegistry[key]); | ||
Object.keys(transformRegistry).forEach(key => delete transformRegistry[key]); | ||
}); | ||
|
||
it('transforms properties using the specified function', () => { | ||
@Register('Person') | ||
class Person { | ||
@Transform(dateStr => { | ||
const date = new Date(dateStr); | ||
// Ensure timezone doesn't affect the test | ||
date.setUTCHours(0, 0, 0, 0); | ||
return date; | ||
}) | ||
birthDate; | ||
|
||
@Transform(str => str.toUpperCase()) | ||
name; | ||
} | ||
|
||
const data = { | ||
__type: 'Person', | ||
birthDate: '1990-01-01', | ||
name: 'Homer Simpson', | ||
}; | ||
|
||
autoTypeCast(data); | ||
|
||
expect(data.birthDate instanceof Date).toBe(true); | ||
expect(data.birthDate.getUTCFullYear()).toBe(1990); | ||
expect(data.name).toBe('HOMER SIMPSON'); | ||
}); | ||
|
||
it('handles nested objects with transformations', () => { | ||
@Register('Food') | ||
class Food { | ||
@Transform(str => str.toLowerCase()) | ||
category; | ||
} | ||
|
||
@Register('Person') | ||
class Person { | ||
foods; | ||
} | ||
|
||
const data = { | ||
__type: 'Person', | ||
foods: [ | ||
{ __type: 'Food', category: 'DONUT', name: 'Jelly' }, | ||
{ __type: 'Food', category: 'PIZZA', name: 'Pepperoni' }, | ||
], | ||
}; | ||
|
||
autoTypeCast(data); | ||
|
||
expect(data.foods[0].category).toBe('donut'); | ||
expect(data.foods[1].category).toBe('pizza'); | ||
}); | ||
}); |