Skip to content

Instantly share code, notes, and snippets.

@kendrelaxman
Last active February 9, 2024 21:29
Show Gist options
  • Save kendrelaxman/dbcd033f568892ef70ea36e3e1b9aac3 to your computer and use it in GitHub Desktop.
Save kendrelaxman/dbcd033f568892ef70ea36e3e1b9aac3 to your computer and use it in GitHub Desktop.
build dynamic schema
// Note: please restart the page if syntax highlighting works bad.
let el = document.querySelector('#header')
let msg: string = 'Hi friend, try edit me!'
el.innerHTML = msg
console.log('it shows results as you type')
class ExampleA{
private records ={
age: 18,
occupation: 'salaried',
job: 'jpmc'
};
private cachedRecods = null;
private schema = {
schemaName: 'Omon',
options:
{
topicName : 'sec',
Fields:[
{
getKey: 'age',
displayKey: 'myage',
},
{
getKey: 'occupation',
displayKey: 'myoccupation',
},
{
getKey: 'job',
displayKey: 'myhomejob',
isPrimary:true,
formatter:(el) =>{
return ['unique','age', 'job']
}
}
]
}
}
constructor(){
this.setRecords();
}
private setRecords(){
const rec = this.records;
let payload = {};
this.schema.options.Fields.map(element =>{
if(element.formatter){
const retValue = element.formatter(element);
console.log(retValue[1])
payload[retValue[0]] = `${rec[retValue[1]]}` + `${rec[retValue[2]]}`
}
else {
payload[element.displayKey] = rec[element.getKey]
}
})
console.log(payload);
}
}
const instance = new ExampleA();
@kendrelaxman
Copy link
Author

type DefaultObject = {
profileData: {
customDataKey: string;
AlgoDataKey: string;
};
Throttle: {
shrinkDataKey: string;
onOff: string;
};
startData: string;
};

type AssignKeys = {
shrinkDataKey?: string;
onOff?: string;
startData?: string;
customDataKey?: string;
AlgoDataKey?: string;
};

type LayoutObject = {
container: {
type: 'field';
editor: {
type: 'input';
dataKey: string;
}[];
}[];
};

class ObjectTransformer {
// ... (the ObjectTransformer class as defined earlier)

// Method to assign values and update layoutObject
assignAndNotify(key: keyof AssignKeys, value: string): void {
// Assign the value to the appropriate key in the transformer
switch (key) {
case 'shrinkDataKey':
this.assignShrinkDataKey(value);
break;
case 'onOff':
this.assignOnOff(value);
break;
case 'startData':
this.assignStartData(value);
break;
case 'customDataKey':
this.assignCustomDataKey(value);
break;
case 'AlgoDataKey':
this.assignAlgoDataKey(value);
break;
default:
// Handle unknown keys
break;
}

// Update the layoutObject with the new value
this.updateLayoutObject(key, value);

}

// Method to update layoutObject with the new value
private updateLayoutObject(key: keyof AssignKeys, value: string): void {
const layoutObject = this.layoutObject; // Assuming you have a way to access the layoutObject
const updatedLayoutObject = JSON.parse(JSON.stringify(layoutObject)); // Deep clone the layoutObject

updatedLayoutObject.container.forEach((container) => {
  container.editor.forEach((editor) => {
    if (editor.dataKey === key) {
      editor.dataKey = value;
    }
  });
});

// Assuming you have a way to update the layoutObject in your application
// For example, you can use React state or dispatch an action in Redux
// Update the layoutObject with the updated value
// this.setLayoutObject(updatedLayoutObject);

}
}

// Usage example:

const defaultObject = {
profileData: {
customDataKey: "",
AlgoDataKey: "",
},
Throttle: {
shrinkDataKey: "",
onOff: "",
},
startData: "",
};

const layoutObject: LayoutObject = {
container: [
{
type: 'field',
editor: [
{
type: 'input',
dataKey: 'AlgoDataKey',
},
{
type: 'input',
dataKey: 'shrinkDataKey',
},
],
},
],
};

