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

/^[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