Automatically assign name to a new record - javascript

looking for a way to automatically update a cell value when a record is created.
I use Airtable, and I would like to automatically assign a developer to the newest record.
Ex. I have 3 developer, christophe/thomas/hugo

Using basic algorithm, but the issue is that when there is a new Biz Dev, we have to manually add him to the arrayBizDev and add a new switch case:
`let arrayBizDev = ["Christophe", "Thomas", "Hugo"]
let nbBizDev = arrayBizDev.length;
let bizDev = "";
switch (nbRdvPris % nbBizDev) {
case 0:
bizDev = "Christophe";
break;
case 1:
bizDev = "Thomas";
break;
case 2:
bizDev = "Hugo";
break;
default:
console.log(erreur);
}
output.set('Business Developper', bizDev);`

Related

Return object using classnames and switch statement

So I have an object build out that has a bunch of methods inside, I want to be able to narrow down some methods and see if there are other ways to do thing, so I will go into more description below:
So I have the following method which returns me a bunch of class names:
function class_names() {
return [
'optanon-category-C0001',
'optanon-category-C0002',
'optanon-category-C0003',
'optanon-category-C0004',
'optanon-category-C0005'
];
}
Now I have another method which basically outputs me a string based on the classname passed inside the parameter:
function classname_output(class_name) {
let output = '';
switch (class_name) {
case 'optanon-category-C0001':
output = 'Strictly Necessary Cookies';
break;
case 'optanon-category-C0002':
output = 'Performance Cookies';
break;
case 'optanon-category-C0003':
output = 'Functional Cookies';
break;
case 'optanon-category-C0004':
output = 'Targeting Cookies';
break;
case 'optanon-category-C0005':
output = 'Social Media Cookies';
break;
default:
output = 'No cookies match the specified class.';
break;
}
return output;
}
Is there a way that I can infuse the two methods into a single method with an object return and then target the object key?
You can have object (dictionary) that maps class names (key) to string (value), and then have a function to return the value if the key exists in the dictionary, or a default "Doesn't exist" string if it doesn't.
const dict = {
'optanon-category-C0001': 'Strictly Necessary Cookies',
'optanon-category-C0002': 'Performance Cookies',
'optanon-category-C0003': 'Functional Cookies',
'optanon-category-C0004': 'Targeting Cookies',
'optanon-category-C0005': 'Social Media Cookies'
};
function check(dict, className) {
return dict[className] ?? 'No cookies match the specified class.';
}
console.log(check(dict, 'optanon-category-C0003'));
console.log(check(dict, 'optanon-category-C0005'));
console.log(check(dict, 'optanon-category-C0000'));
Additional documentation
Nullish coalescing operator

Retrieve data from a switch case