// Instantiate ObjectTransformer
const transformer = new ObjectTransformer(defaultObject);

// Assign values and update layoutObject
transformer.assignAndNotify('AlgoDataKey', 'UpdatedAlgoDataKey');

/*************************************************************************************/

type DefaultObject = {
profileData: {
customDataKey: string;
AlgoDataKey: string;
};
Throttle: {
shrinkDataKey: string;
onOff: string;
};
startData: string;
};

type AssignKeys = {
shrinkDataKey?: string;
onOff?: string;
startData?: string;
customDataKey?: string;
AlgoDataKey?: string;
};

class ObjectTransformer {
private transformedObject: T;
private originalObject: T;

constructor(defaultObject: T) {
this.originalObject = { ...defaultObject };
this.transformedObject = { ...defaultObject };
}

initialize(assignKeys: AssignKeys): T {
this.reset();
Object.keys(assignKeys).forEach((key) => {
if (assignKeys[key] !== undefined) {
this.transformedObject[key] = assignKeys[key]!;
}
});
return { ...this.transformedObject };
}

reset(): T {
this.transformedObject = { ...this.originalObject };
return { ...this.transformedObject };
}

assignShrinkDataKey(value: string): T {
this.transformedObject.Throttle.shrinkDataKey = value;
return { ...this.transformedObject };
}

assignOnOff(value: string): T {
this.transformedObject.Throttle.onOff = value;
return { ...this.transformedObject };
}

assignStartData(value: string): T {
this.transformedObject.startData = value;
return { ...this.transformedObject };
}

assignCustomDataKey(value: string): T {
this.transformedObject.profileData.customDataKey = value;
return { ...this.transformedObject };
}

assignAlgoDataKey(value: string): T {
this.transformedObject.profileData.AlgoDataKey = value;
return { ...this.transformedObject };
}
}

// Usage example:

const defaultObject = {
profileData: {
customDataKey: "",
AlgoDataKey: "",
},
Throttle: {
shrinkDataKey: "",
onOff: "",
},
startData: "",
};

const transformer = new ObjectTransformer(defaultObject);

transformer.initialize({
customDataKey: "NewCustomDataKey",
AlgoDataKey: "NewAlgoDataKey",
startData: "NewStartData",
});

console.log(transformer.assignCustomDataKey("UpdatedCustomDataKey"));
console.log(transformer.reset());

/*******************************/

type DefaultObject = {
profileData: {
customDataKey: string;
AlgoDataKey: string;
};
Throttle: {
shrinkDataKey: string;
onOff: string;
};
startData: string;
};

type AssignKeys = {
shrinkDataKey?: string;
onOff?: string;
startData?: string;
customDataKey?: string;
AlgoDataKey?: string;
};

type LayoutObject = {
container: {
type: 'field';
editor: {
type: 'input';
dataKey: string;
}[];
}[];
};

class ObjectTransformer {
// ... (the ObjectTransformer class as defined earlier)

// Method to assign values and update layoutObject
assignAndNotify(key: keyof AssignKeys, value: string): void {
// Assign the value to the appropriate key in the transformer
switch (key) {
case 'shrinkDataKey':
this.assignShrinkDataKey(value);
break;
case 'onOff':
this.assignOnOff(value);
break;
case 'startData':
this.assignStartData(value);
break;
case 'customDataKey':
this.assignCustomDataKey(value);
break;
case 'AlgoDataKey':
this.assignAlgoDataKey(value);
break;
default:
// Handle unknown keys
break;
}

// Update the layoutObject with the new value
this.updateLayoutObject(key, value);

}

// Method to update layoutObject with the new value
private updateLayoutObject(key: keyof AssignKeys, value: string): void {
const layoutObject = this.layoutObject; // Assuming you have a way to access the layoutObject
const updatedLayoutObject = JSON.parse(JSON.stringify(layoutObject)); // Deep clone the layoutObject

updatedLayoutObject.container.forEach((container) => {
  container.editor.forEach((editor) => {
    if (editor.dataKey === key) {
      editor.dataKey = value;
    }
  });
});

// Assuming you have a way to update the layoutObject in your application
// For example, you can use React state or dispatch an action in Redux
// Update the layoutObject with the updated value
// this.setLayoutObject(updatedLayoutObject);

}
}

