Skip to content

Instantly share code, notes, and snippets.

@mtamadon
Last active April 20, 2018 07:32
Show Gist options
  • Save mtamadon/ac4cbd4fd81a76d3e08954ba0cc78222 to your computer and use it in GitHub Desktop.
Save mtamadon/ac4cbd4fd81a76d3e08954ba0cc78222 to your computer and use it in GitHub Desktop.
AdonisJS Useful Lucid Functions
static contactsCsv2Object(csv){
const lines=csv.split("\n");
const result = []
for(let i=1;i<lines.length;i++){
let currentline=lines[i].split(",");
let obj = {
name : currentline[0],
mobile : currentline[1]
};
result.push(obj);
}
}
//Function:
static async filter(data , fields ){
const { sanitize } = use('Validator')
const sanitizeRules = {}
for (let key in fields){
sanitizeRules[fields[key]]= 'toNull'
}
data = sanitize(data,sanitizeRules )
data = this.NaNEscaper(data)
console.log(data)
let query = this.query()
for ( let key in data){
let value = data[key]
if(fields.includes(key) && data[key]){
if(isNaN(Number(value))){
value = toInt(value)
}
query.where(key , value )
}
}
return {
status: 1,
query : query,
errors : null
}
}
//Usage:
//todo
static normalizeMobile(mobile){
let tempMobile = mobile.toString()
if(tempMobile.startsWith('0')){
tempMobile = tempMobile.slice(1,15)
tempMobile = '98' + tempMobile
return tempMobile
}else if(tempMobile.startsWith('+')){
tempMobile = tempMobile.slice(1,15)
return tempMobile
}else if (tempMobile.startsWith('98')){
tempMobile = tempMobile.slice(0,15)
return tempMobile
}else if (tempMobile.startsWith('9')){
tempMobile = tempMobile.slice(0,15)
tempMobile = '98' + tempMobile
return tempMobile
}else {
return '0'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment