Skip to content

Instantly share code, notes, and snippets.

View kanian's full-sized avatar

Patrick Adou kanian

  • Abidjan, Ivory Coast
View GitHub Profile
setProps<T extends BaseObject>(values: PartialOfProperties<T>): void {
for (let prop in values) {
this[prop] =
values[prop] !== undefined ? values[prop] : this[prop];
}
}
@kanian
kanian / PartialOfProperties.ts
Created August 24, 2020 22:02
PartialOfProperties
import { NonFunctionKeys } from "utility-types"
export type PartialOfProperties<T extends object> = Partial<{ [P in NonFunctionKeys<T>]: T[P] }>
@kanian
kanian / OnlyProperties.ts
Created August 24, 2020 21:49
Extract get the non function keys of an instance
import { NonFunctionKeys } from "utility-types"
export type OnlyProperties<T extends object> = { [P in NonFunctionKeys<T>]: T[P] }
@kanian
kanian / setProps-generic.ts
Created August 24, 2020 20:15
What setProps should look like
setProps<T extends BaseObject>(values: T): void {
//...
}
@kanian
kanian / allSchemaDecorators.ts
Created February 6, 2020 15:32
All the schema decorators on a class
@schema(personSchema, true)
class Person {
@propertySchema(personAgeSchema)
age: number
@propertySchema(personNameSchema)
name: string
constructor(
@paramSchema(personNameSchema) name: string,
@paramSchema(personAgeSchema) age: number
) {
@kanian
kanian / propertySchemaValidation.ts
Created February 6, 2020 15:27
Property Schema Decorator Validation Example
class Person {
@propertySchema(schemas.personAgeSchema)
age: number
@propertySchema(schemas.personNameSchema)
name: string
constructor(name: string, age: number) {
this.age = age
this.name = name
}
}
@kanian
kanian / propertySchema.ts
Created February 6, 2020 15:22
Property Schema Decorator
function propertySchema(schema: Schema) {
return function(target: Object, key: string | symbol): void {
// The property
let val = target[key];
let propertyName = String(key);
// Generate getter and setter
const getter = () => {
return val;
};
@kanian
kanian / paramDecoratorValidation.ts
Last active February 6, 2020 14:50
paramSchema decorator validation example
@schema(personSchema,/*validateParams:*/ true)
class Person {
age: number
name: string
constructor(
@paramSchema(personNameSchema) name: string,
@paramSchema(personAgeSchema) age: number
) {
;(this.age = age), (this.name = name)
}
@kanian
kanian / validateConstructorParams.ts
Created February 6, 2020 14:43
Retrieves existing constructor parameters metadata and validates the given argument against their respective parameter schema
function validateConstructorParams(target: any, args: any[]) {
// Retrieve all constructor parameters metadata
let existingConstrainedParameters: ConstrainedParameterMap = Reflect.getOwnMetadata(
parameterSchemaMetadataKey,
target,
"constructor"
);
// For each retrieved metadata,...
if (existingConstrainedParameters) {
const parameterIndexes = Object.keys(existingConstrainedParameters);
@kanian
kanian / schema_with_parameter_decorators.ts
Created February 6, 2020 14:37
When parameter validation is enabled, validate the parameters
function schema(schema: Schema, validateParams: boolean = false) {
return function validateArgs(target: any) {
// save a reference to the original constructor
var original = target;
// wrap orginal constructor with validation behaviour
var f: any = function(...args) {
// When parameter validation is enabled...
if (validateParams) {
validateConstructorParams(target, args); // validate the parameters
}