// Usage example:

const defaultObject = {
profileData: {
customDataKey: "",
AlgoDataKey: "",
},
Throttle: {
shrinkDataKey: "",
onOff: "",
},
startData: "",
};

const layoutObject: LayoutObject = {
container: [
{
type: 'field',
editor: [
{
type: 'input',
dataKey: 'AlgoDataKey',
},
{
type: 'input',
dataKey: 'shrinkDataKey',
},
],
},
],
};

// Instantiate ObjectTransformer
const transformer = new ObjectTransformer(defaultObject);

// Assign values and update layoutObject
transformer.assignAndNotify('AlgoDataKey', 'UpdatedAlgoDataKey');

/*************************************************************************************/

type DefaultObject = {
profileData: {
customDataKey: string;
AlgoDataKey: string;
};
Throttle: {
shrinkDataKey: string;
onOff: string;
};
startData: string;
};

type AssignKeys = {
shrinkDataKey?: string;
onOff?: string;
startData?: string;
customDataKey?: string;
AlgoDataKey?: string;
};

class ObjectTransformer {
private transformedObject: T;
private originalObject: T;

constructor(defaultObject: T) {
this.originalObject = { ...defaultObject };
this.transformedObject = { ...defaultObject };
}

initialize(assignKeys: AssignKeys): T {
this.reset();
Object.keys(assignKeys).forEach((key) => {
if (assignKeys[key] !== undefined) {
this.transformedObject[key] = assignKeys[key]!;
}
});
return { ...this.transformedObject };
}

reset(): T {
this.transformedObject = { ...this.originalObject };
return { ...this.transformedObject };
}

assignShrinkDataKey(value: string): T {
this.transformedObject.Throttle.shrinkDataKey = value;
return { ...this.transformedObject };
}

assignOnOff(value: string): T {
this.transformedObject.Throttle.onOff = value;
return { ...this.transformedObject };
}

assignStartData(value: string): T {
this.transformedObject.startData = value;
return { ...this.transformedObject };
}

assignCustomDataKey(value: string): T {
this.transformedObject.profileData.customDataKey = value;
return { ...this.transformedObject };
}

assignAlgoDataKey(value: string): T {
this.transformedObject.profileData.AlgoDataKey = value;
return { ...this.transformedObject };
}
}

// Usage example:

const defaultObject = {
profileData: {
customDataKey: "",
AlgoDataKey: "",
},
Throttle: {
shrinkDataKey: "",
onOff: "",
},
startData: "",
};

const transformer = new ObjectTransformer(defaultObject);

transformer.initialize({
customDataKey: "NewCustomDataKey",
AlgoDataKey: "NewAlgoDataKey",
startData: "NewStartData",
});

console.log(transformer.assignCustomDataKey("UpdatedCustomDataKey"));
console.log(transformer.reset());

/******************************************************/

import { Subject, Observable } from 'rxjs';

type DefaultObject = {
profileData: {
customDataKey: string;
AlgoDataKey: string;
};
Throttle: {
shrinkDataKey: string;
onOff: string;
};
startData: string;
};

type AssignKeys = {
shrinkDataKey?: string;
onOff?: string;
startData?: string;
customDataKey?: string;
AlgoDataKey?: string;
};

