Why does Closure Compiler not rename objects with certain names? - google-closure-compiler

When I test the following code in closure compiler at http://closure-compiler.appspot.com:
// ==ClosureCompiler==
// #output_file_name default.js
// #compilation_level ADVANCED_OPTIMIZATIONS
// #formatting pretty_print
// ==/ClosureCompiler==
// These get renamed
window.foo = {};
window.bar = {};
// These don't
window.uid = {};
window.test = {};
The output is:
window.a = {};
window.b = {};
window.uid = {};
window.test = {};
Why does it rename :
window.foo = {};
window.bar = {};
But not:
window.uid = {};
window.test = {};
It seems to be an issue with certain words?

Update
As of the 20150315 release of Closure-compiler, the type based optimizations are enabled by default.
Closure Compiler will not rename properties that have the same name as any property defined on an object in the externs unless the --use_types_for_optimization flag is enabled. See the project FAQ for more details.

Related

Object overwritten in firebase

Data sending from google sheet:
Script using to send data to firebase from google sheet.
function writeData() {
var ss = SpreadsheetApp.openById("####");
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
var department = data[i][0];
var year = data[i][1];
var course = data[i][2];
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}
dataToImport[department][year][course][i] = {
course: data[i][3],
dateAdded: data[i][4],
fileSize: data[i][5],
fileType:data[i][6],
downloadLink: data[i][7],
};
}
var firebaseUrl = "https:url";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
base.setData("",dataToImport);
}
Data that is sent:
Q: My questions that since i had parent electrical and its two childs that were 1 and 2 and each child had a course dsp and aes respectively but only 1 child is send that is the second one.
Why first child is not sent to firebase ?
This code replacing previous entry with new entry. Replace this -
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}
with this -
dataToImport[department] = dataToImport[department] || {};
dataToImport[department][year] = dataToImport[department][year] || {}
dataToImport[department][year][course] = dataToImport[department][year][course] || {}
As a side note to #ra89fi's answer (because I didn't pick up on the overwrite bug).
The line base.setData("",dataToImport); is setting all data at the root of your database ("") with the given data (dataToImport). Any other data in your database will be deleted.
Instead, you should use an update operation.
I unfortunately can't establish which version of the Firebase API you are using. It's not quite the REST API and not quite JavaScript. So here are some relevant documentation links.
REST Update Guide
REST Update Reference
JavaScript (Web) Update Guide
JavaScript (Web) Update Reference

Meaning of interrogation

I'm a little bit confuse about the meaning difference of using "?"
I offen saw this:
var foo?: number = "bar"
But also saw this:
function foo(bar: {baz: ?string}) { ... }
And also saw both together.
I've read about invariants and maybe types, but if I understood it right, both signals have the same meaning, which is: "this type is of kind 'X', but it maybe is null or undefined".
Is it right or am I getting it wrong?
Here are answers to most of your questions:
// Don't know what this is, or why you would use it
// Error: undefined is incompatible with string
var foo1?: string = undefined;
// ?string means string, null, or undefined
var foo2: ?string = undefined;
type FooOptional = { foo?: string };
type FooMaybe = { foo: ?string };
// If it's optional it can be completely omitted
var foo3: FooOptional = {};
// It can also be explicitly set to undefined
var foo4: FooOptional = { foo: undefined };
// But not null!
var foo5: FooOptional = { foo: null };
// If it's a maybe type, it must be specified
// Error: property `foo` not found
var foo6: FooMaybe = {};
// But you can set it explicitly to null or undefined
var foo7: FooMaybe = { foo: null };
var foo8: FooMaybe = { foo: undefined };
(tryflow link)
Using both together (e.g. {foo?: ?string} as a type) usually (but not in all cases) indicates that the author doesn't quite know what type they want to use and have just added question marks until it typechecks. Typically I have found that if I think it through, it makes sense to use either an optional property or a maybe type, but not both.

Cannot find function FirebaseApp.getDatabaseByUrl in object [object Object]

How add data from google spreadsheet to firebase?
When I add library FirebaseApp in Google spreadsheet script but it gives error as "Cannot find function getDatabaseByUrl in object".
I follow the following step:
1. file>Project Properties>(copy the project key)>save
2. Resources>libraries>(paste the project key inside find project key text)>search
3. select version>give identifier name(like "FirebaseApp")
function writeDataToFirebase() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
var Sr_No=data[i][0];;
var Employee_Id=data[i][1];
dataToImport[Sr_No + '-' + Employee_Id] = {
NAME:data[i][3],
DESIGNATION:data[i][4],
BL_Start_Date:data[i][5],
Start_Date_at_Company:data[i][6],
End_DATE:data[i][7],
Mobile:data[i][8],
PAN_CARD_NO:data[i][9],
Email_Address:data[i][10],
DOB:data[i][11]
};
}
// Logger.log(Firebase);
var firebaseUrl = "https://fundoohr.firebaseio.com";
var secret="tzRgYNHu26ZkeJLbP7n2DlVBuogeB9NgSrF9z7a2";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl,secret);
base.setData("", dataToImport);
}
You need to add the firebase library first.
See this link how to install it: https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/firebase