I start in Javascript, and my mission is to retrieve data from an API.
Today I am a step where I have to filter my data according to the user's requests.
Here I want to retrieve the value of the html which is then connected to the switch case in JS. And then fetch the URL of the API according to the value selected by the user.
To make YOU understand, a URL present in the code is equal to a database and I want to select the database to display in my table.
I don't know if I made myself understood. In any case I share my code with you. I've been around my brain, I may have missed something. Any help will be appreciated. Thank's
function boutonSubmit(){
bounceType = document.getElementById('BouncesType').value;
datestart = document.getElementById('dateS').value;
dateend = document.getElementById('dateE').value;
//bounceCode = getElementById('#dateS').value;
var SelectDB = document.getElementById('selectdb').value;
switch(SelectDB){
case 'Desclopinette':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=867XXXXXXXXXXXvGN&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Chrysolum':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=utxyiwq1K8S04WauVIa0&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Acanthius':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=v6PjXXXXXXXXXXX8y682&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Bellapourpre':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=xj530XXXXXXXXXXXRIZlD&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Alibigratis':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=SwyXXXXXXXXXXXdKz7z&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Bullecreatif':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=CN7XXXXXXXXXXXw30llOD&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Cacologia':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=oxjs3XXXXXXXXXXXeGjg&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Elenaparc':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=t02ZrrXXXXXXXXXXXoBm1&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Histoiredepoint':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=juiHFXXXXXXXXXXXSY68y5V&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Iatraliptice':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=LW24pXXXXXXXXXXXTfa28U&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Maitrechic':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=ZbpXXXXXXXXXXXy0S&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Neojaune':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=TA3XXXXXXXXXXXeBJlvc&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'OreilledeLapin':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=uqTXXXXXXXXXXXzf0Eg&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Princecalme':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=3phXXXXXXXXXXXvO4M4lZ9&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
default:
console.log(`Sorry, we are out of ${Selectdb}`)
}
// Is this the right thing to fetch?
fetch(SelectDB)
.then((response) => response.text())
   .then((txtResponse) => {
data = txtResponse;
console.log(data);
data = csvJSON(data);
const tbody = document.querySelector('#user-table tbody');
tbody.innerHTML = '';
data.forEach( (user) => {
const entry = document.createElement('tr')
entry.innerHTML = `
<tr>
<td class="column1">${user.Date}</td>
<td class="column2">${user.Email}</td>
<td class="column3">${user.BounceCode}</td>
<td class="column3">${user.BounceType}</td>
</tr>
`;
          
tbody.appendChild(entry);
});
}
I suppose you better replace switch case with something more suitable for your case. You can exclude base API URL in constant variable
const DATABASE_API = 'https://api7.esv2.com/v2/Api'
then it makes sence to create separate key=value object with apiKeys
const DB_API_KEYS = {
Desclopinette: 'v6PjXXXXXXXXXXX8y682',
Chrysolum: 'utxyiwq1K8S04WauVIa0',
// and so on...
}
after that you're good to go with full url constructing
const dbApiKey = DB_API_KEYS[SelectDB]
if (dbApiKey === undefined) {
console.log(`Sorry, we are out of ${Selectdb}`)
// throw or something
}
const url = `${DATABASE_API}/Bounces?apiKey=${dbApiKey}&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
and finally
fetch(url)

How to preserve variable name in Cheerp (a C++ to JavaScript transpiler)

I'm using Cheerp (https://www.leaningtech.com/cheerp/) to transpile some C++ code into JavaScript.
Is there any option to preserve variable names? Looks like the names get always mangled
Original C++ code:
void myClass::myMethod(int32_T myParam, boolean_T *rty_Result)
{
switch (myParam) {
case Mycase1:
case Mycase2:
case Mycase3:
case Mycase4:
case Mycase5:
*rty_Result = true;
break;
case Mycase6:
*rty_Result = (filter.field1.field2 == 1);
break;
default:
*rty_Result = false;
break;
}
}
Output from Cheerp:
function __ZN8JsBridge12AvailabilityEP9bFilter_Ti(Lthis,filter,myParam){
var tmp0=0;
switch(myParam|0){
case 5:
{
tmp0=filter.a3.i2|0;
return (((tmp0|0)===1?1:0)?1:0)|0;
break;
}
case 1:
case 2:
case 4:
case 6:
case 3:
{
return 1|0;
break;
}
default:{
return 0|0;
break;
}
}
}
I don't find any options in the documentation:
https://github.com/leaningtech/cheerp-meta/wiki
You can try to pass the option:
-cheerp-pretty-code
Source: https://github.com/leaningtech/cheerp-meta/wiki/JavaScript-interoperability#clobbering-names
If that doesn't work, then I'm quite sure that this just unfortunately cannot be accomplished.

converting inform7 table to javascript

I am trying to port an interactive fiction game from inform7 to javascript. A lot of the syntax is easily converted into javascript; some of it can even be optimized after conversion. But the 'Table' feature does not exist natively in javascript, and I can think of many ways to mimic it. This is what the syntax for an inform7 table looks like:
Table of example table
column one column two column three column four
---------- ---------- ------------ ----------
---------- ---------- ------------ ---------
When Play begins:
Choose a blank row from Table of example table;
now column one entry is "text";
now column two entry is 400;
now column three entry is true
now column four entry is [non primitive]
What is the most efficient way to implement this in javascript?
Please keep in mind that I plan on using strict mode, and do not want to even consider the use of 3rd party frameworks/libraries such as JQuery. Only JavaScript as defined in the latest ECMAScript Specs.
Some of my ideas:
using a map:
const tables = new Map();
// to create table
tables.set("wandering monsters", [
new Map([
["monster name","default"]
])
]);
//to get table entry
tables.get("wandering monsters")[0].get("monster name");
using a function:
function table(name, row, key, val, entrylist){
var result;
if (table.data == undefined){
table.data = new Map();
};
switch (arguments.length) {
case 1:
result = table.data.get(name);
break;
case 2:
table.data.set(name, []);
result = table.data.get(name);
break;
case 3:
result = table.data.get(name)[row].get(key);
break;
case 4:
table.data.get(name)[row].set(key, val);
result = table.data.get(name)[row].get(key);
break;
case 5:
table.data.get(name)[row] = new Map(entrylist);
result = table.data.get(name)[row];
break;
default:
result = table.data;
};
return result;
}
im editing on mobile

Javascript, check multiple arrays for number

I'm looking for the best solution here, i've got an idea but thinking it could be done prettier.
I'm making an simple weather application. And i'm using Yahoo Weather api were they have got codes for weather conditions.
Depending on the condition i'm giving a code. Now, there are 50 codes and i've categorised them into 5 categories. In my case ex. my categori Snow contains 15 of Yahoo's condition codes.
Well, if you got a better idea (which i bet there is) be free to suggest.
My thought is to return the matching value from a set of arrays, but not shure how to do it.
My code now looks like this:
function getCondition(code) {
var snow = [1, 2, 3],
sun = [4, 5, 6];
}
What i need is the variable name that contains the matching number of the code?
I've made a JS-Fiddle
http://jsfiddle.net/BH8r6/
The fastest lookup (translating a Yahoo code to your label) is to use the code as array key (if they are sequential).
var weather = [];
weather[0] = "no_weather";
weather[1] = "snow";
weather[2] = "snow";
weather[3] = "snow";
weather[4] = "sun";
weather[5] = "sun";
weather[6] = "sun";
function getCondition(code) {
return weather[code];
}
Why dont you try an associative array when your key is your variable name and your values is the corresponding code for the variable name, thus your code will be something like this:
var myCodeArray=[];
myCodeArray["snow"]=[1, 2, 3];
myCodeArray["sun"] = [4, 5, 6];
now your method getCondition will be
function getCondition(code)
{
for(var definedCodeName in myCodeArray)
{
if(myCodeArray.hasOwnProperty(definedCodeName))
{
var array=myCodeArray[definedCodeName ];
for(var i=0;i<array.length;i++)
{
if(array[i]==code){
return definedCodeName ;}
}
}
}
return "Not found";
}
Demo
Why to complicate everything?! Just use 'switch' :
function getCondition(code) {
switch( code ){
case 1:
case 2:
case 4:
case 6:
return "snow";
case 3:
case 8:
case 9:
return "sun";
case 5:
case 7:
case 10:
return "cloudy";
}
return "none";
}

Categories