class ObjectTransformer {
private transformedObject: T;
private originalObject: T;
private subject: Subject<{ messageId: string, value: string }>;

constructor(defaultObject: T) {
this.originalObject = { ...defaultObject };
this.transformedObject = { ...defaultObject };
this.subject = new Subject<{ messageId: string, value: string }>();
}

initialize(assignKeys: AssignKeys): T {
this.reset();
Object.keys(assignKeys).forEach((key) => {
if (assignKeys[key] !== undefined) {
this.transformedObject[key] = assignKeys[key]!;
this.notifyChange(key, assignKeys[key]!);
}
});
return { ...this.transformedObject };
}

reset(): T {
this.transformedObject = { ...this.originalObject };
return { ...this.transformedObject };
}

assignShrinkDataKey(value: string): T {
this.transformedObject.Throttle.shrinkDataKey = value;
this.notifyChange('shrinkDataKey', value);
return { ...this.transformedObject };
}

assignOnOff(value: string): T {
this.transformedObject.Throttle.onOff = value;
this.notifyChange('onOff', value);
return { ...this.transformedObject };
}

assignStartData(value: string): T {
this.transformedObject.startData = value;
this.notifyChange('startData', value);
return { ...this.transformedObject };
}

assignCustomDataKey(value: string): T {
this.transformedObject.profileData.customDataKey = value;
this.notifyChange('customDataKey', value);
return { ...this.transformedObject };
}

assignAlgoDataKey(value: string): T {
this.transformedObject.profileData.AlgoDataKey = value;
this.notifyChange('AlgoDataKey', value);
return { ...this.transformedObject };
}

// Create an observable to listen for changes
onValueChange(messageId: string): Observable {
return this.subject.asObservable()
.filter(change => change.messageId === messageId)
.map(change => change.value);
}

private notifyChange(messageId: string, value: string) {
this.subject.next({ messageId, value });
}
}

// Usage example:

const defaultObject = {
profileData: {
customDataKey: "",
AlgoDataKey: "",
},
Throttle: {
shrinkDataKey: "",
onOff: "",
},
startData: "",
};

const transformer = new ObjectTransformer(defaultObject);

transformer.initialize({
customDataKey: "NewCustomDataKey",
AlgoDataKey: "NewAlgoDataKey",
startData: "NewStartData",
});

transformer.assignAlgoDataKey("UpdatedAlgoDataKey");

// Listen for changes to AlgoDataKey
transformer.onValueChange("AlgoDataKey").subscribe((value) => {
console.log(AlgoDataKey changed to: ${value});
});

@kendrelaxman
Copy link
Author

/^[0-9]+(.[0-9]+)?(k|m|b)?$

@kendrelaxman
Copy link
Author

type ValidationType = 'required' | 'numbers' | 'letters' | 'numericWithSuffix';

interface ValidationRule {
validateType: ValidationType;
message: string;
}

type ValidationResult = {
state: 'success' | 'warn' | 'error';
validationMessage?: string;
};

class Validator {
private static validateRequired(input: string): boolean {
return input.trim() !== '';
}

private static validateNumbers(input: string): boolean {
return /^\d+$/.test(input);
}

private static validateLetters(input: string): boolean {
return /^[a-zA-Z]+$/.test(input);
}

private static validateNumericWithSuffix(input: string): boolean {
return /^(\d+(.\d+)?)([kmb])?$/i.test(input);
}
private static validateNumericWithSuffix(input: string): boolean {
return /^(\d+(.\d+)?)([kmb])?$/i.test(input);
}

static validate(input: string, validationRules: ValidationRule[]): ValidationResult {
for (const rule of validationRules) {
switch (rule.validateType) {
case 'required':
if (!Validator.validateRequired(input)) {
return { state: 'error', validationMessage: rule.message };
}
break;
case 'numbers':
if (!Validator.validateNumbers(input)) {
return { state: 'error', validationMessage: rule.message };
}
break;
case 'letters':
if (!Validator.validateLetters(input)) {
return { state: 'error', validationMessage: rule.message };
}
break;
case 'numericWithSuffix':
if (!Validator.validateNumericWithSuffix(input)) {
return { state: 'error', validationMessage: rule.message };
}
break;
default:
// Unsupported validation type
return { state: 'error', validationMessage: 'Unsupported validation type' };
}
}

return { state: 'success' };

}
}

// Example usage
const input = '123.45k';
const validationRules: ValidationRule[] = [{ validateType: 'numericWithSuffix', message: 'Invalid number format' }];

const result = Validator.validate(input, validationRules);
console.log(result);

@kendrelaxman
Copy link
Author

git rev-list --all | xargs -n1 git ls-tree --full-name -r --name-only | grep "your_filename"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment