-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added encoding functionality (#13)
* chore: add encoding functionality * chore: removed unused variables * refactor: updated decoder functions parameters * docs: updated readme file
- Loading branch information
Showing
14 changed files
with
530 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { ICodingPropertyType, IDictionary, IBaseCodable } from './internal'; | ||
|
||
// @ts-ignore | ||
export abstract class BaseCodable implements IBaseCodable { | ||
public static CodingProperties: IDictionary<ICodingPropertyType>; | ||
constructor(payload: object) {} | ||
public toJSON = () => ''; | ||
} |
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
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
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,83 @@ | ||
import { | ||
models, | ||
IType, | ||
ICodingPropertyType, | ||
isCodable, | ||
BaseCodable, | ||
get, | ||
errors, | ||
} from '../internal'; | ||
import { IModel, ICodable } from './types'; | ||
|
||
export const encode = <T extends BaseCodable>(codable: T): any => { | ||
if (!(codable instanceof BaseCodable)) { | ||
throw errors.invalidCodable(); | ||
} | ||
|
||
// @ts-ignore | ||
if (!codable.__proto__.constructor.CodingProperties) { | ||
throw errors.missingCodingProperties(); | ||
} | ||
|
||
// @ts-ignore | ||
const codingProperties = codable.__proto__.constructor.CodingProperties; | ||
|
||
// @ts-ignore | ||
return Object.keys(codingProperties) | ||
.map(key => | ||
// @ts-ignore | ||
encodeProperty({ | ||
key, | ||
codingProperty: codingProperties[key], | ||
codable, | ||
}) | ||
) | ||
.reduce((result, item) => { | ||
// @ts-ignore | ||
result[item.key] = item.value; | ||
return result; | ||
}, {}); | ||
}; | ||
|
||
const encodeProperty = ({ | ||
key, | ||
codingProperty, | ||
codable, | ||
}: { | ||
key: string; | ||
codingProperty: ICodingPropertyType; | ||
codable: BaseCodable; | ||
}) => { | ||
const jsonKey = get(codingProperty, 'key', key); | ||
const type: IType | ICodable = get(codingProperty, 'type', codingProperty); | ||
|
||
return encodeValue({ | ||
key: jsonKey, | ||
type, | ||
// @ts-ignore | ||
value: codable[key], | ||
}); | ||
}; | ||
|
||
const encodeValue = ({ | ||
key, | ||
type, | ||
value, | ||
}: { | ||
key: string; | ||
type: IType | ICodable; | ||
value?: object; | ||
}) => { | ||
if (isCodable(type)) { | ||
return { | ||
key, | ||
// @ts-ignore | ||
value: encode(value), | ||
}; | ||
} | ||
const model: IModel = models[type.name](type); | ||
return { | ||
key, | ||
value: model.encode(key, value), | ||
}; | ||
}; |
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
Oops, something went wrong.