I cannot undetstand how works "isSubtypeOf" in Dart mirrors

According to this test, I can not invoke the method "method" with an argument "list" because argument type is not compatible with the type of the method parameter.
Where I am wrong in my test?
import "dart:mirrors";
void main() {
var list = new List<String>();
var listMirror = reflectClass(list.runtimeType);
// Is "List<String>" subtype of "List<String>"?
print(listMirror.isSubtypeOf(listMirror));
// Method with parameter "List<String>"
var method = (List<String> list) {};
var closure = reflect(method) as ClosureMirror;
var function = closure.function;
var parameter = function.parameters.first;
// Is "List<String>" subtype of "List<String>"?
print(parameter.type.isSubtypeOf(listMirror));
print(listMirror.isSubtypeOf(parameter.type));
// Invoke method with arg: "List<String>" on param "List<String>"
method(list);
}
Output:
true
false
false
P.S.
Maybe I not understand something but it still not works.
import "dart:mirrors";
void main() {
var stringMirror = reflectClass(String);
// Query "List<int> get codeUnits"
MethodMirror method = stringMirror.declarations.values
.where((e) => e.simpleName == #codeUnits).first;
// List<int>
var returnType = method.returnType;
print(returnType);
// List
var baseType = reflectClass(List);
print(baseType);
// List<int> is List
print(returnType.isSubtypeOf(baseType));
}
Output:
ClassMirror on 'List'
ClassMirror on 'List'
false
This line is the culprit:
var listMirror = reflectClass(list.runtimeType);
it returns
ClassMirror on '_GrowableList'
if you use
var listMirror = reflectType(List);
it should work.
What you can when you need to get the type from a value at runtime
var listMirror = reflect(list).type.superinterfaces;
...
listMirror.forEach((e) => print(parameter.type.isSubtypeOf(e)));
depending on the situation you may need to make additional checks with
reflect(list).type.mixin;
If one of these checks is true it is a subtype.

Can I pass a function closure (with my parameters) as Firebase's set() method 'oncomplete' argument?

I want to do further processing depending on the success or failure of the set() method, but I need the context of some objects at the time I call the set() method. Otherwise my objects will be out of scope when the oncomplete function is called unless I put them in global - which I don't really want to do.
Here is an example:
function oncomplete_AddTran(tran,client,appt,balance){
/* if named argument 'balance' exists it is safe to assume
Firebase has not 'stepped on' the arguments with it's single
Error object or null */
if(balance typeof object) console.log("my parameters made it here");
}
function addTran(tran, client, appt, balance) {
var TRANS_LOCATION = 'https://xxx.firebaseIO.com/testing/transactions';
var tranListRef = new Firebase(TRANS_LOCATION);
var oncomplete = function() {
oncomplete_AddTran(tran, client, appt, balance); };
var tranref = tranListRef.child(tran.name).set(tran.literal, oncomplete);
}
Yes, it is possible. I am too impatient waiting for the confirmation I was looking for and decided to test myself. Here is the code I used (that works for my purpose):
function oncomplete_AddTran(tran,client,appt,balance){
console.log("arguments passed:" + arguments.length);
// firebase original arguments :: arguments.callee.caller.arguments
var fbargs = arguments.callee.caller.arguments;
}
function addTran(tran, client, appt, balance) {
var TRANS_LOCATION = "https://xxx.firebaseIO.com/testing/transactions";
var tranListRef = new Firebase(TRANS_LOCATION);
var oncomplete = function() {
oncomplete_AddTran(tran, client, appt, balance); };
var tranref = tranListRef.child(tran.name).set(tran.literal, oncomplete);
}
function main() {
var tran = {}; tran.name = "test1"; tran.literal = { tran: "tran" };
var client = {}; client.literal = { client: "client" };
var appt = {}; appt.literal = { appt:"appt" };
var balance = {}; balance.literal = { balance:"balance" };
addTran(tran,client,appt,balance);
}
The arguments were passed as expected but I still don't know how Firebase's set() method will pass the error object to the callback (in the event of an error) because I haven't tried reproducing an error and don't really know if I can.
The default null, and another (undefined) that is supposed to be passed when there is no error is not found in arguments.callee.caller.arguments (see callback function in example above). I am not sure that what I am doing is good practice - seems a bit hacky to me so I won't accept this answer to the question (yet).

Resources